33800 Docs

← Retour

Proposition : Fix PowerShell wrapping deterministe + prompt AI

Date : 03/02/2026 Status : EN ATTENTE Projet : connectors-api Priorite : HAUTE (bug bloquant sur instances Windows)

Probleme

Quand shell_type = powershell sur une instance SSH Windows :

Solution

1. Ajouter shell_type a SSHConfig (ssh.ts:25)

export interface SSHConfig {
  // ... existant ...
  os_type?: 'linux' | 'windows';
  shell_type?: 'bash' | 'cmd' | 'powershell' | 'wsl';  // AJOUT
}

2. Wrapper automatique dans ssh.execute() (ssh.ts:~183)

Apres let fullCommand = command; et avant this.executeSSH(), ajouter un wrapping deterministe :

// Wrap command for the target shell (deterministic, no AI needed)
fullCommand = this.wrapForShell(fullCommand, config);

Nouvelle methode :

/**
 * Wrap command for target shell (deterministic)
 * - powershell: wrap with powershell.exe -NoProfile -Command "..."
 * - wsl: wrap with wsl.exe -e bash -c "..."
 * - cmd/bash: no wrapping needed
 */
wrapForShell(command: string, config: SSHConfig): string {
  const shell = config.shell_type || (config.os_type === 'windows' ? 'cmd' : 'bash');

  if (shell === 'powershell') {
    // Skip if already wrapped
    if (/^powershell(\.exe)?\s/i.test(command)) return command;
    // Escape double quotes for CMD wrapping
    const escaped = command.replace(/"/g, '\\"');
    return `powershell.exe -NoProfile -Command "${escaped}"`;
  }

  if (shell === 'wsl') {
    if (/^wsl(\.exe)?\s/i.test(command)) return command;
    const escaped = command.replace(/"/g, '\\"');
    return `wsl.exe -e bash -c "${escaped}"`;
  }

  // cmd and bash: no wrapping
  return command;
}

3. Ajuster le prompt AI (ai.ts:341-342)

Le prompt PowerShell reste correct — on dit a l'IA d'ecrire du PS pur. Mais on renforce :

const shellInstructions: Record<string, string> = {
  cmd: 'Use CMD syntax only. No PowerShell cmdlets. No Linux commands (tr, awk, sed, grep). Use findstr, for /f, set.',
  powershell: 'Write pure PowerShell cmdlets (Get-Volume, Get-Process, Get-CimInstance, ForEach-Object, etc.). The command will be auto-wrapped in powershell.exe. Do NOT prefix with powershell.exe. Do NOT use CMD commands (findstr, for /f). Do NOT use Linux commands (tr, awk, sed, grep).',
  wsl: 'Use bash/Linux syntax. The command runs inside WSL.',
  bash: 'Use bash syntax.'
};

Et la regle "never repeat" :

- NEVER repeat a command that failed. NEVER reuse a tool/command that caused an error (e.g. if 'tr' failed, do NOT use 'tr' again). Find a COMPLETELY different approach using only ${shell} native commands.

Fichiers modifies

Fichier Modification
src/services/ssh.ts Ajouter shell_type a SSHConfig, methode wrapForShell(), appel dans execute()
src/services/ai.ts Renforcer shellInstructions + regle "never repeat"

Impact

Test

  1. Instance win11-SSH, shell_type = powershell
  2. AI suggest : "montre les IPs de la machine"
  3. AI genere : Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress
  4. Test → wrapping auto → powershell.exe -NoProfile -Command "Get-NetIPAddress ..." → OK