X Tutup
Skip to content

feat: add healthy filter for workspace queries#21743

Merged
sreya merged 7 commits intomainfrom
filtering-k5qd
Feb 5, 2026
Merged

feat: add healthy filter for workspace queries#21743
sreya merged 7 commits intomainfrom
filtering-k5qd

Conversation

@sreya
Copy link
Collaborator

@sreya sreya commented Jan 29, 2026

Adds support for filtering workspaces by health status using healthy:true or healthy:false in the search query.

This is done by changing has-agent to accept a list of statuses and aliasing health:true to has-agent:connected and healthy:false to has-agent:timeout,disconnected.

Fixes #21623

sreya added 2 commits February 2, 2026 22:01
Adds support for filtering workspaces by health status using
healthy:true or healthy:false in the search query.

The filter only applies to running workspaces (status=running or unset).
A workspace is considered unhealthy if any of its agents are:
- Disconnected
- Timed out waiting to connect
- Lost connection (no recent heartbeat)
- In error or shutting down lifecycle states

Fixes #21623
- healthy:true translates to has-agent:connected
- healthy:false translates to has-agent:disconnected,timeout
- Changed has-agent to accept multiple statuses (text array)
- Removed duplicate healthy SQL block
@sreya sreya requested a review from Emyrk February 3, 2026 00:20
@sreya sreya marked this pull request as ready for review February 3, 2026 00:22
@coder-tasks
Copy link
Contributor

coder-tasks bot commented Feb 3, 2026

Documentation Check

Updates Needed

  • docs/user-guides/workspace-management.md - Add healthy filter to the list of supported workspace filters (line 56-71). The new filter should be documented as:

    • healthy - Filters workspaces based on agent health status. Use healthy:true to show workspaces with connected agents, or healthy:false to show workspaces with disconnected or timed out agents. Only applicable for workspaces in "start" transition.
  • docs/reference/api/workspaces.md - Update the q parameter description for GET /workspaces endpoint (line 986) to include healthy in the list of available filter keys. The current list shows: "owner, template, name, status, has-agent, dormant, last_used_after, last_used_before, has-ai-task, has_external_agent" and should be updated to include healthy.

Context

This PR adds support for filtering workspaces by health status using healthy:true or healthy:false. Internally, this translates to the existing has-agent filter values:

  • healthy:truehas-agent:connected
  • healthy:falsehas-agent:disconnected,timeout

The healthy filter provides a more user-friendly way to filter workspaces by agent health without needing to know the specific agent status values.


Automated review via Coder Tasks

Copy link
Contributor

@coder-tasks coder-tasks bot left a comment

Choose a reason for hiding this comment

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

Code Review

Reviewed PR #21743: "feat: add healthy filter for workspace queries"

The implementation looks good overall. The approach of aliasing healthy:true to has-agent:connected and healthy:false to has-agent:disconnected,timeout is clean and leverages the existing filtering infrastructure.

Key Observations

Strengths:

  • ✅ Changes has-agent from single string to array of statuses, which is more flexible
  • ✅ SQL query properly uses = ANY(@has_agent_statuses :: text[]) for array matching
  • ✅ Comprehensive test coverage for both healthy:true and healthy:false scenarios
  • ✅ Tests verify correct behavior with connected, disconnected, and timeout agent states
  • ✅ The healthy filter is implemented as an append operation, allowing it to combine with explicit has-agent filters

Potential Issues:

  1. Filter Combination Behavior (Minor): The current implementation appends statuses when healthy is used. This means:

    • has-agent:connected healthy:false would result in ["connected", "disconnected", "timeout"] - effectively matching all workspaces
    • This might be confusing but is probably acceptable as an edge case
    • Consider adding a test case to document this behavior or add validation to prevent conflicting filters
  2. Missing Test Coverage: No test cases verify the interaction between explicit has-agent filters and the new healthy filter when used together

Minor Suggestions

The code follows Coder's coding standards well. The implementation is clean and maintainable.

filter.HasAgentStatuses = append(filter.HasAgentStatuses, "connected")
} else {
filter.HasAgentStatuses = append(filter.HasAgentStatuses, "disconnected", "timeout")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Question about filter combination behavior:

When both has-agent and healthy filters are used together (e.g., has-agent:connecting healthy:true), they will be combined with append(), potentially creating conflicting filter conditions.

For example:

  • has-agent:connected healthy:false["connected", "disconnected", "timeout"]

This would match workspaces in any state, which might not be the user's intent.

Consider either:

  1. Adding validation to prevent conflicting combinations
  2. Documenting this behavior explicitly
  3. Adding a test case that demonstrates the expected behavior when both filters are used

Which behavior is intended?

Copy link
Member

Choose a reason for hiding this comment

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

If someone is mixing up things like this, just allow it all to be searched. Let's not try to guess what they really wanted.

Name: "HealthyMissing",
Query: "",
Expected: database.GetWorkspacesParams{
HasAgentStatuses: []string{},
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: Add test case for filter combination

Consider adding a test case to verify behavior when both has-agent and healthy are used together:

{
    Name:  "HealthyAndHasAgent",
    Query: "has-agent:connecting healthy:true",
    Expected: database.GetWorkspacesParams{
        HasAgentStatuses: []string{"connecting", "connected"},
    },
},

This would document the expected behavior when filters are combined.

Copy link
Member

Choose a reason for hiding this comment

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

Add this suggestion ☝️

@coder-tasks
Copy link
Contributor

coder-tasks bot commented Feb 3, 2026

Documentation Check

Updates Needed

  • docs/user-guides/workspace-management.md - Fix typo: "health" should be "healthy" in the filter name

The documentation correctly describes the feature, but uses inconsistent naming. The filter name should be healthy (matching the code implementation in coderd/searchquery/search.go:279) rather than health.

Current (incorrect):

- `health` - Only applicable for workspaces in "start" transition...

Should be:

- `healthy` - Only applicable for workspaces in "start" transition...

Automated review via Coder Tasks

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

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

LG, just add that test suggestion

Name: "HealthyMissing",
Query: "",
Expected: database.GetWorkspacesParams{
HasAgentStatuses: []string{},
Copy link
Member

Choose a reason for hiding this comment

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

Add this suggestion ☝️

@sreya sreya merged commit 22ece10 into main Feb 5, 2026
27 of 29 checks passed
@sreya sreya deleted the filtering-k5qd branch February 5, 2026 02:48
@github-actions github-actions bot locked and limited conversation to collaborators Feb 5, 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.

feat: support querying workspaces by health

2 participants

X Tutup