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(editor): Stop answers during a tool, and a stream stops flooding the window

yhh5c0198092878
diff --git a/desktop/electron/agent/host.ts b/desktop/electron/agent/host.ts
index 32981caae..70193ba00 100644
--- a/desktop/electron/agent/host.ts
+++ b/desktop/electron/agent/host.ts
@@ -162,7 +162,13 @@ export function createAgentHost(deps: AgentHostDeps): AgentHost {
error,
});
- const pushStatus = (): void => deps.push({ kind: 'status', status: status() });
+ // Deltas out first: status and transcript share one channel BECAUSE the two
+ // must not be reordered, and a buffered delta is a transcript event that has
+ // not left yet. (Defined below; hoisted, so this reads in the order it runs.)
+ const pushStatus = (): void => {
+ flushDeltas();
+ deps.push({ kind: 'status', status: status() });
+ };
// The conversation as a replayable stream. Bounded, and trimmed by WHOLE runs
// from the front: half a run replays as a run that never ended. Dropping the
@@ -184,12 +190,72 @@ export function createAgentHost(deps: AgentHostDeps): AgentHost {
if (at >= 0) log.length = at;
};
- const emit = (event: AgentEvent): void => {
+ const record = (event: AgentEvent): void => {
log.push(event);
if (log.length > LOG_LIMIT) trimOldestRun();
deps.push({ kind: 'event', event });
};
+ /**
+ * Streaming deltas, merged before they leave main.
+ *
+ * A model writes a tool's arguments one JSON fragment at a time, and a scene
+ * edit's arguments run to tens of kilobytes — hundreds of fragments, back to
+ * back with nothing between them. Forwarded one by one, each cost an IPC
+ * message, a store update that rebuilds the run's whole entry list, and a
+ * re-render of the drawer. The renderer's main thread saturated exactly while
+ * a call was being written, which is why the window froze mid-tool, would not
+ * scroll, and appeared to ignore Stop — a blocked renderer cannot deliver the
+ * click that would have stopped it either.
+ *
+ * Text that arrives 30 times a second reads exactly like text that arrives
+ * 600 times a second, so consecutive deltas of one kind merge and go out on a
+ * frame's delay. Everything else flushes them first: this sits at the one
+ * place events leave for the window precisely so nothing can be reordered
+ * around a merge — a store-side buffer could not promise that.
+ */
+ const FLUSH_MS = 33;
+ let buffered: (AgentEvent & { delta: string }) | null = null;
+ let flushTimer: ReturnType<typeof setTimeout> | null = null;
+
+ const isDelta = (e: AgentEvent): e is AgentEvent & { delta: string } =>
+ e.type === 'text' || e.type === 'thinking' || e.type === 'tool_args';
+
+ /** Same kind, and for arguments the same call — two tools written in one
+ * response must not have their JSON concatenated into one of them. */
+ const mergeable = (a: AgentEvent & { delta: string }, b: AgentEvent & { delta: string }): boolean =>
+ a.type === b.type
+ && (a.type !== 'tool_args' || (a as { id: string }).id === (b as unknown as { id: string }).id);
+
+ const flushDeltas = (): void => {
+ if (flushTimer) { clearTimeout(flushTimer); flushTimer = null; }
+ if (!buffered) return;
+ const merged = buffered;
+ buffered = null;
+ record(merged);
+ };
+
+ /** Throw the buffer away — for a reset, whose log is emptied anyway. */
+ const dropDeltas = (): void => {
+ if (flushTimer) { clearTimeout(flushTimer); flushTimer = null; }
+ buffered = null;
+ };
+
+ const emit = (event: AgentEvent): void => {
+ if (isDelta(event)) {
+ if (buffered && mergeable(buffered, event)) {
+ buffered = { ...buffered, delta: buffered.delta + event.delta };
+ } else {
+ flushDeltas();
+ buffered = event;
+ }
+ if (!flushTimer) flushTimer = setTimeout(flushDeltas, FLUSH_MS);
+ return;
+ }
+ flushDeltas();
+ record(event);
+ };
+
/** Settle every outstanding ask as declined. See invariant 2. */
const settlePending = (): void => {
for (const resolve of pending.values()) resolve({ answer: 'no' });
@@ -301,6 +367,8 @@ export function createAgentHost(deps: AgentHostDeps): AgentHost {
running?.abort();
running = null;
settlePending();
+ // The half-written delta belongs to a conversation that no longer exists.
+ dropDeltas();
session = null;
model = null;
// A new conversation is a new file. The old one keeps whatever it had at
@@ -329,6 +397,7 @@ export function createAgentHost(deps: AgentHostDeps): AgentHost {
running?.abort();
running = null;
settlePending();
+ dropDeltas();
session = null;
model = conversation.model;
conversationId = conversation.id;
diff --git a/desktop/electron/agent/kernel.ts b/desktop/electron/agent/kernel.ts
index 22f8b953d..9a85346ab 100644
--- a/desktop/electron/agent/kernel.ts
+++ b/desktop/electron/agent/kernel.ts
@@ -184,8 +184,8 @@ export async function runTurn(
let end = i;
while (end < calls.length && isRead(calls[end])) end++;
const batch = end - i > 1
- ? await Promise.all(calls.slice(i, end).map((c) => execute(deps, c, allowed)))
- : [await execute(deps, calls[i], allowed)];
+ ? await Promise.all(calls.slice(i, end).map((c) => execute(deps, c, allowed, signal)))
+ : [await execute(deps, calls[i], allowed, signal)];
for (const done of batch) {
outcomes.push(done.outcome);
wrote ||= done.mutated;
@@ -252,11 +252,42 @@ function isRead(call: ToolCall): boolean {
return !!tool && (tool.effect ?? 'read') === 'read';
}
+/** Marks a dispatch the person stopped waiting for. */
+const ABANDONED = Symbol('abandoned');
+
+/**
+ * `work`, or {@link ABANDONED} the moment `signal` aborts — whichever comes first.
+ *
+ * Stop used to be honoured only BETWEEN tool calls, so pressing it during a slow
+ * one did nothing until that one returned, which reads as a dead button. The
+ * dispatch itself cannot be cancelled: it is executing inside the editor window
+ * and will finish, and if it was a write that write will land. What ends
+ * promptly is the TURN — no further calls, no further model round trips — which
+ * is what the person is asking for. The transcript says which of the two
+ * happened rather than implying the work was undone.
+ */
+async function untilAborted<T>(work: Promise<T>, signal: AbortSignal): Promise<T | typeof ABANDONED> {
+ if (signal.aborted) return ABANDONED;
+ let onAbort!: () => void;
+ const stopped = new Promise<typeof ABANDONED>((resolve) => {
+ onAbort = () => resolve(ABANDONED);
+ signal.addEventListener('abort', onAbort, { once: true });
+ });
+ try {
+ return await Promise.race([work, stopped]);
+ } finally {
+ // A turn runs up to MAX_ROUNDS batches of calls; leaving one listener per
+ // call on a signal that lives as long as the turn is a slow leak.
+ signal.removeEventListener('abort', onAbort);
+ }
+}
+
/** Run one call: gate it, dispatch it, and report both ways. */
async function execute(
deps: KernelDeps,
call: ToolCall,
allowed: Set<string>,
+ signal: AbortSignal,
): Promise<{ outcome: ToolOutcome; mutated: boolean }> {
const { emit, driver, confirm } = deps;
const tool = byName.get(call.name);
@@ -308,10 +339,20 @@ async function execute(
}
}
- const result = await runTool(tool, driver, input, true) as {
+ const raced = await untilAborted(runTool(tool, driver, input, true) as Promise<{
content: Array<{ type: string; text?: string; data?: string }>;
isError?: boolean;
- };
+ }>, signal);
+ if (raced === ABANDONED) {
+ // Said plainly, because "stopped" and "did not happen" are different: the
+ // call is still running in the editor and a write will still land.
+ emit({ type: 'tool_end', id: call.id, ok: false, summary: 'stopped — this call was already running and may still complete' });
+ return {
+ outcome: { id: call.id, content: `stopped by the user while ${call.name} was running`, isError: false },
+ mutated: false,
+ };
+ }
+ const result = raced;
const outcome = toOutcome(call.id, result);
emit({
type: 'tool_end',
diff --git a/desktop/tests/agent-host.test.ts b/desktop/tests/agent-host.test.ts
index 765019e6a..1c13c2006 100644
--- a/desktop/tests/agent-host.test.ts
+++ b/desktop/tests/agent-host.test.ts
@@ -429,3 +429,96 @@ describe('a conversation that outlives the session', () => {
expect(h.status().phase).toBe('idle');
});
});
+
+/**
+ * Streaming deltas, merged before they leave main.
+ *
+ * A model writes a tool's arguments one JSON fragment at a time, and one scene
+ * edit's arguments are hundreds of them. One IPC message, one store update and
+ * one drawer re-render each is what froze the window while a call was being
+ * written — and a blocked renderer cannot deliver the Stop click either.
+ */
+describe('streaming deltas leave merged', () => {
+ let messages: AgentMessage[];
+ let driver: ReturnType<typeof vi.fn> & { js: unknown; op: unknown };
+
+ const settled = (h: AgentHost): Promise<void> =>
+ vi.waitFor(() => expect(h.status().phase).toBe('idle'));
+ const host = (steps: StepEvent[][]): AgentHost => createAgentHost({
+ driver: driver as never,
+ push: (m) => messages.push(m),
+ ready: () => true,
+ provider: () => fakeProvider(steps),
+ });
+ const events = () => messages.flatMap((m) => (m.kind === 'event' ? [m.event] : []));
+ const kinds = () => events().map((e) => e.type);
+
+ beforeEach(() => {
+ messages = [];
+ driver = vi.fn(async (method: string) => {
+ if (method === 'mark') return { seq: 1 };
+ if (method === 'stepsSince') return 0;
+ if (method === 'getDiagnostics') return [];
+ if (method === 'getSelectionIds') return [];
+ return null;
+ }) as never;
+ (driver as { js: unknown }).js = vi.fn(async () => null);
+ (driver as { op: unknown }).op = vi.fn(async () => null);
+ });
+
+ const streamed = (...evs: StepEvent[]): StepEvent[][] => [evs, ends()];
+ const text = (delta: string): StepEvent => ({ type: 'text', delta });
+ const args = (id: string, delta: string): StepEvent => ({ type: 'tool_args', id, delta });
+ const stop = (): StepEvent => ({ type: 'stop', reason: 'end_turn' });
+
+ it('merges a run of text into one event', async () => {
+ const h = host(streamed(text('he'), text('ll'), text('o'), stop()));
+ h.send('hi');
+ await settled(h);
+ const said = events().filter((e) => e.type === 'text') as Array<{ delta: string }>;
+ expect(said).toHaveLength(1);
+ expect(said[0].delta).toBe('hello');
+ });
+
+ it("keeps two tools' arguments apart", async () => {
+ const h = host(streamed(
+ args('a', '{"x":'), args('a', '1}'),
+ args('b', '{"y":'), args('b', '2}'),
+ stop(),
+ ));
+ h.send('two');
+ await settled(h);
+ expect(events().filter((e) => e.type === 'tool_args')).toEqual([
+ { type: 'tool_args', id: 'a', delta: '{"x":1}' },
+ { type: 'tool_args', id: 'b', delta: '{"y":2}' },
+ ]);
+ });
+
+ it('does not merge across kinds', async () => {
+ const h = host(streamed(
+ { type: 'thinking', delta: 'hm' }, text('a'), { type: 'thinking', delta: 'ok' }, stop(),
+ ));
+ h.send('hi');
+ await settled(h);
+ expect(kinds().filter((k) => k === 'text' || k === 'thinking')).toEqual(['thinking', 'text', 'thinking']);
+ });
+
+ it('lets nothing overtake a pending delta', async () => {
+ // Transcript and status share one channel BECAUSE their order is meaning;
+ // a buffered delta is a transcript event that has not left yet.
+ const h = host(streamed(text('looking'), { type: 'tool_call', call: call('get_scene_tree') },
+ { type: 'stop', reason: 'tool_use' }));
+ h.send('look');
+ await settled(h);
+ const order = kinds();
+ expect(order.indexOf('text')).toBeGreaterThanOrEqual(0);
+ expect(order.indexOf('text')).toBeLessThan(order.indexOf('tool_start'));
+ });
+
+ it('replays what it merged, not what it received', async () => {
+ const h = host(streamed(text('a'), text('b'), stop()));
+ h.send('hi');
+ await settled(h);
+ expect(h.transcript().filter((e) => e.type === 'text')).toEqual([{ type: 'text', delta: 'ab' }]);
+ });
+});
diff --git a/desktop/tests/agent-kernel.test.ts b/desktop/tests/agent-kernel.test.ts
index 451c92ba4..63d063eb2 100644
--- a/desktop/tests/agent-kernel.test.ts
+++ b/desktop/tests/agent-kernel.test.ts
@@ -425,4 +425,88 @@ describe('the agent turn', () => {
await runTurn(deps(s), 'how does it look', null, new AbortController().signal);
expect(s.context.some((c) => /cannot carry images/i.test(c))).toBe(false);
});
+
+ /**
+ * Stop, pressed while a tool is running.
+ *
+ * It used to be honoured only BETWEEN calls, so a slow one held the turn open
+ * after the click and the button read as dead. The dispatch cannot be
+ * cancelled — it is executing inside the editor window — so what has to end
+ * promptly is the turn.
+ */
+ describe('stopping while a tool is in flight', () => {
+ /** Park the tool dispatch; the checkpoint reads still answer. */
+ function hangingDriver(): { release: () => void } {
+ let release = (): void => {};
+ driver = vi.fn(async (method: string) => {
+ if (method === 'mark') return { seq: 7 };
+ if (method === 'stepsSince') return stepsSince;
+ if (method === 'getDiagnostics') return diagnostics;
+ await new Promise<void>((r) => { release = r; });
+ return null;
+ }) as never;
+ (driver as { js: unknown }).js = vi.fn(async () => null);
+ (driver as { op: unknown }).op = vi.fn(async () => null);
+ return { release: () => { release(); } };
+ }
+
+ /** Let the loop run until `done`, without waiting on wall time. */
+ async function until(done: () => boolean): Promise<void> {
+ for (let i = 0; i < 200 && !done(); i++) await new Promise((r) => { setTimeout(r, 0); });
+ if (!done()) throw new Error('condition never became true');
+ }
+
+ it('ends the turn without waiting for the call to come back', async () => {
+ const stuck = hangingDriver();
+ const ac = new AbortController();
+ const s = fakeSession([asks(call('get_scene_tree')), ends()]);
+ const turn = runTurn(deps(s), 'look', null, ac.signal);
+
+ await until(() => events.some((e) => e.type === 'tool_start'));
+ ac.abort();
+ // Resolves on the abort alone — the dispatch is never released.
+ await expect(turn).resolves.toBeTruthy();
+ expect(events.at(-1)).toMatchObject({ type: 'turn_end', reason: 'aborted' });
+ stuck.release();
+ });
+
+ it('says the call may still land rather than implying it was undone', async () => {
+ const stuck = hangingDriver();
+ const ac = new AbortController();
+ const s = fakeSession([asks(call('save_scene')), ends()]);
+ const turn = runTurn(deps(s), 'save it', null, ac.signal);
+
+ await until(() => events.some((e) => e.type === 'tool_start'));
+ ac.abort();
+ await turn;
+ const end = events.find((e) => e.type === 'tool_end');
+ expect(end).toMatchObject({ ok: false });
+ expect((end as { summary: string }).summary).toMatch(/still complete/i);
+ stuck.release();
+ });
+
+ it('does not feed the model results from a turn nobody waited for', async () => {
+ const stuck = hangingDriver();
+ const ac = new AbortController();
+ const s = fakeSession([asks(call('get_scene_tree')), ends()]);
+ const turn = runTurn(deps(s), 'look', null, ac.signal);
+
+ await until(() => events.some((e) => e.type === 'tool_start'));
+ ac.abort();
+ await turn;
+ expect(s.results).toEqual([]);
+ stuck.release();
+ });
+
+ it('never dispatches when the signal was already aborted', async () => {
+ const stuck = hangingDriver();
+ const ac = new AbortController();
+ ac.abort();
+ const s = fakeSession([asks(call('get_scene_tree')), ends()]);
+ await runTurn(deps(s), 'look', null, ac.signal);
+ // The turn is over before the loop; the parked dispatch was never entered.
+ expect(events.some((e) => e.type === 'tool_start')).toBe(false);
+ stuck.release();
+ });
+ });
});