mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +00:00
9da0cfe34b
P2: - CRF-7: effectiveAllowedRoots now appends the current working directory lazily so the manager picks up the resolved path after the manifest loads. - CRF-8: walkDir error path computes Kind from the basename (kindFromFilename) so .mcp.json and SKILL.md keep a stable resource ID across permission flips. - CRF-9: ComputeAggregateHash / writeLengthPrefixed docs now describe the Netstring-style encoding accurately. - CRF-10: resolveAndBroadcast snapshots inputs under the lock, releases it for the filesystem walk, then re-acquires to swap. - CRF-11: api.go SnapshotResource comment no longer references a nonexistent PayloadBase64 field. P3: - CRF-12: WatcherOptions.MaxDepth; the watcher now mirrors the resolver's depth instead of hardcoding DefaultMaxScanDepth. - CRF-13: drop obsolete "follow-up" sentence in doc.go. - CRF-14: Pusher doc says Agent API v2.10, not proto v30. - CRF-15: Resolver.ResolveContext threads ctx through walk so resolveAndBroadcast can cancel between roots. - CRF-16: new tests cover StatusUnreadable for instruction and MCP-config files (skipped on Windows and root). - CRF-17: TestRunPush_ClosesOnManagerClose covers closedCh path. - CRF-18: drop dead !ok branch on the subscriber channel. - CRF-19: extract readFileResource; readInstructionFile and readMCPConfig share the plumbing. - CRF-24: DefaultPushInitialBackoff / DefaultPushMaxBackoff. - CRF-25: subsequent-push test asserts AggregateHash and Version advance, not just the count. P4: - CRF-26: TestRunPush_RejectedResponseProceeds covers the Accepted=false fast-path. Nit: - CRF-27: slices.SortFunc/SortStableFunc replace sort.Slice. - CRF-28: firstLine uses strings.SplitSeq. - CRF-29: sync.OnceFunc replaces the sync.Once+closure wrapper. - CRF-30: atomic.Int32 replaces bare int32 in watch_test. - CRF-31: redundant sort in resolveLocked already gone with CRF-10. - CRF-32: drop redundant "agentcontext:" prefix from log messages. - CRF-33: resolveAndBroadcast doc no longer claims conditional. Note: - CRF-34: ResourceStatus doc explains the proto-enum offset. - CRF-35: readInstructionFile doc flags the sanitization gap delegated to the chatd follow-up.
205 lines
6.0 KiB
Go
205 lines
6.0 KiB
Go
package agentcontext
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// SourceResponse is the on-wire representation of a Source.
|
|
// Matches the path-only RFC schema; future additions (tags,
|
|
// labels) can land additively without breaking clients.
|
|
type SourceResponse struct {
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
// SourceRequest is the request body for POST /sources.
|
|
type SourceRequest struct {
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
// SnapshotResource is the on-wire representation of a Resource.
|
|
// Payloads are omitted; clients that need the bytes go through
|
|
// the drpc PushContextState path.
|
|
type SnapshotResource struct {
|
|
ID string `json:"id"`
|
|
Kind string `json:"kind"`
|
|
Source string `json:"source"`
|
|
SourcePath string `json:"source_path,omitempty"`
|
|
ContentHash string `json:"content_hash"`
|
|
SizeBytes uint64 `json:"size_bytes"`
|
|
Status string `json:"status"`
|
|
Error string `json:"error,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
}
|
|
|
|
// SnapshotResponse is the on-wire representation of a Snapshot
|
|
// returned by the resync endpoint.
|
|
type SnapshotResponse struct {
|
|
Version uint64 `json:"version"`
|
|
SchemaVersion uint64 `json:"schema_version"`
|
|
AggregateHash string `json:"aggregate_hash"`
|
|
Resources []SnapshotResource `json:"resources"`
|
|
PayloadBytes uint64 `json:"payload_bytes"`
|
|
SnapshotError string `json:"snapshot_error,omitempty"`
|
|
}
|
|
|
|
// API exposes the Manager over HTTP. The routes match the RFC:
|
|
//
|
|
// GET /api/v0/context/sources
|
|
// POST /api/v0/context/sources { path }
|
|
// GET /api/v0/context/sources/{path}
|
|
// DELETE /api/v0/context/sources/{path}
|
|
// POST /api/v0/context/resync
|
|
//
|
|
// {path} is URL-encoded canonical path. Callers pass either the
|
|
// canonical or original path; the handler canonicalizes before
|
|
// matching.
|
|
type API struct {
|
|
manager *Manager
|
|
}
|
|
|
|
// NewAPI wraps the supplied Manager.
|
|
func NewAPI(m *Manager) *API {
|
|
return &API{manager: m}
|
|
}
|
|
|
|
// Routes returns the chi handler for /api/v0/context/*. Mount
|
|
// it at "/api/v0/context".
|
|
func (a *API) Routes() http.Handler {
|
|
r := chi.NewRouter()
|
|
r.Route("/sources", func(r chi.Router) {
|
|
r.Get("/", a.handleListSources)
|
|
r.Post("/", a.handleAddSource)
|
|
r.Get("/{path}", a.handleGetSource)
|
|
r.Delete("/{path}", a.handleRemoveSource)
|
|
})
|
|
r.Post("/resync", a.handleResync)
|
|
return r
|
|
}
|
|
|
|
func (a *API) handleListSources(rw http.ResponseWriter, r *http.Request) {
|
|
sources := a.manager.Sources()
|
|
out := make([]SourceResponse, 0, len(sources))
|
|
for _, s := range sources {
|
|
out = append(out, SourceResponse(s))
|
|
}
|
|
httpapi.Write(r.Context(), rw, http.StatusOK, out)
|
|
}
|
|
|
|
func (a *API) handleAddSource(rw http.ResponseWriter, r *http.Request) {
|
|
var req SourceRequest
|
|
if !httpapi.Read(r.Context(), rw, r, &req) {
|
|
return
|
|
}
|
|
s, err := a.manager.AddSource(Source(req))
|
|
if err != nil {
|
|
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
|
|
Message: "Could not add context source.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
httpapi.Write(r.Context(), rw, http.StatusCreated, SourceResponse(s))
|
|
}
|
|
|
|
func (a *API) handleGetSource(rw http.ResponseWriter, r *http.Request) {
|
|
raw := chi.URLParam(r, "path")
|
|
decoded, err := url.PathUnescape(raw)
|
|
if err != nil {
|
|
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
|
|
Message: "Invalid context source path.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
canonical, ok := a.manager.HasSource(decoded)
|
|
if !ok {
|
|
httpapi.Write(r.Context(), rw, http.StatusNotFound, codersdk.Response{
|
|
Message: "Context source not found.",
|
|
Detail: "No source registered for path " + strconv.Quote(decoded) + ".",
|
|
})
|
|
return
|
|
}
|
|
httpapi.Write(r.Context(), rw, http.StatusOK, SourceResponse{Path: canonical})
|
|
}
|
|
|
|
func (a *API) handleRemoveSource(rw http.ResponseWriter, r *http.Request) {
|
|
raw := chi.URLParam(r, "path")
|
|
decoded, err := url.PathUnescape(raw)
|
|
if err != nil {
|
|
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
|
|
Message: "Invalid context source path.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
if err := a.manager.RemoveSource(decoded); err != nil {
|
|
if errors.Is(err, ErrSourceNotFound) {
|
|
httpapi.Write(r.Context(), rw, http.StatusNotFound, codersdk.Response{
|
|
Message: "Context source not found.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
|
|
Message: "Could not remove context source.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
rw.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (a *API) handleResync(rw http.ResponseWriter, r *http.Request) {
|
|
snap, err := a.manager.Resync(r.Context())
|
|
if err != nil {
|
|
status := http.StatusInternalServerError
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
status = http.StatusGatewayTimeout
|
|
}
|
|
httpapi.Write(r.Context(), rw, status, codersdk.Response{
|
|
Message: "Resync failed.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
httpapi.Write(r.Context(), rw, http.StatusOK, snapshotResponse(snap))
|
|
}
|
|
|
|
// snapshotResponse converts a Snapshot to its on-wire form for
|
|
// the resync endpoint. Payloads are omitted; the per-resource
|
|
// payload bytes ship via the drpc PushContextState path.
|
|
func snapshotResponse(s Snapshot) SnapshotResponse {
|
|
out := SnapshotResponse{
|
|
Version: s.Version,
|
|
SchemaVersion: s.SchemaVersion,
|
|
AggregateHash: hex.EncodeToString(s.AggregateHash[:]),
|
|
Resources: make([]SnapshotResource, 0, len(s.Resources)),
|
|
PayloadBytes: s.PayloadBytes,
|
|
SnapshotError: s.SnapshotError,
|
|
}
|
|
for _, r := range s.Resources {
|
|
out.Resources = append(out.Resources, SnapshotResource{
|
|
ID: r.ID,
|
|
Kind: r.Kind.String(),
|
|
Source: r.Source,
|
|
SourcePath: r.SourcePath,
|
|
ContentHash: hex.EncodeToString(r.ContentHash[:]),
|
|
SizeBytes: r.SizeBytes,
|
|
Status: r.Status.String(),
|
|
Error: r.Error,
|
|
Description: r.Description,
|
|
})
|
|
}
|
|
return out
|
|
}
|