Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): preserve prompts when resuming sessions (#81636)

Peter Kirkham0b738223390c
diff --git a/products/desktop/packages/agent/src/server/agent-server.test.ts b/products/desktop/packages/agent/src/server/agent-server.test.ts
index d12a4ba5d12f..8965a0409095 100644
--- a/products/desktop/packages/agent/src/server/agent-server.test.ts
+++ b/products/desktop/packages/agent/src/server/agent-server.test.ts
@@ -3413,17 +3413,24 @@ describe("AgentServer HTTP Mode", () => {
sessionId: "prior-session",
};
- await s.sendResumeMessage(
- payload,
- createTaskRun({
- id: "test-run-id",
- task: "test-task-id",
- state: {
- pending_user_message: "visible follow-up",
- pending_user_message_ts: "123.456",
- },
- }),
- );
+ const taskRun = createTaskRun({
+ id: "test-run-id",
+ task: "test-task-id",
+ state: {
+ pending_user_message: "visible follow-up",
+ pending_user_message_ts: "123.456",
+ },
+ });
+ vi.spyOn(
+ (
+ s as unknown as {
+ posthogAPI: { getTaskRun: ReturnType<typeof vi.fn> };
+ }
+ ).posthogAPI,
+ "getTaskRun",
+ ).mockResolvedValue(taskRun);
+
+ await s.sendResumeMessage(payload, taskRun);
const [{ prompt: promptBlocks }] = prompt.mock.calls[0] as unknown as [
{ prompt: ContentBlock[] },
@@ -3452,6 +3459,68 @@ describe("AgentServer HTTP Mode", () => {
_meta: { ui: { hidden: true } },
});
});
+
+ it("refetches the run before choosing a native resume prompt", async () => {
+ const s = createServer();
+ await s.start();
+
+ const prompt = vi.fn(async () => ({ stopReason: "cancelled" }));
+ const updateTaskRun = vi.fn(async () => ({}));
+ const internals = s as unknown as {
+ posthogAPI: {
+ getTaskRun: ReturnType<typeof vi.fn>;
+ updateTaskRun: typeof updateTaskRun;
+ };
+ session: {
+ clientConnection: { prompt: typeof prompt };
+ };
+ nativeResume: { sessionId: string; warm: boolean } | null;
+ sendResumeContinuation(
+ payload: JwtPayload,
+ taskRun: TaskRun | null,
+ ): Promise<void>;
+ };
+ internals.session.clientConnection.prompt = prompt;
+ internals.nativeResume = { sessionId: "prior-session", warm: true };
+ internals.posthogAPI.updateTaskRun = updateTaskRun;
+ vi.spyOn(internals.posthogAPI, "getTaskRun").mockResolvedValue(
+ createTaskRun({
+ id: "test-run-id",
+ task: "test-task-id",
+ state: { pending_user_message: "keep my follow-up" },
+ }),
+ );
+
+ await internals.sendResumeContinuation(
+ {
+ task_id: "test-task-id",
+ run_id: "test-run-id",
+ team_id: 1,
+ user_id: 1,
+ distinct_id: "test-distinct-id",
+ mode: "interactive",
+ },
+ createTaskRun({
+ id: "test-run-id",
+ task: "test-task-id",
+ state: { resume_from_run_id: "previous-run" },
+ }),
+ );
+
+ const [{ prompt: promptBlocks }] = prompt.mock.calls[0] as unknown as [
+ { prompt: ContentBlock[] },
+ ];
+ expect(promptBlocks).toEqual([
+ { type: "text", text: "keep my follow-up" },
+ ]);
+ expect(updateTaskRun).toHaveBeenCalledWith(
+ "test-task-id",
+ "test-run-id",
+ {
+ state_remove_keys: ["pending_user_message"],
+ },
+ );
+ });
});
describe("runtime adapter selection", () => {
@@ -3614,6 +3683,7 @@ describe("AgentServer HTTP Mode", () => {
const newSession = vi.fn(async () => ({ sessionId: "fresh-session" }));
const internals = s as unknown as {
+ posthogAPI: { getTaskRun: ReturnType<typeof vi.fn> };
session: {
acpSessionId: string;
clientConnection: {
@@ -3636,6 +3706,12 @@ describe("AgentServer HTTP Mode", () => {
internals.session.clientConnection.prompt = prompt;
internals.session.clientConnection.newSession = newSession;
internals.nativeResume = { sessionId: "prior-session", warm: true };
+ vi.spyOn(internals.posthogAPI, "getTaskRun").mockResolvedValue(
+ createTaskRun({
+ id: "test-run-id",
+ state: { resume_from_run_id: "previous-run" },
+ }),
+ );
internals.loadResumeState = vi.fn(async () => {
internals.resumeState = {
conversation: [
diff --git a/products/desktop/packages/agent/src/server/agent-server.ts b/products/desktop/packages/agent/src/server/agent-server.ts
index 2494175492ca..ad3f7bf89e1c 100644
--- a/products/desktop/packages/agent/src/server/agent-server.ts
+++ b/products/desktop/packages/agent/src/server/agent-server.ts
@@ -2291,6 +2291,7 @@ export class AgentServer {
): Promise<void> {
if (!this.session || !this.resumeState) return;
const resumeState = this.resumeState;
+ taskRun = await this.refreshTaskRunForResume(payload, taskRun);
await this.runResumeTurn(payload, taskRun, "Resume message", async () => {
const conversationSummary = formatConversationForResume(
@@ -2354,6 +2355,7 @@ export class AgentServer {
taskRun: TaskRun | null,
): Promise<void> {
if (!this.session) return;
+ taskRun = await this.refreshTaskRunForResume(payload, taskRun);
await this.runResumeTurn(
payload,
@@ -2391,6 +2393,22 @@ export class AgentServer {
);
}
+ private async refreshTaskRunForResume(
+ payload: JwtPayload,
+ fallback: TaskRun | null,
+ ): Promise<TaskRun | null> {
+ try {
+ return await this.posthogAPI.getTaskRun(payload.task_id, payload.run_id);
+ } catch (error) {
+ this.logger.debug("Failed to refresh task run before resume", {
+ taskId: payload.task_id,
+ runId: payload.run_id,
+ error,
+ });
+ return fallback;
+ }
+ }
+
/**
* A native resume replays the prior transcript verbatim; when that
* transcript no longer fits the context window, every request (including