taskActivityTracker.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Emitter } from "@zcode/rpc";
  2. import type {
  3. IZCodeAgentService,
  4. ZCodeAgentRuntimeLifecycleEvent,
  5. ZCodeAgentWorkspaceTarget,
  6. } from "@zcode/services";
  7. import type { ConversationTelemetryFact } from "@zcode/shared/zcode-protocol-v4";
  8. import { describe, expect, it } from "vitest";
  9. import { createTaskActivityTracker } from "../src/server-core/taskActivityTracker.js";
  10. function fact(
  11. kind: "turn.started" | "turn.terminal",
  12. sessionId: string,
  13. ): ConversationTelemetryFact {
  14. const base = {
  15. version: 1 as const,
  16. eventId: `${kind}-${sessionId}`,
  17. eventSeq: 1,
  18. occurredAt: Date.now(),
  19. sessionId,
  20. };
  21. return kind === "turn.started"
  22. ? { ...base, kind }
  23. : { ...base, kind, status: "success" as const };
  24. }
  25. describe("Core-local TaskActivityTracker", () => {
  26. it("counts authoritative turn facts for all workspace clients and deduplicates by workspaceKey + sessionId", () => {
  27. const lifecycle = new Emitter<ZCodeAgentRuntimeLifecycleEvent>();
  28. const telemetryByWorkspace = new Map<string, Emitter<ConversationTelemetryFact>>();
  29. const workspaceKey = (target: ZCodeAgentWorkspaceTarget): string =>
  30. target.workspaceIdentity?.trim() || target.workspacePath;
  31. const agentService = {
  32. onAgentRuntimeLifecycle: lifecycle.event,
  33. onDynamicConversationTelemetryFact(target: ZCodeAgentWorkspaceTarget) {
  34. const key = workspaceKey(target);
  35. let emitter = telemetryByWorkspace.get(key);
  36. if (!emitter) {
  37. emitter = new Emitter<ConversationTelemetryFact>();
  38. telemetryByWorkspace.set(key, emitter);
  39. }
  40. return emitter.event;
  41. },
  42. } as Pick<IZCodeAgentService, "onAgentRuntimeLifecycle" | "onDynamicConversationTelemetryFact">;
  43. const tracker = createTaskActivityTracker(agentService);
  44. const targetA = { workspacePath: "/same/path", workspaceIdentity: "ssh://host-a/repo" };
  45. const targetB = { workspacePath: "/same/path", workspaceIdentity: "ssh://host-b/repo" };
  46. lifecycle.fire({
  47. ...targetA,
  48. workspaceKey: targetA.workspaceIdentity,
  49. runtimeIdentity: { generation: 1, identity: "runtime-a", workspaceKey: targetA.workspaceIdentity },
  50. state: "available",
  51. });
  52. lifecycle.fire({
  53. ...targetB,
  54. workspaceKey: targetB.workspaceIdentity,
  55. runtimeIdentity: { generation: 1, identity: "runtime-b", workspaceKey: targetB.workspaceIdentity },
  56. state: "available",
  57. });
  58. telemetryByWorkspace.get(targetA.workspaceIdentity)?.fire(fact("turn.started", "session-1"));
  59. telemetryByWorkspace.get(targetA.workspaceIdentity)?.fire(fact("turn.started", "session-1"));
  60. telemetryByWorkspace.get(targetB.workspaceIdentity)?.fire(fact("turn.started", "session-1"));
  61. expect(tracker.readRunningTaskCount()).toBe(2);
  62. telemetryByWorkspace.get(targetA.workspaceIdentity)?.fire(fact("turn.terminal", "session-1"));
  63. expect(tracker.readRunningTaskCount()).toBe(1);
  64. lifecycle.fire({
  65. ...targetB,
  66. workspaceKey: targetB.workspaceIdentity,
  67. runtimeIdentity: { generation: 1, identity: "runtime-b", workspaceKey: targetB.workspaceIdentity },
  68. state: "unavailable",
  69. });
  70. expect(tracker.readRunningTaskCount()).toBe(0);
  71. tracker.dispose();
  72. });
  73. it("publishes count changes immediately for Supervisor task-activity snapshots", () => {
  74. const lifecycle = new Emitter<ZCodeAgentRuntimeLifecycleEvent>();
  75. const telemetry = new Emitter<ConversationTelemetryFact>();
  76. const agentService = {
  77. onAgentRuntimeLifecycle: lifecycle.event,
  78. onDynamicConversationTelemetryFact: () => telemetry.event,
  79. } as Pick<IZCodeAgentService, "onAgentRuntimeLifecycle" | "onDynamicConversationTelemetryFact">;
  80. const tracker = createTaskActivityTracker(agentService);
  81. const counts: number[] = [];
  82. const subscription = tracker.onDidChangeRunningTaskCount((count) => counts.push(count));
  83. lifecycle.fire({
  84. workspacePath: "/repo",
  85. workspaceKey: "/repo",
  86. runtimeIdentity: { generation: 1, identity: "runtime", workspaceKey: "/repo" },
  87. state: "available",
  88. });
  89. telemetry.fire(fact("turn.started", "session"));
  90. telemetry.fire(fact("turn.terminal", "session"));
  91. expect(counts).toEqual([1, 0]);
  92. subscription.dispose();
  93. tracker.dispose();
  94. });
  95. });