Skip to content

Conversation

@ssncferreira
Copy link
Contributor

@ssncferreira ssncferreira commented Nov 10, 2025

Problem

With the new tasks data model, a task starts with an initializing status. However, the API returns current_state: null to represent the agent state, causing the frontend to display "No message available". This PR updates codersdk.Task to return a current_state when the task is initializing with meaningful messages about what's happening during task initialization.

Previous message

Screenshot 2025-11-07 at 09 06 13

New message

Screenshot 2025-11-12 at 11 00 15

Changes

  • Populate current_state with descriptive initialization messages when task status is initializing and no valid app status exists for the current build
  • dbfake: Fix WorkspaceBuild builder to properly handle pending/running jobs by linking tasks without requiring agent/app resources

Note: UI Storybook changes to reflect these new messages will be addressed in a follow-up PR.

Closes: coder/internal#1063

@ssncferreira ssncferreira marked this pull request as ready for review November 12, 2025 11:05
assert.NotEqual(t, "all done", updated.CurrentState.Message)
})

t.Run("InitializingAgentState", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracting the function will mean we can test it on its own and only need to test a couple cases here

Copy link
Contributor Author

@ssncferreira ssncferreira Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial idea was to unit test taskFromDBTaskAndWorkspace, however, the packages are different (coderd and coderd_test). The only way to unit test the extracted function would be to either make it public or create a separate test file with package coderd, but that goes against the current approach for other test files in this directory 😕 Unless I'm missing something really basic 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to test an unexported function, you can create aitasks_internal_test.go with package coderd and test it there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to test an unexported function, you can create aitasks_internal_test.go with package coderd and test it there.

Completely forgot about the pattern with _internal_ files 🤦‍♀️ Addressed in: b1cb7fe

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be pretty cool if we could add these messages to the app status at the time they happen (attached with a timestamp), that way they could be viewed in history/timeline of the task as well. That's a much larger refactor though and this is fine for now, nice clean implementation. 👍🏻

message = "Agent is starting"
default:
message = "Initializing workspace agent"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we take into account the coder app status as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean LatestAppStatus? I thought about adding different messages based on the app status state, for example, if LatestAppStatus.State == idle, the message could be "Agent is idle", or for completed/failed states, we could provide more detailed information to users. Or were you thinking of something else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about WorkspaceAppHealth, specifically WorkspaceAppHealthInitializing. If disabled we can ignore it.

I'm not sure if we need to enrich LatestAppStatus if there already exists an entry 🤔. I imagine users could want to tweak that via having the agent give some context via the mcp report tool.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about WorkspaceAppHealth, specifically WorkspaceAppHealthInitializing. If disabled we can ignore it.

Ah, I see. So this would be the case where the agent is ready but the app is still initializing, correct? Do you think it makes sense to get into that level of detail? We have different possible AppHealth statuses:

WorkspaceAppHealthDisabled     WorkspaceAppHealth = "disabled"
WorkspaceAppHealthInitializing WorkspaceAppHealth = "initializing"
WorkspaceAppHealthHealthy      WorkspaceAppHealth = "healthy"
WorkspaceAppHealthUnhealthy    WorkspaceAppHealth = "unhealthy"

I'm guessing only the initializing here would make sense, because the others would probably affect the overall task status, correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this would be the case where the agent is ready but the app is still initializing, correct?

Yep! 👍🏻

Do you think it makes sense to get into that level of detail?

It would better reflect how we determine the tasks status in the tasks_with_status view.

I'm guessing only the initializing here would make sense, because the others would probably affect the overall task status, correct?

Correct. 👍🏻

We could consider unhealthy as well, but that would open a can of worms I'd prefer to avoid for now (messages for agent health, workspace build failure, etc).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in b1cb7fe

@ssncferreira
Copy link
Contributor Author

It would be pretty cool if we could add these messages to the app status at the time they happen (attached with a timestamp), that way they could be viewed in history/timeline of the task as well. That's a much larger refactor though and this is fine for now, nice clean implementation. 👍🏻

That would be nice, but it would require more changes. The way I see it, we have 2 stages of initialization here:

  1. Provisioning stage: when the workspace is still pending or starting and the agent doesn't even exist yet
  2. Agent initializing: when the agent is connecting/starting

For phase 1, we cannot insert a workspace_app_status because at this stage we don't have an agent or app yet, meaning that agent_id and app_id would be null, which is not supported by the database schema.

For phase 2, we could insert workspace app statuses by having the agent call PatchAppStatus whenever it transitions to Created or Starting lifecycle states. This would give us a proper timeline/history for the agent initialization phases, which would be a nice addition.

The implementation in this PR is simpler and works for both phases, but now that I think about it, I'm not sure if it's hackier compared to properly reporting statuses...We could report statuses for phase 2 (agent initialization) and keep the current implementation for phase 1 (provisioning).
@mafredri Would that be a better approach, or should we keep it as is for now and refactor later if needed?

@mafredri
Copy link
Member

mafredri commented Nov 12, 2025

@ssncferreira let's keep it like this for now, it's trivial enough of a change to redo later 👍🏻. Perhaps we can open up a new ticket to track the improvement.

Good point about phase 1, although I think it applies strictly to the pending phase and perhaps slightly past it. Once the workspace is starting the agent and apps should be created meaning we could update the status in the same place in the provisionerdserver where we perform update on the task_workspace_apps table.

I could see two ways for phase 2, first is as you propose, have the agent do it explicitly. The second is to do it behind the scenes inside coderd on agent lifecycle change (but perhaps that's too much spooky action at a distance?).

@ssncferreira
Copy link
Contributor Author

@ssncferreira let's keep it like this for now, it's trivial enough of a change to redo later 👍🏻. Perhaps we can open up a new ticket to track the improvement.

Good point about phase 1, although I think it applies strictly to the pending phase and perhaps slightly past it. Once the workspace is starting the agent and apps should be created meaning we could update the status in the same place in the provisionerdserver where we perform update on the task_workspace_apps table.

I could see two ways for phase 2, first is as you propose, have the agent do it explicitly. The second is to do it behind the scenes inside coderd on agent lifecycle change (but perhaps that's too much spooky action at a distance?).

Created issue #20749 to track this for future improvements 👍

Copy link
Contributor

@DanielleMaywood DanielleMaywood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me with one minor nit

message = "Initializing workspace applications"
}
default:
message = "Initializing workspace agent"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should never happen, but the fallback is fine, should we log something if this happens?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this should never happen, similarly to the "Initializing workspace applications" path. I think adding a comment here is enough for now. Addressed in 677339d

@ssncferreira ssncferreira merged commit 16b8e60 into main Nov 17, 2025
30 checks passed
@ssncferreira ssncferreira deleted the ssncferreira/fix-task-initializing-state-message branch November 17, 2025 13:24
@github-actions github-actions bot locked and limited conversation to collaborators Nov 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

task status is empty when workspace is starting or startup script is running

5 participants