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.

feat(tasks): default runtime and template from the task profile (#126078)

Charles Vien487be3f9184e
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index 7c1d102d0033..0c15cf073806 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -958,6 +958,8 @@ class Task(Taggable, DeletedMetaFields, models.Model):
# Later runs keep the image the task was first provisioned with.
if carry_sandbox_template and previous_state.get("sandbox_template"):
state["sandbox_template"] = previous_state["sandbox_template"]
+ elif carry_sandbox_template and (stored_profile := task.stored_profile) is not None:
+ state["sandbox_template"] = stored_profile.sandbox_templates[0]
if task._resolves_pr_authorship_per_run() and "pr_authorship_mode" not in state:
from products.tasks.backend.temporal.process_task.utils import (
PrAuthorshipMode,
@@ -1629,7 +1631,7 @@ class Task(Taggable, DeletedMetaFields, models.Model):
profile=profile,
source=source_from_columns(signal_report_id=columns.signal_report_id, hog_flow_id=columns.hog_flow_id),
)
- runtime = Task.Runtime.ACP.value
+ runtime = profile.runtimes[0] if profile is not None else Task.Runtime.ACP.value
if options is not None:
settings = apply_run_options(
options,
@@ -1641,7 +1643,8 @@ class Task(Taggable, DeletedMetaFields, models.Model):
),
creates_run=False,
)
- runtime = settings.runtime
+ if options.runtime is not None:
+ runtime = settings.runtime
repositories = settings.repositories
output_schema = settings.output_schema
mcp_gateway_server_ids = settings.mcp_gateway_server_ids
@@ -1708,7 +1711,7 @@ class Task(Taggable, DeletedMetaFields, models.Model):
client_provenance: TaskClientProvenance | None = None,
output_schema: type[BaseModel] | dict | None = None,
interaction_origin: str | None = None,
- runtime: str = "acp",
+ runtime: str | None = None,
runtime_adapter: str | None = None,
model: str | None = None,
reasoning_effort: str | None = None,
@@ -1769,7 +1772,7 @@ class Task(Taggable, DeletedMetaFields, models.Model):
settings = apply_run_options(
options,
RunSettings(
- runtime=runtime,
+ runtime=runtime or Task.Runtime.ACP.value,
runtime_adapter=runtime_adapter,
model=model,
reasoning_effort=reasoning_effort,
@@ -1786,7 +1789,8 @@ class Task(Taggable, DeletedMetaFields, models.Model):
extra_run_state=extra_run_state,
),
)
- runtime = settings.runtime
+ if options.runtime is not None:
+ runtime = settings.runtime
runtime_adapter = settings.runtime_adapter
model = settings.model
reasoning_effort = settings.reasoning_effort
@@ -1800,6 +1804,8 @@ class Task(Taggable, DeletedMetaFields, models.Model):
initial_permission_mode = settings.initial_permission_mode
mcp_gateway_server_ids = settings.mcp_gateway_server_ids
extra_run_state = settings.extra_run_state
+ if runtime is None:
+ runtime = profile.runtimes[0] if profile is not None else Task.Runtime.ACP.value
task, extra_state = Task._build_task(
team=team,
title=title,
diff --git a/products/tasks/backend/tests/test_models.py b/products/tasks/backend/tests/test_models.py
index 8886e6ebe6a1..7bb12f2688fb 100644
--- a/products/tasks/backend/tests/test_models.py
+++ b/products/tasks/backend/tests/test_models.py
@@ -28,6 +28,7 @@ from products.tasks.backend.facade.profiles import (
PrAuthorship,
Repository,
SourceKind,
+ TaskProfile,
TaskProfileError,
TaskRunOptions,
TaskRunOptionsError,
@@ -55,6 +56,7 @@ SOURCE_ID = uuid.UUID("5ab01f4d-5b1e-4990-9802-4f8792a76759")
FLOW_SOURCE = TaskSource(kind=SourceKind.HOG_FLOW, id=str(SOURCE_ID))
REPORT_PROFILE = replace(BUSINESS_KNOWLEDGE, key="test.report", source_kind=SourceKind.SIGNAL_REPORT)
POSTHOG_AI_BOT_PROFILE = replace(BUSINESS_KNOWLEDGE, key="test.posthog_ai_bot", origin_product="posthog_ai")
+PI_PROFILE = replace(BUSINESS_KNOWLEDGE, key="test.pi", runtimes=("pi",))
# Far enough back that any bump is unambiguously forward, and stable so a test can assert the
# clock did *not* move.
@@ -326,16 +328,28 @@ class TestTask(TestCase):
@parameterized.expand(
[
- ("runtime", TaskRunOptions(runtime="pi"), ("pi", [])),
+ ("runtime", TaskRunOptions(runtime="pi"), None, ("pi", [])),
(
"repositories",
TaskRunOptions(repositories=(Repository(name="posthog/posthog"),)),
+ None,
("acp", ["posthog/posthog"]),
),
+ ("profile_runtime", None, PI_PROFILE, ("pi", [])),
+ (
+ "profile_runtime_with_options",
+ TaskRunOptions(repositories=(Repository(name="posthog/posthog"),)),
+ PI_PROFILE,
+ ("pi", ["posthog/posthog"]),
+ ),
]
)
def test_create_without_run_applies_task_settings(
- self, name: str, options: TaskRunOptions, expected: tuple[str, list[str]]
+ self,
+ name: str,
+ options: TaskRunOptions | None,
+ profile: TaskProfile | None,
+ expected: tuple[str, list[str]],
) -> None:
user = User.objects.create(email=f"{name}@test.com")
Integration.objects.create(team=self.team, kind="github", config={})
@@ -345,7 +359,7 @@ class TestTask(TestCase):
title="t",
description="d",
user_id=user.id,
- origin_product=Task.OriginProduct.USER_CREATED,
+ **({"profile": profile} if profile else {"origin_product": Task.OriginProduct.USER_CREATED}),
options=options,
)
@@ -679,6 +693,45 @@ class TestTask(TestCase):
self.assertIsNone(task.github_integration)
self.assertIsNone(task.github_user_integration)
+ @parameterized.expand(
+ [
+ ("runtime", PI_PROFILE, None, ("pi", "default_base")),
+ ("runtime_also_in_options", PI_PROFILE, TaskRunOptions(runtime="pi"), ("pi", "default_base")),
+ (
+ "template",
+ replace(BUSINESS_KNOWLEDGE, key="test.autoresearch", sandbox_templates=("autoresearch_base",)),
+ None,
+ ("acp", "autoresearch_base"),
+ ),
+ ]
+ )
+ @patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
+ def test_create_and_run_takes_the_default_runtime_and_template_from_the_profile(
+ self,
+ _name: str,
+ profile: TaskProfile,
+ options: TaskRunOptions | None,
+ expected: tuple[str, str],
+ mock_execute_workflow,
+ ):
+ user = User.objects.create(email="[email protected]")
+
+ with (
+ patch.dict("products.tasks.backend.facade.profiles._profiles", {profile.key: profile}),
+ self.captureOnCommitCallbacks(execute=True),
+ ):
+ task = Task.create_and_run(
+ team=self.team,
+ title="Profile defaults",
+ description="Picks no runtime or template",
+ profile=profile,
+ user_id=user.id,
+ options=options,
+ )
+
+ state = TaskRun.objects.get(id=mock_execute_workflow.call_args.kwargs["run_id"]).state
+ self.assertEqual((task.runtime, state.get("sandbox_template")), expected)
+
@patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
def test_create_run_keeps_the_previous_template_and_refuses_a_forbidden_one(self, mock_execute_workflow):
user = User.objects.create(email="[email protected]")