Skip to content

feat: bump workspace last_used_at on chat heartbeat#23205

Merged
johnstcn merged 4 commits intomainfrom
cj/chat-workspace-usage-tracking
Mar 18, 2026
Merged

feat: bump workspace last_used_at on chat heartbeat#23205
johnstcn merged 4 commits intomainfrom
cj/chat-workspace-usage-tracking

Conversation

@johnstcn
Copy link
Member

@johnstcn johnstcn commented Mar 18, 2026

  • coderd: Wires options.WorkspaceUsageTracker into the chatd config.
  • chatd: Adds UsageTracker and calls UsageTracker.Add(workspaceID) on each heartbeat tick
  • chatd: adds tests to verify last_used_at bump behaviour

🤖 This PR was created with the help of Coder Agents, and will be reviewed by my human. 🧑‍💻

@johnstcn johnstcn changed the title feat(chatd): bump workspace last_used_at on chat heartbeat feat: bump workspace last_used_at on chat heartbeat Mar 18, 2026
@johnstcn johnstcn force-pushed the cj/chat-workspace-usage-tracking branch 5 times, most recently from d1214fe to 85f5a58 Compare March 18, 2026 11:54
Copy link
Member Author

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

ready for review

@johnstcn johnstcn marked this pull request as ready for review March 18, 2026 13:17
@johnstcn johnstcn requested review from Copilot and mafredri March 18, 2026 13:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR wires the workspace usage tracker into the chat daemon so that active chats can bump a workspace’s last_used_at via periodic heartbeats, helping prevent workspaces from being considered idle while a chat is running.

Changes:

  • Pass WorkspaceUsageTracker into chatd.New from coderd.New.
  • Extend chatd.Server configuration with a configurable heartbeat interval plus an injectable clock, and bump workspace usage on each heartbeat tick.
  • Add end-to-end tests validating that heartbeats bump workspace usage when a chat is linked to a workspace, and do not when it isn’t.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
coderd/coderd.go Plumbs the existing WorkspaceUsageTracker into the chat daemon config.
coderd/chatd/chatd.go Adds heartbeat configuration/clock injection and bumps usage on heartbeat ticks.
coderd/chatd/chatd_test.go Adds coverage for usage bumping behavior with/without linked workspaces.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Wires through UsageTracker to update chat workspace last_used_at
on heartbeat.
Wires UsageTracker into the chatd Server so that while a chat is
actively processing, the linked workspace's last_used_at is kept
fresh. This prevents premature dormancy/deletion of workspaces
that are being used by agentic chats.

Only last_used_at is bumped (via UsageTracker.Add), not the autostop
deadline. Chat activity alone should not extend build deadlines.

Also makes the chat heartbeat interval configurable via
Config.ChatHeartbeatInterval (default 30s) to support testing.
@johnstcn johnstcn force-pushed the cj/chat-workspace-usage-tracking branch 2 times, most recently from 672a52a to ce92280 Compare March 18, 2026 17:08
@coder-tasks

This comment was marked as outdated.

@johnstcn
Copy link
Member Author

Documentation Check

Updates Needed

  • docs/user-guides/workspace-scheduling.md — The "What counts as workspace activity?" section should mention that active Coder AI chat sessions (built-in chat via chatd) prevent workspace dormancy by bumping last_used_at on each heartbeat. This is distinct from the existing "AI agent task status" entry, which extends the autostop deadline. Chat heartbeats only affect dormancy thresholds, not autostop.

Automated review via Coder Tasks

Good catch, but as chats are EA right now documenting this is premature.

@johnstcn johnstcn force-pushed the cj/chat-workspace-usage-tracking branch 6 times, most recently from 2341d5d to e638dff Compare March 18, 2026 17:47
// linking a workspace mid-conversation). The caller should store
// the returned value so that subsequent calls skip the DB lookup
// once a workspace has been found.
func (p *Server) trackWorkspaceUsage(
Copy link
Member Author

Choose a reason for hiding this comment

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

review: extracted this to its own function for clarity

@johnstcn
Copy link
Member Author

@mafredri should be ready for round 2 now

…workspaces

The heartbeat goroutine captured the chat parameter by value at
acquisition time. When create_workspace linked a workspace mid-
conversation, the heartbeat never saw the updated WorkspaceID
and never bumped last_used_at.

Now, when WorkspaceID is not yet valid, the heartbeat re-reads the
chat from the DB to pick up late associations. Once valid, the
local copy is cached and no further reads are needed.

The test is updated to cover the normal flow: chat created without
a workspace, workspace linked later via UpdateChatWorkspace.
@johnstcn johnstcn force-pushed the cj/chat-workspace-usage-tracking branch from e638dff to 32a1210 Compare March 18, 2026 18:48
@johnstcn johnstcn merged commit 14ed3e3 into main Mar 18, 2026
26 checks passed
@johnstcn johnstcn deleted the cj/chat-workspace-usage-tracking branch March 18, 2026 19:07
@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2026
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.

3 participants