assessBashCommand function

BashCommandAssessment assessBashCommand(
  1. String command
)

Decides whether command can run without asking the user.

This is a conservative heuristic, not a sandbox: anything it does not recognize as read-only requires approval.

Implementation

BashCommandAssessment assessBashCommand(String command) {
  final normalized = command.trim().replaceAll(RegExp(r'\s+'), ' ');
  if (normalized.isEmpty) {
    return BashCommandAssessment.safe;
  }
  final padded = ' $normalized ';

  if (_networkTokens.any(padded.contains)) {
    return const BashCommandAssessment(
      requiresApproval: true,
      reason: 'This command may access the network.',
    );
  }

  if (_mutatingTokens.any(padded.contains) || normalized.replaceAll(_harmlessRedirect, '').contains('>')) {
    return const BashCommandAssessment(
      requiresApproval: true,
      reason: 'This command may modify files or repository state.',
    );
  }

  if (normalized.contains('`') || normalized.contains(r'$(')) {
    return const BashCommandAssessment(
      requiresApproval: true,
      reason: 'Command substitution requires approval.',
    );
  }

  final segments = normalized.split(_commandSeparator).map((segment) => segment.trim()).where((s) => s.isNotEmpty);
  if (segments.every(_isReadOnly)) {
    return BashCommandAssessment.safe;
  }

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