subagent-snapshot-wait.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { readFile, readdir } from "node:fs/promises";
  2. import { join } from "node:path";
  3. /** 观察现有 Host 日志,不查询/改写生产缓存,也不为模型额外制造父 turn。 */
  4. export async function subagentUpdateCursor() {
  5. const root = await browser.electron.execute(() => process.env.ZCODE_E2E_RUNTIME_LOG_DIR!);
  6. const read = async () =>
  7. (
  8. await Promise.all(
  9. (
  10. await readdir(root)
  11. )
  12. .filter((p) => p.endsWith(".log"))
  13. .sort()
  14. .map((p) => readFile(join(root, p), "utf8")),
  15. )
  16. ).join("\n");
  17. const before = await read();
  18. return async (hostPids?: number[], changedPaths: string[] = []) => {
  19. await browser.waitUntil(
  20. async () => {
  21. const after = await read();
  22. const lines = after
  23. .split("\n")
  24. .filter((line) => line.includes("Subagent 配置文件更新完成") && !before.includes(line));
  25. const matches = (pid?: number) =>
  26. changedPaths.length
  27. ? changedPaths.every((path) =>
  28. lines.some(
  29. // Host 的路径元数据经过 JSON 序列化;原始 Windows 反斜杠无法直接匹配日志。
  30. (line) =>
  31. (!pid || line.includes(`[pid:${pid}]`)) &&
  32. line.includes(JSON.stringify(path).slice(1, -1)),
  33. ),
  34. )
  35. : lines.some((line) => !pid || line.includes(`[pid:${pid}]`));
  36. return hostPids?.length ? hostPids.every(matches) : matches();
  37. },
  38. { timeout: 20000, timeoutMsg: "Host 未完成配置文件更新" },
  39. );
  40. };
  41. }