Files
coder/agent/agentcontext/drpc.go
T
Garrett DelfosseandGarrett Delfosse d1487f62f1 fix: resume workspace context push after coderd rejects it (#32534)
When coderd rejected a workspace context push, the agent kept resending
the same snapshot until it restarted, so fixing the bad file on disk did
not resume sync.

Each retry now rebuilds the request from the newest snapshot. coderd
returns `InvalidArgument` for validation failures, and the agent then
waits for the next change instead of retrying. `Initial` stays set until
a push succeeds. A new agent talking to an old coderd gets no code and
keeps retrying, now with the newest snapshot.

Fixes CODAGT-1640. Touches `agent/agentcontext/push.go`, as #31831 does.

<details>
<summary>Plan context</summary>

This is unit A1 of the Agent Plugins plan (CODAGT-1101, parent
CODAGT-1639): fix workspace-context bugs on main before adapting the
plugins stack, because plugins add more ways for coderd to reject a
push.

- Scope: retry with the newest snapshot when files change; coderd marks
validation errors as permanent so the agent waits for the next change.
- Not included: showing the rejection in the chat or the context panel.
- Verify: unit tests with a fake pusher and a mock clock. Revert: plain
revert, no data.

</details>

---

Generated by Coder Agents on behalf of @f0ssel.

---------

Co-authored-by: Garrett Delfosse <garrett@coder.com>
2026-11-24 13:14:03 +02:00

196 lines
6.5 KiB
Go

package agentcontext
import (
"context"
"golang.org/x/xerrors"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/structpb"
"storj.io/drpc/drpcerr"
agentproto "github.com/coder/coder/v2/agent/proto"
)
// DRPCPusher adapts a generated DRPCAgentClient to the
// agentcontext.Pusher interface. The adapter is the only place
// that knows about the wire protobuf types; the rest of the
// package operates on the Go Snapshot/Resource value types.
//
// Use NewDRPCPusher to construct an instance. The pusher's
// behavior is identical to invoking PushContextState directly:
// per-request retries are handled by Manager.RunPush.
type DRPCPusher struct {
client agentproto.DRPCAgentClient210
}
// NewDRPCPusher wraps the supplied drpc client. The client must
// implement the v2.10 Agent API.
func NewDRPCPusher(client agentproto.DRPCAgentClient210) *DRPCPusher {
return &DRPCPusher{client: client}
}
// PushContextState satisfies the Pusher interface.
//
// drpc returns an Unimplemented error when the peer's service
// definition does not include the RPC. The adapter translates
// that into ErrPushUnimplemented so RunPush stops gracefully
// when an old coderd is on the other end.
func (p *DRPCPusher) PushContextState(ctx context.Context, req *PushRequest) (*PushResponse, error) {
if p == nil || p.client == nil {
return nil, xerrors.New("agentcontext: DRPCPusher has no client")
}
resp, err := p.client.PushContextState(ctx, pushRequestToProto(req))
if err != nil {
switch drpcerr.Code(err) {
case drpcerr.Unimplemented:
return nil, ErrPushUnimplemented
case uint64(codes.InvalidArgument):
return nil, xerrors.Errorf("%s: %w", err.Error(), ErrPushInvalid)
}
return nil, err
}
return &PushResponse{Accepted: resp.GetAccepted()}, nil
}
// pushRequestToProto converts the Go push payload to its
// generated protobuf equivalent. The Kind on each Resource
// selects which body variant of the proto oneof is set; a body
// is always set (zero-valued if necessary) so coderd can tell
// the kind even when Status != OK.
func pushRequestToProto(req *PushRequest) *agentproto.PushContextStateRequest {
pb := &agentproto.PushContextStateRequest{
Version: req.Version,
AggregateHash: append([]byte(nil), req.AggregateHash[:]...),
Initial: req.Initial,
SnapshotError: req.SnapshotError,
Resources: make([]*agentproto.ContextResource, 0, len(req.Resources)),
}
for i := range req.Resources {
r := req.Resources[i]
entry := &agentproto.ContextResource{
Source: r.Source,
ContentHash: append([]byte(nil), r.ContentHash[:]...),
Status: resourceStatusToProto(r.Status),
SizeBytes: r.SizeBytes,
Error: r.Error,
}
setResourceBody(entry, r)
if r.SourcePath != "" {
sp := r.SourcePath
entry.SourcePath = &sp
}
pb.Resources = append(pb.Resources, entry)
}
return pb
}
// setResourceBody picks the proto oneof variant for r's Kind and
// populates the kind-specific fields from r. A body is set even
// when status is not OK so coderd can attribute the failure to a
// known kind. Unknown kinds leave the body unset; the recipient
// can surface that as "kind not recognized".
func setResourceBody(entry *agentproto.ContextResource, r Resource) {
switch r.Kind {
case KindInstructionFile:
entry.Body = &agentproto.ContextResource_InstructionFile{
InstructionFile: &agentproto.InstructionFileBody{
Content: append([]byte(nil), r.Payload...),
},
}
case KindSkill:
entry.Body = &agentproto.ContextResource_Skill{
Skill: &agentproto.SkillMetaBody{
Meta: append([]byte(nil), r.Payload...),
Name: r.Name,
Description: r.Description,
},
}
case KindACPHarness:
body := &agentproto.ACPHarnessBody{Slug: r.Source}
if h := r.ACPHarness; h != nil {
body.Slug, body.DisplayName = h.Slug, h.DisplayName
for _, opt := range h.ConfigOptions {
option := &agentproto.ACPConfigOption{Id: opt.ID, Name: opt.Name, Description: opt.Description, CurrentValue: opt.CurrentValue, Category: opt.Category}
for _, v := range opt.Values {
option.Values = append(option.Values, &agentproto.ACPConfigValue{Id: v.ID, Name: v.Name, Description: v.Description, Group: v.Group, GroupName: v.GroupName})
}
body.ConfigOptions = append(body.ConfigOptions, option)
}
}
entry.Body = &agentproto.ContextResource_AcpHarness{AcpHarness: body}
case KindMCPConfig:
// MCPConfigBody is intentionally empty: secrets in env
// blocks must not leave the agent.
entry.Body = &agentproto.ContextResource_McpConfig{
McpConfig: &agentproto.MCPConfigBody{},
}
case KindMCPServer:
entry.Body = &agentproto.ContextResource_McpServer{
McpServer: &agentproto.MCPServerBody{
ServerName: serverNameOrSource(r),
Description: r.Description,
Tools: mcpToolsToProto(r.Tools),
},
}
}
}
// serverNameOrSource returns r.Name when populated and falls
// back to r.Source so providers that have not yet adopted the
// Name field still produce a usable wire value.
func serverNameOrSource(r Resource) string {
if r.Name != "" {
return r.Name
}
return r.Source
}
// mcpToolsToProto converts the Go MCPTool slice to its wire
// representation. InputSchema is marshaled via structpb.NewStruct;
// schemas that fail to convert are dropped from the wire copy
// (the resource ContentHash still detects the change) and the
// tool ships with InputSchema unset rather than failing the
// whole push.
func mcpToolsToProto(in []MCPTool) []*agentproto.MCPTool {
if len(in) == 0 {
return nil
}
out := make([]*agentproto.MCPTool, 0, len(in))
for _, t := range in {
entry := &agentproto.MCPTool{
Name: t.Name,
Description: t.Description,
}
if len(t.InputSchema) > 0 {
if s, err := structpb.NewStruct(t.InputSchema); err == nil {
entry.InputSchema = s
}
}
out = append(out, entry)
}
return out
}
// resourceStatusToProto maps a ResourceStatus to its proto enum.
func resourceStatusToProto(s ResourceStatus) agentproto.ContextResource_Status {
switch s {
case StatusOK:
return agentproto.ContextResource_OK
case StatusOversize:
return agentproto.ContextResource_OVERSIZE
case StatusUnreadable:
return agentproto.ContextResource_UNREADABLE
case StatusInvalid:
return agentproto.ContextResource_INVALID
case StatusExcluded:
return agentproto.ContextResource_EXCLUDED
default:
return agentproto.ContextResource_STATUS_UNSPECIFIED
}
}
// Ensure DRPCPusher continues to satisfy the Pusher interface
// even if the interface gains methods in the future.
var _ Pusher = (*DRPCPusher)(nil)