tui-subagent-observation.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import assert from "node:assert/strict";
  2. import { test } from "node:test";
  3. import {
  4. EventReducer,
  5. InMemorySessionEventStore,
  6. SessionEventType,
  7. createSessionEvent,
  8. createSessionId,
  9. type SessionStorePort,
  10. type SessionInfo,
  11. type MessageWithParts,
  12. } from "@zcode/contracts";
  13. import type { AgentRuntime } from "@zcode/core";
  14. import { createSubagentObservation } from "../../bootstrap/src/app/subagent-observation.js";
  15. const parentId = createSessionId("parent");
  16. const childId = createSessionId("subagent_agent1");
  17. const baseSession = { time: { created: 1, updated: 2 }, taskType: "subagent_child" };
  18. async function observationFixture() {
  19. const events = new InMemorySessionEventStore();
  20. const parent = { ...baseSession, id: parentId } as SessionInfo;
  21. const child = { ...baseSession, id: childId, parentID: parentId } as SessionInfo;
  22. const parentMessages = [
  23. {
  24. info: { role: "assistant", id: "parent-msg", time: { created: 1 } },
  25. parts: [
  26. {
  27. type: "tool",
  28. tool: "Agent",
  29. callID: "launch",
  30. state: {
  31. status: "completed",
  32. input: { description: "Inspect auth", run_in_background: true },
  33. output: JSON.stringify({ agentId: "agent1", childSessionId: childId }),
  34. time: { start: 1, end: 2 },
  35. },
  36. },
  37. ],
  38. },
  39. ] as unknown as MessageWithParts[];
  40. const childMessages = [
  41. {
  42. info: { role: "assistant", id: "answer", time: { created: 3 } },
  43. parts: [{ id: "text", type: "text", text: "persisted answer" }],
  44. },
  45. ] as unknown as MessageWithParts[];
  46. const store = {
  47. getSession: async (id: string) =>
  48. id === parentId ? parent : id === childId ? child : undefined,
  49. messages: async ({ sessionID }: { sessionID: string }) =>
  50. sessionID === parentId ? parentMessages : childMessages,
  51. } as unknown as SessionStorePort;
  52. const runtime = {
  53. getSessionEventStore: () => events,
  54. getProjection: async () => new EventReducer().reduce(await events.getEvents(parentId)),
  55. } as unknown as AgentRuntime;
  56. const app = createSubagentObservation({ runtime, sessionId: parentId, sessionStore: store });
  57. return { app, events, child, childMessages, parentMessages };
  58. }
  59. test("directory uses child lifecycle, not the completed parent Agent launch ACK", async () => {
  60. const f = await observationFixture();
  61. await f.events.append(createSessionEvent(SessionEventType.TurnStarted, childId, {}));
  62. await f.events.append(
  63. createSessionEvent(SessionEventType.SubagentSpawned, parentId, {
  64. childSessionId: childId,
  65. agentId: "agent1",
  66. agentType: "Explore",
  67. parentToolCallId: "launch",
  68. }),
  69. );
  70. let directory = await f.app.readSubagents();
  71. assert.equal(directory.running.length, 1);
  72. assert.equal(directory.ended.total, 0);
  73. await f.events.append(createSessionEvent(SessionEventType.TurnComplete, parentId, {}));
  74. directory = await f.app.readSubagents();
  75. assert.equal(directory.running.length, 1);
  76. await f.events.append(createSessionEvent(SessionEventType.TurnComplete, childId, {}));
  77. directory = await f.app.readSubagents();
  78. assert.equal(directory.running.length, 0);
  79. assert.equal(directory.ended.items[0].status, "success");
  80. });
  81. test("transcript snapshot replaces retained streaming messages and preserves cold persisted output", async () => {
  82. const f = await observationFixture();
  83. let snapshot = await f.app.readSubagentTranscript(childId);
  84. assert.equal(snapshot.messages[0].content, "persisted answer");
  85. await f.events.append(
  86. createSessionEvent(SessionEventType.ModelStreaming, childId, {
  87. kind: "start",
  88. assistantMessageId: "answer",
  89. }),
  90. );
  91. await f.events.append(
  92. createSessionEvent(SessionEventType.ModelStreaming, childId, {
  93. kind: "text_delta",
  94. assistantMessageId: "answer",
  95. delta: "live answer",
  96. }),
  97. );
  98. snapshot = await f.app.readSubagentTranscript(childId);
  99. assert.equal(snapshot.messages[0].content, "");
  100. assert.deepEqual(snapshot.replayMessageIds, ["answer"]);
  101. assert.equal(snapshot.sequenceNumber, 2);
  102. await assert.rejects(() => f.app.readSubagentTranscript(parentId), /does not belong/);
  103. f.child.parentID = createSessionId("other");
  104. await assert.rejects(() => f.app.readSubagentTranscript(childId), /does not belong/);
  105. });