assessBashCommand function

BashCommandAssessment assessBashCommand(
  1. String command
)

Implementation

BashCommandAssessment assessBashCommand(String command) {
  final normalized = ' ${command.trim().replaceAll(RegExp(r'\s+'), ' ')} ';
  if (normalized.trim().isEmpty) {
    return const BashCommandAssessment(
      requiresApproval: false,
      reason: null,
    );
  }

  for (final token in _networkTokens) {
    if (normalized.contains(token)) {
      return const BashCommandAssessment(
        requiresApproval: true,
        reason: 'This command may access the network.',
      );
    }
  }

  for (final token in _mutatingTokens) {
    if (normalized.contains(token)) {
      return const BashCommandAssessment(
        requiresApproval: true,
        reason: 'This command may modify files or repository state.',
      );
    }
  }

  if (normalized.contains('&&') || normalized.contains('||')) {
    return const BashCommandAssessment(
      requiresApproval: true,
      reason: 'Compound commands require approval.',
    );
  }

  for (final prefix in _readOnlyPrefixes) {
    if (normalized.trim() == prefix.trim() || normalized.trim().startsWith(prefix.trim())) {
      return const BashCommandAssessment(
        requiresApproval: false,
        reason: null,
      );
    }
  }

  return const BashCommandAssessment(
    requiresApproval: true,
    reason: 'This command is not recognized as read-only.',
  );
}