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.

feat(editor): a run that was cut off says so, and can be picked back up

esenginea525144e6af4
diff --git a/desktop/electron/agent/kernel.ts b/desktop/electron/agent/kernel.ts
index ac05a45cf..499317a30 100644
--- a/desktop/electron/agent/kernel.ts
+++ b/desktop/electron/agent/kernel.ts
@@ -140,10 +140,9 @@ export async function runTurn(
}
session.pushUser(text);
- // Bounded so a model that keeps calling tools cannot spin forever. Reaching
- // it is reported as an ordinary end of turn — the work so far stands, and
- // the person can say "keep going".
- for (let round = 0; round < MAX_ROUNDS; round++) {
+ // Bounded so a model that keeps calling tools cannot spin forever.
+ let round = 0;
+ for (; round < MAX_ROUNDS; round++) {
// The editor is not frozen while a turn runs: the person can drag an
// entity while the model is thinking. Whatever the agent read before that
// is now possibly stale, and it has no way to notice — an edit made from
@@ -207,6 +206,11 @@ export async function runTurn(
// Everything on the stack now is this turn's own doing.
ours = await undoSteps(deps, mark);
}
+ // The budget ran out rather than the model stopping asking, so the work is
+ // unfinished. Said out loud: reported as an ordinary end of turn, a run cut
+ // off mid-task looks exactly like one that finished, and the sentence that
+ // would have told you otherwise is the one it never got to write.
+ if (round === MAX_ROUNDS) reason = 'max_rounds';
} catch (e) {
reason = signal.aborted ? 'aborted' : 'error';
if (!signal.aborted) emit({ type: 'error', message: (e as Error)?.message ?? String(e) });
diff --git a/desktop/electron/agent/types.ts b/desktop/electron/agent/types.ts
index efbe0285d..2ff2d78e4 100644
--- a/desktop/electron/agent/types.ts
+++ b/desktop/electron/agent/types.ts
@@ -200,7 +200,9 @@ export type AgentEvent =
| { type: 'awaiting_confirm'; request: ConfirmRequest }
/** The turn is over. `steps` is what a single Undo would take back — 0 is the
* signal not to offer one — and `mark` is where it would take it back to. */
- | { type: 'turn_end'; steps: number; mark: unknown; reason: 'end_turn' | 'aborted' | 'error' | 'refusal' }
+ /** `max_rounds` is its own ending because the work is UNFINISHED: reported as
+ * an ordinary end_turn it would look like the agent had said its piece. */
+ | { type: 'turn_end'; steps: number; mark: unknown; reason: 'end_turn' | 'aborted' | 'error' | 'refusal' | 'max_rounds' }
| { type: 'error'; message: string };
/** Everything the kernel needs from outside, so it runs under a test with none
diff --git a/desktop/src/components/AgentDrawer.tsx b/desktop/src/components/AgentDrawer.tsx
index e0f71d8e9..55c431fd3 100644
--- a/desktop/src/components/AgentDrawer.tsx
+++ b/desktop/src/components/AgentDrawer.tsx
@@ -27,12 +27,13 @@ import { useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore } fr
import {
X, Plus, PanelRight, ArrowUp, Square, ChevronRight, ChevronDown, Check, TriangleAlert,
Loader, Copy, Pencil, Eye, KeyRound, Boxes, Stethoscope, Image as ImageIcon, RotateCcw,
- File as FileIcon,
+ File as FileIcon, ArrowRight,
} from 'lucide-react';
import { useEditorStore } from '@/store/editorStore';
import {
useAgent, sendAgentMessage, stopAgentTurn, confirmAgentCall, startNewConversation,
peekEntities, entitiesInInput, effectiveSelection, selectAgentModel, retryAgentTurn, setAgentDraft,
+ RESUMABLE,
type AgentTurn, type AgentEntry, type AgentToolEntry, type AgentProseEntry,
} from '@/store/AgentStore';
import type { ConfirmAnswer } from '../../electron/agent/types';
@@ -557,6 +558,7 @@ function Turn({ turn, isLast, running, until }: {
{tokens > 0 && <span>↑{compact(turn.inputTokens)} ↓{compact(turn.outputTokens)}</span>}
{turn.reason === 'aborted' && <span className="ag-warn">{t('agent.turn.aborted')}</span>}
{turn.reason === 'refusal' && <span className="ag-warn">{t('agent.turn.refusal')}</span>}
+ {turn.reason === 'max_rounds' && <span className="ag-warn">{t('agent.turn.maxRounds')}</span>}
{/* What the run left in the scene — the number the summary is actually
about, which is why it reads here and not only inside the fold. */}
{counts && (
@@ -588,6 +590,19 @@ function Turn({ turn, isLast, running, until }: {
who just pressed Stop is asking. */}
{turn.reason === 'aborted' && <div className="ag-sys">{t('agent.turn.aborted.note')}</div>}
{turn.reason === 'refusal' && <div className="ag-sys">{t('agent.turn.refusal.note')}</div>}
+ {turn.reason === 'max_rounds' && <div className="ag-sys">{t('agent.turn.maxRounds.note')}</div>}
+ {/* A run that could have gone further. Offered only on the run that
+ stopped: picking a stopped run up from three turns ago would carry
+ on from a scene that has since moved. */}
+ {isLast && !running && turn.reason && RESUMABLE.has(turn.reason) && (
+ <button
+ type="button"
+ className="ag-continue"
+ onClick={() => void sendAgentMessage(t('agent.continue.message'))}
+ >
+ <ArrowRight size={13} strokeWidth={2} />{t('agent.continue')}
+ </button>
+ )}
{turn.reason !== null && <ChangeSet turn={turn} until={until} />}
</div>
</Fold>
diff --git a/desktop/src/i18n/messages/agent.ts b/desktop/src/i18n/messages/agent.ts
index 0274d1d4e..01c9b1ad6 100644
--- a/desktop/src/i18n/messages/agent.ts
+++ b/desktop/src/i18n/messages/agent.ts
@@ -85,6 +85,16 @@ export const agentMessages = defineMessages({
zh: '你停止了这一轮——做到一半的改动都留着。',
},
'agent.turn.refusal': { en: 'The model declined this one.', zh: '模型拒绝了这个请求。' },
+ 'agent.turn.maxRounds': { en: 'Ran out of steps.', zh: '步数用尽。' },
+ 'agent.turn.maxRounds.note': {
+ en: 'It reached the limit on tool calls for one run before it was finished. What it already did is there.',
+ zh: '它在干完之前用光了单轮的工具调用次数。已经做完的部分都留着。',
+ },
+ 'agent.continue': { en: 'Carry on', zh: '接着干' },
+ 'agent.continue.message': {
+ en: 'Carry on from where you stopped.',
+ zh: '从你停下的地方接着干。',
+ },
'agent.turn.refusal.note': {
en: 'Nothing was changed by this run, so there is nothing to undo.',
zh: '这一轮什么都没有改动,不需要撤销。',
diff --git a/desktop/src/store/AgentStore.ts b/desktop/src/store/AgentStore.ts
index a2f706b65..b286baa18 100644
--- a/desktop/src/store/AgentStore.ts
+++ b/desktop/src/store/AgentStore.ts
@@ -79,7 +79,10 @@ export interface AgentErrorEntry {
export type AgentEntry = AgentProseEntry | AgentToolEntry | AgentErrorEntry;
-export type TurnReason = 'end_turn' | 'aborted' | 'error' | 'refusal';
+export type TurnReason = 'end_turn' | 'aborted' | 'error' | 'refusal' | 'max_rounds';
+
+/** Endings the run could have gone further from, so the person can say so. */
+export const RESUMABLE: ReadonlySet<TurnReason> = new Set<TurnReason>(['aborted', 'max_rounds']);
export interface AgentTurn {
/** Position in the conversation; stable, so React keys are too. */
diff --git a/desktop/src/theme/agent.css b/desktop/src/theme/agent.css
index 3f27997ca..491e20d61 100644
--- a/desktop/src/theme/agent.css
+++ b/desktop/src/theme/agent.css
@@ -701,6 +701,23 @@
.ag-th:hover .ag-rerun, .ag-rerun:focus-visible { opacity: 1; }
.ag-rerun:hover { color: var(--text-hi); background: var(--hover); }
+/* Pick a stopped run back up. Sits with the note that says it stopped, and
+ reads as an offer rather than as the primary thing to do — the run may well
+ have got far enough already. */
+.ag-continue {
+ align-self: flex-start;
+ display: flex; align-items: center; gap: 6px;
+ padding: 6px 13px;
+ border-radius: var(--r-sm);
+ background: var(--hover);
+ color: var(--text);
+ font-size: var(--fs-sm);
+ transition: background var(--t-fast), color var(--t-fast);
+}
+.ag-continue:hover { background: var(--pressed); color: var(--text-hi); }
+.ag-continue svg { color: var(--text-faint); }
+.ag-continue:hover svg { color: var(--text); }
+
/* ── The change set ─────────────────────────────────────────────────────────
The work product of a run, so it gets real +/~/− semantics and a field-level
before→after inline — not a popover hung off the undo bar. */
diff --git a/desktop/tests/agent-kernel.test.ts b/desktop/tests/agent-kernel.test.ts
index a4cfbc00b..2e8b04cc4 100644
--- a/desktop/tests/agent-kernel.test.ts
+++ b/desktop/tests/agent-kernel.test.ts
@@ -183,6 +183,21 @@ describe('the agent turn', () => {
expect(events.at(-1)).toMatchObject({ type: 'turn_end', reason: 'refusal' });
});
+ // A model that keeps asking for tools is cut off, and the run has to say so:
+ // an end_turn here looks exactly like a run that finished, and the sentence
+ // that would have told you otherwise is the one it never got to write.
+ it('says the step budget ran out rather than reporting a finished run', async () => {
+ const s = fakeSession(Array.from({ length: 60 }, () => asks(call('get_scene_tree'))));
+ await runTurn(deps(s), 'build the whole game', null, new AbortController().signal);
+ expect(events.at(-1)).toMatchObject({ type: 'turn_end', reason: 'max_rounds' });
+ });
+
+ it('does not blame the budget for a run that stopped asking in time', async () => {
+ const s = fakeSession([asks(call('get_scene_tree')), ends()]);
+ await runTurn(deps(s), 'look', null, new AbortController().signal);
+ expect(events.at(-1)).toMatchObject({ type: 'turn_end', reason: 'end_turn' });
+ });
+
it('puts editor context ahead of the user turn, and only when there is some', async () => {
const s = fakeSession([ends()]);
await runTurn(deps(s), 'hi', 'The open document is main.esscene.', new AbortController().signal);