: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): recover auto-publish from run state (#83318)
Paul D'Ambrac69e70a1ae2d
diff --git a/products/desktop/packages/agent/src/server/agent-server.test.ts b/products/desktop/packages/agent/src/server/agent-server.test.tsindex 93b1b37c7448..838c43e8cd0f 100644--- a/products/desktop/packages/agent/src/server/agent-server.test.ts+++ b/products/desktop/packages/agent/src/server/agent-server.test.ts@@ -4491,7 +4491,7 @@ describe("AgentServer HTTP Mode", () => {};} | null;posthogAPI: { getTaskRun: ReturnType<typeof vi.fn> };- resolveWarmActivationSettings(): Promise<string | null>;+ resolveActivationSettings(): Promise<string | null>;buildCloudSystemPrompt(): string;};const makeWarmServer = (@@ -4529,7 +4529,7 @@ describe("AgentServer HTTP Mode", () => {};};posthogAPI: { getTaskRun: ReturnType<typeof vi.fn> };- resolveWarmActivationSettings(): Promise<string | null>;+ resolveActivationSettings(): Promise<string | null>;};t.prewarmedRun = true;t.session = {@@ -4543,8 +4543,8 @@ describe("AgentServer HTTP Mode", () => {})),};- await t.resolveWarmActivationSettings();- await t.resolveWarmActivationSettings();+ await t.resolveActivationSettings();+ await t.resolveActivationSettings();expect(setSessionConfigOption).toHaveBeenCalledOnce();expect(setSessionConfigOption).toHaveBeenCalledWith({@@ -4558,24 +4558,35 @@ describe("AgentServer HTTP Mode", () => {it("upgrades a prewarmed run to auto-publish from run state on the first message", async () => {const t = makeWarmServer({ prewarmed: true, auto_publish: true });- const override = await t.resolveWarmActivationSettings();+ const override = await t.resolveActivationSettings();expect(override).toContain("OVERRIDE PREVIOUS INSTRUCTIONS");expect(override).toContain("gh pr create --draft");// The flip persists for the rest of the session...expect(t.buildCloudSystemPrompt()).toContain("gh pr create --draft");// ...and the override is injected only once.- expect(await t.resolveWarmActivationSettings()).toBeNull();+ expect(await t.resolveActivationSettings()).toBeNull();expect(t.posthogAPI.getTaskRun).toHaveBeenCalledTimes(1);});+ it("recovers auto-publish from run state when the launch flag is missing", async () => {+ const t = makeWarmServer({ auto_publish: true });+ t.prewarmedRun = false;++ const override = await t.resolveActivationSettings();++ expect(override).toContain("OVERRIDE PREVIOUS INSTRUCTIONS");+ expect(t.buildCloudSystemPrompt()).toContain("gh pr create --draft");+ expect(t.posthogAPI.getTaskRun).toHaveBeenCalledOnce();+ });+it("keeps a prewarmed run review-first when run state has no auto_publish", async () => {const t = makeWarmServer({ prewarmed: true });- expect(await t.resolveWarmActivationSettings()).toBeNull();+ expect(await t.resolveActivationSettings()).toBeNull();expect(t.buildCloudSystemPrompt()).toContain("stop with local changes ready for review",);- expect(await t.resolveWarmActivationSettings()).toBeNull();+ expect(await t.resolveActivationSettings()).toBeNull();expect(t.posthogAPI.getTaskRun).toHaveBeenCalledTimes(1);});@@ -4587,7 +4598,7 @@ describe("AgentServer HTTP Mode", () => {{ createPr: false },);- expect(await t.resolveWarmActivationSettings()).toBeNull();+ expect(await t.resolveActivationSettings()).toBeNull();expect(t.posthogAPI.getTaskRun).toHaveBeenCalledOnce();expect(t.buildCloudSystemPrompt()).toContain("stop with local changes ready for review",@@ -4596,12 +4607,12 @@ describe("AgentServer HTTP Mode", () => {it("retries the state fetch on a later message when it fails", async () => {const t = makeWarmServer(new Error("fetch failed"));- expect(await t.resolveWarmActivationSettings()).toBeNull();+ expect(await t.resolveActivationSettings()).toBeNull();t.posthogAPI.getTaskRun = vi.fn(async () => ({state: { prewarmed: true, auto_publish: true },}));- expect(await t.resolveWarmActivationSettings()).toContain(+ expect(await t.resolveActivationSettings()).toContain("gh pr create --draft",);});@@ -4617,8 +4628,8 @@ describe("AgentServer HTTP Mode", () => {setSessionConfigOption,);- await expect(t.resolveWarmActivationSettings()).resolves.toBeNull();- await expect(t.resolveWarmActivationSettings()).resolves.toBeNull();+ await expect(t.resolveActivationSettings()).resolves.toBeNull();+ await expect(t.resolveActivationSettings()).resolves.toBeNull();expect(setSessionConfigOption).toHaveBeenCalledTimes(2);expect(t.posthogAPI.getTaskRun).toHaveBeenCalledTimes(2);diff --git a/products/desktop/packages/agent/src/server/agent-server.ts b/products/desktop/packages/agent/src/server/agent-server.tsindex d1c179d7a1a3..866f7c284341 100644--- a/products/desktop/packages/agent/src/server/agent-server.ts+++ b/products/desktop/packages/agent/src/server/agent-server.ts@@ -443,12 +443,12 @@ export class AgentServer {private resumeState: ResumeState | null = null;private nativeResume: { sessionId: string; warm: boolean } | null = null;private oversizedResumeRetried = false;- // Prewarmed runs boot before the user's first message exists, so the boot-time- // --autoPublish flag can't carry the user's choice; it is resolved from run- // state when the first message arrives (see resolveWarmActivationSettings).+ // Prewarmed runs boot before the user's first message exists, so boot-time+ // CLI flags can't carry the user's choice. Those settings are read from the+ // run's state when the first message arrives (see resolveActivationSettings).private prewarmedRun = false;private prewarmedStartupTurnPending = false;- private warmAutoPublishResolved = false;+ private autoPublishStateResolved = false;private warmReasoningEffortResolved = false;private installedSkillBundles = new Set<string>();private installedSkillBundleInfo = new Map<string, InstalledSkillBundle>();@@ -1208,7 +1208,7 @@ export class AgentServer {// effort while the final composer selection uses another.// Resolve before buildDetectedPrContext so a warm auto-publish upgrade// also flips the detected-PR context to its push variant.- const autoPublishUpgrade = await this.resolveWarmActivationSettings();+ const autoPublishUpgrade = await this.resolveActivationSettings();const hostContext = [...(autoPublishUpgrade ? [autoPublishUpgrade] : []),...(this.detectedPrUrl@@ -1614,7 +1614,7 @@ export class AgentServer {this.preSessionEvents = [];this.prewarmedRun = false;this.prewarmedStartupTurnPending = false;- this.warmAutoPublishResolved = false;+ this.autoPublishStateResolved = false;this.warmReasoningEffortResolved = false;this.logger.debug("Initializing session", {@@ -3535,12 +3535,20 @@ export class AgentServer {);}- /** Apply activation-time settings before a prewarmed session's first turn. */- private async resolveWarmActivationSettings(): Promise<string | null> {- if (!this.prewarmedRun || !this.session) {+ /** Apply settings from run state before the first turn when launch config is incomplete. */+ private async resolveActivationSettings(): Promise<string | null> {+ if (!this.session) {return null;}- if (this.warmReasoningEffortResolved && this.warmAutoPublishResolved) {++ const shouldResolveReasoning =+ this.prewarmedRun && !this.warmReasoningEffortResolved;+ const shouldResolveAutoPublish =+ !this.autoPublishStateResolved &&+ this.config.autoPublish !== true &&+ this.config.createPr !== false &&+ !this.isAutomatedOrigin();+ if (!shouldResolveReasoning && !shouldResolveAutoPublish) {return null;}@@ -3552,14 +3560,16 @@ export class AgentServer {);state = run?.state as Record<string, unknown> | undefined;} catch (error) {- // Keep both settings unresolved so a later message retries. A transient+ // Keep the settings unresolved so a later message retries. A transient// control-plane failure must not prevent the first prompt from running.- this.logger.debug("Failed to fetch warm activation settings", { error });+ this.logger.debug("Failed to fetch activation settings", { error });return null;}- await this.resolveWarmReasoningEffort(state);- return this.resolveWarmAutoPublishUpgrade(state);+ if (shouldResolveReasoning) {+ await this.resolveWarmReasoningEffort(state);+ }+ return this.resolveAutoPublishFromState(state);}private async resolveWarmReasoningEffort(@@ -3596,18 +3606,13 @@ export class AgentServer {}/**- * A prewarmed run boots before the user's first message exists, so the- * --autoPublish flag can't carry the user's choice; the backend persists it- * into the run's state at warm activation instead. Nothing has been sent to- * the agent until that first message arrives, so resolving it here still- * governs the whole conversation: flip the config (so later consumers like- * buildDetectedPrContext see it) and return the auto-publish cloud- * instructions to inject into the first prompt as an override.+ * The backend persists auto-publish in run state. Recover it when an older or+ * incomplete launch path omits the CLI flag, before the agent sees its first prompt.*/- private resolveWarmAutoPublishUpgrade(+ private resolveAutoPublishFromState(state: Record<string, unknown> | undefined,): string | null {- if (this.warmAutoPublishResolved) {+ if (this.autoPublishStateResolved) {return null;}if (@@ -3616,15 +3621,15 @@ export class AgentServer {this.isAutomatedOrigin()) {// The boot decision already publishes (or never may) — nothing to upgrade.- this.warmAutoPublishResolved = true;+ this.autoPublishStateResolved = true;return null;}- this.warmAutoPublishResolved = true;+ this.autoPublishStateResolved = true;if (state?.auto_publish !== true) {return null;}this.config.autoPublish = true;- this.logger.debug("Warm run upgraded to auto-publish from run state");+ this.logger.debug("Run upgraded to auto-publish from run state");return ["IMPORTANT — OVERRIDE PREVIOUS INSTRUCTIONS ABOUT CREATING BRANCHES/PRs.","The user has auto-publish enabled for this run. The review-first cloud task instructions in your system prompt are replaced by the following:",