feat: save, undo and redo direct edits

Step 5 of the map editor. EditSession becomes the single owner of the constraint
set, replacing the pair of refs step 4 kept alongside it: it now holds the saved
operations too, and `fragment()` assembles the constraints plus every operation
they reference — the shape both preview and save send. That removes the class of
bug that produced the earlier 400, because callers can no longer ship a
constraint whose provenance points at nothing.

Save goes through `expectedDocumentVersion`. A 409 adopts the server's version so
the next attempt is checked against reality, and says which version won instead
of failing silently. Undo of a saved gesture appends an inverse operation rather
than rewriting persisted history; undo of an unsaved one just moves the cursor.
Discard drops unsaved commands, cancels anything in flight, and returns the map
to the baseline.

One gesture mints one operation id, released on pointerup. Reusing an id across
gestures put two operations with the same id in the document, which
validateEditDocument() rejects.

The API test now covers the payload the client actually sends — `constraints` +
`operations` merged into the active document, rather than a whole document — and
asserts that a save survives a reload as `exact`. Sending constraints without
their operations is asserted to be rejected rather than written.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
que01andClaude Opus 5 committed 2026-08-27 18:11:52 +08:00
1 parent 92297270f1
commit 38593f6f67
4 files changed
+280 -42

No files matched your search

+67 -2
View File
@@ -40,7 +40,7 @@ const error: EditDiagnostic = {
describe('command stack', () => {
it('starts clean on the loaded baseline', () => {
const session = new EditSession([constraint('c1', 1)], 3);
const session = new EditSession([constraint('c1', 1)], [], 3);
expect(session.constraints()).toHaveLength(1);
expect(session.canUndo).toBe(false);
expect(session.canRedo).toBe(false);
@@ -124,12 +124,77 @@ describe('saved history is append-only', () => {
});
it('adopts the server version after a conflict', () => {
const session = new EditSession([], 1);
const session = new EditSession([], [], 1);
session.setDocumentVersion(7);
expect(session.documentVersion).toBe(7);
});
});
describe('document fragment', () => {
const operation = (id: string, constraintIds: string[]) => ({ id, createdAt: 'now', constraintIds });
it('carries the operations the saved constraints reference', () => {
// Sending a constraint without its operation is a 400 from
// validateEditDocument(), so the fragment has to cover the saved half too.
const session = new EditSession([constraint('c1', 1)], [operation('op', ['c1'])], 2);
expect(session.fragment()).toEqual({
constraints: [constraint('c1', 1)],
operations: [operation('op', ['c1'])],
});
});
it('adds the pending operations of unsaved gestures', () => {
const session = new EditSession([constraint('c1', 1)], [operation('op', ['c1'])], 2);
session.commit([constraint('c1', 1), constraint('c2', 3)], ['c2'], identity(1));
const fragment = session.fragment();
expect(fragment.constraints.map((item) => item.id)).toEqual(['c1', 'c2']);
expect(fragment.operations.map((item) => item.id)).toEqual(['op', 'op-1']);
});
it('does not list an operation twice when a gesture is re-committed', () => {
const session = new EditSession([]);
session.commit([constraint('c1', 1)], ['c1'], identity(1));
session.markSaved(1);
// The same operation is now in the baseline; committing again must not duplicate it.
session.commit([constraint('c1', 5)], ['c1'], identity(1));
expect(session.fragment().operations.map((item) => item.id)).toEqual(['op-1']);
});
});
describe('loading and discarding', () => {
it('adopts a server document as the new baseline with no history', () => {
const session = new EditSession([]);
session.commit([constraint('c1', 2)], ['c1'], identity(1));
session.load([constraint('c9', 4)], [{ id: 'op-9', createdAt: 'now', constraintIds: ['c9'] }], 12);
expect(session.constraints()).toEqual([constraint('c9', 4)]);
expect(session.documentVersion).toBe(12);
expect(session.canUndo).toBe(false);
expect(session.canRedo).toBe(false);
expect(session.dirty).toBe(false);
});
it('mutates in place so the preview requester keeps arbitrating', () => {
// The requester captures the session once; a swapped object would leave it
// deciding staleness for a session nobody reads.
const session = new EditSession([]);
const captured = session;
session.load([constraint('c1', 1)], [], 3);
expect(captured.constraints()).toEqual([constraint('c1', 1)]);
});
it('discards unsaved gestures but keeps saved ones', () => {
const session = new EditSession([]);
session.commit([constraint('c1', 2)], ['c1'], identity(1));
session.markSaved(1);
session.commit([constraint('c1', 2), constraint('c2', 3)], ['c2'], identity(2));
expect(session.dirty).toBe(true);
session.discard();
expect(session.constraints()).toEqual([constraint('c1', 2)]);
expect(session.dirty).toBe(false);
expect(session.canRedo).toBe(false);
});
});
describe('previewSeq arbitration', () => {
it('hands out a monotonic sequence', () => {
const session = new EditSession([]);
+40 -1
View File
@@ -37,6 +37,8 @@ export interface PreviewOutcome {
export class EditSession {
private baseline: RoadConstraint[];
/** Operations of the saved constraints. A constraint whose operation is missing is a 400. */
private baselineOperations: RoadEditOperation[];
private stack: Command[] = [];
private cursor = 0;
private seq = 0;
@@ -49,11 +51,26 @@ export class EditSession {
constraintStates: ConstraintStateReport[] = [];
degraded = false;
constructor(constraints: RoadConstraint[] = [], documentVersion = 0) {
constructor(constraints: RoadConstraint[] = [], operations: RoadEditOperation[] = [], documentVersion = 0) {
this.baseline = [...constraints];
this.baselineOperations = [...operations];
this.version = documentVersion;
}
/**
* The document fragment both preview and save send: the effective constraints
* plus every operation they reference. Assembling it here is what keeps callers
* from shipping constraints whose provenance points at nothing.
*/
fragment(): { constraints: RoadConstraint[]; operations: RoadEditOperation[] } {
const pending = this.pendingOperations();
const ids = new Set(pending.map((operation) => operation.id));
return {
constraints: this.constraints(),
operations: [...this.baselineOperations.filter((operation) => !ids.has(operation.id)), ...pending],
};
}
/** Effective constraint set: the top applied command, or the saved baseline. */
constraints(): RoadConstraint[] {
return this.cursor > 0 ? this.stack[this.cursor - 1].after : this.baseline;
@@ -144,6 +161,7 @@ export class EditSession {
/** After a successful save: current state becomes the baseline, history closes. */
markSaved(documentVersion: number): void {
this.baselineOperations = this.fragment().operations;
this.baseline = this.constraints();
this.stack = this.stack.slice(0, this.cursor).map((command) => ({ ...command, saved: true }));
this.cursor = this.stack.length;
@@ -155,6 +173,27 @@ export class EditSession {
this.version = documentVersion;
}
/**
* Adopt a document the server handed us: new baseline, history cleared.
*
* Mutates in place rather than returning a new session because the preview
* requester holds this object; swapping it would leave the requester arbitrating
* against a session nobody reads.
*/
load(constraints: RoadConstraint[], operations: RoadEditOperation[], documentVersion: number): void {
this.baseline = [...constraints];
this.baselineOperations = [...operations];
this.stack = [];
this.cursor = 0;
this.version = documentVersion;
}
/** Drop every unsaved command and return to the last saved baseline. */
discard(): void {
this.stack = this.stack.filter((command) => command.saved);
this.cursor = this.stack.length;
}
/** Monotonic per-session sequence stamped on every preview request. */
nextPreviewSeq(): number {
this.seq += 1;