clearAndRestart method

Future<void> clearAndRestart({
  1. required ClearAndRestartConfirmation confirm,
})

Resolve an embeddingFingerprintLock by discarding every on-device embedding (chunks table) and rotating the fingerprint baseline.

confirm MUST be ClearAndRestartConfirmation.iUnderstandThisDeletesAllOnDeviceEmbeddings; the parameter exists solely to make the destructive choice visible at every call site. The sources table is preserved so the host app can re-ingest the original documents through the normal addDocument flow.

Throws StateError if no mismatch is active.

Implementation

Future<void> clearAndRestart({
  required ClearAndRestartConfirmation confirm,
}) async {
  // Reference the parameter so static analysis confirms the caller passed
  // an explicit value (typing out the sentinel name is the consent).
  identical(
    confirm,
    ClearAndRestartConfirmation.iUnderstandThisDeletesAllOnDeviceEmbeddings,
  );
  if (_fingerprintLock == null) {
    throw StateError(
      'clearAndRestart() called but no embedding fingerprint mismatch is active.',
    );
  }
  final target = currentEmbeddingFingerprint;
  final deleted = await migration_meta.acknowledgeAndClearEmbeddings(
    confirmation: kEmbeddingClearConfirmationToken,
    newFingerprint: target,
  );
  _fingerprintLock = null;
  debugPrint(
    '[RagEngine] clearAndRestart: deleted $deleted chunks; '
    'fingerprint reset to "$target". Rebuilding empty HNSW index...',
  );
  await _ragService.rebuildIndex(force: true);
}