Files
coder/codersdk/prebuilds.go
Kyle Carberry aaa0dacdb3 fix: infer workspace claim time from build history for /agents delete dialog (#25057)
Closes
[CODAGT-317](https://linear.app/codercom/issue/CODAGT-317/pr-workspaces-sometimes-require-name-confirmation-to-delete).

## Problem

The `/agents` archive-and-delete molly-guard (typing the workspace name)
was firing for chats that had clearly created their own workspace. The
heuristic in `resolveArchiveAndDeleteAction` decides whether
confirmation is needed by comparing the workspace's `created_at` against
the chat's `created_at`:

```ts
return new Date(workspaceCreatedAt) >= new Date(chatCreatedAt);
```

That assumption breaks for **prebuilt workspaces**.
`ClaimPrebuiltWorkspace` rewrites `owner_id`, `name`, `updated_at`,
`last_used_at`, etc., but **never touches `created_at`**, which still
reflects when the prebuild was provisioned by the reconciler, often
hours before the chat exists. Result: every prebuild-claimed workspace
looks pre-existing, so the molly-guard fires.

Concrete example from a real chat:

| Field | Value |
|---|---|
| `chat.created_at` | `2026-05-07T15:12:23Z` |
| `workspace.created_at` (provision) | `2026-05-07T14:22:24Z` |
| `latest_build.created_at` (claim) | `2026-05-07T15:19:09Z` |

`14:22:24 < 15:12:23` so `isWorkspaceAutoCreated` returned false even
though the chat issued the claim.

## Fix (frontend-only)

Derive the moment a workspace was acquired from existing build history
rather than relying on `workspace.created_at`:

- Build #1 initiator = prebuilds system user → workspace was a prebuild
→ use `build_2.created_at` (the claim build) as the acquisition time.
- Build #1 initiator = real user → workspace was created from scratch →
use `workspace.created_at` (unchanged behavior).
- Unclaimed prebuild or no build history → return `null` (force
confirmation; safe degradation for a destructive flow).

The resolver fetches the build list via the existing
`getWorkspaceBuilds` endpoint when the dialog might fire. No new column,
no migration, no schema change. Works retroactively for all existing
claimed prebuilds; no backfill needed.

The prebuilds system user UUID is exposed via
`codersdk.PrebuildsSystemUserID` and typegen'd to `typesGenerated.ts`.
`coderd/database.PrebuildsSystemUserID` parses that constant via
`uuid.MustParse` so the two cannot drift; if the codersdk literal ever
changes, package init fails fast.

## History

The first draft of this PR added a `workspaces.claimed_at` column
populated by `ClaimPrebuiltWorkspace`. After review feedback from
@johnstcn pointing out that the same fact is already implicit in build
history, I pivoted to the frontend-only approach. Subsequent review
notes consolidated the prebuilds system user UUID into a single
typegen'd constant.

## Why not the other open PRs

- **#25055** (`chatKey` cache fallback) only fixes a different
cache-miss path; it explicitly notes it does not address `created_at <
chat.created_at`.
- **#25053** (`chats.workspace_auto_created` boolean) puts the truth on
the wrong side of the schema: "this workspace was claimed at time T" is
a property of the workspace, not the chat. The MCP plumbing it adds is
also unnecessary now that the same answer is available from build
history.

## Test plan

- `pnpm vitest run --project=unit
src/pages/AgentsPage/utils/agentWorkspaceUtils.test.ts` — 40/40 pass;
new cases cover prebuild claim before/after chat, unclaimed prebuild,
missing-build-history fallback, and the fetch-skip when the chat is not
in cache.
- `pnpm lint:types`, `pnpm check`, `make pre-commit`.

<details>
<summary>Disclosure</summary>

Opened on behalf of @kylecarbs by [Coder
Agents](https://coder.com/coder-agents).
</details>
2026-05-10 11:04:55 -04:00

52 lines
1.6 KiB
Go

package codersdk
import (
"context"
"encoding/json"
"net/http"
)
// PrebuildsSystemUserID is the UUID of the Coder prebuilds system
// user. Prebuilt workspaces are owned by this user until they are
// claimed; build #1 of a claimed workspace remains attributed to
// this user as the initiator forever, which is how callers can
// recognize a prebuild claim after the fact.
const PrebuildsSystemUserID = "c42fdf75-3097-471c-8c33-fb52454d81c0"
type PrebuildsSettings struct {
ReconciliationPaused bool `json:"reconciliation_paused"`
}
// GetPrebuildsSettings retrieves the prebuilds settings, which currently just describes whether all
// prebuild reconciliation is paused.
func (c *Client) GetPrebuildsSettings(ctx context.Context) (PrebuildsSettings, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/prebuilds/settings", nil)
if err != nil {
return PrebuildsSettings{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return PrebuildsSettings{}, ReadBodyAsError(res)
}
var settings PrebuildsSettings
return settings, json.NewDecoder(res.Body).Decode(&settings)
}
// PutPrebuildsSettings modifies the prebuilds settings, which currently just controls whether all
// prebuild reconciliation is paused.
func (c *Client) PutPrebuildsSettings(ctx context.Context, settings PrebuildsSettings) error {
res, err := c.Request(ctx, http.MethodPut, "/api/v2/prebuilds/settings", settings)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotModified {
return nil
}
if res.StatusCode != http.StatusOK {
return ReadBodyAsError(res)
}
return nil
}