cruise 0.1.34

YAML-driven coding agent workflow orchestrator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { render, screen, waitFor, cleanup } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App from "../App";
import type { Session, WorkflowEvent } from "../types";
import * as commands from "../lib/commands";

// --- Module mocks --------------------------------------------------------------

vi.mock("@tauri-apps/api/app", () => ({
  getVersion: vi.fn().mockResolvedValue("0.0.0"),
}));

vi.mock("@tauri-apps/api/core", () => ({
  Channel: class {
    onmessage: ((event: unknown) => void) | null = null;
  },
}));

vi.mock("@tauri-apps/plugin-opener", () => ({
  openUrl: vi.fn(),
}));

vi.mock("@tauri-apps/plugin-dialog", () => ({
  open: vi.fn(),
}));

vi.mock("../lib/commands", () => ({
  listSessions: vi.fn(),
  listConfigs: vi.fn(),
  createSession: vi.fn(),
  approveSession: vi.fn(),
  discardSession: vi.fn(),
  getSession: vi.fn(),
  getSessionLog: vi.fn(),
  getSessionPlan: vi.fn(),
  getConfigSteps: vi.fn().mockResolvedValue([]),
  listDirectory: vi.fn(),
  getUpdateReadiness: vi.fn(),
  cleanSessions: vi.fn(),
  deleteSession: vi.fn(),
  runSession: vi.fn(),
  cancelSession: vi.fn(),
  resetSession: vi.fn(),
  respondToOption: vi.fn(),
  runAllSessions: vi.fn(),
  fixSession: vi.fn(),
  askSession: vi.fn(),
  getAppConfig: vi.fn(),
  updateAppConfig: vi.fn(),
}));

vi.mock("../lib/updater", () => ({
  checkForUpdate: vi.fn().mockResolvedValue(null),
  downloadAndInstall: vi.fn(),
}));

vi.mock("../lib/desktopNotifications", () => ({
  notifyDesktop: vi.fn(),
}));

// --- Helpers ------------------------------------------------------------------

function makeSession(overrides: Partial<Session> = {}): Session {
  return {
    id: "session-1",
    phase: "Planned",
    configSource: "default.yaml",
    baseDir: "/home/user/project",
    input: "test task",
    createdAt: "2026-01-01T00:00:00Z",
    workspaceMode: "Worktree",
    planAvailable: true,
    ...overrides,
  };
}

/**
 * Renders the App with the given sessions and navigates to the Run All view.
 * Returns the Channel instance so tests can simulate events.
 */
async function navigateToRunAll(
  sessions: Session[] = [makeSession()],
): Promise<{
  channel: { onmessage: ((event: WorkflowEvent) => void) | null };
  container: HTMLElement;
}> {
  vi.mocked(commands.listSessions).mockResolvedValue(sessions);
  const result = render(<App />);
  await waitFor(() => screen.getByRole("button", { name: /run all/i }));
  await userEvent.click(screen.getByRole("button", { name: /run all/i }));

  // Wait for RunAllView to call runAllSessions and capture the channel
  await waitFor(() => {
    expect(commands.runAllSessions).toHaveBeenCalledTimes(1);
  });
  const channel = vi.mocked(commands.runAllSessions).mock.calls[0][0] as {
    onmessage: ((event: WorkflowEvent) => void) | null;
  };
  return { channel, container: result.container };
}

/** Find the log <pre> element inside the Run All view. */
function getLogPre(container: HTMLElement): HTMLElement {
  const pre = container.querySelector("pre");
  if (!pre) throw new Error("No <pre> element found in Run All view");
  return pre;
}

// --- Tests --------------------------------------------------------------------

describe("Run All: live log display", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(commands.listConfigs).mockResolvedValue([]);
    vi.mocked(commands.getSessionLog).mockResolvedValue("");
    vi.mocked(commands.getSessionPlan).mockResolvedValue("");
    vi.mocked(commands.listDirectory).mockResolvedValue([]);
    vi.mocked(commands.getUpdateReadiness).mockResolvedValue({ canAutoUpdate: true });
    vi.mocked(commands.cleanSessions).mockResolvedValue({ deleted: 0, skipped: 0 });
    vi.mocked(commands.runAllSessions).mockResolvedValue();
    vi.mocked(commands.getAppConfig).mockResolvedValue({ runAllParallelism: 1 });
    vi.mocked(commands.updateAppConfig).mockResolvedValue();
  });

  afterEach(() => {
    cleanup();
  });

  // --- Single session happy path ----------------------------------------------

  it("displays log lines from stepStarted and workflowCompleted events", async () => {
    // Given: Run All is running
    const { channel, container } = await navigateToRunAll();

    // When: a full single-session event sequence flows
    // runAllStarted requires both `total` and `parallelism` (new field for concurrent batch display)
    channel.onmessage!({ event: "runAllStarted", data: { total: 1, parallelism: 1 } });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s1", input: "build feature", total: 1 },
    });
    // stepStarted now carries `sessionId` to attribute concurrent progress to the correct session
    channel.onmessage!({ event: "stepStarted", data: { sessionId: "s1", step: "Write code" } });
    // workflowCompleted now carries `sessionId` to identify which session completed
    channel.onmessage!({
      event: "workflowCompleted",
      data: { sessionId: "s1", run: 1, skipped: 0, failed: 0 },
    });
    channel.onmessage!({
      event: "runAllSessionFinished",
      data: { sessionId: "s1", input: "build feature", phase: "Completed" },
    });

    // Then: the log area contains the step and completion entries
    const logPre = getLogPre(container);
    await waitFor(() => {
      expect(logPre.textContent).toContain("Write code");
    });
    expect(logPre.textContent).toMatch(/Completed.*run: 1/);
    expect(logPre.textContent).toMatch(/skipped: 0/);
  });

  // --- Session boundary lines -------------------------------------------------

  it("shows a boundary line when each session starts", async () => {
    // Given: Run All with 2 sessions
    const { channel, container } = await navigateToRunAll([
      makeSession({ id: "s1", input: "first task" }),
      makeSession({ id: "s2", input: "second task" }),
    ]);

    // When: first session starts
    channel.onmessage!({ event: "runAllStarted", data: { total: 2, parallelism: 1 } });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s1", input: "first task", total: 1 },
    });

    // Then: log contains the first session boundary
    const logPre = getLogPre(container);
    await waitFor(() => {
      expect(logPre.textContent).toContain("first task");
    });
  });

  // --- Log accumulates across sessions ----------------------------------------

  it("accumulates log lines from multiple sessions without clearing", async () => {
    // Given: Run All with 2 sessions
    const { channel, container } = await navigateToRunAll([
      makeSession({ id: "s1", input: "task alpha" }),
      makeSession({ id: "s2", input: "task beta" }),
    ]);

    // When: first session runs to completion
    channel.onmessage!({ event: "runAllStarted", data: { total: 2, parallelism: 1 } });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s1", input: "task alpha", total: 1 },
    });
    channel.onmessage!({ event: "stepStarted", data: { sessionId: "s1", step: "Step A" } });
    channel.onmessage!({
      event: "workflowCompleted",
      data: { sessionId: "s1", run: 1, skipped: 0, failed: 0 },
    });
    channel.onmessage!({
      event: "runAllSessionFinished",
      data: { sessionId: "s1", input: "task alpha", phase: "Completed" },
    });

    // And: second session starts and runs
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s2", input: "task beta", total: 1 },
    });
    channel.onmessage!({ event: "stepStarted", data: { sessionId: "s2", step: "Step B" } });
    channel.onmessage!({
      event: "workflowCompleted",
      data: { sessionId: "s2", run: 1, skipped: 0, failed: 0 },
    });
    channel.onmessage!({
      event: "runAllSessionFinished",
      data: { sessionId: "s2", input: "task beta", phase: "Completed" },
    });
    channel.onmessage!({ event: "runAllCompleted", data: { cancelled: 0 } });

    // Then: both sessions' log lines are present
    const logPre = getLogPre(container);
    await waitFor(() => {
      expect(logPre.textContent).toContain("Step A");
      expect(logPre.textContent).toContain("Step B");
    });
    // The first session's content is not lost
    expect(logPre.textContent).toContain("task alpha");
    expect(logPre.textContent).toContain("task beta");
  });

  // --- workflowFailed ---------------------------------------------------------

  it("shows a failure log line on workflowFailed", async () => {
    // Given: Run All is running
    const { channel, container } = await navigateToRunAll();

    // When: session starts and then fails
    channel.onmessage!({ event: "runAllStarted", data: { total: 1, parallelism: 1 } });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s1", input: "do thing", total: 1 },
    });
    // workflowFailed now carries `sessionId` to route the failure to the correct session
    channel.onmessage!({
      event: "workflowFailed",
      data: { sessionId: "s1", error: "build error: missing dependency" },
    });
    channel.onmessage!({
      event: "runAllSessionFinished",
      data: { sessionId: "s1", input: "do thing", phase: "Failed", error: "build error: missing dependency" },
    });

    // Then: the failure message appears in the log
    const logPre = getLogPre(container);
    await waitFor(() => {
      expect(logPre.textContent).toContain("Failed");
      expect(logPre.textContent).toContain("build error: missing dependency");
    });
  });

  // --- workflowCancelled ------------------------------------------------------

  it("shows a cancellation log line on workflowCancelled", async () => {
    // Given: Run All is running
    const { channel, container } = await navigateToRunAll();

    // When: session starts and is cancelled
    channel.onmessage!({ event: "runAllStarted", data: { total: 1, parallelism: 1 } });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s1", input: "do thing", total: 1 },
    });
    // workflowCancelled now carries `data.sessionId` (no longer a unit variant)
    channel.onmessage!({ event: "workflowCancelled", data: { sessionId: "s1" } });
    channel.onmessage!({
      event: "runAllSessionFinished",
      data: { sessionId: "s1", input: "do thing", phase: "Suspended" },
    });
    channel.onmessage!({ event: "runAllCompleted", data: { cancelled: 1 } });

    // Then: the cancellation indicator appears in the log
    const logPre = getLogPre(container);
    await waitFor(() => {
      expect(logPre.textContent).toMatch(/Cancelled/);
    });
  });

  // --- No duplicate completion lines ------------------------------------------

  it("does not duplicate the completion line from workflowCompleted and runAllSessionFinished", async () => {
    // Given: Run All is running
    const { channel, container } = await navigateToRunAll();

    // When: both workflowCompleted and runAllSessionFinished fire
    channel.onmessage!({ event: "runAllStarted", data: { total: 1, parallelism: 1 } });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s1", input: "do thing", total: 1 },
    });
    channel.onmessage!({
      event: "workflowCompleted",
      data: { sessionId: "s1", run: 1, skipped: 0, failed: 0 },
    });
    channel.onmessage!({
      event: "runAllSessionFinished",
      data: { sessionId: "s1", input: "do thing", phase: "Completed" },
    });

    // Then: "v Completed" appears exactly once in the log
    const logPre = getLogPre(container);
    await waitFor(() => {
      expect(logPre.textContent).toMatch(/Completed/);
    });
    const completedCount = (logPre.textContent!.match(/Completed -- run:/g) ?? []).length;
    expect(completedCount).toBe(1);
  });

  // --- optionRequired preserves log -------------------------------------------

  it("preserves accumulated log lines when optionRequired fires", async () => {
    // Given: Run All is running
    const { channel, container } = await navigateToRunAll();

    // When: some steps run, then optionRequired fires
    channel.onmessage!({ event: "runAllStarted", data: { total: 1, parallelism: 1 } });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s1", input: "interactive task", total: 1 },
    });
    channel.onmessage!({ event: "stepStarted", data: { sessionId: "s1", step: "Analyze code" } });
    channel.onmessage!({
      event: "optionRequired",
      data: {
        requestId: "req-1",
        choices: [{ label: "Yes", kind: "selector", next: "step2" }],
        plan: "# Plan",
      },
    });

    // Then: the log still shows previous entries
    const logPre = getLogPre(container);
    await waitFor(() => {
      expect(logPre.textContent).toContain("Analyze code");
    });
    // And: the option dialog is visible
    expect(screen.getByText("Yes")).toBeInTheDocument();
  });

  // --- Batch start and end messages -------------------------------------------

  it("shows batch start message when runAllStarted fires", async () => {
    // Given: Run All is running
    const { channel, container } = await navigateToRunAll();

    // When: runAllStarted fires — now includes `parallelism` for display/debugging
    channel.onmessage!({ event: "runAllStarted", data: { total: 3, parallelism: 2 } });

    // Then: the log area is visible and contains a start indicator (includes total count)
    const logPre = getLogPre(container);
    await waitFor(() => {
      expect(logPre.textContent).toMatch(/3/);
    });
  });

  it("shows batch completion summary when runAllCompleted fires", async () => {
    // Given: Run All with 2 sessions
    const { channel, container } = await navigateToRunAll([
      makeSession({ id: "s1", input: "task 1" }),
      makeSession({ id: "s2", input: "task 2" }),
    ]);

    // When: both sessions complete and batch finishes
    channel.onmessage!({ event: "runAllStarted", data: { total: 2, parallelism: 1 } });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s1", input: "task 1", total: 1 },
    });
    channel.onmessage!({
      event: "workflowCompleted",
      data: { sessionId: "s1", run: 1, skipped: 0, failed: 0 },
    });
    channel.onmessage!({
      event: "runAllSessionFinished",
      data: { sessionId: "s1", input: "task 1", phase: "Completed" },
    });
    channel.onmessage!({
      event: "runAllSessionStarted",
      data: { sessionId: "s2", input: "task 2", total: 1 },
    });
    channel.onmessage!({
      event: "workflowCompleted",
      data: { sessionId: "s2", run: 1, skipped: 0, failed: 0 },
    });
    channel.onmessage!({
      event: "runAllSessionFinished",
      data: { sessionId: "s2", input: "task 2", phase: "Completed" },
    });
    channel.onmessage!({ event: "runAllCompleted", data: { cancelled: 0 } });

    // Then: the log shows a batch summary
    const logPre = getLogPre(container);
    await waitFor(() => {
      // The "Done" button appears indicating batch completed
      expect(screen.getByRole("button", { name: "Done" })).toBeInTheDocument();
    });
    // Log contains entries from both sessions
    expect(logPre.textContent).toContain("task 1");
    expect(logPre.textContent).toContain("task 2");
  });

  // --- Empty log state before events ------------------------------------------

  it("shows an empty log placeholder before any events arrive", async () => {
    // Given: Run All just started
    const { channel, container } = await navigateToRunAll();

    // Then: log area shows placeholder text before events arrive
    expect(channel.onmessage).not.toBeNull();
    expect(screen.getByRole("heading", { name: "Run All" })).toBeInTheDocument();
    const logPre = getLogPre(container);
    expect(logPre.textContent).toContain("Waiting for events...");
  });
});