product-projection-bash-progress.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { describe, expect, it } from "vitest";
  2. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  3. import {
  4. DELIVERY_PROFILES,
  5. conversationDeltaSchema,
  6. filterConversationDeltasForProfile,
  7. type ToolCallRow,
  8. } from "@zcode/shared/zcode-protocol-v4";
  9. import { ProductProjection } from "../src/zcode-protocol-v4/index.js";
  10. const preview = {
  11. text: "latest",
  12. fullText: "earlier\nlatest",
  13. totalBytes: 40960,
  14. totalLines: 500,
  15. linesEstimated: true,
  16. };
  17. function harness() {
  18. const projection = new ProductProjection("bash-progress", "epoch");
  19. let seq = 0;
  20. const apply = (type: SessionEvent["type"], payload: unknown, source?: unknown) =>
  21. projection.applyEvent({
  22. id: `event-${++seq}`,
  23. sessionId: "bash-progress",
  24. turnId: "turn-1",
  25. traceId: "trace",
  26. timestamp: new Date(1700000000000 + seq),
  27. sequenceNumber: seq,
  28. type,
  29. payload,
  30. ...(source ? { source } : {}),
  31. } as SessionEvent);
  32. apply(SessionEventType.SessionCreated, { mode: "default", contextWindow: 200000 });
  33. apply(SessionEventType.TurnStarted, { turnNumber: 1, input: "run", messageId: "user-1" });
  34. apply(SessionEventType.ToolCallScheduled, {
  35. toolCallId: "bash-1",
  36. toolName: "Bash",
  37. input: { command: "build" },
  38. schedule: { parallelGroups: [["bash-1"]], executionOrder: ["bash-1"] },
  39. });
  40. apply(SessionEventType.ToolCallStarted, { toolCallId: "bash-1", toolName: "Bash" });
  41. const row = () =>
  42. projection.getSnapshot().rows.window.find((r) => r.kind === "toolCall") as ToolCallRow;
  43. const progress = () =>
  44. apply(SessionEventType.ToolCallProgress, {
  45. toolCallId: "bash-1",
  46. toolName: "Bash",
  47. outputPreview: preview,
  48. });
  49. return { projection, apply, row, progress };
  50. }
  51. describe("Bash progress product projection", () => {
  52. it("publishes a bounded preview in whole rows for both delivery profiles", () => {
  53. const h = harness();
  54. const deltas = h.progress();
  55. expect(h.row().outputPreview).toEqual(preview);
  56. expect(
  57. deltas.some((delta) => delta.op === "row.upserted" && delta.row.kind === "toolCall"),
  58. ).toBe(true);
  59. for (const delta of deltas) expect(conversationDeltaSchema.parse(delta)).toEqual(delta);
  60. for (const profile of Object.values(DELIVERY_PROFILES)) {
  61. expect(filterConversationDeltasForProfile(deltas, profile)).toEqual(deltas);
  62. }
  63. expect(h.row().progress).toBeUndefined();
  64. });
  65. it.each([
  66. SessionEventType.ToolCallResult,
  67. SessionEventType.ToolCallError,
  68. SessionEventType.TurnComplete,
  69. ])("clears preview on %s and rejects late progress", (type) => {
  70. const h = harness();
  71. h.progress();
  72. h.apply(
  73. type,
  74. type === SessionEventType.ToolCallResult
  75. ? { toolCallId: "bash-1", result: { success: true, content: "done" }, duration: 1 }
  76. : {
  77. toolCallId: "bash-1",
  78. error: { type: "tool_cancelled", message: "stopped" },
  79. reason: "user",
  80. resultType: "cancelled",
  81. },
  82. );
  83. expect(h.row().outputPreview).toBeUndefined();
  84. expect(h.progress()).toEqual([]);
  85. expect(h.row().outputPreview).toBeUndefined();
  86. });
  87. it("clears the launch preview while the committed background work stays running", () => {
  88. const h = harness();
  89. h.progress();
  90. h.apply(SessionEventType.BackgroundTaskStarted, {
  91. taskId: "background-1",
  92. toolCallId: "bash-1",
  93. taskKind: "bash",
  94. toolName: "Bash",
  95. status: "running",
  96. cancellable: true,
  97. });
  98. h.apply(SessionEventType.ToolCallResult, {
  99. toolCallId: "bash-1",
  100. result: { success: true, content: "Command running in background. Output file: /output.log" },
  101. duration: 1,
  102. });
  103. expect(h.row().outputPreview).toBeUndefined();
  104. expect(h.progress()).toEqual([]);
  105. expect(h.projection.getSnapshot().backgroundWorks).toContainEqual(
  106. expect.objectContaining({ workId: "background-1", status: "running" }),
  107. );
  108. });
  109. it("ignores child tool mirrors in the parent while child topics use normal progress", () => {
  110. const parent = harness();
  111. expect(
  112. parent.apply(SessionEventType.ToolCallProgress, {
  113. toolCallId: "bash-1",
  114. source: "subagent",
  115. outputPreview: preview,
  116. }),
  117. ).toEqual([]);
  118. expect(parent.row().outputPreview).toBeUndefined();
  119. const child = harness();
  120. child.progress();
  121. expect(child.row().outputPreview).toEqual(preview);
  122. });
  123. it("does not create a row for unknown or invalid previews", () => {
  124. const h = harness();
  125. expect(
  126. h.apply(SessionEventType.ToolCallProgress, { toolCallId: "missing", outputPreview: preview }),
  127. ).toEqual([]);
  128. expect(
  129. h.apply(SessionEventType.ToolCallProgress, {
  130. toolCallId: "bash-1",
  131. outputPreview: { ...preview, fullText: "x".repeat(4097) },
  132. }),
  133. ).toEqual([]);
  134. expect(h.row().outputPreview).toBeUndefined();
  135. });
  136. });
  137. describe("Bash result display projection", () => {
  138. it("preserves structured output in both profiles and the final snapshot", () => {
  139. const h = harness();
  140. const display = {
  141. kind: "bash_output",
  142. output: "head",
  143. truncated: true,
  144. outputPath: "/tmp/full.output",
  145. };
  146. const deltas = h.apply(SessionEventType.ToolCallResult, {
  147. toolCallId: "bash-1",
  148. result: { success: true, content: "model preview", display },
  149. duration: 1,
  150. });
  151. expect(h.row().output?.display).toEqual(display);
  152. for (const profile of Object.values(DELIVERY_PROFILES)) {
  153. const filtered = filterConversationDeltasForProfile(deltas, profile);
  154. for (const delta of filtered) expect(conversationDeltaSchema.parse(delta)).toEqual(delta);
  155. }
  156. });
  157. });