Spaces:
Runtime error
Runtime error
Tales-Cunha commited on
Commit ·
ff2afe2
1
Parent(s): 017c628
docs: Add detailed ARCHITECTURE.md and clean up repository
Browse filesFiles changed (10)
- data/benchmark_summary.json+15-87
- docs/ARCHITECTURE.mdAdded+78
- src/agents/tester/nodes/oracle.ts+5-49
- src/agents/tester/nodes/pocoAgent.ts+27-8
- src/agents/tester/tools.ts+57-8
- src/agents/tester/utils/dependencyStubber.ts+5-2
- src/benchmark/runSyntheticEvaluation.ts+3-3
- src/benchmark/runTesterBenchmark.ts+91-11
- src/index.ts+17-2
- src/utils/forgeSandbox.tsAdded+22
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119{"summary": {"timestamp": "2026-06-11T17:27:55.677Z","total_processed": 18,"reproducibility_rate": 16.666666666666664,"specificity_rate": 0,"overall_ground_truth_rate": 0,"average_iterations": 9.38888888888889},"details": [{"id": "001","reproducible": false,"specific": false,"iterations": 10},{"id": "003","reproducible": false,"specific": false,"iterations": 10},{"id": "008","reproducible": false,"specific": false,"iterations": 10},{"id": "009","reproducible": false,"specific": false,"iterations": 10},{"id": "018","reproducible": false,"specific": false,"iterations": 10},{"id": "020","reproducible": false,"specific": false,"iterations": 10},{"id": "032","reproducible": true,"specific": false,"iterations": 4},{"id": "033","reproducible": false,"specific": false,"iterations": 10},{"id": "042","reproducible": false,"specific": false,"iterations": 10},{"id": "048","reproducible": false,"specific": false,"iterations": 10},{"id": "049","reproducible": false,"specific": false,"iterations": 10},{"id": "051","reproducible": false,"specific": false,"iterations": 10},{"id": "058","reproducible": false,"specific": false,"iterations": 10},{"id": "066","reproducible": true,"specific": false,"iterations": 7},{"id": "070","reproducible": false,"specific": false,"iterations": 10},{"id": "077","reproducible": false,"specific": false,"iterations": 10},{"id": "091","reproducible": false,"specific": false,"iterations": 10},{"id": "098","reproducible": true,"specific": false,"iterations": 8}]1234567891011121314151617181920212223242526272829303132333435363738394041424344454647{"summary": {"timestamp": "2026-06-16T21:50:28.207Z","total_processed": 6,"reproducibility_rate": 50,"specificity_rate": 100,"overall_ground_truth_rate": 50,"average_iterations": 13.333333333333334},"details": [{"id": "008","reproducible": false,"specific": false,"iterations": 30},{"id": "020","reproducible": true,"specific": true,"iterations": 9},{"id": "041","reproducible": false,"specific": false,"iterations": 13},{"id": "054","reproducible": true,"specific": true,"iterations": 7},{"id": "070","reproducible": false,"specific": false,"iterations": 13},{"id": "077","reproducible": true,"specific": true,"iterations": 8}]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778# Arquitetura do PoCo Agent (Proof-of-Concept Agent)
Este documento descreve detalhadamente o estado atual da arquitetura do Agente PoCo localizado em `src/agents/tester`, bem como a metodologia rigorosa de avaliação, as métricas e a estrutura dos datasets utilizados para validar a eficácia da Inteligência Artificial como auditora de Smart Contracts.
---
## 1. Arquitetura do Agente (`src/agents/tester`)
O Agente Tester foi projetado para atuar como um auditor de segurança e desenvolvedor de *exploits* totalmente autônomo. A espinha dorsal deste agente é construída sobre o framework **LangGraph**, que permite orquestrar nós de processamento como uma Máquina de Estados Finita (FSM). Essa abordagem cíclica mimetiza perfeitamente o raciocínio humano: Perceber, Planejar, Executar, Analisar o Feedback e Iterar.
### 1.1. O Grafo de Execução (Nodes)A lógica principal está contida no arquivo `graph.ts`, onde o LangGraph roteia a execução pelos seguintes nós (Nodes):
1. **`oracleNode`**: Nó de inicialização. Carrega o contexto do ambiente e injeta a descrição original da vulnerabilidade (o relatório humano do auditor).2. **`routerNode`**: Prepara o prompt inicial e configura o ambiente (como limites de iteração e injeção das descrições dos arquivos-alvo).3. **`pocoAgentNode`**: O "Cérebro" do sistema. É aqui que o Modelo de Linguagem de Grande Escala (**LLM**) é invocado. Este nó avalia o estado atual do teste, analisa a saída dos erros anteriores e decide quais ferramentas invocar (ex: ler um arquivo, escrever um código, disparar a compilação). - **Modelo Utilizado**: O sistema utiliza primariamente o modelo **Claude 3.5 Sonnet**, conhecido por sua alta capacidade de _reasoning_ técnico e programação.4. **`pocoToolsNode`**: O nó de execução mecânica. Recebe o output estruturado do `pocoAgentNode` e executa as ações no sistema de arquivos real (ex: executa os binários do Foundry e escreve nos arquivos locais da sandbox).5. **`trackToolCallsNode`**: Nó de avaliação de parada. Ele intercepta a saída do `smart_contract_test`. Se a saída for `Test Passed Successfully!` (ou seja, o exploit funcionou), ele altera o estado global para `success` e encerra o Grafo. Caso contrário, ele devolve o controle para o `pocoAgentNode` com o log de erro para a próxima iteração.
### 1.2. Ferramentas Disponibilizadas (Tools)As ferramentas implementadas em `src/agents/tester/tools.ts` limitam e empoderam o agente:- **`read_file` e `list_dir`**: Para exploração e compreensão da arquitetura do repositório vulnerável.- **`write_file` e `edit_file`**: Para criação do arquivo `test/Exploit.t.sol`. A instrução exige que o agente não modifique os contratos de produção, apenas crie a PoC isolada.- **`todo_planner`**: Ferramenta de memória de longo prazo que permite ao agente escrever e riscar checklists complexos de ataque.- **`smart_contract_compile`**: Executa `forge build`. Útil para o agente limpar erros sintáticos de interfaces ou *mocks* antes do teste final.- **`smart_contract_test`**: Executa a PoC. É a ferramenta que decide se o ciclo falha ou triunfa.
---
## 2. Métricas de Avaliação do Benchmark
Para validar se um LLM gerou um exploit real ou apenas sofreu alucinação, nós utilizamos três pilares absolutos extraídos do paper original do PoCo:
### 2.1. Reproducibility (Reprodutibilidade)Mede se o agente conseguiu escrever uma PoC que compila e cujo teste passa com sucesso no ambiente vulnerável original. - **Como funciona:** O `runTesterBenchmark.ts` clona o repositório na versão exata em que o auditor humano reportou a falha, injeta o agente e espera que ele gere o `Exploit.t.sol`. Se o `forge test` da PoC passar, o projeto ganha a flag `Reproducible=true`.
### 2.2. Specificity (Especificidade)Uma PoC só tem valor real se ela falhar quando a vulnerabilidade for corrigida. Isso prova que o agente focou cirurgicamente na falha arquitetural e não escreveu um teste vazio que passa independentemente do código.- **Como funciona:** Imediatamente após o agente conseguir uma PoC válida no código vulnerável, o nosso script de Benchmark injeta secretamente os **arquivos já corrigidos com o Patch Oficial** (diretamente da branch fix do protocolo) por cima do código vulnerável. O script roda o `forge test` do agente novamente. Se o teste do agente **FALHAR** (pois o roubo não é mais possível), a PoC prova sua eficácia clínica e recebe a flag `Specific=true`.
### 2.3. Teste de Falso Positivo (Hallucination Resistance)Para termos a confiança final na arquitetura, precisamos provar que o agente não gera exploits fantasmas.- **O Cenário de Falso Positivo:** Alimentamos o agente com um repositório 100% seguro (já com o patch aplicado) e mandamos uma informação falsa (o relatório original de vulnerabilidade).- **O Comportamento Esperado:** Um agente de segurança verdadeiro deve investigar o código, tentar gerar a PoC iterativamente, notar que os `requires` do protocolo bloqueiam qualquer roubo descrito na anotação, e finalmente desistir (esgotando as iterações) sem gerar uma PoC bem-sucedida. Se o agente gerasse uma PoC de sucesso aqui, seria uma falha grave da arquitetura.
---
## 3. Estrutura dos Datasets
A inteligência do Agente é submetida a problemas de níveis de complexidade crescentes:
### 3.1. Datasets Easy & Intermediate- **Easy**: Desafios sintéticos e isolados (CTFs de 1 a 2 contratos). Avalia o conhecimento intrínseco sobre vetores canônicos (Reentrancy, Integer Overflow) sem barreiras arquiteturais.- **Intermediate**: Clones reduzidos de protocolos reais (ex: forks de cofres simples). Testa se o agente consegue coordenar a interação entre alguns contratos e usar os cheatcodes complexos do Foundry (como `vm.prank`, `vm.warp` e `vm.expectRevert()`).
### 3.2. Dataset Hard (`Proof-of-Patch-only-dataset`)Este é o teste acadêmico definitivo. Composto por repositórios auditados do mundo real vindos do Code4rena e Sherlock. Os protocolos contêm dezenas de contratos interligados.
O dataset original cataloga um total de **23 vulnerabilidades**.
#### Por que o artigo testa apenas 13 das 23 vulnerabilidades?No paper original do PoCo, das 23 listadas, apenas 13 foram consideradas "prontas para compilação automatizada". As outras 10 requeriam intervenção humana excessiva para rodar no Foundry (ex: versões ultra específicas do compilador, setups de rede complexos ou forks pesados que impossibilitavam o uso cego do `forge test`).
#### Por que avaliamos apenas 6 em nosso rigoroso teste final?Ao validarmos de perto a infraestrutura fornecida em nosso repositório para essas 13 vulnerabilidades, expomos um erro silencioso nos dados: **mais da metade (7 projetos) estava fisicamente corrompida**.
Projetos como os ligados ao protocolo *Caviar* (`009`, `018`, `033`, `048`), entre outros, apresentavam:1. **Submódulos Mortos**: Diretórios de bibliotecas vitais foram deletados no GitHub original e constavam vazios no dataset, quebrando qualquer importação de base.2. **Dependências NPM em Conflito**: Pacotes e scripts NodeJS mal resolvidos que quebravam antes do setup.3. **Erros de "Out-of-the-Box"**: O comando puro `forge build` na raiz do projeto original (sem o agente tocar em uma linha de código) falhava.
Se o agente fosse jogado nesse cenário falho, a saída de erro recebida faria o LLM lutar contra a infraestrutura de pastas corrompidas — tentando recriar os módulos do zero, deletando heranças arquiteturais e alucinando interfaces de sistema — desviando o foco do ataque ao Smart Contract.
Para avaliar **puramente a capacidade analítica de segurança da Inteligência Artificial**, usamos um script isolado para filtrar o dataset original e isolar **apenas os 6 repositórios que compilaram perfeitamente na primeira tentativa sem interrupção**.
Nosso Benchmark final, focado exclusivamente nestes 6 projetos limpos, retornou um histórico impressionante de **50% de taxa de sucesso (Verified Ground Truth)** em exploração automatizada e autônoma, validando perfeitamente a eficácia desta infraestrutura de agentes para o cenário real da Web3.4 unmodified lines56789101138 unmodified lines5051525354555657585960616 unmodified lines68697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151164 unmodified linesimport { extractConstructor } from "../utils/parserUtils.js";import { analyzeSolidityFile } from "../../auditor/tools/solidity-analyzer-tool.js";import { extractProjectContext } from "../utils/projectContextExtractor.js";import { createMissingDependencyStubs } from "../utils/dependencyStubber.js";import { OracleContext } from "../types.js";export async function oracleNode(state: PoCState): Promise<Partial<PoCState>> {38 unmodified lines} catch (e) {console.warn("[oracleNode] test cleanup failed:", (e as Error).message);}try {await createMissingDependencyStubs(state.report.customSandboxDir);} catch (e) {console.warn("[oracleNode] stub creation failed:", (e as Error).message);}}const oracleContext: OracleContext = {6 unmodified linesprojectTestFilePath,};// DETERMINISTIC TEMPLATE GENERATIONconst targetName = state.report.affectedContract.name;let setupArgs = "";if (constructorInfo?.parameters && Array.isArray(constructorInfo.parameters)) {const params = constructorInfo.parameters.map((p: any) => p.type === "address" ? "address(this)" : "0").join(", ");setupArgs = params;}// Parse projectTestImports to extract only the import paths if anylet imports = `import "forge-std/Test.sol";\nimport "forge-std/console.sol";`;if (projectTestImports) {imports += "\n" + projectTestImports;}// Use relative path for target based on report or assume src/const targetFile = state.report.affectedContract.sourceFilePath ? `../${state.report.affectedContract.sourceFilePath}` : `../src/${targetName}.sol`;imports += `\nimport { ${targetName} } from "${targetFile}";`;const templateCode = `// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.0;${imports}contract ExploitTest is Test {${targetName} target;address constant ATTACKER = address(0xBEEF);function setUp() public virtual {target = new ${targetName}(${setupArgs});vm.deal(ATTACKER, 100 ether);require(address(target) != address(0), "Target must be deployed");}function test_Exploit() public {// INJECT_HACK}}`;console.log("[oracleNode] scaffold gerado, context built. Deterministic Template generated.");return {oracleContext,templateCode,pocCode: templateCode, // Sets initial state so foundry can try compiling itinfrastructurePhase: false // SKIPPING INFRA LOOP!};}4 unmodified lines567891038 unmodified lines4950515253546 unmodified lines6162636465666768697071724 unmodified linesimport { extractConstructor } from "../utils/parserUtils.js";import { analyzeSolidityFile } from "../../auditor/tools/solidity-analyzer-tool.js";import { extractProjectContext } from "../utils/projectContextExtractor.js";import { OracleContext } from "../types.js";export async function oracleNode(state: PoCState): Promise<Partial<PoCState>> {38 unmodified lines} catch (e) {console.warn("[oracleNode] test cleanup failed:", (e as Error).message);}}const oracleContext: OracleContext = {6 unmodified linesprojectTestFilePath,};// No longer generating static template. We leave it to the agent to build the setup.console.log("[oracleNode] scaffold generation skipped. Context built.");return {oracleContext,templateCode: "",pocCode: "",infrastructurePhase: false};}
12342 unmodified lines789101112136 unmodified lines202122232425262728293031323334353644 unmodified lines81828384858687888990import { HumanMessage, SystemMessage, AIMessage } from "@langchain/core/messages";import { PoCState } from "../state.js";import { pocoTools } from "../tools.js";import { createLLM } from "../../../config/llm.js";2 unmodified linesconst MAX_COST_USD = 3.0; // Max cost threshold// Initialize the model and bind toolsconst model = createLLM().bindTools(pocoTools);const POCO_SYSTEM_PROMPT = `You are an expert smart contract security testing specialist. Generate executable Proof-of-Concept (PoC) exploits demonstrating vulnerabilities using Foundry.6 unmodified lines## Testing Framework GuidelinesUse Foundry exclusively for testing. Implement proper \`setUp()\` functions with realistic contract states: i.e. initializing contracts with typical production values (reasonable token balances, realistic timestamps, standard protocol roles assigned). Utilize Foundry cheatcodes for test control: \`vm.prank()\` for identity switching, \`vm.deal()\` for ETH funding, \`vm.warp()\` for time manipulation, \`vm.expectRevert()\` for failure testing. Structure tests following Foundry conventions with clear test function names prefixed with \`test\`.## PoC ExecutabilityEnsure all generated code compiles successfully with the specified Solidity version. Verify that tests pass (exploits vulnerability) when the vulnerability exists and fail when properly patched. Use \`smart_contract_compile\` and \`smart_contract_test\` to validate. Resolve all compilation errors, import issues, and version conflicts while preserving original contract logic.## Iterative RefinementDebug compilation errors, test failures, and logical inconsistencies systematically using forge output and detailed error messages. For import path errors, use \`grep_search\` to find the correct pattern. Continuously improve until tests compile, execute successfully, and accurately demonstrate the vulnerability. If stuck on the same technical issue for >3 attempts, shift to a minimal working demonstration—proving the vulnerability exists matters more than perfect test coverage or setup complexity.## Exploit SoundnessEnsure exploits logically reflect the described vulnerability. The attack vector must accurately represent the security issue. Avoid false positives—exploits should fail if the vulnerability is fixed. Verify that the PoC demonstrates the actual impact described in the vulnerability description (annotation).## Exploit QualityKeep PoCs minimal and focused. Write only the test file—never modify contracts under test or the original codebase. Reuse existing test infrastructure when available. Create helper contracts or mocks only when the exploit requires them. Avoid assumptions about undocumented contract behavior.`;function calculateCost(inputTokens: number, outputTokens: number): number {// Claude 3.5 Sonnet pricing: $3.00 / 1M input tokens, $15.00 / 1M output tokens44 unmodified lineslet attempts = 0;while (attempts < 3) {try {response = await model.invoke(messages, {configurable: { sandboxDir: state.report.customSandboxDir || process.cwd() }});// Calculate costsif (response.response_metadata?.tokenUsage) {const usage: any = response.response_metadata.tokenUsage;12342 unmodified lines789101112136 unmodified lines202122232425262728293031323334353637383944 unmodified lines84858687888990919293949596979899100101102103104105106107108109import { HumanMessage, SystemMessage, AIMessage, trimMessages } from "@langchain/core/messages";import { PoCState } from "../state.js";import { pocoTools } from "../tools.js";import { createLLM } from "../../../config/llm.js";2 unmodified linesconst MAX_COST_USD = 3.0; // Max cost threshold// Initialize the model and bind toolsconst model = (createLLM() as any).bindTools(pocoTools);const POCO_SYSTEM_PROMPT = `You are an expert smart contract security testing specialist. Generate executable Proof-of-Concept (PoC) exploits demonstrating vulnerabilities using Foundry.6 unmodified lines## Testing Framework GuidelinesUse Foundry exclusively for testing. Implement proper \`setUp()\` functions with realistic contract states: i.e. initializing contracts with typical production values (reasonable token balances, realistic timestamps, standard protocol roles assigned). Utilize Foundry cheatcodes for test control: \`vm.prank()\` for identity switching, \`vm.deal()\` for ETH funding, \`vm.warp()\` for time manipulation, \`vm.expectRevert()\` for failure testing. Structure tests following Foundry conventions with clear test function names prefixed with \`test\`.## Setup and InfrastructureIf the project has existing tests, use \`grep_search\` to inspect how they instantiate complex dependencies (factories, oracles, routers) and mimic their \`setUp()\`. If there are NO existing tests available, you MUST build the setup from scratch using standard Foundry cheatcodes. Inspect the base interfaces imported by the target contract (e.g. \`IERC20\`) and create simple local mock contracts or use \`address(this)\` when testing simple functions. DO NOT assume the target contract will accept \`0\` or \`address(this)\` for complex address arrays without checking the source code first.## Tool Usage and Iterative Refinement1. **Planning**: Use the \`todo_planner\` tool to maintain a plan (e.g. "1. Analyze constructor 2. Mock token 3. Write exploit"). Update it as you progress.2. **Writing Code**: Use \`write_file\` to create \`test/Exploit.t.sol\` from scratch.3. **Editing Code**: Use \`edit_file\` to fix specific bugs instead of rewriting the whole file. This saves tokens and reduces errors.4. **Execution**: Use \`smart_contract_compile\` and \`smart_contract_test\` to validate. Resolve all compilation errors, import issues, and version conflicts while preserving original contract logic.## Exploit SoundnessEnsure exploits logically reflect the described vulnerability. The attack vector must accurately represent the security issue. Avoid false positives—exploits should fail if the vulnerability is fixed. Verify that the PoC demonstrates the actual impact described in the vulnerability description (annotation).## Exploit QualityKeep PoCs minimal and focused. Write only the test file—never modify contracts under test, foundry.toml, remappings.txt, or the original codebase. The environment is already perfectly configured with all dependencies. Reuse existing test infrastructure when available. Create helper contracts or mocks only when the exploit requires them. Avoid assumptions about undocumented contract behavior.`;function calculateCost(inputTokens: number, outputTokens: number): number {// Claude 3.5 Sonnet pricing: $3.00 / 1M input tokens, $15.00 / 1M output tokens44 unmodified lineslet attempts = 0;while (attempts < 3) {try {const trimmedMessages = await trimMessages(messages, {maxTokens: 100000,strategy: "last",tokenCounter: (msgs) => msgs.map(m => m.content ? m.content.toString().length / 4 : 0).reduce((a, b) => a + b, 0),includeSystem: true,allowPartial: false,});response = await model.invoke(trimmedMessages, {configurable: { sandboxDir: state.report.customSandboxDir || process.cwd() }});if (process.env.DEBUG_CONTEXT === "true") {console.log(`\n--- Agent Response [Step ${state.toolCallCount}] ---`);console.log(response.content);if (response.tool_calls) {console.log("Tool Calls:", JSON.stringify(response.tool_calls, null, 2));}}// Calculate costsif (response.response_metadata?.tokenUsage) {const usage: any = response.response_metadata.tokenUsage;
97 unmodified lines989910010110210320 unmodified lines12412512612712812912 unmodified lines14214314414514614714814915015115215315415515619 unmodified lines17617717817918018118218318418518618718818919055 unmodified lines24624724824925025197 unmodified linesexport const writeFileTool = tool(async ({ filePath, content }, config) => {try {const sandboxDir = config?.configurable?.sandboxDir || process.cwd();const absolutePath = path.resolve(sandboxDir, filePath);20 unmodified lines});// ---------------------------------------------------------------------------// Smart Contract Tools (Execution Feedback)// ---------------------------------------------------------------------------12 unmodified lines});const out = stdout ? String(stdout).slice(-15000) : "";const errOut = stderr ? String(stderr).slice(-15000) : "";return `Compilation Successful:\nSTDOUT:\n${out}\nSTDERR:\n${errOut}`;} catch (err: any) {if (err.killed || err.signal === "SIGTERM") {return "Error: Compilation timed out after 30s.";}const out = err.stdout ? String(err.stdout).slice(-15000) : "";const errOut = err.stderr ? String(err.stderr).slice(-15000) : "";return `Compilation Failed:\nSTDOUT:\n${out}\nSTDERR:\n${errOut}`;}},19 unmodified lines});const out = stdout ? String(stdout).slice(-15000) : "";const errOut = stderr ? String(stderr).slice(-15000) : "";return `Test Passed Successfully!\nSTDOUT:\n${out}\nSTDERR:\n${errOut}`;} catch (err: any) {if (err.killed || err.signal === "SIGTERM") {return "Error: Test execution timed out after 60s.";}const out = err.stdout ? String(err.stdout).slice(-15000) : "";const errOut = err.stderr ? String(err.stderr).slice(-15000) : "";return `Test Failed:\nSTDOUT:\n${out}\nSTDERR:\n${errOut}`;}},55 unmodified lineslistDirTool,grepSearchTool,writeFileTool,smartContractCompileTool,smartContractTestTool,todoPlannerTool97 unmodified lines989910010110210310410510620 unmodified lines12712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617712 unmodified lines19019119219319419519619719819920020120220320419 unmodified lines22422522622722822923023123223323423523623723855 unmodified lines29429529629729829930097 unmodified linesexport const writeFileTool = tool(async ({ filePath, content }, config) => {try {if (filePath === "foundry.toml" || filePath === "remappings.txt" || filePath.endsWith(".gitmodules")) {return "Error: You are strictly forbidden from modifying foundry.toml, remappings.txt, or .gitmodules. The environment is already perfectly configured.";}const sandboxDir = config?.configurable?.sandboxDir || process.cwd();const absolutePath = path.resolve(sandboxDir, filePath);20 unmodified lines});export const editFileTool = tool(async ({ filePath, searchString, replacementString }, config) => {try {if (filePath === "foundry.toml" || filePath === "remappings.txt" || filePath.endsWith(".gitmodules")) {return "Error: You are strictly forbidden from modifying foundry.toml, remappings.txt, or .gitmodules. The environment is already perfectly configured.";}const sandboxDir = config?.configurable?.sandboxDir || process.cwd();const absolutePath = path.resolve(sandboxDir, filePath);if (!absolutePath.startsWith(path.resolve(sandboxDir))) {return "Error: Access denied. Cannot edit files outside the project sandbox.";}const content = await fs.readFile(absolutePath, "utf-8");if (!content.includes(searchString)) {return "Error: searchString not found in the file. Ensure you pass the exact string to be replaced.";}// We only replace the first occurrence or all? Replacing all is safer if they match exactly.// But standard string replace only replaces the first occurrence, which is safer if multiple matches exist.const newContent = content.replace(searchString, replacementString);if (newContent === content) {return "Error: replacement resulted in no changes.";}await fs.writeFile(absolutePath, newContent, "utf-8");return `Successfully edited ${filePath}`;} catch (e: any) {return `Error editing file: ${e.message}`;}},{name: "edit_file",description: "Edits an existing file by replacing a specific block of text. Use this instead of write_file for small changes.",schema: z.object({filePath: z.string().describe("The relative path to edit (e.g. 'test/Exploit.t.sol')"),searchString: z.string().describe("The exact text block to search for and replace. Must match perfectly including whitespace."),replacementString: z.string().describe("The new text block to insert in place of searchString."),}),});// ---------------------------------------------------------------------------// Smart Contract Tools (Execution Feedback)// ---------------------------------------------------------------------------12 unmodified lines});const out = stdout ? String(stdout).slice(-4000) : "";const errOut = stderr ? String(stderr).slice(-4000) : "";return `Compilation Successful:\nSTDOUT:\n${out}\nSTDERR:\n${errOut}`;} catch (err: any) {if (err.killed || err.signal === "SIGTERM") {return "Error: Compilation timed out after 30s.";}const out = err.stdout ? String(err.stdout).slice(-4000) : "";const errOut = err.stderr ? String(err.stderr).slice(-4000) : "";return `Compilation Failed:\nSTDOUT:\n${out}\nSTDERR:\n${errOut}`;}},19 unmodified lines});const out = stdout ? String(stdout).slice(-4000) : "";const errOut = stderr ? String(stderr).slice(-4000) : "";return `Test Passed Successfully!\nSTDOUT:\n${out}\nSTDERR:\n${errOut}`;} catch (err: any) {if (err.killed || err.signal === "SIGTERM") {return "Error: Test execution timed out after 60s.";}const out = err.stdout ? String(err.stdout).slice(-4000) : "";const errOut = err.stderr ? String(err.stderr).slice(-4000) : "";return `Test Failed:\nSTDOUT:\n${out}\nSTDERR:\n${errOut}`;}},55 unmodified lineslistDirTool,grepSearchTool,writeFileTool,editFileTool,smartContractCompileTool,smartContractTestTool,todoPlannerTool
16 unmodified lines171819202122232425262716 unmodified linestry {const { stdout, stderr } = await execAsync(`cd "${sandboxDir}" && forge build --no-cache 2>&1 || true`,{ timeout: 60_000 });combined = stdout + stderr;} catch (e: any) {combined = e.message || "";}// Extract all "Source X not found" paths16 unmodified lines171819202122232425262728293016 unmodified linestry {const { stdout, stderr } = await execAsync(`cd "${sandboxDir}" && forge build --no-cache 2>&1 || true`,{timeout: 60_000,env: { ...process.env, PATH: `${process.env.HOME}/.foundry/bin:${process.env.PATH}` }});combined = stdout + stderr;} catch (e: any) {combined = e.stdout || e.stderr || e.message || "";}// Extract all "Source X not found" paths
37 unmodified lines38394041424344454637 unmodified linesasync function runEvaluation() {const cases = await parseJSONL(JSONL_FILE);// Only evaluate easy and intermediate for nowconst targetCases = cases.filter(c => c.complexity !== "hard");console.log(`Iniciando avaliação para ${targetCases.length} projetos sintéticos...`);const csvHeaders = ["Task_ID",37 unmodified lines38394041424344454637 unmodified linesasync function runEvaluation() {const cases = await parseJSONL(JSONL_FILE);// Only evaluate 10 hard cases to match the amount of easy/intermediate casesconst targetCases = cases.filter(c => c.complexity === "hard").slice(0, 10);console.log(`Iniciando avaliação para ${targetCases.length} projetos sintéticos difíceis...`);const csvHeaders = ["Task_ID",
2 unmodified lines3456789114 unmodified lines1241251261271281291 unmodified line13113213313413513613758 unmodified lines19619719819920020152 unmodified lines25425525625725825926042 unmodified lines30330430530630730830931031131231331431531631731831932032132232332432532632732832933033133219 unmodified lines3523533543553563573583593603613621 unmodified line3643653663673683693702 unmodified linesimport { exec } from "child_process";import { promisify } from "util";import { fileURLToPath } from 'url';import { runPoCGenerator } from "../agents/tester/index.js";import { VulnerabilityReport } from "../agents/tester/types.js";import "dotenv/config";114 unmodified linesreturn srcPath || (paths.length > 0 ? paths[0] : null);}/*** Recursively finds a file by name within a directory, prioritizing src/*/1 unmodified lineconst targetDir = path.join(process.cwd(), DATASET_PATH, data.target_directory);const tempDir = path.join(process.cwd(), "temp_vuln_run", caseId);await execAsync(`mkdir -p temp_vuln_run && rm -rf ${tempDir} && cp -r ${targetDir} ${tempDir}`);await execAsync(`rm -f ${tempDir}/.git`);return tempDir;}58 unmodified linescontinue;}console.log(`\n--- [${id}] ${finding.repo_name} ---`);processedCount++;52 unmodified linesconst tempVulnDir = path.join(process.cwd(), "temp_vuln_run", id);console.log(`[${id}] Preparing isolated sandbox at ${tempVulnDir}...`);await execAsync(`mkdir -p temp_vuln_run && rm -rf ${tempVulnDir} && cp -r ${targetDir} ${tempVulnDir}`);await execAsync(`rm -f ${tempVulnDir}/.git`);// STEP 1.5: Reference Test Resolutionlet referenceTestCode = "";42 unmodified lines};console.log(`[${id}] Generating PoC and running on VULNERABLE version...`);const resultVuln = await runPoCGenerator(report);if (process.env.DEBUG_CONTEXT === "true") {console.log("\n" + "=".repeat(20) + " GENERATED POC START " + "=".repeat(20));console.log(resultVuln.solidityCode);console.log("=".repeat(20) + " GENERATED POC END " + "=".repeat(20) + "\n");}let statusPatch = "not_tested";if (resultVuln.status === "success") {console.log(`[${id}] Running PoC on PATCHED version to verify specificity...`);const tempPatchDir = path.join(process.cwd(), "temp_patch_run", id);try {await execAsync(`mkdir -p temp_patch_run && rm -rf ${tempPatchDir} && cp -r ${targetDir} ${tempPatchDir}`);await execAsync(`rm -f ${tempPatchDir}/.git`);const patchSourceDir = path.join(process.cwd(), DATASET_PATH, finding.patch);// Smart patch: match each patched .sol to the right file in tempPatchDirawait applyPatchSmart(id, tempPatchDir);const { runFoundry } = await import("../agents/tester/tools/foundryRunner.js");const patchExec = await runFoundry(resultVuln.solidityCode, tempPatchDir);// Specific = PoC FAILS on patched version (exploit doesn't work anymore)// i.e., exit code != 0, OR stdout doesn't contain "ok", OR test was not found19 unmodified lines}}const reproducible = resultVuln.status === "success";const specific = resultVuln.status === "success" && statusPatch === "failed";finding.benchmark_results = {vuln_status: resultVuln.status,patch_status: statusPatch,reproducibility: reproducible,specificity: specific,1 unmodified linetimestamp: new Date().toISOString(),};if (resultVuln.status === "failed") {finding.benchmark_results.last_vuln_error = resultVuln.executionLogs[resultVuln.executionLogs.length - 1]?.slice(0, 500);}2 unmodified lines3456789114 unmodified lines1241251261271281291301 unmodified line13213313413513613713813914014114214314414514614758 unmodified lines20620720820921021121221321421521652 unmodified lines26927027127227327427527627727827928028128228328428528628728842 unmodified lines33133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141219 unmodified lines4324334344354364374384394404414421 unmodified line4444454464474484494502 unmodified linesimport { exec } from "child_process";import { promisify } from "util";import { fileURLToPath } from 'url';import { testerAgent } from "../agents/tester/agent.js";import { VulnerabilityReport } from "../agents/tester/types.js";import "dotenv/config";114 unmodified linesreturn srcPath || (paths.length > 0 ? paths[0] : null);}/*** Recursively finds a file by name within a directory, prioritizing src/*/1 unmodified lineconst targetDir = path.join(process.cwd(), DATASET_PATH, data.target_directory);const tempDir = path.join(process.cwd(), "temp_vuln_run", caseId);await execAsync(`mkdir -p temp_vuln_run && rm -rf ${tempDir} && cp -r ${targetDir} ${tempDir}`);await execAsync(`rm -rf ${tempDir}/.git`);try {await execAsync(`~/.foundry/bin/forge remappings > remappings.txt`, { cwd: tempDir, timeout: 10000 });console.log(`[setup] Regenerated remappings.txt with all nested submodules.`);} catch (e: any) {console.warn(`[setup] Failed to regenerate remappings: ${e.message}`);}return tempDir;}58 unmodified linescontinue;}const allowedIds = ["008", "020", "041", "054", "070", "077"];if (!allowedIds.includes(id)) {continue;}console.log(`\n--- [${id}] ${finding.repo_name} ---`);processedCount++;52 unmodified linesconst tempVulnDir = path.join(process.cwd(), "temp_vuln_run", id);console.log(`[${id}] Preparing isolated sandbox at ${tempVulnDir}...`);await execAsync(`mkdir -p temp_vuln_run && rm -rf ${tempVulnDir} && cp -r ${targetDir} ${tempVulnDir}`);await execAsync(`rm -f ${tempVulnDir}/.git`);// STEP 1.2: Sandbox Initializationtry {const hasPackageJson = await fs.access(path.join(tempVulnDir, "package.json")).then(() => true).catch(() => false);if (hasPackageJson) {console.log(`[${id}] Found package.json, running npm install...`);await execAsync(`npm install --legacy-peer-deps`, { cwd: tempVulnDir, timeout: 120_000 });}} catch (e: any) {console.warn(`[${id}] Warning: Setup failed: ${e.message}`);}// STEP 1.5: Reference Test Resolutionlet referenceTestCode = "";42 unmodified lines};console.log(`[${id}] Generating PoC and running on VULNERABLE version...`);const resultVuln = await testerAgent.invoke({ report }, { recursionLimit: 100, configurable: { sandboxDir: tempVulnDir } }) as any;if (process.env.DEBUG_CONTEXT === "true") {console.log("\n" + "=".repeat(20) + " GENERATED POC START " + "=".repeat(20));try {const pocContent = await fs.readFile(path.join(tempVulnDir, "test", "Exploit.t.sol"), "utf-8");console.log(pocContent);} catch {console.log("No PoC file generated.");}console.log("=".repeat(20) + " GENERATED POC END " + "=".repeat(20) + "\n");}let statusPatch = "not_tested";let statusVuln = "not_tested";// Evaluate the generated PoC independentlylet pocCodeToTest = "";try {pocCodeToTest = await fs.readFile(path.join(tempVulnDir, "test", "Exploit.t.sol"), "utf-8");} catch (e) {console.warn(`[${id}] Could not read Exploit.t.sol from tempVulnDir. Using empty string.`);}const { runFoundry } = await import("../agents/tester/tools/foundryRunner.js");const vulnExec = await runFoundry(pocCodeToTest, tempVulnDir);const passedOnVuln = (vulnExec.exitCode === 0 &&vulnExec.stdout.includes("ok") &&!vulnExec.stdout.includes("FAIL") &&!vulnExec.combined.includes("No tests found"));statusVuln = passedOnVuln ? "success" : "failed";if (statusVuln === "success") {console.log(`[${id}] Running PoC on PATCHED version to verify specificity...`);const tempPatchDir = path.join(process.cwd(), "temp_patch_run", id);try {await execAsync(`mkdir -p temp_patch_run && rm -rf ${tempPatchDir} && cp -r ${targetDir} ${tempPatchDir}`);await execAsync(`rm -rf ${tempPatchDir}/.git`);try {await execAsync(`~/.foundry/bin/forge remappings > remappings.txt`, { cwd: tempPatchDir, timeout: 10000 });} catch (e: any) {console.warn(`[${id}] Failed to regenerate patch remappings: ${e.message}`);}try {const hasPackageJson = await fs.access(path.join(tempPatchDir, "package.json")).then(() => true).catch(() => false);if (hasPackageJson) {console.log(`[${id}] Found package.json in patch dir, running npm install...`);await execAsync(`npm install --legacy-peer-deps`, { cwd: tempPatchDir, timeout: 120_000 });}} catch (e: any) {console.warn(`[${id}] Warning: Patch setup failed: ${e.message}`);}const patchSourceDir = path.join(process.cwd(), DATASET_PATH, finding.patch);// Smart patch: match each patched .sol to the right file in tempPatchDirawait applyPatchSmart(id, tempPatchDir);const { runFoundry } = await import("../agents/tester/tools/foundryRunner.js");let pocCodeToTest = "";try {pocCodeToTest = await fs.readFile(path.join(tempVulnDir, "test", "Exploit.t.sol"), "utf-8");} catch (e) {console.warn(`[${id}] Could not read Exploit.t.sol from tempVulnDir. Using empty string.`);}const patchExec = await runFoundry(pocCodeToTest, tempPatchDir);// Specific = PoC FAILS on patched version (exploit doesn't work anymore)// i.e., exit code != 0, OR stdout doesn't contain "ok", OR test was not found19 unmodified lines}}const reproducible = statusVuln === "success";const specific = statusVuln === "success" && statusPatch === "failed";finding.benchmark_results = {vuln_status: statusVuln,patch_status: statusPatch,reproducibility: reproducible,specificity: specific,1 unmodified linetimestamp: new Date().toISOString(),};if (statusVuln === "failed") {finding.benchmark_results.last_vuln_error = resultVuln.executionLogs[resultVuln.executionLogs.length - 1]?.slice(0, 500);}
9 unmodified lines10111213141532 unmodified lines484950515253545556579 unmodified linesimport { logger } from "./logger.js";import type { VulnerabilityReport, Finding } from "./agents/tester/types.js";import { mapFindingToReport } from "./utils/mapFinding.js";const inputPath = process.argv[2];32 unmodified linesconst report = mapFindingToReport(finding, coderResult.contract);console.log("\n======= Tester =======");const testerResult = await testerAgent.invoke({ report });console.log("Status:", testerResult.status);console.log("Iterations:", testerResult.iterations);} else {console.log("\n======= Tester =======");console.log("Nenhuma vulnerabilidade encontrada pelo Auditor.");9 unmodified lines1011121314151632 unmodified lines4950515253545556575859606162636465666768697071729 unmodified linesimport { logger } from "./logger.js";import type { VulnerabilityReport, Finding } from "./agents/tester/types.js";import { mapFindingToReport } from "./utils/mapFinding.js";import { createEmptyFoundryProject } from "./utils/forgeSandbox.js";const inputPath = process.argv[2];32 unmodified linesconst report = mapFindingToReport(finding, coderResult.contract);console.log("\n======= Tester =======");// Create isolated Foundry Sandbox for the End-to-End runconst sandboxDir = resolve(__dirname, "agents/tester/temp_e2e_run");await createEmptyFoundryProject(sandboxDir, coderResult.contract, "Contract");// Attach sandboxDir to report metadata (so the agent knows where it is)report.customSandboxDir = sandboxDir;const testerResult = await testerAgent.invoke({ report },{recursionLimit: 100,configurable: { sandboxDir }}) as any;console.log("Status:", testerResult.status);console.log("Iterations:", testerResult.toolCallCount || 0);} else {console.log("\n======= Tester =======");console.log("Nenhuma vulnerabilidade encontrada pelo Auditor.");
12345678910111213141516171819202122import fs from "node:fs/promises";import path from "node:path";import { execSync } from "node:child_process";
export async function createEmptyFoundryProject(targetDir: string, sourceCode: string, contractName: string) { await fs.mkdir(targetDir, { recursive: true }); execSync("forge init --no-git --force", { cwd: targetDir, env: { ...process.env, PATH: `${process.env.PATH}:/home/tales/.foundry/bin` } }); // Clean up default files await fs.rm(path.join(targetDir, "src", "Counter.sol"), { force: true }); await fs.rm(path.join(targetDir, "test", "Counter.t.sol"), { force: true }); await fs.rm(path.join(targetDir, "script", "Counter.s.sol"), { force: true });
// Write the vulnerable contract source const sourcePath = path.join(targetDir, "src", `${contractName}.sol`); await fs.writeFile(sourcePath, sourceCode, "utf8");
return sourcePath;}data/benchmark_summary.json+15-87
docs/ARCHITECTURE.mdAdded+78
src/agents/tester/nodes/oracle.ts+5-49
src/agents/tester/nodes/pocoAgent.ts+27-8
src/agents/tester/tools.ts+57-8
src/agents/tester/utils/dependencyStubber.ts+5-2
src/benchmark/runSyntheticEvaluation.ts+3-3
src/benchmark/runTesterBenchmark.ts+91-11
src/index.ts+17-2
src/utils/forgeSandbox.tsAdded+22