[118] Verlauf übersteht Kern-Neustart: TS-Kopie der Schritte, nach Panic wieder eingesetzt

This commit is contained in:
Claude committed 2026-11-05 10:01:34 +00:00
1 parent 40f73fca04
commit 3e1f4e076d
7 files changed
+200 -27

No files matched your search

+9 -2
View File
@@ -1065,8 +1065,15 @@ Prüfung des ganzen Programms (Rust-Kern, TS-Hülle, Build/Sicherheit), nur Code
`coreEpoch()` als ungültig; Hülle (`useProjectFileActions`): neu zeichnen, Autosave-Kopie, Verlaufsanzeige,
`notify(t("core.restarted"))` (gedrosselt, 5 s). Tests: verstecktes `__debug_panic` (keine Fassade);
`engine/coreRecovery.test.ts` (Node) und `e2e/coreRecovery.spec.ts` (Chromium, Test-Naht `?dossierTest` →
`state/testHooks.ts`). Offen: Undo-Verlauf geht beim Neustart verloren (Meldung sagt es) — Wiederaufbau per
Op-Log wäre der nächste Schritt; nicht in Tauri (WKWebView) und nicht im gepackten Electron geprüft. · M
`state/testHooks.ts`). 5.11.: Verlauf übersteht den Neustart — `core/historyBackup.ts` hält eine Kopie der
Kern-Schritte (nach jeder angenommenen Änderung/Undo/Redo/Gruppenende: `doc_history_index` = Laufnummern +
Koaleszenz-Schlüssel, nur neue Schritte als JSON über `doc_history_step`; gleiche Grenzen wie der Kern, Schritte
> 8 MB und Gesamt > 64 M Zeichen werden nicht kopiert → Verlauf endet dort). Nach dem Neustart setzt
`doc_history_import` sie ins neu geöffnete Dokument, nur wenn die Kopie zur Leseansicht gehört (sonst leer +
Meldung `core.restartedHistoryLost`); die abgebrochene Änderung war nie angenommen, wird also nie wiederholt.
Tests: Rust `verlauf_export_import_*` (Gruppen, Koaleszenz, Grenzen), `coreRecovery.test.ts`. Offen: Panic
mitten in Undo/Redo stellt den Stand davor wieder her — erneutes Undo trifft denselben Schritt; nicht in Tauri
(WKWebView) und nicht im gepackten Electron geprüft. · M
- ✅ **[119] Referenz-Ausgaben (golden).** 3.11.: `src/plan/golden.test.ts` — beide Musterprojekte: Grundriss je Geschoss
(1:100, 1:50), Schnitt, Ansicht, 3D-Zusammenfassung als Digest (Zähler, Rahmen, Hash gerundeter Zahlen) in
`src/plan/__golden__/`; Update mit `vitest -u`. Nur Linux geprüft; Hash deckt Detailabweichungen ab, Diff zeigt nur Zähler. · M
+29 -10
View File
@@ -11,6 +11,7 @@ import { fromMarkedJson, toMarkedJson } from "./ops";
import type { CoreOp } from "./ops";
import { opsMutation } from "./opsBuild";
import { StepStates } from "./stepStates";
import { HistoryBackup } from "./historyBackup";
import type { ProjectMutation } from "./opsBuild";
import type { IfcImportResult } from "../io/ifcImport";
import type { MeshImportReport } from "../io/meshImport";
@@ -180,6 +181,8 @@ export class ProjectDocument {
private limits: [number, number] | null = null;
/** Stände grosser Verlaufsschritte (Undo/Redo ohne Ops-Rückgabe, s. `stepStates.ts`). */
private steps = new StepStates();
/** Kopie des Kern-Verlaufs für einen Neustart nach Panic ([118]). */
private hist = new HistoryBackup();
/** Gelieferte Einträge des 3D-Modells je Argument-Satz ([58], {@link model3dParts}). */
private m3d = new Map<string, Model3dDeltaCache>();
@@ -198,14 +201,27 @@ export class ProjectDocument {
/**
* Nach einem Kern-Neustart ([118], `coreWasm.ts`): die alte Instanz samt Handle und Verlauf ist weg;
* das Dokument öffnet sich aus der Leseansicht neu (die ist der letzte angenommene Stand — eine
* Änderung, deren Aufruf den Panic auslöste, war noch nicht übernommen). Der Verlauf beginnt leer.
* Änderung, deren Aufruf den Panic auslöste, war noch nicht übernommen). Der Verlauf kommt aus der
* TS-Kopie zurück (`historyBackup.ts`), wenn sie zu dieser Leseansicht gehört; sonst beginnt er leer.
*/
restoreAfterRestart(): void {
this.m3d.clear();
const wasOpen = this.handle !== 0;
this.handle = 0;
this.steps.clear();
if (wasOpen) this.reopen(this.mirror);
this.historyKept = true;
if (!wasOpen) return this.hist.clear();
this.reopen(this.mirror);
this.historyKept = this.hist.restore(this.handle, this.mirror);
}
/** Ob beim letzten Kern-Neustart der Verlauf erhalten blieb ([118]; Meldung an den Benutzer). */
historyKept = true;
/** Neue Leseansicht nach einer angenommenen Änderung bzw. Undo/Redo; Verlaufskopie nachführen ([118]). */
private setMirror(project: Project): void {
this.mirror = project;
this.hist.sync(this.handle, project);
}
private reopen(project: Project): void {
@@ -293,6 +309,7 @@ export class ProjectDocument {
this.steps.clear();
this.m3d.clear();
if (!this.handle || !core().doc_load_json(this.handle, JSON.stringify(project))) this.reopen(project);
this.hist.sync(this.handle, project);
}
/**
@@ -308,7 +325,7 @@ export class ProjectDocument {
| { ok: false; error: string };
if (!r.ok) return r;
const project = JSON.parse(core().doc_json(this.handle)) as Project;
this.mirror = project;
this.setMirror(project);
return { ok: true, project, warnings: r.warnings, migratedFrom: r.migratedFrom };
}
@@ -382,7 +399,7 @@ export class ProjectDocument {
}
const extra = this.lastExtra();
const project = this.withExtra(applyOps(base, ops, core().doc_last_hits(this.handle)), extra);
this.mirror = project;
this.setMirror(project);
this.rememberCommit(base, project, extra.length ? [...ops, ...extra] : ops);
return { project };
}
@@ -403,7 +420,7 @@ export class ProjectDocument {
if (r.ops.length === 0) return { project: base, ops: r.ops, notice: r.notice };
const extra = this.lastExtra();
const project = this.withExtra(applyOps(base, r.ops, core().doc_last_hits(this.handle)), extra);
this.mirror = project;
this.setMirror(project);
return { project, ops: extra.length ? [...r.ops, ...extra] : r.ops, notice: r.notice };
}
@@ -452,7 +469,7 @@ export class ProjectDocument {
if (r.ops.length === 0) return { project: base, ops: r.ops };
const extra = this.lastExtra();
const project = this.withExtra(applyOps(base, r.ops), extra);
this.mirror = project;
this.setMirror(project);
return { project, ops: extra.length ? [...r.ops, ...extra] : r.ops };
}
@@ -470,7 +487,7 @@ export class ProjectDocument {
if ("problems" in r) return r;
const extra = r.ops.length ? this.lastExtra() : [];
const project = r.ops.length ? this.withExtra(applyOps(base, r.ops), extra) : base;
this.mirror = project;
this.setMirror(project);
const ops = extra.length ? [...r.ops, ...extra] : r.ops;
this.rememberCommit(base, project, ops);
return { ...r, ops, project };
@@ -558,14 +575,14 @@ export class ProjectDocument {
if (this.steps.has(top)) {
if (!core().doc_jump_quiet(this.handle, !undo)) return null;
const project = this.steps.jump(top, base, undo, this.historySeqs()[undo ? 1 : 0]);
this.mirror = project;
this.setMirror(project);
return project;
}
const json = core()[fn](this.handle);
if (!json) return null;
const ops = JSON.parse(json) as CoreOp[];
const project = applyOps(base, ops, core().doc_last_hits(this.handle));
this.mirror = project;
this.setMirror(project);
// Gegenschritt (neu oben auf dem anderen Stapel) führt zwischen denselben Ständen.
const next = this.historySeqs()[undo ? 1 : 0];
if (undo) this.steps.remember(next, project, base, ops);
@@ -582,7 +599,7 @@ export class ProjectDocument {
const json = core().doc_history_jump(this.handle, target);
if (!json) return null;
const project = applyOps(base, JSON.parse(json) as CoreOp[], core().doc_last_hits(this.handle));
this.mirror = project;
this.setMirror(project);
return project;
}
@@ -630,6 +647,7 @@ export class ProjectDocument {
clearHistory(): void {
this.steps.clear();
if (this.handle) core().doc_history_clear(this.handle);
this.hist.sync(this.handle, this.mirror);
}
/** Grenzen des Verlaufs (Schritte, ungefähre Bytes). */
@@ -654,6 +672,7 @@ export class ProjectDocument {
endGroup(): void {
if (this.handle) core().doc_group_end(this.handle);
this.hist.sync(this.handle, this.mirror);
}
}
+91
View File
@@ -0,0 +1,91 @@
// Kopie des Undo/Redo-Verlaufs über einen Kern-Neustart ([118]): Der Verlauf lebt im Rust-Dokument und
// wäre nach einem Panic mit der alten WASM-Instanz weg. `ProjectDocument` gleicht diese Kopie nach jeder
// angenommenen Änderung (und Undo/Redo) ab: der Kern liefert die Laufnummern beider Stapel
// (`doc_history_index`), TS holt nur die Schritte, die es noch nicht kennt (`doc_history_step`, JSON je
// Schritt, unverändert gehalten). Nach dem Neustart setzt `restore` sie in das frisch geöffnete Dokument ein
// (`doc_history_import`) — aber nur, wenn die Kopie zur Leseansicht gehört, aus der das Dokument neu
// geöffnet wurde. Eine Änderung, deren Aufruf den Panic auslöste, war nie angenommen: weder in der
// Leseansicht noch im Verlauf, sie wird also nicht wiederholt.
//
// Speicher: nur Schritte, die der Kern selbst noch hält (gleiche Grenzen wie dort); einzelne Schritte über
// MAX_STEP_BYTES und die ältesten über MAX_TOTAL_CHARS werden nicht kopiert — der Verlauf endet dann dort
// (Undo bis vor den fehlenden Schritt, Redo ebenso).
import { core } from "../engine/coreWasm";
/** Grösster kopierter Schritt (geschätzter Speicher im Kern, Bytes); grössere: Verlauf endet dort. */
export const MAX_STEP_BYTES = 8 << 20;
/** Höchstens so viele Zeichen JSON insgesamt; darüber fallen die ältesten Undo-Schritte weg. */
export const MAX_TOTAL_CHARS = 64 << 20;
interface HistoryIndex {
undo: number[];
redo: number[];
lastKey: string | null;
lastAt: number;
}
export class HistoryBackup {
/** Leseansicht, zu der die Kopie gehört (Identität). */
private at: object | null = null;
private index: HistoryIndex | null = null;
/** JSON je Laufnummer; `null` = nicht kopiert (zu gross). */
private steps = new Map<number, string | null>();
private chars = 0;
/** Kopie an den Verlauf von Dokument `handle` angleichen; `mirror` = dessen Leseansicht. */
sync(handle: number, mirror: object): void {
const json = handle ? core().doc_history_index(handle) : "";
if (!json) return this.clear();
const index = JSON.parse(json) as HistoryIndex;
const live = new Set([...index.undo, ...index.redo]);
for (const [seq, s] of this.steps)
if (!live.has(seq)) {
this.chars -= s?.length ?? 0;
this.steps.delete(seq);
}
for (const seq of live)
if (!this.steps.has(seq)) {
const s = core().doc_history_step(handle, seq, MAX_STEP_BYTES) || null;
this.chars += s?.length ?? 0;
this.steps.set(seq, s);
}
for (const seq of index.undo) {
if (this.chars <= MAX_TOTAL_CHARS) break;
this.chars -= this.steps.get(seq)?.length ?? 0;
this.steps.set(seq, null);
}
this.index = index;
this.at = mirror;
}
/**
* Nach dem Neustart: Verlauf in Dokument `handle` einsetzen, wenn die Kopie zu `mirror` gehört (das
* Dokument wurde aus `mirror` geöffnet). `true` = eingesetzt (oder nichts einzusetzen).
*/
restore(handle: number, mirror: object): boolean {
const index = this.index;
const ok = !index || (this.at === mirror && handle !== 0 && core().doc_history_import(handle, this.importJson(index)));
this.clear();
if (ok) this.sync(handle, mirror); // neue Laufnummern
return ok;
}
clear(): void {
this.at = null;
this.index = null;
this.steps.clear();
this.chars = 0;
}
/** Stapel bis zum jüngsten fehlenden Schritt abgeschnitten (über ihn führt kein Undo/Redo). */
private importJson(index: HistoryIndex): string {
const stack = (seqs: number[]) => {
let from = 0;
seqs.forEach((s, i) => {
if (!this.steps.get(s)) from = i + 1;
});
return seqs.slice(from).map((s) => this.steps.get(s) as string);
};
return `{"undo":[${stack(index.undo).join(",")}],"redo":[${stack(index.redo).join(",")}],"lastKey":${JSON.stringify(index.lastKey)},"lastAt":${JSON.stringify(index.lastAt)}}`;
}
}
+63 -10
View File
@@ -1,7 +1,7 @@
// [118] Wiederanlauf nach Kern-Panic: der versteckte Test-Export `__debug_panic` (web/document.rs)
// panict mitten in einem Dokument-Zugriff — danach wäre die alte Instanz tot (geborgte RefCell).
// Geprüft: `core()` startet (asynchron) neu, die Dokumente öffnen sich aus der Leseansicht, der Store behält
// das Projekt, weitere Abfragen/Änderungen/Undo laufen, der Vorgang wird nicht wiederholt.
// Geprüft: `core()` startet (asynchron) neu, die Dokumente öffnen sich aus der Leseansicht samt Verlauf (TS-Kopie,
// `core/historyBackup.ts`), der Store behält das Projekt, Undo/Redo laufen wie vorher, der Vorgang wird nicht wiederholt.
import { beforeEach, describe, expect, it, vi } from "vitest";
import { core, coreEpoch, coreReady, CoreRestartingError, CoreTrapError, isCoreTrap, onCoreRestart } from "./coreWasm";
import * as raw from "./pkgCore/core.js";
@@ -10,6 +10,7 @@ import { storeDocument } from "../state/historySlice";
import { sampleProject } from "../model/sampleProject";
import { patchItem } from "../core/opsBuild";
import { documentFor, ProjectDocument } from "../core/document";
import { HistoryBackup } from "../core/historyBackup";
import type { Project } from "../model/types";
const st = () => getState();
@@ -59,16 +60,27 @@ describe("Kern-Neustart nach Panic ([118])", () => {
doc.close();
});
it("Panic während einer Store-Änderung: Projekt unverändert, Verlauf leer, danach Änderung + Undo", async () => {
const id = st().project.walls[0].id;
st().applyChange((p) => patchItem(p, "walls", id, { height: 3.1 }));
expect(st().undoDepth).toBe(1);
it("Panic während einer Store-Änderung: Projekt unverändert, Verlauf wiederhergestellt, Undo/Redo wie vorher", async () => {
const [w0, w1] = st().project.walls;
const h0 = w0.height;
st().applyChange((p) => patchItem(p, "walls", w0.id, { height: 3.1 }));
// Koaleszenz (Tippen) und Gruppe: je ein Schritt.
st().applyChange((p) => patchItem(p, "walls", w1.id, { height: 2.5 }), "tippen");
st().applyChange((p) => patchItem(p, "walls", w1.id, { height: 2.6 }), "tippen");
st().groupChanges(() => {
st().applyChange((p) => patchItem(p, "walls", w0.id, { loadBearing: true }));
st().applyChange((p) => patchItem(p, "walls", w1.id, { loadBearing: true }));
});
st().applyChange((p) => patchItem(p, "walls", w0.id, { height: 3.3 }));
st().undo(); // ein Redo-Schritt liegt bereit
expect([st().undoDepth, st().redoDepth]).toEqual([3, 1]);
const states = [st().project];
const before = st().project;
const err = vi.spyOn(console, "error").mockImplementation(() => {});
expect(() =>
st().applyChange((p) => {
core().__debug_panic(storeDocument(p).id);
return patchItem(p, "walls", id, { height: 9 });
return patchItem(p, "walls", w0.id, { height: 9 });
}),
).toThrow(CoreTrapError);
await coreReady();
@@ -76,9 +88,32 @@ describe("Kern-Neustart nach Panic ([118])", () => {
expect(st().project).toBe(before);
expect(st().project.walls[0].height).toBe(3.1);
expectMirror();
// Verlauf nach dem Neustart leer (nicht wiederhergestellt); neue Änderungen haben wieder Undo.
expect(storeDocument(st().project).historyState().undo).toBe(0);
st().applyChange((p) => patchItem(p, "walls", id, { height: 3.4 }));
// Verlauf wiederhergestellt (ohne die abgebrochene Änderung): gleiche Tiefen, Undo/Redo wie vorher.
const doc = storeDocument(st().project);
expect(doc.historyKept).toBe(true);
expect(doc.historyState()).toMatchObject({ undo: 3, redo: 1 });
st().redo();
expect(st().project.walls[0].height).toBe(3.3);
expectMirror();
st().undo();
for (const expected of [
{ h0: 3.1, h1: 2.6, t0: w0.loadBearing, t1: w1.loadBearing }, // Gruppe zurück
{ h0: 3.1, h1: w1.height, t0: w0.loadBearing, t1: w1.loadBearing }, // Tipp-Serie zurück
{ h0, h1: w1.height, t0: w0.loadBearing, t1: w1.loadBearing }, // erste Änderung zurück
]) {
st().undo();
const [a, b] = st().project.walls;
expect({ h0: a.height, h1: b.height, t0: a.loadBearing, t1: b.loadBearing }).toEqual(expected);
expectMirror();
}
expect(st().canUndo).toBe(false);
st().redo();
st().redo();
st().redo();
expect(st().project.walls[0].loadBearing).toBe(true);
expect(JSON.parse(storeDocument(st().project).json())).toEqual(JSON.parse(JSON.stringify(states[0])));
// Neue Änderungen nach dem Neustart: eigener Undo-Schritt.
st().applyChange((p) => patchItem(p, "walls", w0.id, { height: 3.4 }));
expect(st().project.walls[0].height).toBe(3.4);
st().undo();
expect(st().project.walls[0].height).toBe(3.1);
@@ -87,6 +122,24 @@ describe("Kern-Neustart nach Panic ([118])", () => {
expect(() => documentFor(st().project).query("kennzahlen")).not.toThrow();
});
it("Verlaufskopie nur für die Leseansicht, zu der sie gehört", () => {
const doc = new ProjectDocument(structuredClone(sampleProject) as Project);
const id = doc.current.walls[0].id;
const r = doc.commit(doc.current, patchItem(doc.current, "walls", id, { height: 2.9 }), undefined, 0);
expect("project" in r).toBe(true);
const backup = new HistoryBackup();
backup.sync(doc.id, doc.current);
const other = new ProjectDocument(structuredClone(sampleProject) as Project);
expect(backup.restore(other.id, other.current)).toBe(false);
expect(other.historyState().undo).toBe(0);
backup.sync(doc.id, doc.current);
const fresh = new ProjectDocument(doc.current);
expect(backup.restore(fresh.id, fresh.current)).toBe(true);
expect(fresh.historyState().undo).toBe(1);
expect(fresh.undo(fresh.current)?.walls[0].height).toBe(sampleProject.walls[0].height);
for (const d of [doc, other, fresh]) d.close();
});
it("isCoreTrap: Trap ja, gewöhnlicher Fehler nein", () => {
expect(isCoreTrap(new WebAssembly.RuntimeError("unreachable"))).toBe(true);
expect(isCoreTrap(new Error("Kern-Abfrage: Dokument nicht offen"))).toBe(false);
+2 -1
View File
@@ -2415,7 +2415,8 @@ export const de = {
"startup.previousCrashed": "Dossier wurde beim letzten Mal nicht sauber beendet. Ungesicherte Arbeit wird beim Öffnen zur Wiederherstellung angeboten.",
"project.open.failed": "Projekt konnte nicht geöffnet werden.",
"core.restarting": "Kern startet nach einem internen Fehler neu …",
"core.restarted": "Interner Fehler im Kern – der Vorgang wurde abgebrochen, das Projekt ist wiederhergestellt. Der Rückgängig-Verlauf wurde dabei geleert.",
"core.restarted": "Interner Fehler im Kern – der Vorgang wurde abgebrochen, Projekt und Rückgängig-Verlauf sind wiederhergestellt.",
"core.restartedHistoryLost": "Interner Fehler im Kern – der Vorgang wurde abgebrochen, das Projekt ist wiederhergestellt. Der Rückgängig-Verlauf liess sich nicht wiederherstellen und wurde geleert.",
"project.save.failed": "Projekt konnte nicht gespeichert werden; die Datei und die Wiederherstellungskopie bleiben unverändert.",
"menu.native.clashCheck": "Kollisionsprüfung …",
"menu.native.kennzahlen": "Kennzahlen und Raumprogramm …",
+2 -1
View File
@@ -2365,7 +2365,8 @@ export const en: Record<TranslationKey, string> = {
"startup.previousCrashed": "Dossier did not shut down cleanly last time. Unsaved work is offered for recovery when you open it.",
"project.open.failed": "Project could not be opened.",
"core.restarting": "Core is restarting after an internal error …",
"core.restarted": "Internal error in the core – the operation was aborted and the project has been restored. The undo history was cleared.",
"core.restarted": "Internal error in the core – the operation was aborted; the project and the undo history have been restored.",
"core.restartedHistoryLost": "Internal error in the core – the operation was aborted and the project has been restored. The undo history could not be restored and was cleared.",
"project.save.failed": "Project could not be saved; the file and the recovery copy are unchanged.",
"menu.native.clashCheck": "Clash detection …",
"menu.native.kennzahlen": "Key figures and room programme …",
+4 -3
View File
@@ -155,7 +155,7 @@ export function useProjectFileActions(
return () => window.clearInterval(id);
}, []);
// Kern-Panic ([118], `engine/coreWasm.ts`): der Kern ist neu gestartet und hat das Projekt aus der
// Leseansicht wieder geöffnet (Verlauf leer). Sofort eine Autosave-Kopie, Verlaufsanzeige nachführen,
// Leseansicht wieder geöffnet samt Verlauf (TS-Kopie, `core/historyBackup.ts`). Sofort eine Autosave-Kopie, Verlaufsanzeige nachführen,
// melden (gedrosselt) und neu zeichnen (Kern-Handles der Ansichten sind neu aufzubauen).
const [, setCoreTick] = useState(0);
useEffect(
@@ -169,8 +169,9 @@ export function useProjectFileActions(
} catch (e) {
console.error("Autosave nach Kern-Neustart fehlgeschlagen.", e);
}
setState(historyPatch(storeDocument(getState().project)));
notify(t("core.restarted"), "error");
const doc = storeDocument(getState().project);
setState(historyPatch(doc));
notify(t(doc.historyKept ? "core.restarted" : "core.restartedHistoryLost"), "error");
}),
[],
);