Skip to content

Commit ddb1e1d

Browse files
2234839claude
andcommitted
debug: add key-path logging to middleware and ws-client
Add console.log at critical paths to diagnose timeout issues: - dispatchCode: log instance, code preview, SSE connection count - broadcastCode: log connection count - POST /result: log instance and waiter count - Client handleCode: log receive, exec start/end, snapshot, send - Client postResult: log send, response status, retry - Client SSE: log code/reload events Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a93e737 commit ddb1e1d

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-pilot",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "AI Agent 驾驶浏览器的导航工具 — 打通 浏览器运行时 → Dev Server → 源码 → IDE 的完整链路",
55
"type": "module",
66
"bin": {

src/client/ws-client.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,15 @@ export const wsClientCode = `
176176
177177
/** 通过 HTTP POST 发送执行结果(失败自动重试一次,确保 CLI 不超时) */
178178
function postResult(result) {
179+
console.log('[Pilot] postResult sending, success=' + result.success + ', instance=' + __pilot_instanceId);
179180
fetch(apiUrl('/__pilot/result'), {
180181
method: 'POST',
181182
headers: { 'Content-Type': 'application/json', 'X-Pilot-Instance': __pilot_instanceId, 'X-Pilot-Title': document.title || '' },
182183
body: JSON.stringify(result)
183-
}).catch(function() {
184+
}).then(function(resp) {
185+
console.log('[Pilot] postResult response status=' + resp.status);
186+
}).catch(function(err) {
187+
console.log('[Pilot] postResult failed, retrying in 200ms: ' + err.message);
184188
/** 后台 tab 或网络抖动导致首次失败时,200ms 后重试一次 */
185189
setTimeout(function() {
186190
fetch(apiUrl('/__pilot/result'), {
@@ -204,21 +208,27 @@ export const wsClientCode = `
204208
205209
/** 执行代码并发送结果的通用处理 */
206210
function handleCode(code) {
211+
console.log('[Pilot] handleCode received, isExecuting=' + isExecuting + ', code=' + code.slice(0, 40));
207212
if (isExecuting) {
208213
pendingCode = code;
214+
console.log('[Pilot] handleCode queued (already executing)');
209215
return;
210216
}
211217
isExecuting = true;
218+
console.log('[Pilot] execCode starting...');
212219
var result = execCode(code);
220+
console.log('[Pilot] execCode done, typeof result=' + typeof result + ', isPromise=' + (result && typeof result.then === 'function'));
213221
/** 等待 Vue nextTick + 浏览器渲染后再采集 snapshot,确保 DOM 已更新
214222
* 后台 tab 时 requestAnimationFrame 不触发,直接发送结果 */
215223
function sendWithSnapshot(result) {
216224
/** 安全采集 snapshot,出错时不阻塞结果发送 */
217225
function safeSnapshot() {
218226
try { return window.__pilot_snapshot ? window.__pilot_snapshot() : undefined; } catch(e) { return undefined; }
219227
}
228+
console.log('[Pilot] sendWithSnapshot, hidden=' + document.hidden + ', hasSnapshot=' + !!window.__pilot_snapshot);
220229
if (window.__pilot_snapshot && !document.hidden) {
221230
requestAnimationFrame(function() {
231+
console.log('[Pilot] rAF callback, sending result');
222232
result.snapshot = safeSnapshot();
223233
sendResult(result);
224234
/** exec 完成,检查是否有排队的代码需要执行 */
@@ -228,12 +238,14 @@ export const wsClientCode = `
228238
} else if (window.__pilot_snapshot) {
229239
/** 后台 tab 时 rAF 不触发,用 setTimeout 兜底确保 snapshot 采集 */
230240
setTimeout(function() {
241+
console.log('[Pilot] setTimeout callback (background tab), sending result');
231242
result.snapshot = safeSnapshot();
232243
sendResult(result);
233244
isExecuting = false;
234245
if (pendingCode) { var next = pendingCode; pendingCode = null; handleCode(next); }
235246
}, 50);
236247
} else {
248+
console.log('[Pilot] no snapshot function, sending result directly');
237249
sendResult(result);
238250
isExecuting = false;
239251
if (pendingCode) { var next = pendingCode; pendingCode = null; handleCode(next); }
@@ -256,11 +268,13 @@ export const wsClientCode = `
256268
window.__pilot_es = es;
257269
258270
es.addEventListener('code', function(e) {
271+
console.log('[Pilot] SSE code event received, gen=' + myGen + ', data=' + e.data.slice(0, 40));
259272
if (myGen !== window.__pilot_gen) return;
260273
handleCode(e.data);
261274
});
262275
263276
es.addEventListener('reload', function() {
277+
console.log('[Pilot] SSE reload event received, bridge_active=' + !!window.__pilot_bridge_active);
264278
es.close();
265279
/** Console Bridge 模式下不 reload(控制台注入的代码会丢失) */
266280
if (!window.__pilot_bridge_active) {

src/server/middleware.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function createMiddleware(options: ResolvedPilotOptions, pilotVersion?: s
4545
/** 向指定实例的所有 SSE 连接广播代码 */
4646
function broadcastCode(instanceId: string, code: string): void {
4747
const connections = sseConnections[instanceId]
48+
console.log(`[Pilot] broadcastCode instance=${instanceId} conns=${connections?.length ?? 0}`)
4849
if (!connections || connections.length === 0) return
4950
for (const res of connections) {
5051
res.write(`event: code\ndata: ${code}\n\n`)
@@ -53,6 +54,8 @@ export function createMiddleware(options: ResolvedPilotOptions, pilotVersion?: s
5354

5455
/** 通过 SSE 广播代码给浏览器(同时写 pending.js 供轮询 fallback 使用) */
5556
function dispatchCode(instanceId: string, code: string): void {
57+
const preview = code.slice(0, 40).replace(/\n/g, ' ')
58+
console.log(`[Pilot] dispatchCode instance=${instanceId} code="${preview}" sseConns=${sseConnections[instanceId]?.length ?? 0}`)
5659
bridge.clearExecResult(instanceId)
5760
bridge.clearExecDone(instanceId)
5861
lastBrowserActivity[instanceId] = Date.now()
@@ -290,6 +293,7 @@ export function createMiddleware(options: ResolvedPilotOptions, pilotVersion?: s
290293

291294
/** ---------- POST /__pilot/result ---------- */
292295
if (endpoint === PILOT_ENDPOINTS.result && req.method === 'POST') {
296+
console.log(`[Pilot] POST /result instance=${instanceId} waiters=${execWaiters[instanceId]?.length ?? 0}`)
293297
lastBrowserActivity[instanceId] = Date.now()
294298
/** POST /result 时同步更新 title(SPA 场景下 title 可能动态变化) */
295299
const clientTitle = (req.headers['x-pilot-title'] as string) || ''

0 commit comments

Comments
 (0)