cdd0be328a
Critical runtime bugs:
- terminal.html WebSSH完全不可用(URL前缀/JSON解析/Content-Type三处错误)
- servers.py路由遮蔽:/logs被/{id}拦截,3个前端页面同步日志查询失败
- scripts.html startExecPoll()→startExecPolling(),长任务快速执行崩溃
- agent.py {value!r!s:.50}格式串非法,agent发非数值时ValueError
- alerts.html d.daily.reduce()无null检查,API返回空数据时TypeError
Resource leak / stability:
- websocket.py僵尸连接未关闭TCP,文件描述符泄漏
- websocket.py _last_alert_time字典无限增长(加1小时过期清理)
- asyncssh_pool.py全忙时超过MAX_CONNECTIONS无限增长
- self_monitor.py Telegram告警无冷却,宕机时每30秒刷屏
- schedule_runner.py一次性调度执行超60秒会重复触发
- 限速脚本EXPIRE每次重置窗口可绕过(改用Lua原子脚本)
Security:
- JWT access token加token_version声明,改密码后旧token立失效(零宽限)
- INSTALL_MODE导入时常量→动态函数,安装后JWT认证不再残留禁用
- install.py /lock端点加管理员存在性验证,防止阻断安装
- ServerUpdate schema移除connectivity只读字段,防止伪造连接状态
Frontend fixes:
- doExec()缺r.ok检查、commands.html null检查
- _server_to_dict()补last_checked_at+ssh_key_public
- _field_match()逗号cron表达式修复
- alerts类型显示、SSH会话名称、搜索高亮定位
- 一次性/循环定时任务(run_mode+fire_at+自动禁用)
Dead code removed (400+ lines):
- SyncService batch_push/_push_single等5个方法(零调用者)
- 5个未使用schema(SyncCommands/SyncConfig/SyncSftp/FileDeploy/PaginatedResponse)
- 6个零调用service方法、3个无前端API端点
- 4个未使用import
Schema migrations:
- push_schedules: run_mode + fire_at列,cron_expr改NULL
- servers: 7个新列 + ssh_key_private/public VARCHAR(500)→TEXT
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
159 lines
4.9 KiB
TypeScript
159 lines
4.9 KiB
TypeScript
// Complete implementation of condition-based waiting utilities
|
|
// From: Lace test infrastructure improvements (2025-10-03)
|
|
// Context: Fixed 15 flaky tests by replacing arbitrary timeouts
|
|
|
|
import type { ThreadManager } from '~/threads/thread-manager';
|
|
import type { LaceEvent, LaceEventType } from '~/threads/types';
|
|
|
|
/**
|
|
* Wait for a specific event type to appear in thread
|
|
*
|
|
* @param threadManager - The thread manager to query
|
|
* @param threadId - Thread to check for events
|
|
* @param eventType - Type of event to wait for
|
|
* @param timeoutMs - Maximum time to wait (default 5000ms)
|
|
* @returns Promise resolving to the first matching event
|
|
*
|
|
* Example:
|
|
* await waitForEvent(threadManager, agentThreadId, 'TOOL_RESULT');
|
|
*/
|
|
export function waitForEvent(
|
|
threadManager: ThreadManager,
|
|
threadId: string,
|
|
eventType: LaceEventType,
|
|
timeoutMs = 5000
|
|
): Promise<LaceEvent> {
|
|
return new Promise((resolve, reject) => {
|
|
const startTime = Date.now();
|
|
|
|
const check = () => {
|
|
const events = threadManager.getEvents(threadId);
|
|
const event = events.find((e) => e.type === eventType);
|
|
|
|
if (event) {
|
|
resolve(event);
|
|
} else if (Date.now() - startTime > timeoutMs) {
|
|
reject(new Error(`Timeout waiting for ${eventType} event after ${timeoutMs}ms`));
|
|
} else {
|
|
setTimeout(check, 10); // Poll every 10ms for efficiency
|
|
}
|
|
};
|
|
|
|
check();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Wait for a specific number of events of a given type
|
|
*
|
|
* @param threadManager - The thread manager to query
|
|
* @param threadId - Thread to check for events
|
|
* @param eventType - Type of event to wait for
|
|
* @param count - Number of events to wait for
|
|
* @param timeoutMs - Maximum time to wait (default 5000ms)
|
|
* @returns Promise resolving to all matching events once count is reached
|
|
*
|
|
* Example:
|
|
* // Wait for 2 AGENT_MESSAGE events (initial response + continuation)
|
|
* await waitForEventCount(threadManager, agentThreadId, 'AGENT_MESSAGE', 2);
|
|
*/
|
|
export function waitForEventCount(
|
|
threadManager: ThreadManager,
|
|
threadId: string,
|
|
eventType: LaceEventType,
|
|
count: number,
|
|
timeoutMs = 5000
|
|
): Promise<LaceEvent[]> {
|
|
return new Promise((resolve, reject) => {
|
|
const startTime = Date.now();
|
|
|
|
const check = () => {
|
|
const events = threadManager.getEvents(threadId);
|
|
const matchingEvents = events.filter((e) => e.type === eventType);
|
|
|
|
if (matchingEvents.length >= count) {
|
|
resolve(matchingEvents);
|
|
} else if (Date.now() - startTime > timeoutMs) {
|
|
reject(
|
|
new Error(
|
|
`Timeout waiting for ${count} ${eventType} events after ${timeoutMs}ms (got ${matchingEvents.length})`
|
|
)
|
|
);
|
|
} else {
|
|
setTimeout(check, 10);
|
|
}
|
|
};
|
|
|
|
check();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Wait for an event matching a custom predicate
|
|
* Useful when you need to check event data, not just type
|
|
*
|
|
* @param threadManager - The thread manager to query
|
|
* @param threadId - Thread to check for events
|
|
* @param predicate - Function that returns true when event matches
|
|
* @param description - Human-readable description for error messages
|
|
* @param timeoutMs - Maximum time to wait (default 5000ms)
|
|
* @returns Promise resolving to the first matching event
|
|
*
|
|
* Example:
|
|
* // Wait for TOOL_RESULT with specific ID
|
|
* await waitForEventMatch(
|
|
* threadManager,
|
|
* agentThreadId,
|
|
* (e) => e.type === 'TOOL_RESULT' && e.data.id === 'call_123',
|
|
* 'TOOL_RESULT with id=call_123'
|
|
* );
|
|
*/
|
|
export function waitForEventMatch(
|
|
threadManager: ThreadManager,
|
|
threadId: string,
|
|
predicate: (event: LaceEvent) => boolean,
|
|
description: string,
|
|
timeoutMs = 5000
|
|
): Promise<LaceEvent> {
|
|
return new Promise((resolve, reject) => {
|
|
const startTime = Date.now();
|
|
|
|
const check = () => {
|
|
const events = threadManager.getEvents(threadId);
|
|
const event = events.find(predicate);
|
|
|
|
if (event) {
|
|
resolve(event);
|
|
} else if (Date.now() - startTime > timeoutMs) {
|
|
reject(new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`));
|
|
} else {
|
|
setTimeout(check, 10);
|
|
}
|
|
};
|
|
|
|
check();
|
|
});
|
|
}
|
|
|
|
// Usage example from actual debugging session:
|
|
//
|
|
// BEFORE (flaky):
|
|
// ---------------
|
|
// const messagePromise = agent.sendMessage('Execute tools');
|
|
// await new Promise(r => setTimeout(r, 300)); // Hope tools start in 300ms
|
|
// agent.abort();
|
|
// await messagePromise;
|
|
// await new Promise(r => setTimeout(r, 50)); // Hope results arrive in 50ms
|
|
// expect(toolResults.length).toBe(2); // Fails randomly
|
|
//
|
|
// AFTER (reliable):
|
|
// ----------------
|
|
// const messagePromise = agent.sendMessage('Execute tools');
|
|
// await waitForEventCount(threadManager, threadId, 'TOOL_CALL', 2); // Wait for tools to start
|
|
// agent.abort();
|
|
// await messagePromise;
|
|
// await waitForEventCount(threadManager, threadId, 'TOOL_RESULT', 2); // Wait for results
|
|
// expect(toolResults.length).toBe(2); // Always succeeds
|
|
//
|
|
// Result: 60% pass rate → 100%, 40% faster execution
|