#!/usr/bin/env node /* 真机新会话实时进度复现(raw CDP 版):用户 18:45 实测「新会话卡住无进度」的同场景复现。 直开 /mobile/chat//new → 发多步任务 → 全程采样等待提示与实时步骤区。 */ import { spawnSync } from 'node:child_process'; import { readFileSync, mkdirSync } from 'node:fs'; import { resolve, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { connectWebView } from './android-webview-cdp.mjs'; const here = dirname(fileURLToPath(import.meta.url)); const outDir = resolve(here, '../test-results/newsession-20261202'); mkdirSync(outDir, { recursive: true }); const ADB = 'E:/coding-environment/AndroidSDK/platform-tools/adb.exe'; const SERIAL = '3e529a34'; const PORT = 9444; const AGENT = process.env.AGENT_ID || 'agent-node-app-9ffwfdi7k2'; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const adb = (...args) => (spawnSync(ADB, ['-s', SERIAL, ...args], { encoding: 'utf8', timeout: 30000, windowsHide: true }).stdout || '').trim(); /* 1. 启动 App + 转发 WebView */ adb('shell', 'am', 'start', '-n', 'cn.fmode.app/.MainActivity'); await sleep(6000); for (let i = 0; i < 20; i++) { const pid = adb('shell', 'pidof', 'cn.fmode.app').split(/\s+/)[0]; if (pid && adb('shell', 'cat', '/proc/net/unix').includes(`webview_devtools_remote_${pid}`)) { adb('forward', '--remove', `tcp:${PORT}`); adb('forward', `tcp:${PORT}`, `localabstract:webview_devtools_remote_${pid}`); break; } await sleep(1000); } const page = await connectWebView(PORT); console.log('已连入 WebView, URL =', await page.evaluate(() => location.href)); /* 2. 注入登录态,直开新会话路由(与 App 内「+」同一路径) */ const login = JSON.parse(readFileSync(resolve(here, '../.claude/dev-login.json'), 'utf8')); await page.evaluate((d) => { localStorage.setItem('fmode_session_token', d.token); localStorage.setItem('fmode_me', JSON.stringify(d.me)); }, login); await page.evaluate((a) => { location.href = `https://localhost/mobile/chat/${a}/new`; }, AGENT); await sleep(12000); console.log('新会话 URL =', await page.evaluate(() => location.href)); await page.screenshot(resolve(outDir, 'dn-00-blank.png')); const hasInput = await page.evaluate(() => !!document.querySelector('textarea')); if (!hasInput) { console.log('FAIL: 没有输入框(preparingRoom 卡住?)'); await page.screenshot(resolve(outDir, 'dn-99-noinput.png')); page.close(); process.exit(1); } await page.fill('textarea', '请用 terminal 依次执行 echo dns-step-1、sleep 20、echo dns-step-2、sleep 20、echo dns-step-3,每步之间都报告进度。'); await page.evaluate(() => document.querySelector('.m-send')?.click()); console.log('已发送多步任务(真机新会话首条消息)'); let sawWait = false, sawLive = false, maxSteps = 0, doneShot = false; for (let i = 0; i < 75; i++) { const probe = await page.evaluate(() => { const turns = [...document.querySelectorAll('app-assistant-turn')]; const last = turns[turns.length - 1]; const live = document.querySelector('app-live-activity'); return { wait: [...document.querySelectorAll('.card-wait')].map((e) => (e.textContent || '').trim().slice(0, 40)), live: live ? (live.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 60) : null, steps: document.querySelectorAll('app-live-activity .step, app-live-activity li, app-live-activity [class*=step]').length, lastTurn: last ? (last.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 60) : null, }; }); console.log(`[+${i * 2}s]`, JSON.stringify(probe)); if (probe.wait.length && !sawWait) { sawWait = true; await page.screenshot(resolve(outDir, 'dn-01-wait.png')); } if (probe.live && !sawLive) { sawLive = true; await page.screenshot(resolve(outDir, 'dn-02-live.png')); } if (probe.steps > maxSteps) maxSteps = probe.steps; if (probe.lastTurn && (probe.lastTurn.includes('已完成') || probe.lastTurn.includes('dns-step-3')) && !doneShot) { doneShot = true; await page.screenshot(resolve(outDir, 'dn-03-done.png')); break; } await sleep(2000); } console.log('=== 真机新会话复现结论 ==='); console.log('① 等待提示:', sawWait ? 'OK' : 'FAIL'); console.log('② 实时步骤区:', sawLive ? `OK(最多 ${maxSteps} 步)` : 'FAIL(复现了用户反馈)'); console.log('③ 回合完成:', doneShot ? 'OK' : '未等到结束'); page.close();