-
Notifications
You must be signed in to change notification settings - Fork 57
feat(sessions): opt-in clear history and continue from plan #3462
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
igennova
wants to merge
2
commits into
PostHog:main
Choose a base branch
from
igennova:fix/704
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,103 @@ | ||
| import type { PermissionRequest } from "@posthog/shared"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| buildApprovedPlanContinuationPrompt, | ||
| CLEAR_AND_CONTINUE_OPTION_ID, | ||
| extractPlanMarkdownFromPermission, | ||
| isClearAndContinueOption, | ||
| isPlanApprovalAcceptOption, | ||
| isPlanApprovalPermission, | ||
| resolveClearAndContinueExecutionMode, | ||
| shouldContinueFromApprovedPlan, | ||
| toApprovedExecutionMode, | ||
| } from "./planContinuation"; | ||
|
|
||
| function makePermission( | ||
| overrides: Partial<PermissionRequest> & { | ||
| toolCall?: PermissionRequest["toolCall"]; | ||
| } = {}, | ||
| ): PermissionRequest { | ||
| return { | ||
| taskRunId: "run-1", | ||
| receivedAt: 0, | ||
| options: [], | ||
| ...overrides, | ||
| } as PermissionRequest; | ||
| } | ||
|
|
||
| describe("planContinuation", () => { | ||
| it("detects plan approval permissions", () => { | ||
| expect( | ||
| isPlanApprovalPermission( | ||
| makePermission({ toolCall: { kind: "switch_mode" } as never }), | ||
| ), | ||
| ).toBe(true); | ||
| expect( | ||
| isPlanApprovalPermission( | ||
| makePermission({ toolCall: { kind: "execute" } as never }), | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it("recognizes approve option ids", () => { | ||
| expect(isPlanApprovalAcceptOption("auto")).toBe(true); | ||
| expect(isPlanApprovalAcceptOption("reject_with_feedback")).toBe(false); | ||
| expect(isClearAndContinueOption(CLEAR_AND_CONTINUE_OPTION_ID)).toBe(true); | ||
| expect(isClearAndContinueOption("auto")).toBe(false); | ||
| }); | ||
|
|
||
| it("only continues for the explicit clear-and-continue option", () => { | ||
| const permission = makePermission({ | ||
| toolCall: { kind: "switch_mode" } as never, | ||
| }); | ||
| expect(shouldContinueFromApprovedPlan(permission, "auto")).toBe(false); | ||
| expect( | ||
| shouldContinueFromApprovedPlan(permission, CLEAR_AND_CONTINUE_OPTION_ID), | ||
| ).toBe(true); | ||
| expect( | ||
| shouldContinueFromApprovedPlan(permission, "reject_with_feedback"), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it("extracts trimmed plan markdown from rawInput", () => { | ||
| const permission = makePermission({ | ||
| toolCall: { | ||
| kind: "switch_mode", | ||
| rawInput: { plan: " ## Steps\n- one " }, | ||
| } as never, | ||
| }); | ||
| expect(extractPlanMarkdownFromPermission(permission)).toBe( | ||
| "## Steps\n- one", | ||
| ); | ||
| expect( | ||
| extractPlanMarkdownFromPermission( | ||
| makePermission({ toolCall: { kind: "switch_mode" } as never }), | ||
| ), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("maps approve option to execution mode", () => { | ||
| expect(toApprovedExecutionMode("auto")).toBe("auto"); | ||
| expect(toApprovedExecutionMode("acceptEdits")).toBe("acceptEdits"); | ||
| }); | ||
|
|
||
| it("resolves clear-and-continue execution mode from answers", () => { | ||
| expect( | ||
| resolveClearAndContinueExecutionMode({ executionMode: "acceptEdits" }), | ||
| ).toBe("acceptEdits"); | ||
| expect(resolveClearAndContinueExecutionMode({})).toBe("default"); | ||
| expect(resolveClearAndContinueExecutionMode()).toBe("default"); | ||
| }); | ||
|
|
||
| it("builds a continuation prompt that embeds the plan", () => { | ||
| const blocks = buildApprovedPlanContinuationPrompt("## Fix\n- patch auth"); | ||
| expect(blocks).toHaveLength(1); | ||
| expect(blocks[0]?.type).toBe("text"); | ||
| const textBlock = blocks[0]; | ||
| if (textBlock?.type !== "text") { | ||
| throw new Error("expected text block"); | ||
| } | ||
| expect(textBlock.text).toContain("## Fix\n- patch auth"); | ||
| expect(textBlock.text).toContain("execute this plan directly"); | ||
| }); | ||
| }); |
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,94 @@ | ||
| import type { ContentBlock } from "@agentclientprotocol/sdk"; | ||
| import type { ExecutionMode, PermissionRequest } from "@posthog/shared"; | ||
|
|
||
| /** Option ids offered when approving ExitPlanMode (see buildExitPlanModePermissionOptions). */ | ||
| export const PLAN_APPROVAL_ACCEPT_OPTION_IDS = [ | ||
| "auto", | ||
| "default", | ||
| "acceptEdits", | ||
| "bypassPermissions", | ||
| ] as const; | ||
|
|
||
| export type PlanApprovalAcceptOptionId = | ||
| (typeof PLAN_APPROVAL_ACCEPT_OPTION_IDS)[number]; | ||
|
|
||
| /** | ||
| * Opt-in plan-approval option: clear planning context, then continue from the | ||
| * approved plan. Kept in sync with `CLEAR_AND_CONTINUE_OPTION_ID` in the Claude | ||
| * permission-options module (agent cannot import core). | ||
| */ | ||
| export const CLEAR_AND_CONTINUE_OPTION_ID = "clearAndContinue"; | ||
|
|
||
| /** Answers key used by the plan UI to carry the ModeSelector choice. */ | ||
| export const CLEAR_AND_CONTINUE_EXECUTION_MODE_KEY = "executionMode"; | ||
|
|
||
| export function isPlanApprovalPermission( | ||
| permission: PermissionRequest, | ||
| ): boolean { | ||
| return permission.toolCall?.kind === "switch_mode"; | ||
| } | ||
|
|
||
| export function isPlanApprovalAcceptOption( | ||
| optionId: string, | ||
| ): optionId is PlanApprovalAcceptOptionId { | ||
| return (PLAN_APPROVAL_ACCEPT_OPTION_IDS as readonly string[]).includes( | ||
| optionId, | ||
| ); | ||
| } | ||
|
|
||
| export function isClearAndContinueOption(optionId: string): boolean { | ||
| return optionId === CLEAR_AND_CONTINUE_OPTION_ID; | ||
| } | ||
|
|
||
| /** | ||
| * Only the explicit "clear history and continue from plan" option triggers a | ||
| * fresh implementation session — normal approve keeps the planning context. | ||
| */ | ||
| export function shouldContinueFromApprovedPlan( | ||
| permission: PermissionRequest, | ||
| optionId: string, | ||
| ): boolean { | ||
| return ( | ||
| isPlanApprovalPermission(permission) && isClearAndContinueOption(optionId) | ||
| ); | ||
| } | ||
|
|
||
| export function extractPlanMarkdownFromPermission( | ||
| permission: PermissionRequest, | ||
| ): string | null { | ||
| const rawInput = permission.toolCall?.rawInput as | ||
| | { plan?: unknown } | ||
| | undefined; | ||
| const plan = rawInput?.plan; | ||
| if (typeof plan !== "string") return null; | ||
| const trimmed = plan.trim(); | ||
| return trimmed.length > 0 ? trimmed : null; | ||
| } | ||
|
|
||
| export function toApprovedExecutionMode( | ||
| optionId: PlanApprovalAcceptOptionId, | ||
| ): ExecutionMode { | ||
| return optionId; | ||
| } | ||
|
|
||
| export function resolveClearAndContinueExecutionMode( | ||
| answers?: Record<string, string>, | ||
| ): ExecutionMode { | ||
| const mode = answers?.[CLEAR_AND_CONTINUE_EXECUTION_MODE_KEY]; | ||
| if (mode && isPlanApprovalAcceptOption(mode)) { | ||
| return toApprovedExecutionMode(mode); | ||
| } | ||
| return "default"; | ||
| } | ||
|
|
||
| export function buildApprovedPlanContinuationPrompt( | ||
| planMarkdown: string, | ||
| ): ContentBlock[] { | ||
| const text = [ | ||
| "The user approved the implementation plan below. Planning is complete — execute this plan directly.", | ||
| "Do not re-enter plan mode unless the user asks for a significant change in direction.", | ||
| "", | ||
| planMarkdown, | ||
| ].join("\n"); | ||
| return [{ type: "text", text }]; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The option id is duplicated here and in the Claude permission-options module on purpose — @posthog/agent can't import @posthog/core (import-direction rule), so there's no shared home. The doc comment above flags they must stay in sync. Open to a better spot (e.g. @posthog/shared).