Skip to content

An open-source, faster Devin alternative

TypeScript

api: tags + tagsLastUpdatedAt on GET /sandboxes* responses

Igor Zalutski7be01bd0ee2b
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index 50a3498e..30ff57f4 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -598,7 +598,7 @@ func (s *Server) getSandbox(c echo.Context) error {
}
}
- return c.JSON(http.StatusOK, sb)
+ return c.JSON(http.StatusOK, s.withTagsHydrated(c, sb, id))
}
// getSandboxRemote looks up a sandbox via the PG session record and returns
@@ -631,6 +631,7 @@ func (s *Server) getSandboxRemote(c echo.Context, sandboxID string) error {
if s.sandboxDomain != "" {
resp["sandboxDomain"] = s.sandboxDomain
}
+ s.mergeTagsInto(c.Request().Context(), resp, sandboxID)
return c.JSON(http.StatusOK, resp)
}
@@ -659,6 +660,7 @@ func (s *Server) getSandboxRemote(c echo.Context, sandboxID string) error {
resp["patchError"] = *session.PatchError
}
+ s.mergeTagsInto(c.Request().Context(), resp, sandboxID)
return c.JSON(http.StatusOK, resp)
}
@@ -763,7 +765,7 @@ func (s *Server) listSandboxes(c echo.Context) error {
})
}
- return c.JSON(http.StatusOK, sandboxes)
+ return c.JSON(http.StatusOK, s.withTagsHydratedList(c, sandboxes))
}
// listSandboxesRemote queries PG for the org's running sandboxes and returns
@@ -790,6 +792,7 @@ func (s *Server) listSandboxesRemote(c echo.Context) error {
}
result := make([]map[string]interface{}, 0, len(sessions))
+ ids := make([]string, 0, len(sessions))
for _, sess := range sessions {
entry := map[string]interface{}{
"sandboxID": sess.SandboxID,
@@ -809,6 +812,29 @@ func (s *Server) listSandboxesRemote(c echo.Context) error {
}
result = append(result, entry)
+ ids = append(ids, sess.SandboxID)
+ }
+
+ // One batched tag fetch for all sandboxes in the page — cheaper
+ // than N GetSandboxTags round-trips. Fail-soft: if the tag table
+ // is unreachable, list still returns without tag fields rather
+ // than 500.
+ if s.store != nil {
+ if sets, err := s.store.GetSandboxTagsMulti(c.Request().Context(), ids); err == nil {
+ for _, entry := range result {
+ sid, _ := entry["sandboxID"].(string)
+ set := sets[sid]
+ if set.Tags == nil {
+ set.Tags = map[string]string{}
+ }
+ entry["tags"] = set.Tags
+ if set.LastUpdatedAt != nil {
+ entry["tagsLastUpdatedAt"] = set.LastUpdatedAt.UTC().Format(time.RFC3339)
+ } else {
+ entry["tagsLastUpdatedAt"] = nil
+ }
+ }
+ }
}
return c.JSON(http.StatusOK, result)
diff --git a/internal/api/sandbox_tags_hydrate.go b/internal/api/sandbox_tags_hydrate.go
new file mode 100644
index 00000000..90d531b5
--- /dev/null
+++ b/internal/api/sandbox_tags_hydrate.go
@@ -0,0 +1,102 @@
+package api
+
+import (
+ "context"
+ "encoding/json"
+ "time"
+
+ "github.com/labstack/echo/v4"
+)
+
+// Tag hydration helpers for the GET /sandboxes* response additions.
+// All four read paths (local/remote × get/list) need to surface
+// `tags` + `tagsLastUpdatedAt` — these helpers factor out the
+// marshal-to-map + merge step so each handler stays one line.
+//
+// Fail-soft: if the store isn't configured or the tag query errors,
+// the sandbox response is returned as-is rather than 500. Tags are
+// purely additive and should never break the primary sandbox read.
+
+// mergeTagsInto mutates a response map, adding `tags` (always, even
+// if empty) and `tagsLastUpdatedAt` (nil when the sandbox has no
+// tags). Intended for the remote paths, which already build maps.
+func (s *Server) mergeTagsInto(ctx context.Context, resp map[string]interface{}, sandboxID string) {
+ if s.store == nil {
+ return
+ }
+ set, err := s.store.GetSandboxTags(ctx, sandboxID)
+ if err != nil {
+ return
+ }
+ if set.Tags == nil {
+ set.Tags = map[string]string{}
+ }
+ resp["tags"] = set.Tags
+ if set.LastUpdatedAt != nil {
+ resp["tagsLastUpdatedAt"] = set.LastUpdatedAt.UTC().Format(time.RFC3339)
+ } else {
+ resp["tagsLastUpdatedAt"] = nil
+ }
+}
+
+// withTagsHydrated takes an arbitrary sandbox value (as returned by
+// the manager in local mode) and returns either the original value
+// unchanged (no store, no tags) or a generic map with tag fields
+// merged in. Marshalling through JSON preserves whatever fields the
+// sandbox type emits without binding this package to its shape.
+func (s *Server) withTagsHydrated(c echo.Context, sb interface{}, sandboxID string) interface{} {
+ if s.store == nil {
+ return sb
+ }
+ buf, err := json.Marshal(sb)
+ if err != nil {
+ return sb
+ }
+ m := map[string]interface{}{}
+ if err := json.Unmarshal(buf, &m); err != nil {
+ return sb
+ }
+ s.mergeTagsInto(c.Request().Context(), m, sandboxID)
+ return m
+}
+
+// withTagsHydratedList is the list-response analogue. One batched
+// tag fetch for the whole page; per-item lookup inside the loop.
+func (s *Server) withTagsHydratedList(c echo.Context, sandboxes interface{}) interface{} {
+ if s.store == nil {
+ return sandboxes
+ }
+ buf, err := json.Marshal(sandboxes)
+ if err != nil {
+ return sandboxes
+ }
+ var arr []map[string]interface{}
+ if err := json.Unmarshal(buf, &arr); err != nil {
+ return sandboxes
+ }
+
+ ids := make([]string, 0, len(arr))
+ for _, m := range arr {
+ if id, ok := m["sandboxID"].(string); ok && id != "" {
+ ids = append(ids, id)
+ }
+ }
+ sets, err := s.store.GetSandboxTagsMulti(c.Request().Context(), ids)
+ if err != nil {
+ return sandboxes
+ }
+ for _, m := range arr {
+ sid, _ := m["sandboxID"].(string)
+ set := sets[sid]
+ if set.Tags == nil {
+ set.Tags = map[string]string{}
+ }
+ m["tags"] = set.Tags
+ if set.LastUpdatedAt != nil {
+ m["tagsLastUpdatedAt"] = set.LastUpdatedAt.UTC().Format(time.RFC3339)
+ } else {
+ m["tagsLastUpdatedAt"] = nil
+ }
+ }
+ return arr
+}