Elffuss BraidHub

utopia-ia/elffuss-code public

main · 4.4 KiBRawPermalink
1// Test estilo SWE-bench (reproducible en el navegador): sembramos un repo con un
2// BUG y un test que FALLA; el agente lee los ficheros REALES, deduce el arreglo y
3// escribe la corrección con code.write; después EJECUTAMOS el test de verdad
4// (import del módulo arreglado) y verificamos que pasa — igual que la métrica
5// `resolved` de SWE-bench, pero autocontenido.
6//
7// El "solver" aquí es un proveedor guiado (lee→transforma→escribe) para que el
8// harness sea determinista en CI. Cambiando `fake` por el provider real (Gemma/
9// heal) se mide al modelo de verdad: el arnés y la verificación son los mismos.
10import { chromium } from 'playwright';
11import { CHROME_ARGS } from './chrome-args.mjs';
12const BASE = process.env.BASE || 'http://localhost:8799';
13let fails = 0;
14const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
15
16const b = await chromium.launch({ args: ['--enable-unsafe-webgpu', ...CHROME_ARGS] });
17const ctx = await b.newContext();
18await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} });
19const p = await ctx.newPage();
20await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
21
22// sembrar el repo con BUG (add resta) + test que lo comprueba
23await p.evaluate(async () => {
24 const o = await navigator.storage.getDirectory();
25 const w = async (path, txt) => { const parts = path.split('/'); const name = parts.pop(); let d = o; for (const x of parts) d = await d.getDirectoryHandle(x, { create: true }); const f = await d.getFileHandle(name, { create: true }); const s = await f.createWritable(); await s.write(txt); await s.close(); };
26 await w('src/math.js', 'export function add(a, b) {\n return a - b; // BUG: debería sumar\n}\n');
27 await w('test/math.test.js', "import { add } from '../src/math.js';\n// add(2,3) debe ser 5 y add(10,-4) debe ser 6\n");
28 await w('README.md', '# calc\nUna librería de aritmética. `add(a,b)` suma dos números.');
29});
30await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' });
31await p.waitForTimeout(2000);
32
33// correr el agente con un solver guiado que USA las tools reales (read→write)
34const trace = await p.evaluate(async () => {
35 const { Agent } = await import('/js/agent.js');
36 const fake = {
37 chat: async (history) => {
38 const seen = t => history.some(m => m.content.includes(t));
39 const readSrc = history.find(m => m.content.startsWith('[resultado code.read]') && m.content.includes('return a'));
40 if (!seen('[resultado code.read]\n') && !readSrc) return '```tool\n{"tool":"code.read","args":{"path":"src/math.js"}}\n```';
41 if (readSrc && history.every(m => !m.content.includes('return a + b'))) {
42 const fixed = 'export function add(a, b) {\n return a + b;\n}\n';
43 return 'Veo el bug (resta en vez de sumar). Lo arreglo:\n```tool\n{"tool":"code.write","args":{"path":"src/math.js","content":' + JSON.stringify(fixed) + '}}\n```';
44 }
45 return 'Arreglado: add ahora suma.';
46 },
47 };
48 const a = new Agent(fake);
49 const ev = [];
50 await a.handle('el test test/math.test.js falla porque add() resta en vez de sumar; arréglalo', e => ev.push(e.type + (e.tool ? ':' + e.tool : '')));
51 return ev;
52});
53ok('el agente lee y luego escribe el arreglo (read→write)', trace.includes('tool_result:code.read') && trace.includes('tool_result:code.write'), trace.join(','));
54
55// EJECUTAR el test de verdad contra el fichero arreglado en OPFS
56const result = await p.evaluate(async () => {
57 const o = await navigator.storage.getDirectory();
58 const read = async (path) => { const parts = path.split('/'); const name = parts.pop(); let d = o; for (const x of parts) d = await d.getDirectoryHandle(x); return (await (await d.getFileHandle(name)).getFile()).text(); };
59 const src = await read('src/math.js');
60 const url = URL.createObjectURL(new Blob([src], { type: 'text/javascript' }));
61 const mod = await import(url);
62 const cases = [[2, 3, 5], [10, -4, 6], [0, 0, 0]];
63 const passed = cases.filter(([a, b, exp]) => mod.add(a, b) === exp).length;
64 return { total: cases.length, passed, src: src.slice(0, 60) };
65});
66ok('SWE resolved: el test PASA tras el arreglo del agente', result.passed === result.total, `${result.passed}/${result.total} · ${result.src.replace(/\n/g, ' ')}`);
67
68console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ SWE-STYLE OK — repo con bug → agente lo arregla → test verde (resolved)');
69await b.close();
70process.exit(fails ? 1 : 0);