-
Notifications
You must be signed in to change notification settings - Fork 41
fix(sessions): durably write-ahead the start prompt so it can't be lost #2666
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
pauldambra
wants to merge
4
commits into
main
Choose a base branch
from
posthog-code/durable-prompt-outbox
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
4 commits
Select commit
Hold shift + click to select a range
1e9cc00
fix(sessions): durably write-ahead the start prompt so it can't be lost
pauldambra bc252db
refactor(sessions): address review — dedupe write-ahead save, log rec…
pauldambra ebc75e6
fix(sessions): keep written-ahead prompt when send resolves without d…
pauldambra 9ec4d33
feat(sessions): recover owed prompts on resume + cover both routes wi…
pauldambra 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import type { ContentBlock } from "@agentclientprotocol/sdk"; | ||
| import type { Adapter, ExecutionMode } from "@posthog/shared"; | ||
|
|
||
| /** | ||
| * A durable, write-ahead record of a prompt the user is trying to start a | ||
| * local task run with. | ||
| * | ||
| * The prompt is the one thing in the start-a-task flow that the user cannot | ||
| * cheaply reproduce, yet today it only exists in memory until a session has | ||
| * fully initialized and `session/prompt` has been delivered. If session | ||
| * initialization throws or times out (common in large monorepos, where init | ||
| * can exceed the 30s `SESSION_VALIDATION_TIMEOUT_MS` budget), or the app is | ||
| * reloaded/quit/crashes during the retry window, the prompt is lost. | ||
| * | ||
| * To make that loss very unlikely we persist this record BEFORE any | ||
| * agent/session setup is attempted, and only clear it once the prompt has | ||
| * actually been delivered to the agent. (Persistence is async and | ||
| * best-effort, so it is not an absolute guarantee — a crash in the first | ||
| * moments of a cold start can still race the write.) A persisted record | ||
| * therefore means "this prompt is owed delivery and has not been delivered | ||
| * yet" — the basis for recovering it on the next connect, whether that connect | ||
| * starts a fresh run or resumes the stranded one. | ||
| */ | ||
| export interface PendingPromptRecord { | ||
| taskId: string; | ||
| taskTitle: string; | ||
| repoPath: string; | ||
| initialPrompt: ContentBlock[]; | ||
| executionMode?: ExecutionMode; | ||
| adapter?: Adapter; | ||
| model?: string; | ||
| reasoningLevel?: string; | ||
| /** Epoch ms when the prompt was first written ahead. */ | ||
| createdAt: number; | ||
| } | ||
|
|
||
| /** | ||
| * Durable storage for {@link PendingPromptRecord}s, keyed by `taskId` (one | ||
| * in-flight prompt per task — retries reuse the same key). Implementations | ||
| * must survive an app restart. | ||
| */ | ||
| export interface PendingPromptStore { | ||
| /** Write-ahead (or overwrite) the pending prompt for a task. */ | ||
| save(record: PendingPromptRecord): void; | ||
| /** Get the pending prompt for a task, if one is owed delivery. */ | ||
| get(taskId: string): PendingPromptRecord | undefined; | ||
| /** Clear the pending prompt for a task once delivered or abandoned. */ | ||
| remove(taskId: string): void; | ||
| } |
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
72 changes: 72 additions & 0 deletions
72
packages/ui/src/features/sessions/pendingPromptStore.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,72 @@ | ||
| import type { ContentBlock } from "@agentclientprotocol/sdk"; | ||
| import type { PendingPromptRecord } from "@posthog/core/sessions/pendingPrompt"; | ||
| import { beforeEach, describe, expect, it } from "vitest"; | ||
| import { | ||
| pendingPromptStore, | ||
| usePendingPromptStore, | ||
| } from "./pendingPromptStore"; | ||
|
|
||
| function record( | ||
| taskId: string, | ||
| text: string, | ||
| overrides: Partial<PendingPromptRecord> = {}, | ||
| ): PendingPromptRecord { | ||
| const initialPrompt: ContentBlock[] = [{ type: "text", text }]; | ||
| return { | ||
| taskId, | ||
| taskTitle: `Task ${taskId}`, | ||
| repoPath: "/repo", | ||
| initialPrompt, | ||
| createdAt: 1, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function storedTaskIds(): string[] { | ||
| return Object.keys(usePendingPromptStore.getState().promptsByTaskId).sort(); | ||
| } | ||
|
|
||
| describe("pendingPromptStore", () => { | ||
| beforeEach(() => { | ||
| usePendingPromptStore.setState({ promptsByTaskId: {} }); | ||
| }); | ||
|
|
||
| it("saves and reads back a pending prompt by taskId", () => { | ||
| pendingPromptStore.save(record("t1", "do the thing")); | ||
|
|
||
| expect(pendingPromptStore.get("t1")?.initialPrompt).toEqual([ | ||
| { type: "text", text: "do the thing" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("returns undefined when no prompt is owed", () => { | ||
| expect(pendingPromptStore.get("missing")).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("overwrites the record for a task on a re-save (retry reuses the key)", () => { | ||
| pendingPromptStore.save(record("t1", "first")); | ||
| pendingPromptStore.save(record("t1", "second")); | ||
|
|
||
| expect(storedTaskIds()).toEqual(["t1"]); | ||
| expect(pendingPromptStore.get("t1")?.initialPrompt).toEqual([ | ||
| { type: "text", text: "second" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("removes a delivered prompt and leaves others intact", () => { | ||
| pendingPromptStore.save(record("t1", "one")); | ||
| pendingPromptStore.save(record("t2", "two")); | ||
|
|
||
| pendingPromptStore.remove("t1"); | ||
|
|
||
| expect(pendingPromptStore.get("t1")).toBeUndefined(); | ||
| expect(pendingPromptStore.get("t2")).toBeDefined(); | ||
| expect(storedTaskIds()).toEqual(["t2"]); | ||
| }); | ||
|
|
||
| it("remove is a no-op for an unknown task", () => { | ||
| pendingPromptStore.save(record("t1", "one")); | ||
| pendingPromptStore.remove("nope"); | ||
| expect(storedTaskIds()).toEqual(["t1"]); | ||
| }); | ||
| }); |
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,60 @@ | ||
| import type { | ||
| PendingPromptRecord, | ||
| PendingPromptStore, | ||
| } from "@posthog/core/sessions/pendingPrompt"; | ||
| import { electronStorage } from "@posthog/ui/shell/rendererStorage"; | ||
| import { create } from "zustand"; | ||
| import { persist } from "zustand/middleware"; | ||
|
|
||
| interface PendingPromptState { | ||
| /** Map of taskId -> the prompt owed delivery for that task. */ | ||
| promptsByTaskId: Record<string, PendingPromptRecord>; | ||
| } | ||
|
|
||
| interface PendingPromptActions { | ||
| savePrompt: (record: PendingPromptRecord) => void; | ||
| getPrompt: (taskId: string) => PendingPromptRecord | undefined; | ||
| removePrompt: (taskId: string) => void; | ||
| } | ||
|
|
||
| type PendingPromptStoreState = PendingPromptState & PendingPromptActions; | ||
|
|
||
| export const usePendingPromptStore = create<PendingPromptStoreState>()( | ||
| persist( | ||
| (set, get) => ({ | ||
| promptsByTaskId: {}, | ||
|
|
||
| savePrompt: (record) => | ||
| set((state) => ({ | ||
| promptsByTaskId: { | ||
| ...state.promptsByTaskId, | ||
| [record.taskId]: record, | ||
| }, | ||
| })), | ||
|
|
||
| getPrompt: (taskId) => get().promptsByTaskId[taskId], | ||
|
|
||
| removePrompt: (taskId) => | ||
|
pauldambra marked this conversation as resolved.
|
||
| set((state) => { | ||
| if (!(taskId in state.promptsByTaskId)) return state; | ||
| const { [taskId]: _removed, ...rest } = state.promptsByTaskId; | ||
| return { promptsByTaskId: rest }; | ||
| }), | ||
| }), | ||
| { | ||
| name: "pending-prompt-storage", | ||
| storage: electronStorage, | ||
| partialize: (state) => ({ promptsByTaskId: state.promptsByTaskId }), | ||
| }, | ||
| ), | ||
| ); | ||
|
|
||
| /** | ||
| * Non-hook adapter implementing the core {@link PendingPromptStore} contract, | ||
| * wired into the session service dependencies. | ||
| */ | ||
| export const pendingPromptStore: PendingPromptStore = { | ||
| save: (record) => usePendingPromptStore.getState().savePrompt(record), | ||
| get: (taskId) => usePendingPromptStore.getState().getPrompt(taskId), | ||
| remove: (taskId) => usePendingPromptStore.getState().removePrompt(taskId), | ||
| }; | ||
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.