Skip to content

Pre-Execution Hooks

Pre-execution hooks run after workspace setup but before the AI agent executes. Use them to prepare the environment for the agent.

  • Environment variable injection
  • Search and replace in source code
  • Code generation
  • File setup and templating
  • Mock data creation
  • Dependency installation

The script hook runs shell commands:

pre_execution:
- name: script
config:
command: bash
args:
- "-c"
- |
echo "Setting up environment..."
mkdir -p ${WORKSPACE_DIR}/config
pre_execution:
- name: script
config:
command: bash # Command to run
args: # Arguments array
- "-c"
- "echo 'Hello'"
timeout_ms: 30000 # Timeout in milliseconds
env: # Additional environment variables
MY_VAR: "value"

The shell or executable to run:

command: bash
# or
command: node
# or
command: python

Arguments passed to the command:

args:
- "-c"
- |
echo "Multi-line script"
mkdir -p ./temp

Maximum execution time in milliseconds:

timeout_ms: 60000 # 1 minute

Default: 30000 (30 seconds)

Additional environment variables:

env:
API_KEY: "${SECRET_API_KEY}"
DEBUG: "true"

Pre-execution hooks have access to:

VariableDescription
WORKSPACE_DIRFull path to workspace
REPO_DIRPath to cloned repository
ARTIFACTS_DIRPath for output artifacts
TEST_CASE_NAMEName from suite config
REPO_URLRepository URL
BRANCHBranch being tested
pre_execution:
- name: script
config:
command: bash
args:
- "-c"
- |
mkdir -p ${WORKSPACE_DIR}/config
cat > ${WORKSPACE_DIR}/config/auth.json << EOF
{
"jwtSecret": "test-secret-key",
"tokenExpiry": "24h"
}
EOF
pre_execution:
- name: script
config:
command: bash
args:
- "-c"
- |
cd ${REPO_DIR}
npm install
timeout_ms: 120000 # 2 minutes for npm install
pre_execution:
- name: script
config:
command: node
args:
- "-e"
- |
const fs = require('fs');
const data = Array.from({length: 100}, (_, i) => ({
id: i + 1,
name: `User ${i + 1}`
}));
fs.writeFileSync(
process.env.REPO_DIR + '/fixtures/users.json',
JSON.stringify(data, null, 2)
);
pre_execution:
- name: script
config:
command: bash
args:
- "-c"
- |
cd ${REPO_DIR}
# Replace placeholder with actual value
find . -type f -name "*.ts" -exec \
sed -i 's/API_PLACEHOLDER/${API_URL}/g' {} \;
env:
API_URL: "https://api.example.com"

Hooks run sequentially in order:

pre_execution:
# First: Install dependencies
- name: script
config:
command: bash
args: ["-c", "cd ${REPO_DIR} && npm install"]
timeout_ms: 120000
# Second: Generate config
- name: script
config:
command: bash
args: ["-c", "echo '{\"env\": \"test\"}' > ${REPO_DIR}/config.json"]
# Third: Seed database
- name: script
config:
command: bash
args: ["-c", "cd ${REPO_DIR} && npm run db:seed"]

If a pre-execution hook fails:

  1. Execution stops immediately
  2. The agent is NOT executed
  3. Evaluation is marked as failed
  4. Error details are logged
pre_execution:
- name: script
config:
command: bash
args:
- "-c"
- |
set -e # Exit on first error
# Validate prerequisites
if [ ! -f "${REPO_DIR}/package.json" ]; then
echo "Error: package.json not found"
exit 1
fi
# Proceed with setup
cd ${REPO_DIR}
npm install
  1. Use environment variables for secrets, never hardcode
  2. Limit permissions in scripts
  3. Validate inputs before using in commands
  4. Set appropriate timeouts to prevent hanging