Files

94 lines
3.3 KiB
Bash

#!/bin/bash
# Agent repo init script — clones reference repos and working repo
# AR-12 through AR-15, AR-32
set -euo pipefail
echo "=== agent-repo/v1 init.sh ==="
# Ensure workspace directories exist
mkdir -p /workspace/reference
mkdir -p /workspace/project
mkdir -p /workspace/.agent-output
# Clone reference branches (AR-12, AR-13)
if [ -n "${REFERENCE_BRANCHES:-}" ] && [ "${REFERENCE_BRANCHES:-}" != "[]" ]; then
echo "Cloning reference branches..."
echo "$REFERENCE_BRANCHES" | python3 -c "
import json, sys, subprocess, os
refs = json.load(sys.stdin)
for ref in refs:
name = ref.get('name', '')
repo_url = ref.get('repo_url', '')
branch = ref.get('branch', 'main')
dest = f'/workspace/reference/{name}'
print(f'Cloning {repo_url} ({branch}) -> {dest}')
result = subprocess.run(
['git', 'clone', '--depth', '1', '--branch', branch,
'-c', 'core.symlinks=false', repo_url, dest],
capture_output=True, text=True
)
if result.returncode != 0:
print(f'ERROR: Failed to clone {repo_url}: {result.stderr}', file=sys.stderr)
sys.exit(1)
# Strip any symlinks (security: prevent /proc/1/environ exfiltration)
subprocess.run(['find', dest, '-type', 'l', '-exec', 'rm', '{}', ';'])
# Make reference read-only
subprocess.run(['chmod', '-R', 'a-w', dest])
print(f'Cloned {name} successfully')
"
fi
# Clone agent repo working branch (AR-14)
if [ -n "${AGENT_REPO_URL:-}" ] && [ -n "${AGENT_BRANCH:-}" ]; then
echo "Cloning agent working repo: $AGENT_REPO_URL (branch: $AGENT_BRANCH)"
if git clone --depth 1 --branch "$AGENT_BRANCH" "$AGENT_REPO_URL" /workspace/project 2>/dev/null; then
echo "Cloned existing branch $AGENT_BRANCH"
else
echo "Branch $AGENT_BRANCH does not exist, creating fresh clone..."
# Clone default branch, then checkout new branch
if git clone --depth 1 "$AGENT_REPO_URL" /workspace/project; then
cd /workspace/project
git checkout -b "$AGENT_BRANCH"
else
echo "ERROR: Failed to clone agent repo $AGENT_REPO_URL" >&2
exit 1
fi
fi
fi
# Clean up results/ from any previous run (AR-15)
if [ -d /workspace/project/results ]; then
rm -rf /workspace/project/results
fi
mkdir -p /workspace/project/results
# Populate .gitignore (AR-32)
GITIGNORE=/workspace/project/.gitignore
cat >> "$GITIGNORE" << 'GITIGNORE_EOF'
# Agent-repo auto-generated gitignore entries
*.key
*.pem
*.p12
*.pfx
.env
*.env
*.secret
GITIGNORE_EOF
# Handle retry: clone previous attempt branch as read-only reference (AR-16)
if [ -n "${AGENT_PREVIOUS_BRANCH:-}" ] && [ "${AGENT_RETRY_COUNT:-0}" -gt 0 ]; then
echo "Cloning previous attempt branch: $AGENT_PREVIOUS_BRANCH"
git clone --depth 1 --branch "$AGENT_PREVIOUS_BRANCH" \
-c core.symlinks=false \
"$AGENT_REPO_URL" /workspace/reference/previous-attempt 2>/dev/null || \
echo "Warning: Could not clone previous attempt branch (non-fatal)"
if [ -d /workspace/reference/previous-attempt ]; then
find /workspace/reference/previous-attempt -type l -exec rm {} \;
chmod -R a-w /workspace/reference/previous-attempt
fi
fi
export AGENT_WORKING_DIR="/workspace/project"
echo "AGENT_WORKING_DIR=$AGENT_WORKING_DIR"
echo "=== agent-repo/v1 init.sh complete ==="