resolveJobs function

JobsDecision resolveJobs({
  1. required int? requested,
  2. required int fileCount,
  3. required MachineInfo machine,
  4. bool resolved = false,
  5. String? incompatibleReason,
})

Decides the worker count from the user's requested value.

requested: null = flag/env not given → auto (the best default: the watchdog makes it safe), 0 = auto (use the recommendation), N = use N, clamped to kMaxJobs and warned about when it will not fit in memory. incompatibleReason forces serial (e.g. --profile) and explains why.

For resolved scans an unset value stays serial (each resolved worker is a full analyzer context — opt in explicitly) but a hint is shown.

Implementation

JobsDecision resolveJobs({
  required int? requested,
  required int fileCount,
  required MachineInfo machine,
  bool resolved = false,
  String? incompatibleReason,
}) {
  final rec = recommendJobs(
    fileCount: fileCount,
    cores: machine.cores,
    availableMemoryMb: machine.availableMemoryMb,
    isCi: machine.isCi,
    resolved: resolved,
  );
  final notes = <String>[];
  final cpuNote = machine.cpuLimitedByContainer
      ? '${machine.cores} CPUs (container limit)'
      : '${machine.cores} CPUs';

  if (incompatibleReason != null) {
    if (requested != null && requested != 1) {
      notes.add('--jobs ignored: $incompatibleReason. Running serially.');
    }
    return JobsDecision(jobs: 1, notes: notes);
  }

  // Unset: auto for the cheap syntactic scan; serial (with a hint) for the
  // heavy resolved scan.
  if (requested == null) {
    if (resolved) {
      if (fileCount >= kMinResolvedFilesPerWorker * 2 && rec.jobs > 1) {
        notes.add(
          'Tip: a resolved scan of $fileCount files can use up to '
          '${rec.jobs} workers (~$kResolvedWorkerMemoryBudgetMb MB each; '
          '$cpuNote, limited by ${rec.limitedBy}). '
          'Use --jobs ${rec.jobs} to opt in.',
        );
      }
      return JobsDecision(jobs: 1, notes: notes);
    }
    if (rec.jobs > 1 && fileCount >= kHintFileThreshold) {
      notes.add(
        'Using ${rec.jobs} workers ($cpuNote, limited by ${rec.limitedBy}). '
        'Pass --jobs 1 to scan serially or --jobs N to choose.',
      );
      return JobsDecision(jobs: rec.jobs, notes: notes);
    }
    return JobsDecision(jobs: 1, notes: notes);
  }

  if (requested == 0) {
    notes.add(
      'Using ${rec.jobs} worker(s) (--jobs auto; $cpuNote, limited by '
      '${rec.limitedBy}).',
    );
    return JobsDecision(jobs: rec.jobs, notes: notes);
  }

  var jobs = requested;
  if (jobs > kMaxJobs) {
    notes.add('--jobs $requested capped to $kMaxJobs.');
    jobs = kMaxJobs;
  }
  if (jobs > fileCount) jobs = fileCount < 1 ? 1 : fileCount;
  final perWorker = resolved
      ? kResolvedWorkerMemoryBudgetMb
      : kWorkerMemoryBudgetMb;
  final mem = machine.availableMemoryMb;
  if (jobs > 1 && mem != null && jobs * perWorker > mem * kMemoryUseFraction) {
    notes.add(
      'Warning: --jobs $jobs may need ~${jobs * perWorker} MB but only '
      '$mem MB is available; recommended is ${rec.jobs}. The scan will '
      'throttle itself if memory runs low.',
    );
  } else if (jobs > machine.cores) {
    notes.add(
      'Note: --jobs $jobs exceeds the ${machine.cores} available CPUs.',
    );
  } else if (jobs > rec.jobs) {
    notes.add('Note: --jobs $jobs exceeds the recommended ${rec.jobs}.');
  }
  return JobsDecision(jobs: jobs, notes: notes);
}