Skip to content

Commit f4e5bf9

Browse files
committed
Generate CBM candidate pack for ContextBench
1 parent ba7f2f3 commit f4e5bf9

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: ContextBench CBM Candidates One
2+
3+
on:
4+
push:
5+
branches: [master]
6+
paths:
7+
- .github/workflows/contextbench-cbm-candidates-one.yml
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
cbm-candidates:
15+
runs-on: ubuntu-latest
16+
timeout-minutes: 20
17+
env:
18+
ROOT: /tmp/contextbench-cbm-candidates-one
19+
TASK_PAYLOADS: /tmp/contextbench-cbm-candidates-one/task-payloads.json
20+
CHECKOUT_ROOT: /tmp/contextbench-checkouts
21+
CBM_BIN: /tmp/contextbench-cbm-candidates-one/tool/codebase-memory-mcp
22+
steps:
23+
- uses: actions/checkout@v4
24+
- uses: pnpm/action-setup@v2
25+
with:
26+
version: 10
27+
- uses: actions/setup-node@v4
28+
with:
29+
node-version: '24'
30+
cache: pnpm
31+
- uses: actions/setup-python@v5
32+
with:
33+
python-version: '3.11'
34+
- name: Install and materialize Go task
35+
run: |
36+
set -euxo pipefail
37+
mkdir -p "$ROOT" "$CHECKOUT_ROOT" "$ROOT/tool"
38+
pnpm install --frozen-lockfile
39+
python -m pip install "tree-sitter==0.20.4" "tree-sitter-languages==1.10.2" datasets pyarrow
40+
curl -fsSL "https://github.com/DeusData/codebase-memory-mcp/releases/download/v0.6.1/codebase-memory-mcp-linux-amd64.tar.gz" -o "$ROOT/tool/cbm.tar.gz"
41+
tar -xzf "$ROOT/tool/cbm.tar.gz" -C "$ROOT/tool"
42+
chmod +x "$CBM_BIN" || true
43+
git clone --depth 1 https://github.com/EuniAI/ContextBench.git "$ROOT/ContextBench-official"
44+
node scripts/contextbench-runner.mjs --validate-fixtures
45+
node scripts/contextbench-select-slice.mjs --write-task-payloads --out "$TASK_PAYLOADS" --checkout-root "$CHECKOUT_ROOT"
46+
node scripts/contextbench-select-slice.mjs --materialize-checkouts --payloads "$TASK_PAYLOADS" --max-tasks 3
47+
- name: Print CBM candidates
48+
run: |
49+
cat > "$ROOT/cbm-pack.mjs" <<'NODE'
50+
import { spawnSync } from 'node:child_process';
51+
import { basename, relative } from 'node:path';
52+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
53+
const root = process.env.ROOT;
54+
const outDir = root + '/pack';
55+
mkdirSync(outDir, { recursive: true });
56+
const payloads = JSON.parse(readFileSync(process.env.TASK_PAYLOADS, 'utf8'));
57+
const task = payloads.tasks[2];
58+
const repo = task.repo_checkout_path;
59+
function run(cmd, args, opts = {}) { const started = Date.now(); const r = spawnSync(cmd, args, { cwd: opts.cwd || process.cwd(), env: opts.env || process.env, encoding: 'utf8', timeout: opts.timeoutMs || 600000, maxBuffer: 128 * 1024 * 1024 }); return { command: [cmd, ...args].join(' '), cwd: opts.cwd || process.cwd(), status: r.status, signal: r.signal, error: r.error?.message || null, durationMs: Date.now() - started, stdout: r.stdout || '', stderr: r.stderr || '' }; }
60+
function q(s) { return String(s || '').replace(/[`*_#>\[\](){},.;:!?/\\]/g, ' ').split(/\s+/).filter((w) => w.length >= 4).slice(0, 10).join(' '); }
61+
function jsonish(s) { const t = String(s || '').trim(); if (!t) return null; try { return JSON.parse(t); } catch {} for (const [a,b] of [['{','}'],['[',']']]) { const i=t.indexOf(a), j=t.lastIndexOf(b); if (i>=0 && j>i) { try { return JSON.parse(t.slice(i,j+1)); } catch {} } } return null; }
62+
function norm(file) { let f = String(file || '').replace(/^file:\/\//, ''); if (!f) return ''; if (f.startsWith(repo)) f = relative(repo, f); f = f.replaceAll('\\\\','/').replace(/^\/+/, '').replace(/^\.\//, ''); if (!f || f.startsWith('tmp/') || f.includes('://') || f.includes('..')) return ''; return f; }
63+
function add(locs, file, start = 1, end = start) { const clean = norm(file); if (!clean) return; const s = Math.max(1, Number(start) || 1); locs.push({ file: clean, start: s, end: Math.max(s, Number(end) || s), source: 'codebase-memory-mcp' }); }
64+
function walk(v, locs) { if (!v || typeof v !== 'object') return; if (Array.isArray(v)) { for (const x of v) walk(x, locs); return; } add(locs, v.file || v.path || v.file_path || v.relative_path || v.filename || v.source_path, v.start_line || v.line || 1, v.end_line || v.line || 1); for (const x of Object.values(v)) walk(x, locs); }
65+
function collect(text, locs) { const parsed = jsonish(text); if (parsed) walk(parsed, locs); const re = /([A-Za-z0-9_.\/-]+\.(?:go|mod|sum|json|yml|yaml|md|ts|tsx|js|jsx|py|rs|java|c|cc|cpp|h|hpp))(?::|#L|\s+line\s+)?(\d+)?/g; let m; while ((m = re.exec(String(text || ''))) !== null) add(locs, m[1], m[2] || 1, m[2] || 1); }
66+
function uniq(locs) { const seen = new Set(), out = []; for (const loc of locs) { const k = `${loc.file}:${loc.start}:${loc.end}`; if (!seen.has(k)) { seen.add(k); out.push(loc); if (out.length >= 80) break; } } return out; }
67+
const query = q(task.problem_statement);
68+
const env = { ...process.env, CBM_CACHE_DIR: outDir + '/cbm-cache', CBM_DIAGNOSTICS: '1' };
69+
const setup = run(process.env.CBM_BIN, ['--version'], { env, timeoutMs: 60000 });
70+
const index = run(process.env.CBM_BIN, ['cli', 'index_repository', JSON.stringify({ repo_path: repo })], { cwd: repo, env, timeoutMs: 2700000 });
71+
const project = (jsonish(index.stdout) || jsonish(index.stderr) || {}).project || basename(repo);
72+
const graph = run(process.env.CBM_BIN, ['cli', 'search_graph', JSON.stringify({ project, query, limit: 40 })], { cwd: repo, env, timeoutMs: 120000 });
73+
const code = run(process.env.CBM_BIN, ['cli', 'search_code', JSON.stringify({ project, pattern: query.split(/\s+/)[0] || '.', mode: 'compact', limit: 40 })], { cwd: repo, env, timeoutMs: 120000 });
74+
for (const [name, value] of Object.entries({ setup, index, graph, code })) writeFileSync(`${outDir}/${name}.json`, JSON.stringify(value, null, 2));
75+
const locs = []; for (const r of [graph, code]) { collect(r.stdout, locs); collect(r.stderr, locs); }
76+
const candidates = uniq(locs);
77+
const pack = { task: { instance_id: task.instance_id, repo: task.repo, base_commit: task.base_commit, problem_statement: task.problem_statement }, query, lane: { lane: 'codebase-memory-mcp', setupStatus: setup.status === 0 ? 'completed' : 'setup_failed', indexStatus: index.status === 0 ? 'completed' : 'index_failed', toolCallable: graph.status === 0 || code.status === 0, candidateCount: candidates.length, setupIndex: { setupDurationMs: setup.durationMs, indexDurationMs: index.durationMs, queryDurationMs: graph.durationMs + code.durationMs }, candidates } };
78+
writeFileSync(`${outDir}/cbm-candidate-pack.json`, JSON.stringify(pack, null, 2));
79+
console.log('CBM_CANDIDATE_PACK_JSON_START');
80+
console.log(JSON.stringify({ ...pack, lane: { ...pack.lane, candidates: candidates.slice(0, 60) } }, null, 2));
81+
console.log('CBM_CANDIDATE_PACK_JSON_END');
82+
NODE
83+
node "$ROOT/cbm-pack.mjs"
84+
- name: Upload CBM candidate pack
85+
if: always()
86+
uses: actions/upload-artifact@v4
87+
with:
88+
name: contextbench-cbm-candidates-one
89+
path: /tmp/contextbench-cbm-candidates-one/pack
90+
retention-days: 14

0 commit comments

Comments
 (0)