File size: 8,118 Bytes
92848be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# `/test-deploy` — How It Works & Lessons Learned

A maintainer comments `/test-deploy` on any open PR to run automated
sanity checks and benchmarks against the new task on the deploy server.
Results are posted back as a PR comment.

---

## Architecture

```
GitHub PR comment (/test-deploy)
    │
    ▼
GitHub Actions: test-deploy.yml        (runs on ubuntu-latest)
    │  1. Get PR metadata (number, head_ref, base_ref)
    │  2. SSH key setup (webfactory/ssh-agent)
    │  3. SCP test_on_server.py → deploy server /tmp/
    │  4. SSH: python3 /tmp/test_on_server.py <repo_path> <pr> <head_ref> <base_ref>
    │  5. SCP /tmp/test_deploy_results.json ← deploy server
    │  6. Post formatted results as PR comment
    │
    ▼
Deploy server (159.65.5.105)
    │  test_on_server.py:
    │  1. Acquire fcntl lock (serialize concurrent SSH sessions)
    │  2. git fetch upstream pull/<N>/head
    │  3. git checkout -B pr-test-<N> FETCH_HEAD
    │  4. git diff upstream/<base_ref>...HEAD  → detect changed tasks
    │  5. For each task: oracle → finance-zero → claude-code (opus) → claude-code (haiku)
    │  6. Write JSON results to /tmp/test_deploy_results.json
    │  7. finally: git checkout <original_branch> && git branch -D pr-test-<N>
```

### Agents run per task

| Agent | Model | Purpose |
|-------|-------|---------|
| oracle | built-in | Verifies reference solution passes all tests (reward must = 1) |
| finance-zero | gemini-3-flash-preview | Zero-shot baseline; if reward=1, task may be too easy |
| claude-code | claude-opus-4-6 | Primary AI benchmark |
| claude-code | claude-haiku-4-5-20251001 | Secondary AI benchmark (cheaper/faster) |

---

## Server Setup

### Required environment variables (`/etc/environment`)

PAM loads this file for non-interactive SSH sessions. Without it,
`harbor` and API keys are invisible to the test script.

```
PATH=/root/miniconda3/envs/finance-bench/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-...
GEMINI_API_KEY=AIzaSy...
```

> **Why `/etc/environment` and not `.bashrc`?**  
> Non-interactive SSH sessions do not source `.bashrc` or `.bash_profile`.
> PAM reads `/etc/environment` regardless of session type, as long as
> `UsePAM yes` is set in `sshd_config` (the default on Ubuntu).

### Git remotes on the server

```
origin   → git@github.com:beckybyte/QuantitativeFinance-Bench.git  (fork)
upstream → git@github.com:QF-Bench/QuantitativeFinance-Bench.git   (real repo)
```

PRs target `upstream`, so the diff must be against `upstream/<base_ref>`,
not `origin/<base_ref>`. Using `origin` caused the wrong tasks to be
detected (the fork's main diverged from QF-Bench's main).

### GitHub Secrets required

Set these in **both** `beckybyte/QuantitativeFinance-Bench` and
`QF-Bench/QuantitativeFinance-Bench`:

| Secret | Value |
|--------|-------|
| `DEPLOY_SSH_KEY` | Raw OpenSSH private key (`-----BEGIN OPENSSH PRIVATE KEY-----`) |
| `DEPLOY_USER` | `root` |
| `DEPLOY_HOST` | `159.65.5.105` |
| `DEPLOY_REPO_PATH` | `/root/ke/QuantitativeFinance-Bench` |

---

## Pitfalls & Lessons Learned

### 1. SSH private key format in GitHub Secrets
**Problem:** Pasting a multi-line private key into a GitHub Secret sometimes
strips newlines, causing `error in libcrypto` on connection.  
**Fix:** Use `webfactory/ssh-agent@v0.9.0` — it handles multi-line secrets
correctly without any base64 encoding tricks.

### 2. `ssh-keyscan -H` not available on GitHub Actions runners
**Problem:** The `ssh-keyscan` binary on the runner didn't support `-H`.  
**Fix:** Remove `ssh-keyscan` entirely; use `StrictHostKeyChecking=accept-new`
on both `scp` and `ssh` commands instead.

### 3. `harbor` not found in non-interactive SSH sessions
**Problem:** `harbor` lives in the conda env. Non-interactive SSH doesn't
source `.bashrc`, so `harbor` wasn't in `PATH`.  
**Fix:** Add the conda env's `bin/` to `PATH` in `/etc/environment`.

### 4. Wrong tasks detected — `origin` vs `upstream` diff
**Problem:** `git diff origin/main...HEAD` compared against the **fork's**
main, not QF-Bench's main. When the fork's main diverged, it detected
tasks from unrelated merged PRs instead of the current PR.  
**Fix:** Fetch both remotes and diff against `upstream/<base_ref>`:
```python
sh(["git", "fetch", "origin", args.base_ref])
sh(["git", "fetch", "upstream", args.base_ref])
diff_out = sh(["git", "diff", "--name-only", f"upstream/{args.base_ref}...HEAD"]).stdout
```

### 5. Duplicate webhook deliveries → concurrent runs → stale results
**Problem:** A single `/test-deploy` comment can trigger 4+ simultaneous
workflow runs (GitHub webhook retry behavior). Concurrent runs race on
`git checkout` and the results file, leaving stale data from a prior PR.  
**Fix (two layers):**
- Workflow-level: `concurrency.group` per PR with `cancel-in-progress: true`
- Server-level: `fcntl.flock(lock_fh, fcntl.LOCK_EX)` serializes SSH sessions

### 6. Unhandled exceptions left stale results visible
**Problem:** When `test_on_server.py` crashed before reaching `fail_and_exit()`
(e.g. due to stale git state), it exited silently, leaving the old
`/tmp/test_deploy_results.json` from a previous PR. The workflow then posted
that old PR's results to the new PR's comment.  
**Fix:** Wrap `main()` in a top-level `try/except` that always writes an error
JSON before exiting.

### 7. Repo left on `pr-test-<N>` branch after SIGTERM
**Problem:** When `test_on_server.py` was killed mid-run (e.g. `timeout`
command, SSH disconnect), the `finally` block didn't execute, leaving the
repo checked out on `pr-test-<N>`. The next run then had
`original_branch == test_branch`, causing the cleanup to fail and the repo
to remain dirty for subsequent runs.  
**Mitigation:** If you ever manually kill a test run, restore the repo:
```bash
git -C /root/ke/QuantitativeFinance-Bench checkout main
git -C /root/ke/QuantitativeFinance-Bench branch -D pr-test-<N>
```

### 8. `DEPLOY_REPO_PATH` secret empty → `os.chdir('')`
**Problem:** If `DEPLOY_REPO_PATH` is not set in a repo's GitHub Secrets,
`'${{ secrets.DEPLOY_REPO_PATH }}'` expands to `''`, causing
`FileNotFoundError: [Errno 2] No such file or directory: ''`.  
**Fix:** Ensure `DEPLOY_REPO_PATH=/root/ke/QuantitativeFinance-Bench` is
set in both the fork's and QF-Bench's repository secrets.

### 9. Committing to `pr-test-<N>` branch by mistake
**Problem:** While a test was running, `git commit` in the repo directory
landed on the active `pr-test-<N>` branch, not `main`. When the test
cleaned up (`git branch -D pr-test-<N>`), the commit was deleted.  
**Fix:** Never run `git` commands in the repo directory while a test is
running. Use `git -C <other-path>` or a worktree on a different branch,
or simply wait for the test to finish and restore `main` first.

### 10. `CLAUDE_CODE_OAUTH_TOKEN` env var overrides OAuth login
**Problem:** On a developer machine, if `CLAUDE_CODE_OAUTH_TOKEN` is set
in the environment (e.g. copied from server setup), it takes precedence
over the OAuth token stored by `claude login`, causing 401 errors even
after a successful login.  
**Fix:** `unset CLAUDE_CODE_OAUTH_TOKEN` (and remove it from `~/.zshrc` /
`~/.bashrc` if set there). The env var is only needed on the deploy server.

---

## Triggering Tests

Only PRs comments from repo **OWNER**, **MEMBER**, or **COLLABORATOR** trigger
the workflow. Comment `/test-deploy` on any open PR.

The workflow posts a reaction (👀) immediately to acknowledge the trigger,
runs the benchmarks, then posts the results table and a final reaction
(🚀 on success, 👎 on failure).

---

## Files

| File | Purpose |
|------|---------|
| `.github/workflows/test-deploy.yml` | GitHub Actions workflow |
| `.github/scripts/test_on_server.py` | Script that runs on the deploy server |
| `/tmp/test_deploy_results.json` | Results file written by the script (server) |
| `/tmp/test_deploy.lock` | fcntl lock file to serialize concurrent runs (server) |
| `/etc/environment` | PAM-loaded env vars for non-interactive SSH sessions (server) |