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 timeline that says when a run happened, and stops offering what it already gave back

yhhc73c4c17c385
diff --git a/desktop/src/engine/historyGroups.ts b/desktop/src/engine/historyGroups.ts
index eefde02cf..ef6c91b4b 100644
--- a/desktop/src/engine/historyGroups.ts
+++ b/desktop/src/engine/historyGroups.ts
@@ -46,6 +46,8 @@ export interface TurnRow {
revertable: boolean;
/** Every step in it has been undone — the whole row reads as taken back. */
undone: boolean;
+ /** A rewind already took this run back; its transaction no longer holds. */
+ reverted: boolean;
}
export type HistoryRow = StepRow | TurnRow;
@@ -76,7 +78,23 @@ export function historyRows(
const claimed = new Map<number, TurnRow>();
const rows: HistoryRow[] = [];
+ // A run that recorded no steps still has its files to show, and it belongs
+ // where it RAN: appended after the walk, it sat below runs that came later
+ // and read as the newest thing on the timeline.
+ const stepless = windows
+ .filter((w) => w.turn.files.length > 0 && !steps.some((s) => s.id > w.from && s.id <= w.until));
+ let next = 0;
+ const flushStepless = (before: number): void => {
+ while (next < stepless.length && stepless[next].from < before) {
+ const w = stepless[next++];
+ const row = turnRow(w.turn, w.until);
+ claimed.set(w.turn.id, row);
+ rows.push(row);
+ }
+ };
+
for (const step of steps) {
+ flushStepless(step.id);
const w = owner(step.id);
if (!w) {
rows.push({ kind: 'step', step });
@@ -92,20 +110,13 @@ export function historyRows(
}
row.steps.push(step);
}
-
- // A turn that recorded no steps still has its files to show — the case a
- // steps-only reading of a turn missed entirely.
- for (const w of windows) {
- if (!claimed.has(w.turn.id) && w.turn.files.length > 0) {
- const row = turnRow(w.turn, w.until);
- claimed.set(w.turn.id, row);
- rows.push(row);
- }
- }
+ flushStepless(Infinity);
for (const row of rows) {
if (row.kind !== 'turn') continue;
- row.undone = row.steps.length > 0 && row.steps.every((s) => s.undone);
+ // A rewind hands a transaction's copies back and cannot re-apply them, so a
+ // run it took is spent whether or not it had steps to undo.
+ row.undone = row.reverted || (row.steps.length > 0 && row.steps.every((s) => s.undone));
// A files-only run has no steps to undo, and its transaction is the whole
// of what there is to take back.
row.revertable = !row.undone && (row.steps.length > 0 || row.files.length > 0);
@@ -127,6 +138,7 @@ function turnRow(turn: AgentTurn, endSeq: number): TurnRow {
endSeq,
revertable: false,
undone: false,
+ reverted: turn.reverted === true,
};
}
diff --git a/desktop/src/engine/rewind.ts b/desktop/src/engine/rewind.ts
index 0814d9059..77128e82d 100644
--- a/desktop/src/engine/rewind.ts
+++ b/desktop/src/engine/rewind.ts
@@ -17,7 +17,7 @@
*/
import { EditorHistory, type HistoryMark } from './EditorHistory';
import { Toasts } from '@/store/Toasts';
-import type { AgentTurn } from '@/store/AgentStore';
+import { markReverted, type AgentTurn } from '@/store/AgentStore';
import type { FileChange } from '../../electron/agent/types';
import { t } from '@/i18n';
@@ -42,7 +42,7 @@ export interface RewindPlan {
*/
export function planRewind(point: number, turns: readonly AgentTurn[]): RewindPlan {
const runs: RewindPlan['runs'] = turns
- .filter((t) => t.tx && ((t.mark as HistoryMark | null)?.seq ?? -1) >= point)
+ .filter((t) => t.tx && !t.reverted && ((t.mark as HistoryMark | null)?.seq ?? -1) >= point)
.map((t) => ({ id: t.id, prompt: t.prompt, tx: t.tx! }));
const files = turns
.filter((t) => runs.some((r) => r.id === t.id))
@@ -70,6 +70,9 @@ export async function rewind(plan: RewindPlan): Promise<RewindResult> {
if (plan.runs.length === 0) return { steps, restored: [], unjournaled: [], failed: [] };
const result = await window.estella?.agent?.revertFiles?.(plan.runs.map((r) => r.tx));
+ // Their copies are spent whatever came of the write: a second attempt has
+ // nothing left to hand back, and offering one says otherwise.
+ markReverted(plan.runs.map((r) => r.id));
return {
steps,
restored: result?.restored ?? [],
diff --git a/desktop/src/store/AgentStore.ts b/desktop/src/store/AgentStore.ts
index 38d05fc47..866df8402 100644
--- a/desktop/src/store/AgentStore.ts
+++ b/desktop/src/store/AgentStore.ts
@@ -174,6 +174,9 @@ export interface AgentTurn {
*/
tx: string | null;
files: readonly FileChange[];
+ /** A rewind has already handed this transaction's copies back. It stays on
+ * the timeline as what happened; what it no longer is, is revertable. */
+ reverted?: boolean;
/**
* Whether the WORK held — the one thing about a turn the model does not get
* to report. `reason` says how it stopped; this says whether what it stopped
@@ -426,6 +429,14 @@ export async function selectAgentModel(providerId: string, model: string): Promi
}
export const dismissCheckpoint = (turnId: number): void => useAgent.setState({ checkpointDone: turnId });
+/** Record that a rewind took these runs, so nothing offers to take them again. */
+export function markReverted(ids: readonly number[]): void {
+ const taken = new Set(ids);
+ useAgent.setState((s) => ({
+ turns: s.turns.map((t) => (taken.has(t.id) ? { ...t, reverted: true } : t)),
+ }));
+}
+
/** What a finished turn's Revert would cover. See {@link revertScope}. */
export interface RevertScope {
steps: number;
@@ -444,7 +455,7 @@ export interface RevertScope {
* undo cannot reach. `stepsNow` is the stack past the mark, the caller's to read.
*/
export function revertScope(turn: AgentTurn, stepsNow: number): RevertScope | null {
- if (turn.reason === null) return null;
+ if (turn.reason === null || turn.reverted) return null;
if (turn.steps === 0 && turn.files.length === 0) return null;
return {
steps: turn.steps,
diff --git a/desktop/tests/history-groups.test.ts b/desktop/tests/history-groups.test.ts
index 9772be5f7..233b8aaf5 100644
--- a/desktop/tests/history-groups.test.ts
+++ b/desktop/tests/history-groups.test.ts
@@ -100,6 +100,27 @@ describe('folding the timeline into rows', () => {
expect(turns(rows)[0]).toMatchObject({ files: [{ path: 'src/HP.ts' }], tx: 'tx-1' });
});
+ // Appended after the walk, a run that recorded no steps sat below every run
+ // that came later and read as the newest thing on the timeline.
+ it('puts a files-only run where it ran, not after the steps that followed it', () => {
+ const rows = historyRows(
+ [step(1), step(2)],
+ [turn(0, 0, 0, { tx: 'tx-a', files: [file('src/A.ts')] }), turn(1, 1, 2)],
+ );
+ expect(rows.map((r) => r.kind)).toEqual(['turn', 'step', 'turn']);
+ expect((rows[0] as TurnRow).id).toBe(0);
+ expect((rows[2] as TurnRow).id).toBe(1);
+ });
+
+ it('keeps two files-only runs in the order they ran', () => {
+ const rows = historyRows([step(1)], [
+ turn(0, 0, 0, { tx: 'tx-a', files: [file('src/A.ts')] }),
+ turn(1, 0, 0, { tx: 'tx-b', files: [file('src/B.ts')] }),
+ ]);
+ expect(turns(rows).map((r) => r.id)).toEqual([0, 1]);
+ expect(rows[2]).toMatchObject({ kind: 'step', step: { id: 1 } });
+ });
+
it('shows nothing for a turn that did neither', () => {
expect(historyRows([], [turn(0, 0, 0)])).toEqual([]);
});
@@ -140,6 +161,16 @@ describe('which turn may offer a Revert', () => {
const rows = historyRows([step(1), step(2)], [turn(0, 0, 1)]);
expect(turns(rows)[0].revertable).toBe(true);
});
+
+ // A reverted transaction hands its copies back and cannot re-apply them, so a
+ // second press has nothing to give — and a files-only run has no undone step
+ // to work that out from.
+ it('withdraws it from a run a rewind already took', () => {
+ const rows = historyRows([], [turn(0, 0, 0, {
+ tx: 'tx-1', files: [file('src/HP.ts')], reverted: true,
+ })]);
+ expect(turns(rows)[0]).toMatchObject({ revertable: false, undone: true });
+ });
});
describe('what a row counts', () => {
diff --git a/desktop/tests/rewind-plan.test.ts b/desktop/tests/rewind-plan.test.ts
index 3c652e17c..38df557a7 100644
--- a/desktop/tests/rewind-plan.test.ts
+++ b/desktop/tests/rewind-plan.test.ts
@@ -63,6 +63,14 @@ describe('what going back to a point takes with it', () => {
expect(plan.runs).toEqual([]);
});
+ // Its copies went back the first time. Counted again, the confirmation states
+ // a reach that includes files nothing can restore.
+ it('ignores a run an earlier rewind already took', () => {
+ const plan = planRewind(0, [turn(0, 0, 3, { reverted: true }), turn(1, 3, 6)]);
+ expect(plan.runs.map((r) => r.id)).toEqual([1]);
+ expect(plan.files.map((f) => f.path)).toEqual(['src/run1.ts']);
+ });
+
// What it cannot put back is named in the plan, so the confirmation can say
// it BEFORE the gesture rather than a toast saying it afterwards.
it('names what it would leave behind', () => {