commit method

Writes plan under okf's exclusive bundle lock: exactly the bytes prepare judged, or nothing.

cancel is checked before the lock is taken and again once it is held; a cancelled commit writes nothing, throws WayfinderException.cancelled, and leaves the plan committable. Once okf starts writing, the commit runs to its end. Waiting for the lock itself cannot be cancelled: okf's lock blocks while another process holds the bundle.

Throws KnowledgeWriteRefusedException for a plan that is not committable, KnowledgeStaleWriteException when the bundle, or a project file the plan's selection came from (KnowledgeWritePlan.projectFiles), changed since prepare, and KnowledgeWriteFailedException when the bundle cannot be locked or written, or a project file exists but cannot be read. A plan commits once; committing it again throws a StateError.

A plan whose KnowledgeWritePlan.diff is empty takes this path too. The checks run under the lock, the plan is consumed, and no document is rewritten. A second commit throws StateError.

The project files are checked under okf's lock of the bundle, which WayfinderProfileResolver.relock and get do not take: they hold the project's get lock instead. So one that rewrites wayfinder.json or wayfinder.lock after the check and before okf writes goes unseen, and the write commits as it was judged. A host that both relocks and writes serializes the two itself, such as by queueing them on the one isolate that does its okf bundle I/O. A project file that exists but cannot be read fails the commit; only a path that is absent counts as the absent digest.

Implementation

Future<KnowledgeCommitResult> commit(
  KnowledgeWritePlan plan, {
  WayfinderCancellation? cancel,
}) async {
  final prepared = plan._prepared;
  if (prepared == null || !plan.committable) {
    throw KnowledgeWriteRefusedException(plan);
  }
  throwIfCancelled(cancel);
  try {
    return await OkfBundleLock.write(plan.bundle, () async {
      throwIfCancelled(cancel);
      final changed = await plan.projectFiles.changed();
      if (changed.isNotEmpty) {
        throw KnowledgeStaleWriteException(
          '${changed.map(p.basename).toSet().join(' and ')} changed after '
          'the write was prepared (${changed.join(', ')}). Prepare it again.',
          projectFiles: List.unmodifiable(changed),
        );
      }
      try {
        final result = await const OkfBundleChangeApplier().commit(prepared);
        return KnowledgeCommitResult(changedPaths: result.changedPaths);
      } on OkfStalePreparedChangeException {
        throw const KnowledgeStaleWriteException(
          'The bundle changed after the write was prepared. Prepare it '
          'again.',
        );
      }
    });
  } on FileSystemException catch (error) {
    throw KnowledgeWriteFailedException._of(error);
  } on OkfWriteRollbackException catch (error) {
    throw KnowledgeWriteFailedException(
      'The write failed (${error.writeError}) and so did its rollback '
      '(${error.rollbackErrors.join('; ')}); the bundle may hold part of '
      'it.',
      partial: true,
    );
  }
}