fix(agent): complete automation runs after end_turn#2141
Closed
annikaschmid wants to merge 2 commits into
Closed
Conversation
Contributor
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
packages/agent/src/server/question-relay.test.ts:554-667
**Parameterise these two test cases**
The team convention is to always prefer parameterised tests (`it.each`), and the two new cases share nearly identical setup — they only differ in the `state` value returned by `getTaskRun` and the resulting assertion on `updateTaskRunSpy`. Leaving them as separate `it` blocks duplicates ~50 lines and makes future maintenance harder. A single `it.each` over `[{ state: { automation_id: "automation-1" }, expectCompleted: true }, { state: {}, expectCompleted: false }]` (or similar) would express both behaviours without repeating the session-setup scaffolding.
### Issue 2 of 2
packages/agent/src/server/question-relay.test.ts:667
**Resume path not covered by tests**
`maybeCompleteAutomationRun` is called in both `sendInitialTaskMessage` and `sendResumeMessage` (lines 1279-1280 in `agent-server.ts`), but only the initial path is tested. A bug in the resume path — e.g. `taskRun` being `null` or the run already being in a terminal state — would go undetected. Adding at least one test that calls `sendResumeMessage` with an automation `state` would fully express the intended contract.
Reviews (1): Last reviewed commit: "fix(agent): complete automation runs aft..." | Re-trigger Greptile |
Contributor
|
Reviews (2): Last reviewed commit: "test(agent): address automation run revi..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Automation-triggered background runs on
us.posthog.comcan answer successfully and logInitial task message completed {"stopReason":"end_turn"}, but the task run remains non-terminal.Because
TaskAutomation.last_run_statuscollapses every non-terminal run torunning, the automation UI stays stuck on Running even after the answer has already been delivered.Root cause
AgentServeronly explicitly updates task run status for error stop reasons. On successfulend_turnit relays the answer, but it never patches the run tocompleted, so automation runs can remain open until inactivity timeout or another external completion signal.Fix
taskRun.state.automation_idend_turn, relay the response as before and then patch the run tocompletedWhy this is needed
Automations are one-shot scheduled/manual executions, not ongoing chat sessions. Once an automation has produced its final answer, the backend should move it to a terminal state immediately so downstream surfaces report Success instead of being stuck on Running.
Validation
pnpm exec vitest run src/server/question-relay.test.tspnpm exec tsc --noEmitpnpm exec biome check packages/agent/src/server/agent-server.ts packages/agent/src/server/question-relay.test.ts