-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(tasks): clear a finished cloud run's conversation without a sandbox #76943
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2c7e954
feat(tasks): clear a finished cloud run's conversation without a sandbox
haacked 90a387c
chore(tasks): register clear_conversation as a (disabled) MCP tool
haacked 503f3d8
fix(tasks): address review findings on the finished-run /clear
haacked cca74da
fix(tasks): drop the web /clear gate when the latest agent lacks the …
haacked 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
123 changes: 123 additions & 0 deletions
123
products/desktop/packages/core/src/sessions/sessionServiceCloudClear.test.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,123 @@ | ||
| import type { AgentSession } from "@posthog/shared"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { POSTHOG_NOTIFICATIONS } from "./acpNotifications"; | ||
| import { SessionService, type SessionServiceDeps } from "./sessionService"; | ||
|
|
||
| const TASK_ID = "task-1"; | ||
| const TASK_RUN_ID = `run-${TASK_ID}`; | ||
|
|
||
| function createHarness({ | ||
| conversationClear = true, | ||
| }: { | ||
| conversationClear?: boolean; | ||
| } = {}) { | ||
| const sessions: Record<string, AgentSession> = { | ||
| [TASK_RUN_ID]: { | ||
| taskRunId: TASK_RUN_ID, | ||
| taskId: TASK_ID, | ||
| taskTitle: "Test task", | ||
| channel: "", | ||
| events: [], | ||
| startedAt: 1, | ||
| status: "connected", | ||
| isCloud: true, | ||
| cloudStatus: "completed", | ||
| conversationClear, | ||
| isPromptPending: false, | ||
| isCompacting: false, | ||
| promptStartedAt: null, | ||
| pendingPermissions: new Map(), | ||
| pausedDurationMs: 0, | ||
| messageQueue: [], | ||
| optimisticItems: [], | ||
| } as unknown as AgentSession, | ||
| }; | ||
|
|
||
| const appendEvents = vi.fn(); | ||
| const clearTaskRunConversation = vi.fn().mockResolvedValue(undefined); | ||
| const runTaskInCloud = vi.fn(); | ||
|
|
||
| const deps = { | ||
| store: { | ||
| getSessions: () => sessions, | ||
| getSessionByTaskId: (taskId: string) => | ||
| Object.values(sessions).find((s) => s.taskId === taskId), | ||
| appendEvents, | ||
| updateSession: vi.fn(), | ||
| appendOptimisticItem: vi.fn(), | ||
| clearTailOptimisticItems: vi.fn(), | ||
| }, | ||
| h: { | ||
| getCloudPromptTransport: (prompt: string) => ({ | ||
| promptText: prompt, | ||
| messageText: prompt, | ||
| filePaths: [], | ||
| skillBundles: [], | ||
| }), | ||
| }, | ||
| log: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, | ||
| toast: { error: vi.fn(), info: vi.fn() }, | ||
| track: vi.fn(), | ||
| getIsOnline: () => true, | ||
| addDirectoryDialog: { open: false }, | ||
| getAuthenticatedClient: async () => ({ | ||
| clearTaskRunConversation, | ||
| runTaskInCloud, | ||
| }), | ||
| trpc: { | ||
| agent: { | ||
| onSessionIdleKilled: { subscribe: () => ({ unsubscribe: vi.fn() }) }, | ||
| }, | ||
| }, | ||
| } as unknown as SessionServiceDeps; | ||
|
|
||
| return { | ||
| service: new SessionService(deps), | ||
| appendEvents, | ||
| clearTaskRunConversation, | ||
| runTaskInCloud, | ||
| }; | ||
| } | ||
|
|
||
| describe("SessionService /clear on a finished cloud run", () => { | ||
| it("records the boundary and renders it without resuming into a new run", async () => { | ||
| const { service, appendEvents, clearTaskRunConversation, runTaskInCloud } = | ||
| createHarness(); | ||
|
|
||
| const result = await service.sendPrompt(TASK_ID, "/clear"); | ||
|
|
||
| expect(result).toEqual({ stopReason: "end_turn" }); | ||
| expect(clearTaskRunConversation).toHaveBeenCalledWith(TASK_ID, TASK_RUN_ID); | ||
| // Resuming would spin a whole sandbox to clear a conversation the next run | ||
| // rebuilds from the log anyway. | ||
| expect(runTaskInCloud).not.toHaveBeenCalled(); | ||
|
|
||
| // A finished run streams nothing back, so the thread is painted from here. | ||
| // The user message must be a session/prompt request: the renderer drops raw | ||
| // user_message_chunks, so painting one would show only the divider. | ||
| const [, events] = appendEvents.mock.calls[0]; | ||
| expect( | ||
| events.map((e: { message: { method: string } }) => e.message.method), | ||
| ).toEqual(["session/prompt", POSTHOG_NOTIFICATIONS.CONVERSATION_CLEARED]); | ||
| }); | ||
|
|
||
| it("resumes into a new run when the agent cannot honour the boundary", async () => { | ||
| // An older agent ignores the marker and resumes the conversation it was meant to | ||
| // retire, so recording one would claim a clear that never happens. | ||
| const { service, clearTaskRunConversation } = createHarness({ | ||
| conversationClear: false, | ||
| }); | ||
|
|
||
| await service.sendPrompt(TASK_ID, "/clear").catch(() => undefined); | ||
|
|
||
| expect(clearTaskRunConversation).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("still resumes into a new run for an ordinary message", async () => { | ||
| const { service, clearTaskRunConversation } = createHarness(); | ||
|
|
||
| await service.sendPrompt(TASK_ID, "keep going").catch(() => undefined); | ||
|
|
||
| expect(clearTaskRunConversation).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.