File size: 7,704 Bytes
c6856c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
EXP_ROOT="${ROOT}/experiments/ablation"
RUNNER="${ROOT}/experiments/lab_bench/scripts/run_labbench_with_hypobioos.py"
PYTHON_BIN="${BIOMANUS_PYTHON:-/225040511/miniconda3/envs/biomni_e1/bin/python}"
GRAPH_BUILDER="${ROOT}/build_generated_mcp_graph.py"
GRAPH_SANITIZER="${ROOT}/experiments/lab_bench/scripts/sanitize_labbench_mcp_graph.py"

BACKGROUND=0
VARIANT="all"
DEBUG=0
DEV_SIZE="${BIOMANUS_LABBENCH_DEV_SIZE:-45}"
TEST_SIZE="${BIOMANUS_LABBENCH_TEST_SIZE:-315}"
SHARD_COUNT="${BIOMANUS_LABBENCH_SHARD_COUNT:-10}"
SPLITS_TEXT="${BIOMANUS_LABBENCH_SPLITS:-test}"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --background) BACKGROUND=1; shift ;;
    --foreground) BACKGROUND=0; shift ;;
    --variant) VARIANT="$2"; shift 2 ;;
    --debug) DEBUG=1; shift ;;
    --dev-size) DEV_SIZE="$2"; shift 2 ;;
    --test-size) TEST_SIZE="$2"; shift 2 ;;
    --shard-count) SHARD_COUNT="$2"; shift 2 ;;
    --splits) SPLITS_TEXT="$2"; shift 2 ;;
    *) echo "Unknown argument: $1" >&2; exit 2 ;;
  esac
done

if [[ "${BACKGROUND}" -eq 1 ]]; then
  mkdir -p "${EXP_ROOT}/logs"
  LOG="${EXP_ROOT}/logs/labbench_ablation_$(date -u +%Y%m%d_%H%M%S).log"
  SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
  CMD=(
    "${SCRIPT}" "--foreground"
    "--variant" "${VARIANT}"
    "--dev-size" "${DEV_SIZE}"
    "--test-size" "${TEST_SIZE}"
    "--shard-count" "${SHARD_COUNT}"
    "--splits" "${SPLITS_TEXT}"
  )
  if [[ "${DEBUG}" -eq 1 ]]; then CMD+=("--debug"); fi
  nohup "${CMD[@]}" > "${LOG}" 2>&1 < /dev/null &
  echo "Started LAB-Bench ablation in background."
  echo "PID: $!"
  echo "Log: ${LOG}"
  exit 0
fi

if [[ -f "${ROOT}/.env" ]]; then
  # shellcheck disable=SC1090
  source "${ROOT}/.env"
fi

if [[ -n "${DEEPSEEK_API_KEY:-}" ]]; then
  export BIOMNI_SOURCE="${BIOMNI_SOURCE:-Custom}"
  export BIOMNI_LLM="${BIOMNI_LLM:-${DEEPSEEK_MODEL_NAME:-deepseek-chat}}"
  export BIOMNI_CUSTOM_BASE_URL="${BIOMNI_CUSTOM_BASE_URL:-${DEEPSEEK_BASE_URL:-https://api.deepseek.com/v1}}"
  export BIOMNI_CUSTOM_API_KEY="${BIOMNI_CUSTOM_API_KEY:-${DEEPSEEK_API_KEY}}"
fi
if [[ -n "${BIOMNI_CUSTOM_BASE_URL:-}" && ! "${BIOMNI_CUSTOM_BASE_URL}" =~ ^https?:// ]]; then
  export BIOMNI_CUSTOM_BASE_URL="${DEEPSEEK_BASE_URL:-https://api.deepseek.com/v1}"
fi
if [[ -z "${ANTHROPIC_API_KEY:-}" && -z "${OPENAI_API_KEY:-}" && -z "${BIOMNI_CUSTOM_API_KEY:-}" ]]; then
  echo "Missing LLM API key. Set DEEPSEEK_API_KEY, BIOMNI_CUSTOM_API_KEY, OPENAI_API_KEY, or ANTHROPIC_API_KEY." >&2
  exit 2
fi
mkdir -p "${EXP_ROOT}/logs"

if [[ "${SHARD_COUNT}" -lt 1 ]]; then
  echo "--shard-count must be at least 1" >&2
  exit 2
fi

read -r -a SPLIT_ARRAY <<< "${SPLITS_TEXT}"

TMP_GRAPH_DIR=""
cleanup() {
  [[ -n "${TMP_GRAPH_DIR}" && -d "${TMP_GRAPH_DIR}" ]] && rm -rf "${TMP_GRAPH_DIR}"
}
trap cleanup EXIT

prepare_clean_graph() {
  local full_graph_dir="${BIOMANUS_LABBENCH_FULL_GRAPH_DIR:-}"
  if [[ -z "${full_graph_dir}" ]]; then
    full_graph_dir="$(find "${ROOT}/graph_outputs" -maxdepth 1 -type d -name 'mcp_generated_graph_all_*' | sort | tail -n 1)"
  fi
  if [[ -z "${full_graph_dir}" ]]; then
    "${PYTHON_BIN}" "${GRAPH_BUILDER}" \
      --preset all \
      --output-root "${ROOT}/graph_outputs" \
      --python-cmd "${PYTHON_BIN}" > /dev/null 2>&1
    full_graph_dir="$(find "${ROOT}/graph_outputs" -maxdepth 1 -type d -name 'mcp_generated_graph_all_*' | sort | tail -n 1)"
  fi
  if [[ -z "${full_graph_dir}" || ! -d "${full_graph_dir}" ]]; then
    echo "Could not locate or build a full MCP graph." >&2
    exit 2
  fi

  TMP_GRAPH_DIR="$(mktemp -d /tmp/biomanus_ablation_labbench_clean_graph_XXXXXX)"
  "${PYTHON_BIN}" "${GRAPH_SANITIZER}" \
    --source-graph-dir "${full_graph_dir}" \
    --out-dir "${TMP_GRAPH_DIR}" > "${EXP_ROOT}/logs/last_labbench_graph_sanitize.json"
  echo "Using clean LAB-Bench MCP graph: ${TMP_GRAPH_DIR}"
  cat "${TMP_GRAPH_DIR}/labbench_sanitize_summary.json"
}

prepare_clean_graph

run_variant() {
  local key="$1"
  local label="$2"
  shift 2
  local out_root="${EXP_ROOT}/results/${key}/labbench"
  local agent_root="${EXP_ROOT}/agent_runtime/${key}"
  local shard_log_root="${EXP_ROOT}/results/${key}/labbench_shard_logs"
  mkdir -p "${out_root}" "${agent_root}" "${shard_log_root}"

  echo "=== LAB-Bench ${label} ==="
  for eval_name in DbQA SeqQA; do
    local compact="${EXP_ROOT}/results/${key}/labbench_${eval_name}.jsonl"
    local reasoning="${EXP_ROOT}/results/${key}/labbench_${eval_name}_reasoning.log"
    touch "${compact}" "${reasoning}"

    local -a pids=()
    local -a labels=()
    for (( shard_index=0; shard_index<SHARD_COUNT; shard_index++ )); do
      local run_label="${key}_${eval_name}_shard$(printf '%02d' "$((shard_index + 1))")of$(printf '%02d' "${SHARD_COUNT}")"
      local shard_log="${shard_log_root}/${run_label}.log"
      local args=(
        --evals "${eval_name}"
        --splits "${SPLIT_ARRAY[@]}"
        --dev-size "${DEV_SIZE}"
        --test-size "${TEST_SIZE}"
        --output-root "${out_root}"
        --agent-root "${agent_root}"
        --graph-dir "${TMP_GRAPH_DIR}"
        --run-label "${run_label}"
        --shard-index "${shard_index}"
        --shard-count "${SHARD_COUNT}"
        --compact-results-path "${compact}"
        --reasoning-log-path "${reasoning}"
        --compact-output-only
        --skip-existing-results
        --timeout-seconds "${BIOMANUS_LABBENCH_TIMEOUT_SECONDS:-600}"
        "$@"
      )
      if [[ "${DEBUG}" -eq 1 ]]; then args+=(--debug); fi
      echo "[launcher] launching ${run_label} log=${shard_log}" | tee -a "${reasoning}"
      "${PYTHON_BIN}" "${RUNNER}" "${args[@]}" > "${shard_log}" 2>&1 &
      pids+=("$!")
      labels+=("${run_label}")
    done

    local failed=0
    for idx in "${!pids[@]}"; do
      local pid="${pids[$idx]}"
      local run_label="${labels[$idx]}"
      if wait "${pid}"; then
        echo "[launcher] shard ${run_label} completed" | tee -a "${reasoning}"
      else
        local status=$?
        failed=1
        echo "[launcher] shard ${run_label} failed exit_code=${status} log=${shard_log_root}/${run_label}.log" | tee -a "${reasoning}"
      fi
    done
    if [[ "${failed}" -ne 0 ]]; then
      return 1
    fi
  done
}

case "${VARIANT}" in
  biomanus)
    run_variant "biomanus" "full" --use-graph-retriever --use-mcp --mcp-retrieval-mode graph
    ;;
  mcp_flat)
    run_variant "mcp_flat" "MCP + flat retrieval" --use-graph-retriever --use-mcp --mcp-retrieval-mode flat
    ;;
  mcp_metadata)
    run_variant "mcp_metadata" "MCP + metadata retrieval" --use-graph-retriever --use-mcp --mcp-retrieval-mode metadata
    ;;
  minus_graph)
    run_variant "minus_graph" "minus graph" --no-graph-retriever --use-mcp
    ;;
  minus_mcp)
    run_variant "minus_mcp" "minus MCP" --use-graph-retriever --no-mcp
    ;;
  minus_mcp_graph)
    run_variant "minus_mcp_graph" "minus MCP and graph" --no-graph-retriever --no-mcp
    ;;
  all)
    run_variant "biomanus" "full" --use-graph-retriever --use-mcp --mcp-retrieval-mode graph
    run_variant "mcp_flat" "MCP + flat retrieval" --use-graph-retriever --use-mcp --mcp-retrieval-mode flat
    run_variant "mcp_metadata" "MCP + metadata retrieval" --use-graph-retriever --use-mcp --mcp-retrieval-mode metadata
    run_variant "minus_graph" "minus graph" --no-graph-retriever --use-mcp
    run_variant "minus_mcp" "minus MCP" --use-graph-retriever --no-mcp
    run_variant "minus_mcp_graph" "minus MCP and graph" --no-graph-retriever --no-mcp
    ;;
  *)
    echo "Unknown variant: ${VARIANT}" >&2
    exit 2
    ;;
esac