|
| 1 | +import * as assert from "assert" |
| 2 | + |
| 3 | +import { setDefaultSuiteTimeout } from "./test-utils" |
| 4 | +import { waitUntilCompleted, waitFor } from "./utils" |
| 5 | + |
| 6 | +// Regression test for the "Work #1 (no message)" title-clobber bug reported |
| 7 | +// against Zoo Code v3.76.0 (Discord, 2026-08-06). |
| 8 | +// |
| 9 | +// Root cause: Task#resumeTaskFromHistory() is started fire-and-forget by |
| 10 | +// scheduleTask() after createTaskWithHistoryItem() adds the task to the |
| 11 | +// registry, so `clineMessages` is [] until the first disk read resolves. |
| 12 | +// ClineProvider#evictCurrentTask() (called by clearCurrentTask / the |
| 13 | +// Back-to-parent / Go-to-subtask buttons) calls abortTask(), which calls |
| 14 | +// saveClineMessages() → taskMetadata() while the array is still empty. |
| 15 | +// taskMetadata() then persists the "no_messages" placeholder title, |
| 16 | +// permanently clobbering the real title in the history store. |
| 17 | +// |
| 18 | +// The test exercises the race by: |
| 19 | +// 1. Running a task to completion so a real title is persisted. |
| 20 | +// 2. Starting resumeTask() (same path as showTaskWithId) without awaiting it. |
| 21 | +// 3. Polling until the task appears on the stack, then immediately evicting — |
| 22 | +// the task is on the stack but its message load is still in flight. |
| 23 | +// 4. Asserting the stored title still matches the original. |
| 24 | +// |
| 25 | +// NOTE: Because the extension host reads task messages from disk in the same |
| 26 | +// process as this test, the I/O window is very tight (< 1ms on local disk). |
| 27 | +// The race is not reliably triggerable from the e2e layer; the canonical |
| 28 | +// regression anchor is the unit test in |
| 29 | +// src/core/task/__tests__/Task.resume-eviction-race.spec.ts, which controls |
| 30 | +// the timing via a deferred promise. This e2e test serves as a smoke test that |
| 31 | +// the resume-then-evict flow does not blow up and that the stored title is |
| 32 | +// correct after a round-trip. |
| 33 | +suite("Resume eviction race (title clobber regression)", function () { |
| 34 | + setDefaultSuiteTimeout(this) |
| 35 | + |
| 36 | + test("evicting a mid-resume task does not overwrite its stored title", async () => { |
| 37 | + const api = globalThis.api |
| 38 | + |
| 39 | + const ORIGINAL_TITLE = |
| 40 | + "RESUME_EVICTION_RACE_SMOKE: complete immediately with 'Resume eviction smoke completed.'" |
| 41 | + |
| 42 | + // Step 1 — run a task to completion so a real title is persisted. |
| 43 | + const taskId = await waitUntilCompleted({ |
| 44 | + api, |
| 45 | + start: () => |
| 46 | + api.startNewTask({ |
| 47 | + configuration: { |
| 48 | + mode: "ask", |
| 49 | + autoApprovalEnabled: true, |
| 50 | + enableCheckpoints: false, |
| 51 | + }, |
| 52 | + text: ORIGINAL_TITLE, |
| 53 | + }), |
| 54 | + }) |
| 55 | + |
| 56 | + const beforeResume = await api.getTaskHistoryItem(taskId) |
| 57 | + assert.ok(beforeResume, "Task should be in history after completion") |
| 58 | + assert.ok( |
| 59 | + beforeResume.task?.includes("RESUME_EVICTION_RACE_SMOKE"), |
| 60 | + `Persisted title before resume should contain the prompt marker (got "${beforeResume.task}")`, |
| 61 | + ) |
| 62 | + |
| 63 | + // Drain the stack so we start clean. |
| 64 | + while (api.getCurrentTaskStack().length > 0) { |
| 65 | + await api.clearCurrentTask() |
| 66 | + } |
| 67 | + |
| 68 | + // Step 2 — fire resumeTask() without awaiting it. resumeTask() calls |
| 69 | + // createTaskWithHistoryItem() which adds the task to the registry and |
| 70 | + // calls scheduleTask() (fire-and-forget). The task's run() and |
| 71 | + // resumeTaskFromHistory() start in the background. |
| 72 | + const resumePromise = api.resumeTask(taskId) |
| 73 | + |
| 74 | + // Step 3 — wait only until the task appears on the stack (i.e. |
| 75 | + // createTaskWithHistoryItem has returned and addClineToStack has run), |
| 76 | + // then immediately evict. This minimises the gap between the eviction |
| 77 | + // and the in-flight message load, giving the best chance of hitting the |
| 78 | + // race window before readTaskMessages() resolves. |
| 79 | + await waitFor(() => api.getCurrentTaskStack().includes(taskId)) |
| 80 | + await api.clearCurrentTask() |
| 81 | + |
| 82 | + // Let the resume settle. |
| 83 | + await resumePromise.catch(() => {}) |
| 84 | + |
| 85 | + // Step 4 — the stored title must still be the real one. |
| 86 | + const afterEviction = await api.getTaskHistoryItem(taskId) |
| 87 | + assert.ok(afterEviction, "Task should still be in history after eviction") |
| 88 | + |
| 89 | + // Before the fix this would be "Task #N (No messages)" / "工作 #N (無訊息)". |
| 90 | + assert.strictEqual( |
| 91 | + afterEviction.task, |
| 92 | + beforeResume.task, |
| 93 | + `Title must not change during resume eviction. Got: "${afterEviction.task}"`, |
| 94 | + ) |
| 95 | + }) |
| 96 | +}) |
0 commit comments