connect-remote-error-log.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { readdir, readFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { resolveE2EStorageRoot } from "#e2e/helpers/e2e-runtime-paths";
  4. const CONNECT_REMOTE_ERROR_PREFIX = "[connect-remote] caught error: ";
  5. export interface ConnectRemoteFailure {
  6. requestId?: string;
  7. message: string;
  8. code?: string;
  9. }
  10. /** 按时间顺序读取 Desktop main 日志里 connect-remote 失败的结构化错误。 */
  11. export async function readConnectRemoteFailures(): Promise<ConnectRemoteFailure[]> {
  12. // E2E reporter 把 desktop 日志重定向到 worker 专属目录;未接入 reporter 时落在隔离 HOME。
  13. const logDir =
  14. process.env.ZCODE_E2E_RUNTIME_LOG_DIR?.trim() || join(resolveE2EStorageRoot(), "v2", "logs");
  15. const failures: ConnectRemoteFailure[] = [];
  16. const names = await readdir(logDir).catch(() => []);
  17. for (const name of names.filter((entry) => entry.endsWith(".log")).sort()) {
  18. for (const line of (await readFile(join(logDir, name), "utf8")).split("\n")) {
  19. const index = line.indexOf(CONNECT_REMOTE_ERROR_PREFIX);
  20. if (index < 0) continue;
  21. try {
  22. const payload = JSON.parse(line.slice(index + CONNECT_REMOTE_ERROR_PREFIX.length)) as {
  23. requestId?: unknown;
  24. message?: unknown;
  25. code?: unknown;
  26. };
  27. if (typeof payload.message !== "string") continue;
  28. failures.push({
  29. message: payload.message,
  30. ...(typeof payload.requestId === "string" ? { requestId: payload.requestId } : {}),
  31. ...(typeof payload.code === "string" ? { code: payload.code } : {}),
  32. });
  33. } catch {
  34. // 日志行被截断时跳过,只认完整的结构化错误。
  35. }
  36. }
  37. }
  38. return failures;
  39. }
  40. /** 读取 Desktop main 日志里最近一次 connect-remote 失败的原始错误。 */
  41. export async function readLastConnectRemoteError(): Promise<string | null> {
  42. return (await readConnectRemoteFailures()).at(-1)?.message ?? null;
  43. }
  44. const REMOTE_WORKSPACE_UNRESPONSIVE_MESSAGE = "Remote workspace directory did not respond within";
  45. /**
  46. * 统计远程项目目录 admission 无响应的失败次数。新建连接走 connect-remote,已有连接上的重试走
  47. * Host 的 bind 路径(“failed to prepare bound remote workspace”),两者都要计入。
  48. */
  49. export async function countRemoteWorkspaceUnresponsiveFailures(): Promise<number> {
  50. const logDir =
  51. process.env.ZCODE_E2E_RUNTIME_LOG_DIR?.trim() || join(resolveE2EStorageRoot(), "v2", "logs");
  52. let count = 0;
  53. const names = await readdir(logDir).catch(() => []);
  54. for (const name of names.filter((entry) => entry.endsWith(".log")).sort()) {
  55. for (const line of (await readFile(join(logDir, name), "utf8")).split("\n")) {
  56. if (!line.includes(REMOTE_WORKSPACE_UNRESPONSIVE_MESSAGE)) continue;
  57. if (
  58. line.includes(CONNECT_REMOTE_ERROR_PREFIX) ||
  59. line.includes("failed to prepare bound remote workspace")
  60. )
  61. count += 1;
  62. }
  63. }
  64. return count;
  65. }