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): three things a real gateway found that a fake one could not

esengine6146535ec376
diff --git a/desktop/electron/agent/anthropic.ts b/desktop/electron/agent/anthropic.ts
index b1d06290e..96867bccd 100644
--- a/desktop/electron/agent/anthropic.ts
+++ b/desktop/electron/agent/anthropic.ts
@@ -216,14 +216,27 @@ export function compactHistory(
turnStarts: readonly number[],
dropped: number,
keepRuns: number,
-): { messages: Message[]; turnStarts: number[]; dropped: number } | null {
+ folded: readonly string[] = [],
+): { messages: Message[]; turnStarts: number[]; dropped: number; folded: string[] } | null {
const cut = turnStarts.length - keepRuns;
if (cut <= 0) return null;
const at = turnStarts[cut];
- const asked = turnStarts.slice(0, cut)
- .map((start, i) => `${dropped + i + 1}. ${firstText(messages[start])}`);
+ // Everything ever folded, not only this pass. The note a fold produces is NOT
+ // in turnStarts, so the next one splices straight over it — carrying the asks
+ // in a value of their own is what stops the oldest request disappearing one
+ // compaction at a time. (Caught against a real gateway: the first fold kept
+ // "remember the passphrase", the second silently ate it.)
+ const asked = [
+ ...folded,
+ ...turnStarts.slice(0, cut).map((start, i) => `${dropped + i + 1}. ${firstText(messages[start])}`),
+ ];
+ // A conversation folded many times would otherwise grow a note that is itself
+ // the problem. The oldest go first: their edits are furthest downstream.
+ const shown = asked.length > MAX_FOLDED_ASKS
+ ? [`(${asked.length - MAX_FOLDED_ASKS} earlier requests omitted)`, ...asked.slice(-MAX_FOLDED_ASKS)]
+ : asked;
const note = 'Earlier in this conversation you were asked, in order:\n'
- + `${asked.join('\n')}\n`
+ + `${shown.join('\n')}\n`
+ 'The tool calls and results from those runs were dropped to keep this conversation '
+ 'inside its context window. Whatever they changed is in the scene — read it back if '
+ 'you need the current state rather than trusting this summary.';
@@ -238,16 +251,26 @@ export function compactHistory(
turnStarts: turnStarts.slice(cut).map((s) => s - shift),
// Coordinates count from the start of the CONVERSATION, not of what is left.
dropped: dropped + cut,
+ folded: asked,
};
}
-/** What a person's turn said, as one line for the compaction note. They are
- * pushed as plain strings; the block form is handled for completeness. */
+/** How many past requests the note quotes before it elides the oldest. */
+const MAX_FOLDED_ASKS = 20;
+
+/**
+ * What a person's turn said, as one line for the compaction note.
+ *
+ * The FIRST text block only. A turn is pushed as a plain string and then gets
+ * the editor context appended as a second block (flushContext), which is the
+ * same paragraph every turn — joining them would quote the state of the editor
+ * once per folded run and bury the sentence that was actually said.
+ */
function firstText(message: Message | undefined): string {
const content = message?.content;
const text = typeof content === 'string'
? content
- : content?.flatMap((b) => (b.type === 'text' ? [b.text] : [])).join(' ') ?? '';
+ : content?.find((b) => b.type === 'text')?.text ?? '';
const flat = text.replace(/\s+/g, ' ').trim();
if (!flat) return '(no text)';
return flat.length > 200 ? `${flat.slice(0, 197)}…` : flat;
@@ -263,8 +286,10 @@ class AnthropicSession implements AgentSession {
/** Runs folded away by {@link compactOldest}. Turn coordinates count from the
* start of the CONVERSATION, so they must survive their messages. */
private dropped = 0;
- /** What the last call was billed for its input — the size of this whole
- * conversation as the endpoint measures it, which beats counting it here. */
+ /** What was asked in every folded run, oldest first — see compactHistory. */
+ private folded: string[] = [];
+ /** What the last call was billed for its input. Authoritative WHERE it counts
+ * the whole request — see {@link contextUsed}, which does not assume it does. */
private lastInputTokens = 0;
constructor(
@@ -294,12 +319,40 @@ class AnthropicSession implements AgentSession {
this.pending.length = 0;
}
+ /**
+ * How full the context is, in tokens — the larger of what the endpoint billed
+ * and what this conversation obviously weighs.
+ *
+ * Measured against a real gateway, `input_tokens` is NOT always the whole
+ * request: DeepSeek reported 33 for a first turn whose system prompt and 75
+ * tool schemas are thousands, so trusting it alone meant compaction would
+ * never fire and the conversation would hit the wall it exists to prevent.
+ * Nor can the estimate simply replace it — chars/4 is a rule of thumb that
+ * under-counts CJK badly, which is most of what this editor's users type.
+ *
+ * So: whichever is bigger. Both err by under-reporting, and the cost of
+ * over-reporting is one compaction that was not needed yet.
+ */
+ private contextUsed(): number {
+ let chars = this.system.length;
+ for (const tool of this.tools) {
+ chars += tool.name.length + tool.description.length + JSON.stringify(tool.schema).length;
+ }
+ for (const message of this.messages) {
+ chars += typeof message.content === 'string'
+ ? message.content.length
+ : JSON.stringify(message.content).length;
+ }
+ return Math.max(this.lastInputTokens, Math.ceil(chars / 4));
+ }
+
private compactOldest(keepRuns: number): void {
- const next = compactHistory(this.messages, this.turnStarts, this.dropped, keepRuns);
+ const next = compactHistory(this.messages, this.turnStarts, this.dropped, keepRuns, this.folded);
if (!next) return;
this.messages.splice(0, this.messages.length, ...next.messages);
this.turnStarts.splice(0, this.turnStarts.length, ...next.turnStarts);
this.dropped = next.dropped;
+ this.folded = next.folded;
}
pushContext(text: string): void {
@@ -356,10 +409,8 @@ class AnthropicSession implements AgentSession {
async *step(signal: AbortSignal): AsyncIterable<StepEvent> {
this.flushContext();
- // Fold the oldest runs away BEFORE a call that would not fit. Measured by
- // what the endpoint billed last time rather than by counting here: the
- // tokeniser is theirs, and an estimate that drifts low fails the turn.
- if (this.lastInputTokens > this.opts.contextWindow * COMPACT_AT) {
+ // Fold the oldest runs away BEFORE a call that would not fit.
+ if (this.contextUsed() > this.opts.contextWindow * COMPACT_AT) {
this.compactOldest(KEEP_WHOLE_RUNS);
}
diff --git a/desktop/electron/agent/host.ts b/desktop/electron/agent/host.ts
index 4a21976ee..e7361f336 100644
--- a/desktop/electron/agent/host.ts
+++ b/desktop/electron/agent/host.ts
@@ -247,6 +247,9 @@ export function createAgentHost(deps: AgentHostDeps): AgentHost {
phase = 'idle';
error = null;
log.length = 0;
+ // Straight to the window, not through `emit`: the log it would join was
+ // just emptied, and a window attaching later has nothing to reset.
+ deps.push({ kind: 'event', event: { type: 'conversation_reset' } });
pushStatus();
return status();
},
diff --git a/desktop/electron/agent/types.ts b/desktop/electron/agent/types.ts
index f84d36a96..543e49ed1 100644
--- a/desktop/electron/agent/types.ts
+++ b/desktop/electron/agent/types.ts
@@ -159,6 +159,15 @@ export type AgentEvent =
* header of a past run has to say which one answered it. `index` is the
* session's own coordinate for this run — see {@link AgentSession.turnIndex}. */
| { type: 'turn_start'; prompt: string; model: string; index: number }
+ /**
+ * The session was dropped; run coordinates start over at zero.
+ *
+ * Said out loud rather than left implied, because a mirror that keeps the old
+ * runs would then see the NEW run 0 as one it already has — runs are matched
+ * by the session's index, and two sessions both have a run 0. Every path that
+ * drops the conversation emits this, so no caller has to remember to.
+ */
+ | { type: 'conversation_reset' }
| { type: 'tool_start'; call: ToolCall; effect: NonNullable<CatalogTool['effect']> }
| { type: 'tool_end'; id: string; ok: boolean; summary: string; image?: string }
| { type: 'awaiting_confirm'; request: ConfirmRequest }
diff --git a/desktop/src/store/AgentStore.ts b/desktop/src/store/AgentStore.ts
index b131cacb6..744a85fbb 100644
--- a/desktop/src/store/AgentStore.ts
+++ b/desktop/src/store/AgentStore.ts
@@ -266,6 +266,10 @@ function patchTool(entries: readonly AgentEntry[], id: string, patch: Partial<Ag
* transcript for something the user never asked.
*/
export function applyAgentEvent(turns: readonly AgentTurn[], event: AgentEvent): AgentTurn[] {
+ // A new session numbers its runs from zero again, so keeping the old ones
+ // would make the next turn_start look like a repeat of one already held.
+ if (event.type === 'conversation_reset') return [];
+
if (event.type === 'turn_start') {
// Idempotent, so replaying the stream over a transcript that already has
// some of it cannot double a run (see attachAgentBridge).
diff --git a/desktop/tests/agent-compaction.test.ts b/desktop/tests/agent-compaction.test.ts
index b62607cb0..94e8aacc1 100644
--- a/desktop/tests/agent-compaction.test.ts
+++ b/desktop/tests/agent-compaction.test.ts
@@ -83,6 +83,34 @@ describe('compacting a long conversation', () => {
expect(out.dropped).toBe(13);
});
+ // The note a fold writes is NOT one of the turn starts, so the next fold
+ // splices straight over it. Carrying the asks forward is what stops the
+ // earliest request vanishing one compaction at a time — which is exactly what
+ // a real gateway showed happening: fold one kept the passphrase, fold two ate it.
+ it('keeps what was asked in EVERY fold, not just the last one', () => {
+ const first = compactHistory(...Object.values(history(6)) as [Message[], number[]], 0, 2)!;
+ expect(String(first.messages[0].content)).toContain('ask 0');
+
+ const grown = {
+ messages: [...first.messages, { role: 'user' as const, content: 'ask 6' }],
+ turnStarts: [...first.turnStarts, first.messages.length],
+ };
+ const second = compactHistory(grown.messages, grown.turnStarts, first.dropped, 1, first.folded)!;
+
+ const note = String(second.messages[0].content);
+ expect(note).toContain('ask 0'); // from the first fold
+ expect(note).toContain('ask 4'); // folded by the second
+ });
+
+ it('elides the oldest once the note would grow unbounded', () => {
+ const many = Array.from({ length: 40 }, (_, i) => `${i}. old ask ${i}`);
+ const out = compactHistory(...Object.values(history(5)) as [Message[], number[]], 0, 2, many)!;
+ const note = String(out.messages[0].content);
+ expect(note).toContain('earlier requests omitted');
+ expect(note).not.toContain('old ask 0');
+ expect(note).toContain('old ask 39');
+ });
+
// Compacting twice is the ordinary case in a conversation long enough to need
// it once — the second pass must fold the note itself away with the rest.
it('can be applied again to its own output', () => {
diff --git a/desktop/tests/agent-transcript.test.ts b/desktop/tests/agent-transcript.test.ts
index 77c4239a2..cf27697fd 100644
--- a/desktop/tests/agent-transcript.test.ts
+++ b/desktop/tests/agent-transcript.test.ts
@@ -273,8 +273,22 @@ describe('which entities a turn touched', () => {
expect([...touchedEntities(turns, 0)]).toEqual([2]);
expect([...touchedEntities(turns, 1)]).toEqual([]);
});
+
+ // Two sessions both have a run 0. A mirror that kept the old runs would treat
+ // the new one as a repeat of what it already had and drop it — which is
+ // exactly what happened the first time this was driven against a real gateway.
+ it('starts over when the conversation is dropped', () => {
+ const after = fold(
+ started('old one'), ended(),
+ { type: 'conversation_reset' },
+ started('new one'),
+ );
+ expect(after.map((t) => t.prompt)).toEqual(['new one']);
+ expect(after.map((t) => t.id)).toEqual([0]);
+ });
});
+
// The row's result column is one cell wide. It used to hold the first 20
// characters of whatever the tool returned — for a JSON answer that is `[{"id`,
// which tells you nothing and hides the count that would have.
@@ -298,4 +312,4 @@ describe('a result as one cell', () => {
it('says nothing for an empty result', () => {
expect(briefResult(' ')).toBe('');
});
-});
+});
\ No newline at end of file