Skip to main content

Undo and redo

How a document stores an undo depends on its base-reducer version, header.protocolVersions["base-reducer"]. The version is fixed by CREATE_DOCUMENT and is part of the derived document id, so a document never changes version.

VersionUNDO is stored asREDO
1, 2a NOOP whose skip removes the operation before itreplays the undone action from the document's clipboard; version 2 refuses it once the stream holds an undo
3refused; undo is a RETRACT { target } the author signs, naming the undone action by idrefused; redo is a RETRACT of the retraction

On version 3 a retraction names its target, so where it lands in the stream does not change what it undoes. Replicas that hold the same operations derive the same state, whatever order the operations arrived in.

Feature flag

Base-reducer 3 needs the undoV3 reactor flag, which defaults to on. A reactor with it off refuses to create a version 3 document and never receives one from a peer.

A reactor with it on still refuses a version 3 document whose model it cannot run as version 3: the model's reducer is not built by createReducer, or the model cannot be loaded. The refusal is an UnsupportedProtocolVersionError, on writes, on reads, and on receipt from a peer before any load.

A reactor that stores version 3 documents of a model it cannot run refuses to start and names the type (base-reducer 3 for <type>). One without undoV3 refuses to start too, naming the version and how many stored documents need it (N stored document(s) require base-reducer 3, which this reactor does not run). withUnsupportedStoredDocuments("read-only") starts either with those documents read-only. A model loaded after start is checked when it loads, and a refusal is logged and emitted as STORED_MODEL_REFUSED.

Turning it off​

HostSetting
SwitchboardREACTOR_UNDO_V3=false, or protocol.featureFlags.undoV3: false in its config
Connectconnect.reactor.featureFlags.undoV3: false in powerhouse.config.json
LibraryReactorBuilder.withExecutorConfig({ featureFlags: { undoV3: false } })
ph-cli hostreactor.protocol.featureFlags.undoV3: false in the host file (projected to REACTOR_UNDO_V3)

undoV3 has no prerequisites. A peer that runs without it is held from version 3 documents until it upgrades. Turning it off only stops new documents taking version 3: a host with it off that stores a version 3 document refuses to start, unless REACTOR_UNSUPPORTED_STORED_DOCUMENTS=read-only (or withUnsupportedStoredDocuments("read-only")) starts it with those documents read-only. Version 1 and 2 documents behave the same with the flag on or off.

Versions this build does not know​

A document whose base-reducer number this build does not know is read, derived and synced as the highest version the reader runs. A reactor, and its in-process client, use the highest version the reactor supports: 3 with undoV3 on, 2 with it off. The GraphQL light client and a model's reducer use 3. A reactor still refuses a write to such a document unless its protocol support lists the number.

Which version a document takes​

A document's version is fixed when it is created, and its creator chooses it. A client chooses in one place, newDocument on the read half, which heads an uncreated document with the versions getCreateProtocolVersions selects: version 3 when undoV3 is on and the model's reducer is built by createReducer, otherwise version 2. createEmpty is newDocument then create, and create takes the header as given. The members of the parent drive do not narrow the base-reducer version: a peer that cannot run it is held from the document until it upgrades. A reactor that does not run the chosen version refuses the document. A call can name a version:

const document = await client.read.newDocument("powerhouse/todo-list", {
parentIdentifier: driveId,
protocolVersions: { "base-reducer": 2 },
});
await client.write.drives.addFile(driveId, document);

A document headed outside a client, such as one from a model's utils.createDocument(), takes version 2, and keeps it when it is created or added to a drive. IReactor.create gives a header without protocol versions the versions the reactor selects for its type, as newDocument does: 3 for a createReducer model with undoV3 on, otherwise 2. A stored history or an imported .phd file keeps the versions it carries, and a .phd file without them is replayed at version 1.

The GraphQL light client takes the Switchboard's createDefaults answer for the document's type and parent as given, so a remote document's version follows the Switchboard's undoV3. It caps the answer at the versions its own build runs, so a newer Switchboard's base-reducer 4 becomes 3, and refuses a malformed answer or a version of another known protocol it does not run. It then fits base-reducer down to what its own documentModels can run. A Switchboard from before per-type selection answers for every type; against one without the query the client picks version 2, but cannot create there: it also reads the signature policy from that query.

An existing document keeps its version. To move one to version 3, copy it through client.write.drives.copyNode with the version to take:

await client.write.drives.copyNode(driveId, nodeId, targetFolderId, undefined, {
protocolVersions: { "base-reducer": 3 },
});

The copy starts from the source's current state, so the source's history cannot be undone in it. A copy without protocolVersions keeps its source's version. ReactorDriveClient replays the source's history into the copy and refuses protocolVersions.

Undoing from a client​

Call undo, redo and undoRedoAvailability on the write half, client.write. They work on every version; undo and redo return a WriteHandle.

On version 3 the client resolves the target with undoRedoTargets and submits a RETRACT of it signed by the client's own signer. On versions 1 and 2 it submits UNDO or REDO. The GraphQL light client does the same, resolving the target through the Switchboard's undoRedoTargets query; see the light client's methods. Either way the author signs, so a policy must grant the author RETRACT for undo on version 3.

RemoteDocumentController from @powerhousedao/reactor-browser runs the version the remote document's header names. Its undo(count = 1, scope = "global") and redo(count = 1, scope = "global") keep their signature. On versions 1 and 2, and on a header without versions, they apply UNDO or REDO locally, as before. On version 3 each step resolves its target from the controller's own log, including actions not yet pushed, and applies a RETRACT that its signer signs on push; every target resolves before anything applies, and a count that is not a positive integer throws a RangeError. A new document is headed with the host's versions for its type and created from that header on its first push, so undo before the push follows the same version.

The RETRACT names the row the call saw as the latest, so an edit that lands in between is not undone. If the target is retracted or gone by the time the RETRACT runs, the job fails with RetractionRefusedError, and the handle's stages reject with a JobFailedError naming it; the client does not resolve again or retry.

To retract a specific action, submit retract yourself. It is exported from document-model and @powerhousedao/shared/document-model:

import { retract } from "document-model";

await client.write.execute(documentId, "main", [retract(actionId, "global")]);

A submitted UNDO or REDO on version 3 fails with SubmittedUndoRefusedError. The model's reducer refuses one the same way, so reducer(document, undo()) on a version 3 document throws SubmittedUndoRefusedError rather than retracting anything. On versions 1 and 2 the reactor still resolves one and signs the NOOP it stores with its own signer.

Rules on version 3​

  • An action is live if no live RETRACT names it. Liveness depends on which operations are present, not on their order.
  • Undo targets the last applied content operation in the scope, by any author.
  • Redo targets the last live retraction of content. Content applied after the undo leaves nothing to redo.
  • SET_NAME and SET_PREFERRED_EDITOR cannot be undone. Undo passes over them, and a RETRACT naming one is stored but retracts nothing.
  • A denied RETRACT retracts nothing.
  • An operation that throws applies nothing. Undo passes over it.
  • skip is only the rewind a reshuffle writes. It never undoes anything.
  • Operations with the same timestamp are ordered by action id. Action ids made in one process sort in creation order, so one client's same-millisecond actions keep their order. The guarantee is per process: actions made in different processes, such as two workflow piece workers or a Connect tab and its shared worker, are ordered by id with no guarantee of creation order.
  • A job's actions run in this order, not in array order. The reactor sorts every job on a version 3 document the same way its peers sort what they receive.

On execute, a RETRACT whose target is not in the scope's stream, or is already retracted, is refused. On sync it is admitted: a RETRACT that arrives before its target does nothing until the target arrives.

The rules do not run in the auth and document scopes, or in version 1 and 2 documents. On a version 3 document, a RETRACT submitted to the auth or document scope is refused. On version 1 and 2 documents a RETRACT is an ordinary action, as before this release: it is stored and changes nothing. A RETRACT received from a peer into a scope the rules do not run is stored and does nothing.

Errors​

ErrorFieldsThrown when
RetractionRefusedErrordocumentId, target, reasonA submitted RETRACT names an absent target ("target-missing") or one already retracted ("target-retracted")
ReservedActionErrordocumentId, actionTypeA write carries an action type the client may not submit, such as a RETRACT to the auth or document scope of a version 3 document
SubmittedUndoRefusedErrordocumentId, actionTypeA write, or a peer's load, carries UNDO or REDO to a version 3 document
UndoOperationRefusedErrordocumentId, scopeA peer's load carries an UNDO operation to a version 1 or 2 document, which stores an undo as a NOOP

All four are exported from @powerhousedao/reactor and are terminal: the job fails without a retry. Through the write half they reach you as a JobFailedError whose job.error.name names the class; see Error handling.

undo and redo on version 3 throw NoUndoRedoTargetError (from document-model, message Cannot undo: nothing to undo in scope history, or redo) before submitting anything when there is no target.