: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(tasks): keep workflow AI task runs open so the convo can go on when triggered from slack (#90109)
Harley Alexander920d8274dc2b
diff --git a/products/desktop/packages/agent/src/adapters/claude/claude-agent.ts b/products/desktop/packages/agent/src/adapters/claude/claude-agent.tsindex 4d2ee56cd956..7ddd4148f9eb 100644--- a/products/desktop/packages/agent/src/adapters/claude/claude-agent.ts+++ b/products/desktop/packages/agent/src/adapters/claude/claude-agent.ts@@ -2605,6 +2605,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {typeof meta?.taskOriginProduct === "string"? meta.taskOriginProduct: undefined;+ const endRunWhenDone = meta?.endRunWhenDone === true;const spokenNarration = resolveSpokenNarration(meta);const bedrockGatewayVariant = resolveBedrockGatewayVariant(meta);const requestFinish = this.buildRequestFinish(taskId, meta?.taskRunId);@@ -2628,6 +2629,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {background: meta?.mode === "background",peerMessaging: process.env.POSTHOG_AGENT_PEER_MESSAGING === "1",taskOriginProduct,+ endRunWhenDone,},);return server ? { [LOCAL_TOOLS_MCP_NAME]: server } : {};diff --git a/products/desktop/packages/agent/src/adapters/claude/types.ts b/products/desktop/packages/agent/src/adapters/claude/types.tsindex 676300fff8fb..ee07660d7d9e 100644--- a/products/desktop/packages/agent/src/adapters/claude/types.ts+++ b/products/desktop/packages/agent/src/adapters/claude/types.ts@@ -237,6 +237,8 @@ export type NewSessionMeta = {*/channelMode?: boolean;taskOriginProduct?: string;+ /** Workflow-action opt-in: exposes the `finish` tool to a workflow-origin run. */+ endRunWhenDone?: boolean;/*** The user's spoken-narration setting at session start. Gates the speak* tool and its prompt instructions. Unset falls back by environment: clouddiff --git a/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.ts b/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.tsindex 97fdab3cbabd..3e607a0954e6 100644--- a/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.ts+++ b/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.ts@@ -126,6 +126,7 @@ type AppServerSessionMeta = {spokenNarration?: boolean;baseBranch?: string;taskOriginProduct?: string;+ endRunWhenDone?: boolean;posthogExecPermissionRegex?: string;nativeGoal?: NativeGoalState;};@@ -688,6 +689,7 @@ export class CodexAppServerAgent extends BaseAcpAgent {baseBranch: meta.baseBranch,peerMessaging: process.env.POSTHOG_AGENT_PEER_MESSAGING === "1",taskOriginProduct: meta.taskOriginProduct,+ endRunWhenDone: meta.endRunWhenDone === true,};}diff --git a/products/desktop/packages/agent/src/adapters/local-tools/registry.ts b/products/desktop/packages/agent/src/adapters/local-tools/registry.tsindex d67df8d57ddc..7b370abf2b5b 100644--- a/products/desktop/packages/agent/src/adapters/local-tools/registry.ts+++ b/products/desktop/packages/agent/src/adapters/local-tools/registry.ts@@ -54,6 +54,13 @@ export interface LocalToolGateMeta {*/peerMessaging?: boolean;taskOriginProduct?: string;+ /**+ * Workflow-action opt-in (run state `end_run_when_done`): exposes the `finish`+ * tool to a workflow-origin run, ending it the moment the agent is done.+ * Without it the run stays live for its idle window so its Slack reply can+ * relay and thread replies still reach the agent.+ */+ endRunWhenDone?: boolean;}/**diff --git a/products/desktop/packages/agent/src/adapters/local-tools/tools/finish.test.ts b/products/desktop/packages/agent/src/adapters/local-tools/tools/finish.test.tsindex 7b4b1d32c763..3d4636178514 100644--- a/products/desktop/packages/agent/src/adapters/local-tools/tools/finish.test.ts+++ b/products/desktop/packages/agent/src/adapters/local-tools/tools/finish.test.ts@@ -18,21 +18,29 @@ describe("finish tool", () => {it.each([{- name: "background cloud run with requestFinish",+ name: "background cloud run with an unknown origin (fetch failed)",ctx: { cwd: "/repo", requestFinish },meta: { environment: "cloud", background: true },- expected: true,+ expected: false,},{name: "background cloud run without requestFinish or run ids",ctx: { cwd: "/repo" },- meta: { environment: "cloud", background: true },+ meta: {+ environment: "cloud",+ background: true,+ taskOriginProduct: "user_created",+ },expected: false,},{name: "background cloud run with run ids only (out-of-process adapter)",ctx: { cwd: "/repo", taskId: "task-1", taskRunId: "run-1" },- meta: { environment: "cloud", background: true },+ meta: {+ environment: "cloud",+ background: true,+ taskOriginProduct: "user_created",+ },expected: true,},{@@ -53,6 +61,37 @@ describe("finish tool", () => {meta: { environment: "local", background: true },expected: false,},+ {+ name: "workflow-origin background cloud run",+ ctx: { cwd: "/repo", requestFinish },+ meta: {+ environment: "cloud",+ background: true,+ taskOriginProduct: "workflow",+ },+ expected: false,+ },+ {+ name: "workflow-origin run with the end-run opt-in",+ ctx: { cwd: "/repo", requestFinish },+ meta: {+ environment: "cloud",+ background: true,+ taskOriginProduct: "workflow",+ endRunWhenDone: true,+ },+ expected: true,+ },+ {+ name: "non-workflow-origin background cloud run",+ ctx: { cwd: "/repo", requestFinish },+ meta: {+ environment: "cloud",+ background: true,+ taskOriginProduct: "user_created",+ },+ expected: true,+ },{name: "no gate meta",ctx: { cwd: "/repo", requestFinish },diff --git a/products/desktop/packages/agent/src/adapters/local-tools/tools/finish.ts b/products/desktop/packages/agent/src/adapters/local-tools/tools/finish.tsindex 115911e0cf8d..d2abac91d6af 100644--- a/products/desktop/packages/agent/src/adapters/local-tools/tools/finish.ts+++ b/products/desktop/packages/agent/src/adapters/local-tools/tools/finish.ts@@ -88,9 +88,22 @@ export const finishTool = defineLocalTool({description: FINISH_TOOL_DESCRIPTION,schema: finishSchema,alwaysLoad: true,+ // Workflow-origin runs are excluded unless run state carries the end-run+ // key: they reply into the Slack thread that triggered them, and that relay+ // only fires after the turn ends. `finish` marks the run terminal mid-turn,+ // which makes the backend drop the relay (relay_task_run_message skips+ // terminal runs), so the reply is lost every time. Without the key those+ // runs end via their inactivity timeout instead. The backend writes the key+ // for every workflow run with no Slack thread binding, so only thread-bound+ // runs pay the timeout. An unknown origin also hides the tool: the origin+ // fetch fails soft, and exposing `finish` on a blip would silently eat a+ // workflow run's reply, while hiding it only costs a bounded idle window.isEnabled: (ctx, meta) =>meta?.environment === "cloud" &&meta?.background === true &&+ ((meta?.taskOriginProduct !== undefined &&+ meta?.taskOriginProduct !== "workflow") ||+ meta?.endRunWhenDone === true) &&resolveRequestFinish(ctx) !== undefined,handler: async (ctx, args): Promise<LocalToolResult> => {const requestFinish = resolveRequestFinish(ctx);diff --git a/products/desktop/packages/agent/src/server/agent-server.ts b/products/desktop/packages/agent/src/server/agent-server.tsindex 9502e838eb40..fcda4f589264 100644--- a/products/desktop/packages/agent/src/server/agent-server.ts+++ b/products/desktop/packages/agent/src/server/agent-server.ts@@ -2017,6 +2017,7 @@ export class AgentServer {...(preTask?.origin_product && {taskOriginProduct: preTask.origin_product,}),+ ...(runState?.end_run_when_done === true && { endRunWhenDone: true }),...(this.config.baseBranch && { baseBranch: this.config.baseBranch }),...(runtimeAdapter === "claude" &&this.config.contextWindow && {