apply method

Future<KnowledgeAdoptionResult> apply(
  1. KnowledgeAdoptionPlan plan, {
  2. WayfinderCancellation? cancel,
  3. Future<void> hostStep()?,
  4. void onStep(
    1. AdoptionStepReport report
    )?,
})

Writes plan, one AdoptionStep at a time, then validates the bundle.

The check step refuses a plan with conflicts (adoption-conflict), one another adoption's Profile cache planned (adoption-invalid), and one whose project files, bundle, skill directories, host inputs or Profile cache changed since planning (adoption-stale, naming the changed paths in KnowledgeAdoptionException.changedPaths, and resolution-stale), before writing anything. It takes the resolver's get lock of the project and holds it until every step has finished or been undone, so no get or other adoption changes the project meanwhile; while another holder has it, the check fails with resolution-busy. cancel is checked as each step starts (cancelled).

hostStep runs the host's own writes, such as placing KnowledgeAdoptionPlan.agentInstructions in AGENTS.md, as the AdoptionStep.host step after Wayfinder's files are in place. When it throws, every earlier step is undone like any other failure; adoption cannot undo the host's writes, so it undoes its own partial writes before throwing. Once it returns, its writes stay, whatever follows. A file it writes belongs in the request's hostInputs, so the check step refuses the plan when that file changed since planning.

onStep hears each step as it happens, on the calling isolate: an AdoptionStepOutcome.started report as a step begins, then its end (done, skipped or failed), and, after a failure, an undone or undo-failed report for each earlier step as its undo finishes, latest first. A host shows progress with it. When it throws for a step's started, done or skipped report, that step fails with what it threw, its own writes and the earlier steps are undone, and apply throws as for any failure, so a test can stop adoption at any step. What it throws for a report of a failure or an undo is ignored, since apply already reports that failure, and so is what it throws for the host step's done, since the host's writes are then in place and adoption cannot undo them.

When a step fails, the steps before it are undone in reverse order and KnowledgeAdoptionException reports each step, so the project holds no partial bundle unless a step reports undo-failed. Validation runs last, after the lock is released, and undoes nothing: a gate other than PASS returns a result that is not KnowledgeAdoptionResult.complete. It binds the bundle within the plan's project root, so it never reads a wayfinder.json above it, and judges it through the adoption's KnowledgeAssessor with cancel. A validation that fails to run, cancelled or stopped by the assessor included, reports the validate step failed with its code and leaves KnowledgeAdoptionResult.validation null.

Implementation

Future<KnowledgeAdoptionResult> apply(
  KnowledgeAdoptionPlan plan, {
  WayfinderCancellation? cancel,
  Future<void> Function()? hostStep,
  void Function(AdoptionStepReport report)? onStep,
}) async {
  final run = _Run(onStep);
  final root = plan.projectRoot;
  try {
    run.start(AdoptionStep.check, cancel);
    if (plan.conflicts case [final first, ...final rest]) {
      throw WayfinderException(
        'adoption-conflict',
        '${first.message}'
            '${rest.isEmpty ? '' : ' The plan has ${rest.length} more '
                      'conflict${rest.length == 1 ? '' : 's'}.'}',
      );
    }
    if (plan._dataDirectory != _dataDirectoryOf(resolver)) {
      throw _invalid(
        'The plan resolved the Profile into ${plan._dataDirectory}, and '
        "this adoption's resolver uses ${_dataDirectoryOf(resolver)}. Plan "
        'again with this adoption.',
      );
    }
    await withProjectLock(
      resolver,
      root,
      () => _write(plan, run, cancel, hostStep),
    );
  } on Object catch (error) {
    // _write records its failure and undoes its steps while it still holds
    // the project; these finish a failure before or after it.
    run.fail(error);
    await run.undo();
    throw run.failure(error);
  }

  ProfileValidationResult? validation;
  try {
    run.start(AdoptionStep.validate, cancel);
    validation = await _validate(plan, cancel);
    run.done(const [], 'Gate ${validation.gate.wireValue}.');
  } on Object catch (error) {
    validation = null;
    run.fail(error);
  }
  return KnowledgeAdoptionResult(
    projectRoot: root,
    bundle: plan.bundle,
    steps: List.unmodifiable([
      for (final record in run.records) record.report,
    ]),
    written: List.unmodifiable([for (final file in plan.files) file.path]),
    validation: validation,
    agentInstructions: plan.agentInstructions,
  );
}