-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcodex-research-loop.sh
More file actions
executable file
·114 lines (92 loc) · 3.09 KB
/
Copy pathcodex-research-loop.sh
File metadata and controls
executable file
·114 lines (92 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INTERVAL_SECONDS="${1:-120}"
PROMPT_FILE="${2:-$ROOT_DIR/.claude/commands/research.md}"
LOG_DIR="${LOG_DIR:-/tmp/codex-research-loop-logs}"
CODEX_BIN="${CODEX_BIN:-codex}"
CORPUS_DIR="${CORPUS_DIR:-$HOME/Desktop/projects/just-understanding-data/diffcore-eval-corpus}"
PLAN_DIR="${PLAN_DIR:-$HOME/.codex/plans}"
RUN_ONCE="${RUN_ONCE:-0}"
DANGEROUS="${DANGEROUS:-0}"
KEEP_REMOTE_MCP="${KEEP_REMOTE_MCP:-0}"
if ! [[ "$INTERVAL_SECONDS" =~ ^[0-9]+$ ]] || [[ "$INTERVAL_SECONDS" -lt 1 ]]; then
echo "error: interval must be a positive integer number of seconds" >&2
exit 1
fi
if ! command -v "$CODEX_BIN" >/dev/null 2>&1; then
echo "error: '$CODEX_BIN' is not installed or not on PATH" >&2
exit 1
fi
if [[ ! -f "$PROMPT_FILE" ]]; then
echo "error: prompt file not found: $PROMPT_FILE" >&2
exit 1
fi
mkdir -p "$LOG_DIR" "$PLAN_DIR"
build_codex_args() {
codex_args=(exec -C "$ROOT_DIR" --color always --add-dir "$PLAN_DIR")
if [[ "$KEEP_REMOTE_MCP" != "1" ]]; then
codex_args+=(
-c "mcp_servers.openaiDeveloperDocs.enabled=false"
-c "mcp_servers.auggie.enabled=false"
-c "mcp_servers.linear.enabled=false"
-c "mcp_servers.context7.enabled=false"
)
fi
if [[ -d "$CORPUS_DIR" ]]; then
codex_args+=(--add-dir "$CORPUS_DIR")
fi
if [[ "$DANGEROUS" == "1" ]]; then
codex_args+=(--dangerously-bypass-approvals-and-sandbox)
else
codex_args+=(--full-auto)
fi
if [[ -n "${CODEX_MODEL:-}" ]]; then
codex_args+=(-m "$CODEX_MODEL")
fi
if [[ -n "${CODEX_PROFILE:-}" ]]; then
codex_args+=(-p "$CODEX_PROFILE")
fi
}
render_prompt() {
cat <<EOF
Execute exactly one autonomous research-loop iteration for this repository.
Treat the attached instruction file as the canonical task spec. If it mentions
Claude Code, slash commands like /research, or an Agent tool, map those to the
closest Codex equivalent and continue.
Do one run, make the changes you judge appropriate, then exit cleanly.
EOF
cat "$PROMPT_FILE"
}
iteration=0
while true; do
iteration=$((iteration + 1))
timestamp="$(date '+%Y-%m-%d-%H%M%S')"
log_file="$LOG_DIR/run-$timestamp.log"
last_message_file="$LOG_DIR/run-$timestamp.last.txt"
codex_args=()
build_codex_args
ln -sfn "$log_file" "$LOG_DIR/latest.log"
ln -sfn "$last_message_file" "$LOG_DIR/latest.last.txt"
echo
echo "============================================================"
echo "Codex research loop #$iteration at $(date '+%Y-%m-%d %H:%M:%S')"
echo "Repo: $ROOT_DIR"
echo "Prompt: $PROMPT_FILE"
echo "Log: $log_file"
echo "============================================================"
set +e
render_prompt | "$CODEX_BIN" "${codex_args[@]}" -o "$last_message_file" - 2>&1 | tee "$log_file"
exit_code=${PIPESTATUS[1]}
set -e
if [[ $exit_code -eq 0 ]]; then
echo "run #$iteration finished successfully"
else
echo "run #$iteration failed with exit code $exit_code"
fi
if [[ "$RUN_ONCE" == "1" ]]; then
break
fi
echo "sleeping for $INTERVAL_SECONDS seconds"
sleep "$INTERVAL_SECONDS"
done