: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.
API
The HTTP surface — and, because of how PostHog's codegen works, considerably more than that.
These serializers are the source of truth for three downstream artifacts: the REST API itself, the generated frontend TypeScript types, and the 29 autoresearch-* MCP tools that the sandbox agent uses to drive its own training run.
A vague help_text here becomes a vague tool description that a model has to guess at. Treat serializer annotations as agent-facing documentation, because they are.
The autoresearch-* MCP tools that expose this API to the sandbox agent are declared in ../../mcp/tools.yaml.
What lives here
views/views.pyFive viewsets, registered in../routes.pyunderproject_autoresearch_*basenames and nested pipeline-first.AutoresearchPipelineViewSet— full CRUD plus the lifecycle actions:train,score,validate_online,archive,pause,resume, the pre-create helperstemplates,resolve-template,validate, the read-onlyonline_performance, which returns realized metrics per model per validated date from the validation runs, and the read-onlylimits, which returns the project's limits from../limits.pywith its current usage.AutoresearchTrainingRunViewSet— read plus create, and the agent's entire write surface:iterations,materialize-features,complete,artifacts,artifacts/upload,artifacts/get,artifacts/delete, plushistory.AutoresearchModelViewSet,AutoresearchRunViewSet— read-only.AutoresearchSuggestionViewSet— human or agent hypotheses, plusrespond.
views/serializers.pyRequest and response shapes, plusresolve_target(), which turns a pipeline'starget_eventortarget_definition(an action reference) into the resolved target the rest of the product uses. It refuses the product's ownautoresearch_predictionevent, and an action with a step that can match it, because the labeler and online validation exclude that event from every scan.
Access control
Every viewset sets scope_object = "autoresearch" and splits scope_object_read_actions / scope_object_write_actions, so custom actions are classified explicitly rather than inheriting a default.
AutoresearchAccessPermission gates on the autoresearch feature flag via ../access.py.
A new @action must be added to one of those two lists. Omitting it is not a compile error and not a test failure — it is a permissions bug.
The agent is a client here
This is the part that makes this package unusual. During a training run the sandbox agent is an authenticated API client, and AutoresearchTrainingRunViewSet is how it records everything it does:
agent server
│ POST training_runs/<id>/iterations → validated, stored as AutoresearchIteration
│ POST .../materialize-features → features + labels parquet into its sandbox
│ POST .../artifacts/upload → bundle files into object storage
│ POST .../complete → server-side champion selection
Consequences worth internalizing:
- Everything the agent sends is untrusted. SQL it submits gets executed; paths it supplies become storage keys.
iterationsruns it through../training/recipe_validation.pyandartifacts/uploadthroughnormalize_artifact_path()/MAX_ARTIFACT_BYTESin../training/artifacts.py. A bundle holds at mostMAX_BUNDLE_FILES(facade) files, andmodel.pklis reserved for the framework's fit. - The write surface closes when the run does.
iterations,materialize-features,complete,artifacts/upload, andartifacts/deleteall require the run to be RUNNING (or PENDING forcomplete), so a finished run's artifact bundle is frozen — scoring reads it, nothing rewrites it. Artifact reads stay open. Recording, uploading and deleting take the run row lock that completion takes, so the status check and the write see one state: an iteration cannot land on a completed run, and a bundle file cannot change after completion validated the bundle. Recording also refuses a newiteration_numberonce the run'siteration_budgetis used. - Nested writes are bound to the pipeline in the URL. A run id under another pipeline's route is a 404, the same as the nested reads.
completedoes not accept a champion. It triggerscomplete_training_run()in../training/promotion.py, which picks the champion server-side from the recorded iterations. The agent cannot promote itself.- A suggestion only moves forward.
respondrefuses a status below the current one (queued, picked up, then acted on or dismissed), refusesacted_onuntil an iteration is linked, and requires a reason to dismiss; recording an iteration withparent_suggestionis what advances a suggestion toacted_on. The same status again only updates the note. materialize-featuresis sandbox-scoped. It verifies the sandbox belongs to this training run before writing anything into it, resolves the sandbox provider from the persisted sandbox id, and writes each request's parquet into its own directory so overlapping requests cannot read each other's files. It runs caller-authored HogQL, so it needsquery:readon a scoped token and takes the ClickHouse throttles, like the pipeline viewset's query-backed actions.- If the agent cannot reach these tools, a run burns its full budget doing nothing and fails with
"Agent recorded no iterations before the run ended."That symptom is almost always MCP connectivity, not the model.
Where the rest of the system meets this package
- Routing —
../routes.py(register_routes), nested pipeline → models / runs / training_runs / suggestions. - Frontend types — generated into
../../frontend/generated/via drf-spectacular + Orval. Never hand-edit those; change the serializer and regenerate withhogli build:openapi. - Calls into —
../training/(train, complete),../inference/(score),../evaluation/(validate_online),../dataset/(validate, templates,resolve_target).
Declare the response when it differs from the request
create validates with AutoresearchPipelineCreateSerializer but responds with AutoresearchPipelineSerializer. drf-spectacular infers responses from get_serializer_class(), so without an explicit responses= it documents the write shape as the response.
That broke the MCP build: autoresearch-create sets enrich_url: '{id}', the generated handler read result.id, and the declared type had no id. Now pinned with @extend_schema(responses={201: AutoresearchPipelineSerializer}).
Any action whose response shape differs from its request needs the same treatment — the mismatch is invisible in Python and only shows up as a TypeScript error two codegen steps downstream.
When editing this flow
- Annotate everything.
help_texton fields,@extend_schemaon custom actions. These flow straight into MCP tool schemas and generated types; an unannotated action produces an empty schema an agent cannot use. - Classify every new
@actionintoscope_object_read_actionsorscope_object_write_actions. - Validate everything the agent can write, in the viewset, before it reaches storage or the query engine.
- Regenerate after serializer changes (
hogli build:openapi) and commit the generated files — CI checks for drift. - Keep champion selection out of this layer. Endpoints record and trigger;
../training/promotion.pydecides. - If you add an endpoint or change the agent's write surface, update this file to match.