// R190: 251 langgraph 派发器(基于 R150/R173 修复版) const BASE = process.env.BASE || 'http://192.168.16.251:4096'; const DIR = process.env.DIR || '/sc-data/cfc/cfc-langgraph'; const LOG = process.env.LOG || '/tmp/r190_pytest.log'; const AUTH = 'Basic ' + Buffer.from('opencode:' + (process.env.OPENCODE_SERVER_PASSWORD || 'IwinTrue@123')).toString('base64'); const H = { Authorization: AUTH, 'Content-Type': 'application/json' }; const LOC = '?location%5Bdirectory%5D=' + encodeURIComponent(DIR); async function j(url, opt) { try { const r = await fetch(url, Object.assign({ headers: H }, opt || {})); const t = await r.text(); try { return { status: r.status, json: JSON.parse(t) }; } catch { return { status: r.status, text: t }; } } catch (e) { return { status: 0, err: e.message }; } } async function sh(cmd, capMs) { const cr = await j(BASE + '/api/shell' + LOC, { method: 'POST', body: JSON.stringify({ command: cmd, cwd: DIR }) }); const id = cr.json && cr.json.data && cr.json.data.id; if (!id) return { err: 'create ' + cr.status + ' ' + JSON.stringify(cr).slice(0, 200) }; const t0 = Date.now(); while (Date.now() - t0 < (capMs || 60000)) { await new Promise(r => setTimeout(r, 2500)); const s = await j(BASE + '/api/shell/' + id + LOC); const d = (s.json && s.json.data) || null; if (!d) continue; if (d.status && d.status !== 'running') { const o = await j(BASE + '/api/shell/' + id + '/output' + LOC + '&limit=200000'); return { code: d.status, out: (o.json && o.json.data && o.json.data.output) || '' }; } } return { err: 'timeout' }; } (async () => { const mode = process.argv[2] || 'kick'; if (LOG.includes(':') || LOG.includes('\\')) { console.error('BAD_LOG_PATH=' + LOG + ' 请使用远端 POSIX 路径(如 /tmp/rX_pytest.log),不要经由本地 env 注入'); process.exit(2); } if (mode === 'kick') { // 先清理残留 pytest 进程(B-177-1 经验),再启动 const clean = await sh("ps -eo pid,etime,cmd | grep -E 'pytest|local_report' | grep -v grep | awk '{print $1}' | xargs -r kill -9 2>/dev/null; echo CLEANED", 30000); const h = await sh('cd ' + DIR + ' && git rev-parse --short HEAD; date', 30000); const inner = "cd " + DIR + " && .venv/bin/python -m pytest -q --ignore=tests/qna --ignore=tests/test_chat.py > " + LOG + " 2>&1; rc=$?; echo EXIT=$rc >> " + LOG; const cmd = "rm -f " + LOG + "; setsid nohup bash -c '" + inner + "' >/dev/null 2>&1 & echo KICKED"; const r = await sh(cmd, 40000); console.log('CLEAN=' + JSON.stringify(clean).slice(0, 200)); console.log('KICK=' + JSON.stringify(r).slice(0, 400)); console.log('NODE_HEAD=' + ((h.out || h.err || '').trim())); } else if (mode === 'read') { const r = await sh('tail -c 4000 ' + LOG, 45000); console.log(r.err ? 'READ_ERR=' + r.err : r.out); } else if (mode === 'run') { const r = await sh(process.argv[3], 120000); console.log(r.err ? 'ERR=' + r.err : r.out); } })();