mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: persist agent-pushed workspace context snapshots in coderd (#26145)
Replaces the v2.10 `PushContextState` stub with a real coderd write path. Phase 1 of the chat-side persistence story; nothing reads these rows yet. Follows [#25983](https://github.com/coder/coder/pull/25983) and unblocks [CODAGT-569](https://linear.app/codercom/issue/CODAGT-569/enable-agent-api-v210-pushcontextstate-bump-currentminor-wire-coderd). ## What ships ### Schema (`000517_workspace_agent_context.{up,down}.sql`) Two new tables plus `api_key_scope` enum extensions: - `workspace_agent_context_snapshots` (PK `workspace_agent_id` to `workspace_agents(id) ON DELETE CASCADE`): one row per agent, overwritten per push. Holds `version`, `schema_version`, `aggregate_hash`, `snapshot_error`, `received_at`. - `workspace_agent_context_resources` (PK `(workspace_agent_id, source)`): per-resource state. `body_kind` and `status` are `TEXT` + `CHECK` so adding new wire kinds (the RFC's reserved PLUGIN/HOOK/SUBAGENT/COMMAND) is a one-line CHECK update plus a Go switch case. ### SQLC queries (`coderd/database/queries/workspaceagentcontext.sql`) - `UpsertWorkspaceAgentContextSnapshot` - `UpsertWorkspaceAgentContextResource` - `DeleteStaleWorkspaceAgentContextResources` (delete-where-source-not-in) - `GetLatestWorkspaceAgentContextSnapshot` - `ListWorkspaceAgentContextResources` ### Handler (`coderd/agentapi/context.go`) `ContextAPI` is a new sub-API. `PushContextState`: 1. Rejects `schema_version > 1` with a non-`Unimplemented` error so a forward-incompatible agent fails loudly during rollout instead of slipping into the permanent fallback path the `Unimplemented` translation reserves for old coderd deployments. 2. Validates resources: no empty/duplicate sources, every variant maps to a known body kind, every status maps to a known enum value, the `Body` oneof is set (even when status is non-OK, mirroring the wire guarantee so coderd can attribute failures to a known kind). 3. Inside `Database.InTx`, reads the existing snapshot. If the push is not `initial` and `version` is not strictly greater, returns `accepted = false` and leaves stored state untouched. Otherwise upserts the snapshot row, upserts each resource, then runs the stale-source prune so the snapshot and resource rows always agree. 4. Returns `accepted = true` on success. Resource bodies are stored as `protojson(body oneof variant)` in `body JSONB` with `body_kind` as the discriminator. Adding a new field to an existing variant is zero work since `protojson` tolerates new fields; adding a new variant is a CHECK + switch case. ### RBAC + dbauthz - New `ResourceWorkspaceAgentContext` (Create/Read/Update/Delete). - New `SubjectTypeAgentContext` plus `subjectAgentContext` system role and `dbauthz.AsAgentContext` helper. The push handler elevates to this subject; the agent's own role does not get direct write access to the table. - New `workspace_agent_context:*` API key scopes registered in the enum migration; internal-only (not added to `externalLowLevel`). ### Audit These rows are agent-pushed state, not user-authored. They are intentionally not added to `AuditActionMap` and not enumerated in `enterprise/audit/table.go`, matching `boundary_logs`, `workspace_agent_memory_resource_monitor`, etc. `enterprise/audit` tests pass unchanged. ## Tests - `coderd/agentapi/context_test.go`: 12 subtests covering accepts/rejects (schema version, empty/duplicate source, unknown status, missing body), version semantics (stale dropped, same-version replay dropped, `initial=true` overwrites lower version), variant coverage, non-OK status persistence, and the empty-active-set prune case. - `coderd/database/dbauthz/dbauthz_test.go`: 5 `MethodTestSuite` cases covering the new queries. - `coderd/rbac/roles_test.go`: `WorkspaceAgentContext` permission row asserting no human role currently has access. - `coderd/database/migrations/testdata/fixtures/000517_workspace_agent_context.up.sql`: one snapshot + one resource per known body kind plus a non-OK status, so the migration test suite never lands with these tables empty. ## Out of scope (later phases) - Chat hydration (`chats.context_aggregate_hash`, `last_injected_context`). - Dirty-bit fan-out and `PUT /chats/{id}/context`. - Agent-side `POST /api/v0/context/resync` barrier and the `coder exp chat context` CLI. - `codersdk` chat-context wire types and the dashboard Sources drawer. - Removal of the chatd per-turn pull fallback. ## Compat property This is a pure write path. If anything here returns errors the agent's `RunPush` loop backs off, no chat behavior changes, and the workspace keeps behaving exactly like it did before v2.10. <details> <summary>Implementation plan and decision log</summary> Key design calls: 1. **Concurrency**: Accept iff `req.Initial || req.Version > existing.Version`. The strict RFC reading ("version comparison is authoritative") locks restarted agents out because their per-process counter resets to 1; honoring `initial=true` reflects the real reboot reality while still rejecting steady-state replays/out-of-order pushes. 2. **Body encoding**: `protojson` over the oneof variant body proto, stored in JSONB with `body_kind` discriminator. Structured at the API/Go layer, schema-tolerant at the storage layer, and Phase 2 readers round-trip back via `protojson.Unmarshal`. 3. **Schema version rejection**: returns a normal error, not `Unimplemented`. The agent's `RunPush` loop only short-circuits on `Unimplemented`; that escape hatch is reserved for old coderd deployments. A forward-incompatible agent should retry-and-back-off, not flip the connection into permanent fallback. 4. **Validation strictness**: empty sources, duplicate sources, `STATUS_UNSPECIFIED`, and missing `Body` oneof variants are rejected before any write so a misbehaving agent cannot poison the snapshot table. Phase 2 readers can trust every row maps to a known proto variant. </details> _This PR was authored by Coder Agents on Kyle Carberry's behalf._
This commit is contained in:
@@ -514,14 +514,28 @@ WHERE
|
||||
AND deleted = FALSE;
|
||||
|
||||
-- name: DeleteWorkspaceSubAgentByID :exec
|
||||
UPDATE
|
||||
workspace_agents
|
||||
SET
|
||||
deleted = TRUE
|
||||
WHERE
|
||||
id = $1
|
||||
AND parent_id IS NOT NULL
|
||||
AND deleted = FALSE;
|
||||
-- Soft-deletes a single sub-agent (a child agent such as a devcontainer
|
||||
-- agent). Called from the DeleteSubAgent RPC when a sub-agent is torn
|
||||
-- down, which can happen mid-build without a full workspace rebuild.
|
||||
--
|
||||
-- Agent context rows are hard-deleted for the same reason as in
|
||||
-- SoftDeletePriorWorkspaceAgents: they only describe live agents, the
|
||||
-- rebuild-time soft-delete queries skip already-deleted agents, and
|
||||
-- agents are never hard-deleted, so the rows would otherwise orphan
|
||||
-- forever.
|
||||
WITH soft_deleted_agents AS (
|
||||
UPDATE workspace_agents
|
||||
SET deleted = TRUE
|
||||
WHERE id = @id
|
||||
AND parent_id IS NOT NULL
|
||||
AND deleted = FALSE
|
||||
RETURNING id
|
||||
), purged_context_resources AS (
|
||||
DELETE FROM workspace_agent_context_resources
|
||||
WHERE workspace_agent_id IN (SELECT id FROM soft_deleted_agents)
|
||||
)
|
||||
DELETE FROM workspace_agent_context_snapshots
|
||||
WHERE workspace_agent_id IN (SELECT id FROM soft_deleted_agents);
|
||||
|
||||
-- name: GetWorkspaceAgentsForMetrics :many
|
||||
SELECT
|
||||
@@ -577,17 +591,30 @@ LIMIT 1;
|
||||
-- provisionerdserver when a workspace build completes, after the new
|
||||
-- build's agents have been inserted, so running agents are not
|
||||
-- deleted while a build is still queued or provisioning.
|
||||
UPDATE workspace_agents
|
||||
SET deleted = TRUE
|
||||
WHERE id IN (
|
||||
SELECT wa.id
|
||||
FROM workspace_agents wa
|
||||
JOIN workspace_resources wr ON wr.id = wa.resource_id
|
||||
JOIN workspace_builds wb ON wb.job_id = wr.job_id
|
||||
WHERE wb.workspace_id = @workspace_id
|
||||
AND wb.id <> @current_build_id
|
||||
AND wa.deleted = FALSE
|
||||
);
|
||||
--
|
||||
-- Agent context rows (workspace_agent_context_snapshots and
|
||||
-- workspace_agent_context_resources) only describe live agents, and
|
||||
-- agents are never un-deleted, so they are hard-deleted here instead
|
||||
-- of accumulating alongside the soft-deleted agent rows.
|
||||
WITH soft_deleted_agents AS (
|
||||
UPDATE workspace_agents
|
||||
SET deleted = TRUE
|
||||
WHERE id IN (
|
||||
SELECT wa.id
|
||||
FROM workspace_agents wa
|
||||
JOIN workspace_resources wr ON wr.id = wa.resource_id
|
||||
JOIN workspace_builds wb ON wb.job_id = wr.job_id
|
||||
WHERE wb.workspace_id = @workspace_id
|
||||
AND wb.id <> @current_build_id
|
||||
AND wa.deleted = FALSE
|
||||
)
|
||||
RETURNING id
|
||||
), purged_context_resources AS (
|
||||
DELETE FROM workspace_agent_context_resources
|
||||
WHERE workspace_agent_id IN (SELECT id FROM soft_deleted_agents)
|
||||
)
|
||||
DELETE FROM workspace_agent_context_snapshots
|
||||
WHERE workspace_agent_id IN (SELECT id FROM soft_deleted_agents);
|
||||
|
||||
-- name: SoftDeleteWorkspaceAgentsByWorkspaceID :exec
|
||||
-- Marks every non-deleted agent belonging to the given workspace as
|
||||
@@ -595,13 +622,24 @@ WHERE id IN (
|
||||
-- itself is soft-deleted, so the agent instance-identity auth path
|
||||
-- (which filters on workspace_agents.deleted) doesn't keep seeing
|
||||
-- orphaned rows.
|
||||
UPDATE workspace_agents
|
||||
SET deleted = TRUE
|
||||
WHERE id IN (
|
||||
SELECT wa.id
|
||||
FROM workspace_agents wa
|
||||
JOIN workspace_resources wr ON wr.id = wa.resource_id
|
||||
JOIN workspace_builds wb ON wb.job_id = wr.job_id
|
||||
WHERE wb.workspace_id = @workspace_id
|
||||
AND wa.deleted = FALSE
|
||||
);
|
||||
--
|
||||
-- Agent context rows are hard-deleted for the same reason as in
|
||||
-- SoftDeletePriorWorkspaceAgents.
|
||||
WITH soft_deleted_agents AS (
|
||||
UPDATE workspace_agents
|
||||
SET deleted = TRUE
|
||||
WHERE id IN (
|
||||
SELECT wa.id
|
||||
FROM workspace_agents wa
|
||||
JOIN workspace_resources wr ON wr.id = wa.resource_id
|
||||
JOIN workspace_builds wb ON wb.job_id = wr.job_id
|
||||
WHERE wb.workspace_id = @workspace_id
|
||||
AND wa.deleted = FALSE
|
||||
)
|
||||
RETURNING id
|
||||
), purged_context_resources AS (
|
||||
DELETE FROM workspace_agent_context_resources
|
||||
WHERE workspace_agent_id IN (SELECT id FROM soft_deleted_agents)
|
||||
)
|
||||
DELETE FROM workspace_agent_context_snapshots
|
||||
WHERE workspace_agent_id IN (SELECT id FROM soft_deleted_agents);
|
||||
|
||||
Reference in New Issue
Block a user