feat: integrate agent context snapshots into chats (#26389)

Makes the chat context foundation from #26385 live. That PR added the
storage columns, writer queries, and a dormant
`agentapi.ContextDirtyMarker` trigger with no production callers; this
PR wires them together end to end.

When a workspace agent pushes a context snapshot, bound chats now
hydrate to that snapshot's hash, and a later push with a different hash
flips already-pinned chats to dirty (emitting a `context_dirty` watch
event after the transaction commits). Chat creation pins the agent's
latest snapshot when one already exists. The experimental chat API
exposes this as `Chat.Context` (`*ChatContext` with `dirty`,
`dirty_since`, `error`), and a new `PUT
/api/experimental/chats/{chat}/context` endpoint re-pins the agent's
latest snapshot and clears the dirty marker.

`context_dirty_resources` stays NULL (the resource-level diff is
deferred to the UI phase) and the live per-turn context pull is
unchanged.

The end-to-end test provisions a workspace agent via the echo
provisioner, connects it over the Agent API v2.10, and exercises the
full path: an initial push hydrates a bound chat (clean), a second push
with a different hash marks it dirty, the API reports the dirty state,
and the refresh endpoint clears it.

<details>
<summary>Decision log</summary>

- **API shape — sub-struct.** Dirty state is surfaced as
`codersdk.Chat.Context *ChatContext { Dirty bool; DirtySince *time.Time;
Error string }` rather than flat fields, matching the RFC's named
`ChatContext` type and leaving room for future fields (resource diff,
sources). `db2sdk.Chat` populates it when the chat is context-tracked
(`len(ContextAggregateHash) > 0`), dirty, or carries a snapshot error,
and leaves it nil (`omitempty`) otherwise. `Dirty` mirrors
`context_dirty_since` being set.
- **Marker wiring.** The chat daemon is injected directly as the
`agentapi.ContextDirtyMarker`. It is unconditionally constructed (only
its background worker is gated), so the marker is always non-nil and the
wiring matches every other `api.chatDaemon` call site. `agentapi` still
treats a nil marker as "chatd absent", so `PushContextState` stays a
pure write path for any future caller that does not wire chatd in.
- **Refresh is atomic.** `RefreshChatContext` reads the agent's latest
snapshot and re-pins the chat in one repeatable-read transaction, so a
concurrent push cannot land between the read and the write and leave the
chat pinned to a stale hash with the dirty marker cleared.
- **Hydrate + dirty run inside the push transaction.** The fan-out
shares the push's transaction so a concurrent refresh cannot interleave
with the version gate; `context_dirty` watch events publish only after
commit. The pinned hash on dirtied chats is intentionally left unchanged
— the refresh endpoint re-pins it.
- **Dirtied chats keep their pinned hash.** Drift is advisory: a dirty
chat stays usable, and refreshing is the only path that advances the
pinned hash.
- **Test binds `chats.agent_id` directly.** In production the binding is
set lazily during a chat turn (`chatd.persistBuildAgentBinding`); the
test sets it via `dbgen` so it exercises the context flow rather than
turn resolution.

Plan: `coderd/x/chatd` context integration + E2E (sub-struct API,
create-time + push-time hydration, refresh endpoint;
`context_dirty_resources` and the per-turn pull untouched).

</details>

🤖 Generated by Coder Agents on behalf of @kylecarbs
This commit is contained in:
Kyle Carberry
2026-06-16 17:46:47 +00:00
committed by GitHub
parent 9e7eedc9e9
commit bca0ce04ca
16 changed files with 1145 additions and 57 deletions
+434 -47
View File
@@ -36,6 +36,11 @@ Experimental: this endpoint is subject to change.
{}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -189,6 +194,10 @@ Status Code **200**
| `» build_id` | string(uuid) | false | | |
| `» children` | [codersdk.Chat](schemas.md#codersdkchat) | false | | Children holds child (subagent) chats nested under this root chat. Always initialized to an empty slice so the JSON field is present as []. Child chats cannot create their own subagents, so nesting depth is capped at 1 and this slice is always empty for child chats. |
| `» client_type` | [codersdk.ChatClientType](schemas.md#codersdkchatclienttype) | false | | |
| `» context` | [codersdk.ChatContext](schemas.md#codersdkchatcontext) | false | | Context reports the chat's pinned workspace-context state and whether it has drifted from the agent's latest pushed snapshot. Nil when the chat has no pinned context yet. |
| `»» dirty` | boolean | false | | Dirty is true when the agent's latest snapshot hash differs from the chat's pinned hash. |
| `»» dirty_since` | string(date-time) | false | | Dirty since is when drift was first detected; nil when not dirty. |
| `»» error` | string | false | | Error is the snapshot-level error copied from the pinned snapshot (empty when healthy). |
| `» created_at` | string(date-time) | false | | |
| `» diff_status` | [codersdk.ChatDiffStatus](schemas.md#codersdkchatdiffstatus) | false | | |
| `»» additions` | integer | false | | |
@@ -382,6 +391,11 @@ Experimental: this endpoint is subject to change.
"build_id": "bfb1f3fa-bf7b-43a5-9e0b-26cc050e44cb",
"children": [],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -516,6 +530,11 @@ Experimental: this endpoint is subject to change.
}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -801,6 +820,11 @@ Experimental: this endpoint is subject to change.
{}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -989,6 +1013,11 @@ Experimental: this endpoint is subject to change.
"build_id": "bfb1f3fa-bf7b-43a5-9e0b-26cc050e44cb",
"children": [],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -1123,6 +1152,11 @@ Experimental: this endpoint is subject to change.
}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -1311,62 +1345,18 @@ Experimental: this endpoint is subject to change.
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Get chat diff contents
## Refresh chat context
### Code samples
```shell
# Example request using curl
curl -X GET http://coder-server:8080/api/experimental/chats/{chat}/diff \
curl -X PUT http://coder-server:8080/api/experimental/chats/{chat}/context \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`GET /api/experimental/chats/{chat}/diff`
Experimental: this endpoint is subject to change.
### Parameters
| Name | In | Type | Required | Description |
|--------|------|--------------|----------|-------------|
| `chat` | path | string(uuid) | true | Chat ID |
### Example responses
> 200 Response
```json
{
"branch": "string",
"chat_id": "efc9fe20-a1e5-4a8c-9c48-f1b30c1e4f86",
"diff": "string",
"provider": "string",
"pull_request_url": "string",
"remote_origin": "string"
}
```
### Responses
| Status | Meaning | Description | Schema |
|--------|---------------------------------------------------------|-------------|------------------------------------------------------------------|
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.ChatDiffContents](schemas.md#codersdkchatdiffcontents) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Interrupt chat
### Code samples
```shell
# Example request using curl
curl -X POST http://coder-server:8080/api/experimental/chats/{chat}/interrupt \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`POST /api/experimental/chats/{chat}/interrupt`
`PUT /api/experimental/chats/{chat}/context`
Experimental: this endpoint is subject to change.
@@ -1392,6 +1382,11 @@ Experimental: this endpoint is subject to change.
"build_id": "bfb1f3fa-bf7b-43a5-9e0b-26cc050e44cb",
"children": [],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -1526,6 +1521,378 @@ Experimental: this endpoint is subject to change.
}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
"approved": true,
"author_avatar_url": "string",
"author_login": "string",
"base_branch": "string",
"changed_files": 0,
"changes_requested": true,
"chat_id": "efc9fe20-a1e5-4a8c-9c48-f1b30c1e4f86",
"commits": 0,
"deletions": 0,
"head_branch": "string",
"pr_number": 0,
"pull_request_draft": true,
"pull_request_state": "string",
"pull_request_title": "string",
"refreshed_at": "2019-08-24T14:15:22Z",
"reviewer_count": 0,
"stale_at": "2019-08-24T14:15:22Z",
"url": "string"
},
"files": [
{
"created_at": "2019-08-24T14:15:22Z",
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"mime_type": "string",
"name": "string",
"organization_id": "7c60d51f-b44e-4682-87d6-449835ea4de6",
"owner_id": "8826ee2e-7933-4665-aef2-2393f84a0d05"
}
],
"has_unread": true,
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"labels": {
"property1": "string",
"property2": "string"
},
"last_error": {
"detail": "string",
"kind": "generic",
"message": "string",
"provider": "string",
"retryable": true,
"status_code": 0
},
"last_injected_context": [
{
"args": [
0
],
"args_delta": "string",
"completed_at": "2019-08-24T14:15:22Z",
"content": "string",
"context_file_agent_id": {
"uuid": "string",
"valid": true
},
"context_file_content": "string",
"context_file_directory": "string",
"context_file_os": "string",
"context_file_path": "string",
"context_file_skill_meta_file": "string",
"context_file_truncated": true,
"created_at": "2019-08-24T14:15:22Z",
"data": [
0
],
"end_line": 0,
"file_id": {
"uuid": "string",
"valid": true
},
"file_name": "string",
"is_error": true,
"is_media": true,
"mcp_server_config_id": {
"uuid": "string",
"valid": true
},
"media_type": "string",
"name": "string",
"parsed_commands": [
[
"string"
]
],
"provider_executed": true,
"provider_metadata": [
0
],
"result": [
0
],
"result_delta": "string",
"result_reset": true,
"signature": "string",
"skill_description": "string",
"skill_dir": "string",
"skill_name": "string",
"source_id": "string",
"start_line": 0,
"text": "string",
"title": "string",
"tool_call_id": "string",
"tool_name": "string",
"type": "text",
"url": "string"
}
],
"last_model_config_id": "30ebb95f-c255-4759-9429-89aa4ec1554c",
"last_turn_summary": "string",
"mcp_server_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"organization_id": "7c60d51f-b44e-4682-87d6-449835ea4de6",
"owner_id": "8826ee2e-7933-4665-aef2-2393f84a0d05",
"owner_name": "string",
"owner_username": "string",
"parent_chat_id": "c3609ee6-3b11-4a93-b9ae-e4fabcc99359",
"pin_order": 0,
"plan_mode": "plan",
"root_chat_id": "2898031c-fdce-4e3e-8c53-4481dd42fcd7",
"shared": true,
"status": "waiting",
"title": "string",
"updated_at": "2019-08-24T14:15:22Z",
"warnings": [
"string"
],
"workspace_id": "0967198e-ec7b-4c6b-b4d3-f71244cadbe9"
}
```
### Responses
| Status | Meaning | Description | Schema |
|--------|---------------------------------------------------------|-------------|------------------------------------------|
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.Chat](schemas.md#codersdkchat) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Get chat diff contents
### Code samples
```shell
# Example request using curl
curl -X GET http://coder-server:8080/api/experimental/chats/{chat}/diff \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`GET /api/experimental/chats/{chat}/diff`
Experimental: this endpoint is subject to change.
### Parameters
| Name | In | Type | Required | Description |
|--------|------|--------------|----------|-------------|
| `chat` | path | string(uuid) | true | Chat ID |
### Example responses
> 200 Response
```json
{
"branch": "string",
"chat_id": "efc9fe20-a1e5-4a8c-9c48-f1b30c1e4f86",
"diff": "string",
"provider": "string",
"pull_request_url": "string",
"remote_origin": "string"
}
```
### Responses
| Status | Meaning | Description | Schema |
|--------|---------------------------------------------------------|-------------|------------------------------------------------------------------|
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.ChatDiffContents](schemas.md#codersdkchatdiffcontents) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Interrupt chat
### Code samples
```shell
# Example request using curl
curl -X POST http://coder-server:8080/api/experimental/chats/{chat}/interrupt \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`POST /api/experimental/chats/{chat}/interrupt`
Experimental: this endpoint is subject to change.
### Parameters
| Name | In | Type | Required | Description |
|--------|------|--------------|----------|-------------|
| `chat` | path | string(uuid) | true | Chat ID |
### Example responses
> 200 Response
```json
{
"agent_id": "2b1e3b65-2c04-4fa2-a2d7-467901e98978",
"archived": true,
"build_id": "bfb1f3fa-bf7b-43a5-9e0b-26cc050e44cb",
"children": [
{
"agent_id": "2b1e3b65-2c04-4fa2-a2d7-467901e98978",
"archived": true,
"build_id": "bfb1f3fa-bf7b-43a5-9e0b-26cc050e44cb",
"children": [],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
"approved": true,
"author_avatar_url": "string",
"author_login": "string",
"base_branch": "string",
"changed_files": 0,
"changes_requested": true,
"chat_id": "efc9fe20-a1e5-4a8c-9c48-f1b30c1e4f86",
"commits": 0,
"deletions": 0,
"head_branch": "string",
"pr_number": 0,
"pull_request_draft": true,
"pull_request_state": "string",
"pull_request_title": "string",
"refreshed_at": "2019-08-24T14:15:22Z",
"reviewer_count": 0,
"stale_at": "2019-08-24T14:15:22Z",
"url": "string"
},
"files": [
{
"created_at": "2019-08-24T14:15:22Z",
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"mime_type": "string",
"name": "string",
"organization_id": "7c60d51f-b44e-4682-87d6-449835ea4de6",
"owner_id": "8826ee2e-7933-4665-aef2-2393f84a0d05"
}
],
"has_unread": true,
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"labels": {
"property1": "string",
"property2": "string"
},
"last_error": {
"detail": "string",
"kind": "generic",
"message": "string",
"provider": "string",
"retryable": true,
"status_code": 0
},
"last_injected_context": [
{
"args": [
0
],
"args_delta": "string",
"completed_at": "2019-08-24T14:15:22Z",
"content": "string",
"context_file_agent_id": {
"uuid": "string",
"valid": true
},
"context_file_content": "string",
"context_file_directory": "string",
"context_file_os": "string",
"context_file_path": "string",
"context_file_skill_meta_file": "string",
"context_file_truncated": true,
"created_at": "2019-08-24T14:15:22Z",
"data": [
0
],
"end_line": 0,
"file_id": {
"uuid": "string",
"valid": true
},
"file_name": "string",
"is_error": true,
"is_media": true,
"mcp_server_config_id": {
"uuid": "string",
"valid": true
},
"media_type": "string",
"name": "string",
"parsed_commands": [
[
"string"
]
],
"provider_executed": true,
"provider_metadata": [
0
],
"result": [
0
],
"result_delta": "string",
"result_reset": true,
"signature": "string",
"skill_description": "string",
"skill_dir": "string",
"skill_name": "string",
"source_id": "string",
"start_line": 0,
"text": "string",
"title": "string",
"tool_call_id": "string",
"tool_name": "string",
"type": "text",
"url": "string"
}
],
"last_model_config_id": "30ebb95f-c255-4759-9429-89aa4ec1554c",
"last_turn_summary": "string",
"mcp_server_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"organization_id": "7c60d51f-b44e-4682-87d6-449835ea4de6",
"owner_id": "8826ee2e-7933-4665-aef2-2393f84a0d05",
"owner_name": "string",
"owner_username": "string",
"parent_chat_id": "c3609ee6-3b11-4a93-b9ae-e4fabcc99359",
"pin_order": 0,
"plan_mode": "plan",
"root_chat_id": "2898031c-fdce-4e3e-8c53-4481dd42fcd7",
"shared": true,
"status": "waiting",
"title": "string",
"updated_at": "2019-08-24T14:15:22Z",
"warnings": [
"string"
],
"workspace_id": "0967198e-ec7b-4c6b-b4d3-f71244cadbe9"
}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -2316,6 +2683,11 @@ Experimental: this endpoint is subject to change.
"build_id": "bfb1f3fa-bf7b-43a5-9e0b-26cc050e44cb",
"children": [],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -2450,6 +2822,11 @@ Experimental: this endpoint is subject to change.
}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -2998,6 +3375,11 @@ Experimental: this endpoint is subject to change.
"build_id": "bfb1f3fa-bf7b-43a5-9e0b-26cc050e44cb",
"children": [],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -3132,6 +3514,11 @@ Experimental: this endpoint is subject to change.
}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
+37 -3
View File
@@ -1925,6 +1925,11 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
"build_id": "bfb1f3fa-bf7b-43a5-9e0b-26cc050e44cb",
"children": [],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -2059,6 +2064,11 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -2202,6 +2212,7 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
| `build_id` | string | false | | |
| `children` | array of [codersdk.Chat](#codersdkchat) | false | | Children holds child (subagent) chats nested under this root chat. Always initialized to an empty slice so the JSON field is present as []. Child chats cannot create their own subagents, so nesting depth is capped at 1 and this slice is always empty for child chats. |
| `client_type` | [codersdk.ChatClientType](#codersdkchatclienttype) | false | | |
| `context` | [codersdk.ChatContext](#codersdkchatcontext) | false | | Context reports the chat's pinned workspace-context state and whether it has drifted from the agent's latest pushed snapshot. Nil when the chat has no pinned context yet. |
| `created_at` | string | false | | |
| `diff_status` | [codersdk.ChatDiffStatus](#codersdkchatdiffstatus) | false | | |
| `files` | array of [codersdk.ChatFileMetadata](#codersdkchatfilemetadata) | false | | |
@@ -2327,6 +2338,24 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
| `acquire_batch_size` | integer | false | | |
| `debug_logging_enabled` | boolean | false | | |
## codersdk.ChatContext
```json
{
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
}
```
### Properties
| Name | Type | Required | Restrictions | Description |
|---------------|---------|----------|--------------|------------------------------------------------------------------------------------------|
| `dirty` | boolean | false | | Dirty is true when the agent's latest snapshot hash differs from the chat's pinned hash. |
| `dirty_since` | string | false | | Dirty since is when drift was first detected; nil when not dirty. |
| `error` | string | false | | Error is the snapshot-level error copied from the pinned snapshot (empty when healthy). |
## codersdk.ChatDiffContents
```json
@@ -3748,6 +3777,11 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
{}
],
"client_type": "ui",
"context": {
"dirty": true,
"dirty_since": "2019-08-24T14:15:22Z",
"error": "string"
},
"created_at": "2019-08-24T14:15:22Z",
"diff_status": {
"additions": 0,
@@ -3909,9 +3943,9 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
#### Enumerated Values
| Value(s) |
|------------------------------------------------------------------------------------------------------------------|
| `action_required`, `created`, `deleted`, `diff_status_change`, `status_change`, `summary_change`, `title_change` |
| Value(s) |
|-----------------------------------------------------------------------------------------------------------------------------------|
| `action_required`, `context_dirty`, `created`, `deleted`, `diff_status_change`, `status_change`, `summary_change`, `title_change` |
## codersdk.ConnectionLatency