Skip to content

Commit 1dfa595

Browse files
committed
test(task): cover request continuation and update lifecycle doc
1 parent a52e4fc commit 1dfa595

2 files changed

Lines changed: 57 additions & 23 deletions

File tree

‎docs/architecture/task-lifecycle-target-architecture.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The bugs come from the runtime layer and from state that escapes the checked red
6161

6262
### Run state is a set of loose booleans
6363

64-
`Task` models mutually exclusive run phases with independent flags. Examples: `abort`, `didFinishAbortingStream`, `isInitialized`, `isPaused`, `isWaitingForFirstChunk`, `isStreaming`, `didRejectTool`, `didAlreadyUseTool`, `didToolFailInCurrentTurn`, `didCompleteReadingStream`. Dispatch re-entrancy uses `presentAssistantMessageLocked` and `presentAssistantMessageHasPendingUpdates`.
64+
`Task` models mutually exclusive run phases with independent flags. Examples: `abort`, `didFinishAbortingStream`, `isInitialized`, `isWaitingForFirstChunk`, `isStreaming`, `didRejectTool`, `didAlreadyUseTool`, `didToolFailInCurrentTurn`, `didCompleteReadingStream`. Dispatch re-entrancy uses `presentAssistantMessageLocked` and `presentAssistantMessageHasPendingUpdates`.
6565

6666
N independent booleans admit 2^N representable states. Only a few are legal. The type system cannot reject the rest. Some combinations are legal phases. For example, `abort` and `isStreaming` are both true during the graceful drain window (`Task.ts:2685, 3484, 3756`). The type system cannot tell a legal combination from an illegal one. The one flag-combination defect with a source is the duplicated render state in #325.
6767

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -444,30 +444,30 @@ describe("Cline", () => {
444444
baseProviderState = await mockProvider.getState()
445445
})
446446

447-
describe("empty-response retries", () => {
448-
function stream(chunks: ApiStreamChunk[]): AsyncGenerator<ApiStreamChunk> {
449-
return (async function* () {
450-
yield* chunks
451-
})()
452-
}
447+
function stream(chunks: ApiStreamChunk[]): AsyncGenerator<ApiStreamChunk> {
448+
return (async function* () {
449+
yield* chunks
450+
})()
451+
}
453452

454-
async function createTaskWithManualRetries() {
455-
const task = new Task({
456-
provider: mockProvider,
457-
apiConfiguration: mockApiConfig,
458-
task: "test task",
459-
startTask: false,
460-
})
461-
const state = await mockProvider.getState()
462-
vi.spyOn(mockProvider, "getState").mockResolvedValue({
463-
...state,
464-
apiConfiguration: mockApiConfig,
465-
autoApprovalEnabled: false,
466-
})
467-
vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined)
468-
return task
469-
}
453+
async function createTaskWithManualRetries() {
454+
const task = new Task({
455+
provider: mockProvider,
456+
apiConfiguration: mockApiConfig,
457+
task: "test task",
458+
startTask: false,
459+
})
460+
const state = await mockProvider.getState()
461+
vi.spyOn(mockProvider, "getState").mockResolvedValue({
462+
...state,
463+
apiConfiguration: mockApiConfig,
464+
autoApprovalEnabled: false,
465+
})
466+
vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined)
467+
return task
468+
}
470469

470+
describe("empty-response retries", () => {
471471
it("restores the user message before a confirmed empty-response retry", async () => {
472472
const task = await createTaskWithManualRetries()
473473
let retryHistory: ApiMessage[] | undefined
@@ -521,6 +521,40 @@ describe("Cline", () => {
521521
})
522522
})
523523

524+
describe("request continuation", () => {
525+
it("sends the no-tool-use reminder in a second request when the first response has no tool use", async () => {
526+
const task = await createTaskWithManualRetries()
527+
let secondRequestHistory: ApiMessage[] | undefined
528+
529+
const attemptApiRequestSpy = vi
530+
.spyOn(task, "attemptApiRequest")
531+
.mockImplementationOnce(() => stream([{ type: "text", text: "no tool here" }]))
532+
.mockImplementationOnce(() => {
533+
secondRequestHistory = structuredClone(task.apiConversationHistory)
534+
throw new Error("stop after continuation request")
535+
})
536+
537+
await task.recursivelyMakeClineRequests([{ type: "text", text: "original user request" }])
538+
539+
expect(attemptApiRequestSpy).toHaveBeenCalledTimes(2)
540+
expect(secondRequestHistory).toMatchObject([
541+
{
542+
role: "user",
543+
content: expect.arrayContaining([expect.objectContaining({ text: "original user request" })]),
544+
},
545+
{ role: "assistant", content: [{ type: "text", text: "no tool here" }] },
546+
{
547+
role: "user",
548+
content: expect.arrayContaining([
549+
expect.objectContaining({
550+
text: expect.stringContaining("You did not use a tool in your previous response"),
551+
}),
552+
]),
553+
},
554+
])
555+
})
556+
})
557+
524558
describe("output token limit mid-stream", () => {
525559
async function* truncatedStream(): AsyncGenerator<ApiStreamChunk> {
526560
yield { type: "text", text: "partial answer" }

0 commit comments

Comments
 (0)