|
| 1 | +// Le dossier d'une tâche que la tentative précédente a laissé, et qu'on ne peut |
| 2 | +// plus effacer : la tâche doit finir en échec VISIBLE, avec la vraie cause. |
| 3 | +// |
| 4 | +// ─── LA PANNE QUE CE BANC TIENT FERMÉE ─────────────────────────────────────── |
| 5 | +// |
| 6 | +// Hive 0.5.0, application de bureau sous Windows : la ruche redémarre pendant |
| 7 | +// qu'une ouvrière travaille, et le dossier de la tâche reste, clone compris. |
| 8 | +// À la tentative suivante, `prepareWorkspace` ne pouvait plus l'effacer |
| 9 | +// (`effacerDossier`, workspace.ts, dit pourquoi) et le refus partait sous |
| 10 | +// « clone impossible : EPERM … » — une panne de dépôt ou d'identifiants qui |
| 11 | +// n'existait pas. Le compte des refus concluait bien, mais en « aucun agent |
| 12 | +// qui fonctionne — réparez l'agent » : deux fausses pistes pour l'opérateur. |
| 13 | +// |
| 14 | +// Ici le dossier est TENU pour de vrai, par le moyen que chaque système offre : |
| 15 | +// un processus vivant dont c'est le répertoire courant sous Windows (un tel |
| 16 | +// dossier ne s'efface pas), un sous-dossier sans droit d'écriture ailleurs |
| 17 | +// (hors root, qui passe outre). Puis le vrai chemin : une Reine réelle, un |
| 18 | +// `HiveNodeClient`, et la mesure de ce que l'écran reçoit — le refus, puis |
| 19 | +// l'échec borné et l'alerte de l'accueil (`/api/cockpit`). |
| 20 | + |
| 21 | +import { type ChildProcess, execFileSync, spawn } from 'node:child_process'; |
| 22 | +import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 23 | +import os from 'node:os'; |
| 24 | +import path from 'node:path'; |
| 25 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 26 | +import type { AgentAdapter } from '../src/adapters/index.js'; |
| 27 | +import { HiveNodeClient } from '../src/node-client/client.js'; |
| 28 | +import { createServer } from '../src/orchestrator/server.js'; |
| 29 | + |
| 30 | +const TOKEN = 'jeton-dossier-tache-tenu-long'; |
| 31 | +const WINDOWS = process.platform === 'win32'; |
| 32 | +const RACINE = typeof process.getuid === 'function' && process.getuid() === 0; |
| 33 | + |
| 34 | +const aNettoyer: (() => void)[] = []; |
| 35 | +afterEach(() => { |
| 36 | + for (const f of aNettoyer.splice(0).reverse()) f(); |
| 37 | +}); |
| 38 | + |
| 39 | +function git(cwd: string, ...args: string[]): void { |
| 40 | + execFileSync('git', ['-c', '[email protected]', '-c', 'user.name=Banc Hive', ... args], { |
| 41 | + cwd, |
| 42 | + stdio: 'ignore', |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +/** Tient `dossier` jusqu'au nettoyage du banc. */ |
| 47 | +function tenir(dossier: string): void { |
| 48 | + const objets = path.join(dossier, '.git', 'objects', 'pack'); |
| 49 | + mkdirSync(objets, { recursive: true }); |
| 50 | + writeFileSync(path.join(objets, 'pack-banc.pack'), 'objet\n'); |
| 51 | + if (WINDOWS) { |
| 52 | + const garde: ChildProcess = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { |
| 53 | + cwd: objets, |
| 54 | + stdio: 'ignore', |
| 55 | + }); |
| 56 | + aNettoyer.push(() => garde.kill()); |
| 57 | + return; |
| 58 | + } |
| 59 | + chmodSync(objets, 0o555); |
| 60 | + aNettoyer.push(() => chmodSync(objets, 0o755)); |
| 61 | +} |
| 62 | + |
| 63 | +describe.skipIf(RACINE)('un dossier de tâche qui ne s’efface plus', () => { |
| 64 | + it( |
| 65 | + 'refus qui nomme le dossier tenu, puis échec borné que l’accueil DIT — sans parler de clone', |
| 66 | + { timeout: 90_000 }, |
| 67 | + async () => { |
| 68 | + const racine = mkdtempSync(path.join(os.tmpdir(), 'hive-dossier-tenu-')); |
| 69 | + aNettoyer.push(() => rmSync(racine, { recursive: true, force: true, maxRetries: 5 })); |
| 70 | + const amont = path.join(racine, 'amont'); |
| 71 | + mkdirSync(amont); |
| 72 | + git(amont, 'init', '-q', '-b', 'main'); |
| 73 | + writeFileSync(path.join(amont, 'a.txt'), 'base\n'); |
| 74 | + git(amont, 'add', '-A'); |
| 75 | + git(amont, 'commit', '-q', '-m', 'base'); |
| 76 | + |
| 77 | + let lance = false; |
| 78 | + const adapter: AgentAdapter = { |
| 79 | + name: 'jamais-lance', |
| 80 | + async run() { |
| 81 | + lance = true; |
| 82 | + return { success: true, diff: '', logs: '', subAgents: [] }; |
| 83 | + }, |
| 84 | + }; |
| 85 | + const server = await createServer({ |
| 86 | + port: 0, |
| 87 | + host: '127.0.0.1', |
| 88 | + token: TOKEN, |
| 89 | + corsOrigins: ['http://localhost:5173'], |
| 90 | + dbPath: path.join(racine, 'hive.db'), |
| 91 | + simulation: false, |
| 92 | + tickMs: 20, |
| 93 | + }); |
| 94 | + const work = path.join(racine, 'work'); |
| 95 | + const client = new HiveNodeClient({ |
| 96 | + url: `ws://127.0.0.1:${server.port}/ws`, |
| 97 | + token: TOKEN, |
| 98 | + name: 'noeud-tenu', |
| 99 | + ownerName: 'banc', |
| 100 | + agentType: 'custom', |
| 101 | + nodeId: 'noeud-tenu', |
| 102 | + maxConcurrency: 1, |
| 103 | + workRoot: work, |
| 104 | + adapter, |
| 105 | + quiet: true, |
| 106 | + }); |
| 107 | + client.start(); |
| 108 | + const attendre = async (condition: () => boolean, message: string): Promise<void> => { |
| 109 | + const limite = Date.now() + 75_000; |
| 110 | + while (Date.now() < limite) { |
| 111 | + if (condition()) return; |
| 112 | + await new Promise((resolve) => setTimeout(resolve, 25)); |
| 113 | + } |
| 114 | + throw new Error(message); |
| 115 | + }; |
| 116 | + try { |
| 117 | + await attendre( |
| 118 | + () => server.store.listNodes().some((n) => n.status === 'online'), |
| 119 | + 'le nœud ne rejoint pas la ruche', |
| 120 | + ); |
| 121 | + const nodeId = server.store.listNodes()[0]!.id; |
| 122 | + const projet = server.store.createProject({ name: 'Dossier tenu', repoUrl: amont }); |
| 123 | + const t = server.store.createTask({ |
| 124 | + projectId: projet.id, |
| 125 | + title: 'Ligne', |
| 126 | + prompt: 'ligne', |
| 127 | + }); |
| 128 | + // Le reste de la tentative tuée : `<work>/tasks/<tâche>-<nœud court>`. |
| 129 | + tenir(path.join(work, 'tasks', `${t.id}-${nodeId.slice(0, 8)}`)); |
| 130 | + server.store.patchTask(t.id, { status: 'ready' }); |
| 131 | + |
| 132 | + const refus = () => |
| 133 | + server.store |
| 134 | + .listEvents(0, 1000) |
| 135 | + .filter((e) => e.type === 'task_rejected' && e.payload.taskId === t.id); |
| 136 | + await attendre(() => refus().length > 0, 'la tâche n’est jamais refusée'); |
| 137 | + const premier = refus()[0]!.payload; |
| 138 | + expect(premier).toMatchObject({ infra: true, avantAgent: true }); |
| 139 | + expect(String(premier.reason)).toMatch( |
| 140 | + /^dossier de la tâche impossible à vider — un processus le tient-il encore \? \((EBUSY|EPERM|EACCES|ENOTEMPTY) : \.git/, |
| 141 | + ); |
| 142 | + |
| 143 | + // Un seul nœud : le compte des refus conclut (`rejectTask`), et l'échec |
| 144 | + // se lit à l'accueil avec la vraie cause — une préparation, pas un agent. |
| 145 | + await attendre(() => server.store.getTask(t.id)?.status === 'failed', 'jamais échouée'); |
| 146 | + const reponse = await fetch(`http://127.0.0.1:${server.port}/api/cockpit`, { |
| 147 | + headers: { 'x-hive-token': TOKEN }, |
| 148 | + }); |
| 149 | + const { alertes } = (await reponse.json()) as { alertes: Record<string, unknown>[] }; |
| 150 | + expect(alertes.find((a) => a.taskId === t.id)).toMatchObject({ |
| 151 | + genre: 'refus', |
| 152 | + definitif: true, |
| 153 | + avantAgent: true, |
| 154 | + raison: expect.stringMatching(/^dossier de la tâche impossible à vider/), |
| 155 | + }); |
| 156 | + expect(lance).toBe(false); |
| 157 | + } finally { |
| 158 | + client.stop(); |
| 159 | + await server.stop(); |
| 160 | + } |
| 161 | + }, |
| 162 | + ); |
| 163 | +}); |
0 commit comments