Elffuss BraidHub

utopia-ia/elffuss-code public

99a5df28cf0f · 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';
11const BASE = process.env.BASE || 'http://localhost:8799';
12let fails = 0;
13const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
14
15const b = await chromium.launch({ args: ['--enable-unsafe-webgpu', '--use-angle=metal'] });
16const ctx = await b.newContext();
17await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} });
18const p = await ctx.newPage();
19await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
20
21// sembrar el repo con BUG (add resta) + test que lo comprueba
22await p.evaluate(async () => {
23 const o = await navigator.storage.getDirectory();
24 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(); };
25 await w('src/math.js', 'export function add(a, b) {\n return a - b; // BUG: debería sumar\n}\n');
26 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");
27 await w('README.md', '# calc\nUna librería de aritmética. `add(a,b)` suma dos números.');
28});
29await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' });
30await p.waitForTimeout(2000);
31
32// correr el agente con un solver guiado que USA las tools reales (read→write)
33const trace = await p.evaluate(async () => {
34 const { Agent } = await import('/js/agent.js');
35 const fake = {
36 chat: async (history) => {
37 const seen = t => history.some(m => m.content.includes(t));
38 const readSrc = history.find(m => m.content.startsWith('[resultado code.read]') && m.content.includes('return a'));
39 if (!seen('[resultado code.read]\n') && !readSrc) return '```tool\n{"tool":"code.read","args":{"path":"src/math.js"}}\n```';
40 if (readSrc && history.every(m => !m.content.includes('return a + b'))) {
41 const fixed = 'export function add(a, b) {\n return a + b;\n}\n';
42 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```';
43 }
44 return 'Arreglado: add ahora suma.';
45 },
46 };
47 const a = new Agent(fake);
48 const ev = [];
49 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 : '')));
50 return ev;
51});
52ok('el agente lee y luego escribe el arreglo (read→write)', trace.includes('tool_result:code.read') && trace.includes('tool_result:code.write'), trace.join(','));
53
54// EJECUTAR el test de verdad contra el fichero arreglado en OPFS
55const result = await p.evaluate(async () => {
56 const o = await navigator.storage.getDirectory();
57 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(); };
58 const src = await read('src/math.js');
59 const url = URL.createObjectURL(new Blob([src], { type: 'text/javascript' }));
60 const mod = await import(url);
61 const cases = [[2, 3, 5], [10, -4, 6], [0, 0, 0]];
62 const passed = cases.filter(([a, b, exp]) => mod.add(a, b) === exp).length;
63 return { total: cases.length, passed, src: src.slice(0, 60) };
64});
65ok('SWE resolved: el test PASA tras el arreglo del agente', result.passed === result.total, `${result.passed}/${result.total} · ${result.src.replace(/\n/g, ' ')}`);
66
67console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ SWE-STYLE OK — repo con bug → agente lo arregla → test verde (resolved)');
68await b.close();
69process.exit(fails ? 1 : 0);