Repository navigation
feat(task): add runState field, write helpers, and projection (RSK-04) #1985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // npx vitest run core/task/__tests__/Task.run-shadow.spec.ts | ||
|
|
||
| import { providerIdentifiers } from "@roo-code/types/provider-identifiers" | ||
| import { TelemetryService } from "@roo-code/telemetry" | ||
|
|
||
| import { makeExtensionContext, makeUri } from "../../../test-utils/vscode" | ||
| import { Task } from "../Task" | ||
| import { ClineProvider } from "../../webview/ClineProvider" | ||
| import { initialRunState } from "../run-state/runState" | ||
|
|
||
| vi.mock("../../../i18n", () => ({ t: (key: string) => key })) | ||
| vi.mock("../../ignore/RooIgnoreController") | ||
|
|
||
| function makeTask(): Task { | ||
| const provider = { | ||
| log: vi.fn(), | ||
| context: makeExtensionContext({ globalStorageUri: makeUri("/mock/global-storage") }), | ||
| } | ||
| // Double cast: the constructor reads only these provider members. | ||
| return new Task({ | ||
| provider: provider as unknown as ClineProvider, | ||
| apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "test-api-key" }, | ||
| task: "test task", | ||
| startTask: false, | ||
| }) | ||
| } | ||
|
|
||
| describe("Task run-state write helpers", () => { | ||
| let task: Task | ||
|
|
||
| beforeEach(() => { | ||
| if (!TelemetryService.hasInstance()) { | ||
| TelemetryService.createInstance([]) | ||
| } | ||
| task = makeTask() | ||
| }) | ||
|
|
||
| it("starts with the initial run state and no rejections", () => { | ||
| expect(task.runState).toEqual(initialRunState) | ||
| expect(task.runRejections).toEqual([]) | ||
| }) | ||
|
|
||
| it("sets the field and drives the event for each stream and phase helper", () => { | ||
| task.markInitialized() | ||
| task.markStreamStarted() | ||
| task.markStreamCleanupFinished() | ||
|
|
||
| expect(task.isInitialized).toBe(true) | ||
| expect(task.isStreaming).toBe(true) | ||
| expect(task.didFinishAbortingStream).toBe(true) | ||
| expect(task.runState.phase).toBe("running") | ||
| expect(task.runState.stream).toEqual({ tag: "live", generation: 1, cleanupFinished: true }) | ||
|
|
||
| task.markStreamEnded() | ||
| expect(task.isStreaming).toBe(false) | ||
| expect(task.runState.stream).toEqual({ tag: "none" }) | ||
| expect(task.runRejections).toEqual([]) | ||
| }) | ||
|
|
||
| it("keeps a stale didFinishAbortingStream on the next stream so the kernel mismatch shows race R-1", () => { | ||
| task.markInitialized() | ||
| task.markStreamStarted() | ||
| task.markStreamCleanupFinished() | ||
| task.markStreamEnded() | ||
| task.markStreamStarted() | ||
|
|
||
| expect(task.didFinishAbortingStream).toBe(true) | ||
| expect(task.runState.stream).toEqual({ tag: "live", generation: 2, cleanupFinished: false }) | ||
| }) | ||
|
|
||
| it("requestAbort(false) sets abort only", () => { | ||
| task.requestAbort(false) | ||
|
|
||
| expect([task.abort, task.abandoned]).toEqual([true, false]) | ||
| expect(task.runState.latches).toMatchObject({ abort: true, abandoned: false }) | ||
| }) | ||
|
|
||
| it("requestAbort(true) sets abort and abandoned", () => { | ||
| task.requestAbort(true) | ||
|
|
||
| expect([task.abort, task.abandoned]).toEqual([true, true]) | ||
| expect(task.runState.latches).toMatchObject({ abort: true, abandoned: true }) | ||
| }) | ||
|
|
||
| it("requestAbandon sets abandoned after an abort", () => { | ||
| task.requestAbort(false) | ||
| task.requestAbandon() | ||
|
|
||
| expect(task.abandoned).toBe(true) | ||
| expect(task.runState.latches.abandoned).toBe(true) | ||
| expect(task.runRejections).toEqual([]) | ||
| }) | ||
|
|
||
| it("requestDispose sets abort and the disposed latch", () => { | ||
| task.requestDispose() | ||
|
|
||
| expect(task.abort).toBe(true) | ||
| expect(task.runState.latches).toMatchObject({ abort: true, disposed: true }) | ||
| }) | ||
|
|
||
| it("drives the ask events", () => { | ||
| task.markInitialized() | ||
| task.markAskStarted({ tag: "approval", askTs: 10 }) | ||
| expect(task.runState.ask).toEqual({ tag: "approval", askTs: 10 }) | ||
|
|
||
| task.markAskSettled(10) | ||
| expect(task.runState.ask).toEqual({ tag: "none" }) | ||
|
|
||
| task.markAskStarted({ tag: "completion", askTs: 20 }) | ||
| task.markCompletionAccepted(20) | ||
| expect(task.runState.phase).toBe("completed") | ||
| expect(task.runRejections).toEqual([]) | ||
| }) | ||
|
|
||
| it("keeps the field write and records the event when the kernel rejects it", () => { | ||
| task.requestAbandon() | ||
|
|
||
| expect(task.abandoned).toBe(true) | ||
| expect(task.runState.latches.abandoned).toBe(false) | ||
| expect(task.runRejections).toEqual([{ event: { tag: "abandonRequested" }, state: initialRunState }]) | ||
| }) | ||
|
|
||
| it("keeps the first abort reason in the field and in the kernel", () => { | ||
| task.setAbortReason("user_cancelled") | ||
| task.setAbortReason("streaming_failed") | ||
|
|
||
| expect(task.abortReason).toBe("user_cancelled") | ||
| expect(task.runState.latches.reason).toBe("user_cancelled") | ||
| expect(task.runRejections).toEqual([]) | ||
| }) | ||
| }) |
104 changes: 104 additions & 0 deletions
104
src/core/task/run-state/__tests__/runStateShadow.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { initialRunState, nextRunState, type RunEvent, type RunState } from "../runState" | ||
| import { | ||
| applyRunEvent, | ||
| MAX_RUN_REJECTIONS, | ||
| projectRunState, | ||
| readRunStateFields, | ||
| type RunRejection, | ||
| type RunStateFields, | ||
| } from "../runStateShadow" | ||
|
|
||
| const idle: RunStateFields = { | ||
| abort: false, | ||
| abandoned: false, | ||
| isInitialized: false, | ||
| isStreaming: false, | ||
| didFinishAbortingStream: false, | ||
| } | ||
|
|
||
| function drive(events: RunEvent[]): RunState { | ||
| return events.reduce((state, event) => { | ||
| const result = nextRunState(state, event) | ||
| if (!("ok" in result)) { | ||
| throw new Error(`expected ${event.tag} to be accepted`) | ||
| } | ||
| return result.ok | ||
| }, initialRunState as RunState) | ||
| } | ||
|
|
||
| describe("projectRunState", () => { | ||
| it("keeps each field value in its own slot", () => { | ||
| const fields: RunStateFields = { | ||
| abort: true, | ||
| abandoned: false, | ||
| abortReason: "user_cancelled", | ||
| isInitialized: true, | ||
| isStreaming: true, | ||
| didFinishAbortingStream: true, | ||
| } | ||
| expect(projectRunState(fields)).toEqual(fields) | ||
| expect(projectRunState({ ...fields, abort: false, abandoned: true })).toMatchObject({ | ||
| abort: false, | ||
| abandoned: true, | ||
| }) | ||
| }) | ||
|
|
||
| it("reports didFinishAbortingStream false without a live stream, even when the field is stale", () => { | ||
| expect(projectRunState({ ...idle, didFinishAbortingStream: true }).didFinishAbortingStream).toBe(false) | ||
| }) | ||
|
|
||
| it("agrees with the kernel getters after the same sequence of writes", () => { | ||
| const completed = drive([ | ||
| { tag: "initialized" }, | ||
| { tag: "askStarted", ask: { tag: "completion", askTs: 1 } }, | ||
| { tag: "completionAccepted", askTs: 1 }, | ||
| ]) | ||
| const cases: Array<[RunState, RunStateFields]> = [ | ||
| [initialRunState, idle], | ||
| [drive([{ tag: "initialized" }]), { ...idle, isInitialized: true }], | ||
| [ | ||
| drive([{ tag: "initialized" }, { tag: "streamStarted" }, { tag: "streamCleanupFinished" }]), | ||
| { ...idle, isInitialized: true, isStreaming: true, didFinishAbortingStream: true }, | ||
| ], | ||
| [ | ||
| drive([ | ||
| { tag: "initialized" }, | ||
| { tag: "reasonSet", reason: "user_cancelled" }, | ||
| { tag: "abortRequested", abandoned: false }, | ||
| ]), | ||
| { ...idle, isInitialized: true, abort: true, abortReason: "user_cancelled" }, | ||
| ], | ||
| [drive([{ tag: "abortRequested", abandoned: true }]), { ...idle, abort: true, abandoned: true }], | ||
| [completed, { ...idle, isInitialized: true }], | ||
| ] | ||
| for (const [state, fields] of cases) { | ||
| expect(projectRunState(fields)).toEqual(readRunStateFields(state)) | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| describe("applyRunEvent", () => { | ||
| it("returns the next state and records nothing when the kernel accepts the event", () => { | ||
| const rejections: RunRejection[] = [] | ||
| const next = applyRunEvent(initialRunState, { tag: "initialized" }, rejections) | ||
| expect(next.phase).toBe("running") | ||
| expect(rejections).toEqual([]) | ||
| }) | ||
|
|
||
| it("keeps the state and records the event and state when the kernel rejects the event", () => { | ||
| const rejections: RunRejection[] = [] | ||
| const next = applyRunEvent(initialRunState, { tag: "streamEnded" }, rejections) | ||
| expect(next).toBe(initialRunState) | ||
| expect(rejections).toEqual([{ event: { tag: "streamEnded" }, state: initialRunState }]) | ||
| }) | ||
|
|
||
| it("drops the oldest rejection when the buffer is full", () => { | ||
| const rejections: RunRejection[] = [] | ||
| for (let askTs = 0; askTs < MAX_RUN_REJECTIONS + 5; askTs++) { | ||
| applyRunEvent(initialRunState, { tag: "completionAccepted", askTs }, rejections) | ||
| } | ||
| expect(rejections).toHaveLength(MAX_RUN_REJECTIONS) | ||
| expect(rejections[0].event).toEqual({ tag: "completionAccepted", askTs: 5 }) | ||
| expect(rejections.at(-1)?.event).toEqual({ tag: "completionAccepted", askTs: MAX_RUN_REJECTIONS + 4 }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { | ||
| abandoned, | ||
| abort, | ||
| abortReason, | ||
| didFinishAbortingStream, | ||
| isInitialized, | ||
| isStreaming, | ||
| nextRunState, | ||
| type AbortReason, | ||
| type RunEvent, | ||
| type RunState, | ||
| } from "./runState" | ||
|
|
||
| export type RunStateFields = { | ||
| abort: boolean | ||
| abandoned: boolean | ||
| abortReason?: AbortReason | ||
| isInitialized: boolean | ||
| isStreaming: boolean | ||
| didFinishAbortingStream: boolean | ||
| } | ||
|
|
||
| export type RunRejection = { event: RunEvent; state: RunState } | ||
|
|
||
| export const MAX_RUN_REJECTIONS = 100 | ||
|
|
||
| export const readRunStateFields = (state: RunState): RunStateFields => ({ | ||
|
Check warning on line 27 in src/core/task/run-state/runStateShadow.ts
|
||
| abort: abort(state), | ||
| abandoned: abandoned(state), | ||
| abortReason: abortReason(state), | ||
| isInitialized: isInitialized(state), | ||
| isStreaming: isStreaming(state), | ||
| didFinishAbortingStream: didFinishAbortingStream(state), | ||
| }) | ||
|
|
||
| // The kernel reports didFinishAbortingStream only for a live stream, so a leftover field (race R-1) projects false. | ||
| export function projectRunState(fields: RunStateFields): RunStateFields { | ||
| return { ...fields, didFinishAbortingStream: fields.isStreaming && fields.didFinishAbortingStream } | ||
| } | ||
|
|
||
| export function applyRunEvent(state: RunState, event: RunEvent, rejections: RunRejection[]): RunState { | ||
| const result = nextRunState(state, event) | ||
| if ("ok" in result) { | ||
| return result.ok | ||
| } | ||
| rejections.push({ event, state }) | ||
| if (rejections.length > MAX_RUN_REJECTIONS) { | ||
| rejections.shift() | ||
| } | ||
| return state | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.