An open-source, faster Devin alternative
TypeScript
feat(logship): phase 2 — read API for sandbox session logs
Igor Zalutskie2dd99e3a813
diff --git a/.agents/work/sandbox-session-logs-impl.md b/.agents/work/sandbox-session-logs-impl.mdindex 36456722..e40d6fda 100644--- a/.agents/work/sandbox-session-logs-impl.md+++ b/.agents/work/sandbox-session-logs-impl.md@@ -948,3 +948,22 @@ phase-4 turn-on watch.- *2026-05-05* — design doc merged-pending (PR #226 draft).Implementation plan written. No code changes yet. Phase 0 isnext.+- *2026-05-05* — Phase 0 shipped (`f49de82`): config + ConfigureLogship+ RPC plumbing. No behaviour change yet. Mid-implementation pivot:+ the design's "existing env-injection mechanism" assumption was+ wrong — `SetEnvs` is for user commands, not the agent process —+ so we added one new RPC instead. Both docs updated.+- *2026-05-05* — Phase 1 shipped (`9d0804b`): in-VM forwarder ++ exec tee. Package moved from `cmd/agent/internal/logship/` to+ `internal/logship/` because of Go's `internal/` visibility rule+ (the original location couldn't be imported by `internal/agent/`,+ which we need). Unit tests for LineWriter / drop-oldest / pre-+ Activate hold all pass.+- *2026-05-05* — Phase 2 shipped: read API at+ `GET /api/sandboxes/:id/logs` (SSE). Adds `AxiomQueryToken` to+ config (server-only; never reaches the browser). Auth via+ existing dashboard middleware + `GetSandboxSessionInOrg` for+ org ownership. APL builder pins `sandbox_id == :id` as the first+ predicate (security-load-bearing — covered by unit test). Live+ tail polls Axiom every 1s with a moving `_time` cursor; SSE+ keepalive every 15s. Phase 3 (UI tab) is next.diff --git a/cmd/server/main.go b/cmd/server/main.goindex deea9947..b0b1bb17 100644--- a/cmd/server/main.go+++ b/cmd/server/main.go@@ -395,6 +395,14 @@ func main() {// Create API serverserver := api.NewServer(mgr, ptyMgr, cfg.APIKey, opts)+ // Wire Axiom read-only token for the sandbox session logs API.+ // Token never leaves this process; the UI proxies its queries through+ // /api/sandboxes/:id/logs. Empty token disables the endpoint (503).+ server.SetAxiomQueryConfig(cfg.AxiomQueryToken, cfg.AxiomDataset)+ if cfg.AxiomQueryToken != "" {+ log.Printf("opensandbox: sandbox session logs read API enabled (dataset=%s)", cfg.AxiomDataset)+ }+// Per-sandbox autoscaler. Tier-aligned (1/4/8/16 GB), opt-in per// sandbox via PUT /api/sandboxes/:id/autoscale.//diff --git a/internal/api/router.go b/internal/api/router.goindex fe979caa..2cee4d4a 100644--- a/internal/api/router.go+++ b/internal/api/router.go@@ -56,6 +56,19 @@ type Server struct {redisClient *redis.Client // nil if Redis not configured (for health checks)adminEvents *AdminEventBus // real-time event bus for admin dashboardready int32 // atomic: 1 = ready, 0 = not ready++ // Axiom log query (sandbox session logs read API).+ // Empty token = endpoint returns 503.+ axiomQueryToken string+ axiomDataset string+}++// SetAxiomQueryConfig wires the read-only Axiom token and dataset for+// the sandbox session logs read API. Token never leaves the control+// plane; the UI proxies through us.+func (s *Server) SetAxiomQueryConfig(queryToken, dataset string) {+ s.axiomQueryToken = queryToken+ s.axiomDataset = dataset}// pendingCreate tracks an async sandbox creation.@@ -205,6 +218,11 @@ func NewServer(mgr sandbox.Manager, ptyMgr *sandbox.PTYManager, apiKey string, oapi.GET("/sandboxes/:id", s.getSandbox)api.DELETE("/sandboxes/:id", s.killSandbox)+ // Sandbox session logs (design + work in .agents/sandbox-session-logs).+ // SSE: historical batch + 1s-poll live tail. Query token never leaves+ // the control plane; the UI talks to us, we talk to Axiom.+ api.GET("/sandboxes/:id/logs", s.getSandboxLogs)+// Reserved capacity (spec: ws-pricing/design/001-reserved-capacity-squares.md)api.GET("/capacity/calendar", s.getCapacityCalendar)api.POST("/capacity/reservations", s.createCapacityReservation)diff --git a/internal/api/sandbox_logs.go b/internal/api/sandbox_logs.gonew file mode 100644index 00000000..59bd62fd--- /dev/null+++ b/internal/api/sandbox_logs.go@@ -0,0 +1,363 @@+package api++import (+ "bytes"+ "context"+ "encoding/json"+ "fmt"+ "io"+ "log"+ "net/http"+ "net/url"+ "strconv"+ "strings"+ "time"++ "github.com/labstack/echo/v4"++ "github.com/opensandbox/opensandbox/internal/auth"+)++// allowedSources is the closed set of source values a client may filter+// on. Anything else gets a 400 — never fed into the APL string.+var allowedSources = map[string]struct{}{+ "var_log": {},+ "exec_stdout": {},+ "exec_stderr": {},+ "agent": {},+}++// getSandboxLogs streams sandbox session logs as Server-Sent Events.+//+// Flow:+//+// 1. Auth check via existing dashboard middleware (caller has an org).+// 2. Sandbox-ownership check via GetSandboxSessionInOrg — refuses if+// the caller's org doesn't own this sandbox.+// 3. Initial historical batch: APL with sandbox_id == :id + the+// caller's filters, sort asc, limit. Emit one SSE event per row.+// 4. If tail=true (default), poll Axiom every 1s with a moving+// `_time > last_seen` cursor; emit deltas. Send `: keepalive\n\n`+// every 15s if nothing new arrived to keep proxies happy.+// 5. Stream ends when the client disconnects or the request context+// is otherwise cancelled.+//+// The query token is held server-side and never reaches the browser —+// this whole endpoint exists to keep that invariant clean.+//+// Query params:+//+// tail true|false (default true)+// since RFC3339 (default sandbox.StartedAt)+// until RFC3339 (default now; ignored when tail=true)+// q free-text search (escaped, "where line contains")+// source comma-separated subset of allowedSources+// limit int, default 1000, max 10000 (historical batch only)+func (s *Server) getSandboxLogs(c echo.Context) error {+ if s.axiomQueryToken == "" || s.axiomDataset == "" {+ return c.JSON(http.StatusServiceUnavailable, map[string]string{+ "error": "sandbox session logs are not configured on this deployment",+ })+ }++ orgUUID, hasOrg := auth.GetOrgID(c)+ if !hasOrg {+ return c.JSON(http.StatusUnauthorized, map[string]string{"error": "unauthorized"})+ }++ sandboxID := c.Param("id")+ if sandboxID == "" {+ return c.JSON(http.StatusBadRequest, map[string]string{"error": "missing sandbox id"})+ }++ // Authorization: the caller's org must own this sandbox. Returns+ // 404 (not 403) on mismatch to avoid leaking sandbox existence+ // across orgs — same contract as the rest of /api/sandboxes/:id/*.+ ctx := c.Request().Context()+ if s.store == nil {+ return c.JSON(http.StatusServiceUnavailable, map[string]string{+ "error": "sandbox session logs require a database",+ })+ }+ session, err := s.store.GetSandboxSessionInOrg(ctx, orgUUID, sandboxID)+ if err != nil || session == nil {+ return c.JSON(http.StatusNotFound, map[string]string{"error": "sandbox not found"})+ }++ q, err := parseLogQuery(sandboxID, session.StartedAt, c.QueryParams())+ if err != nil {+ return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})+ }++ // SSE headers + immediate flush so the browser knows the stream is open.+ c.Response().Header().Set("Content-Type", "text/event-stream")+ c.Response().Header().Set("Cache-Control", "no-cache")+ c.Response().Header().Set("Connection", "keep-alive")+ c.Response().Header().Set("X-Accel-Buffering", "no") // disable nginx buffering+ c.Response().WriteHeader(http.StatusOK)+ c.Response().Flush()++ // Initial historical batch.+ rows, err := s.queryAxiom(ctx, q.toAPL(s.axiomDataset, false))+ if err != nil {+ log.Printf("api: sandbox %s logs: initial query failed: %v", sandboxID, err)+ writeSSEComment(c.Response(), "initial query failed")+ return nil+ }+ for _, ev := range rows {+ writeSSEEvent(c.Response(), ev)+ }+ c.Response().Flush()++ if !q.tail {+ return nil+ }++ // Live tail. Cursor advances as new events arrive. Use a tiny+ // (~1s) overlap on the first tail tick to avoid losing events that+ // were ingested between the historical query and now — the+ // duplicates are harmless because each event has a unique _time ++ // content (and the UI dedupes by _time/content if it cares).+ var cursor time.Time+ if len(rows) > 0 {+ cursor = rows[len(rows)-1].Time+ } else {+ cursor = q.since+ }+ cursor = cursor.Add(-1 * time.Second)++ tick := time.NewTicker(1 * time.Second)+ defer tick.Stop()+ keepalive := time.NewTicker(15 * time.Second)+ defer keepalive.Stop()++ for {+ select {+ case <-ctx.Done():+ return nil+ case <-tick.C:+ tailQ := q+ tailQ.since = cursor+ tailQ.until = time.Time{} // open-ended+ newRows, err := s.queryAxiom(ctx, tailQ.toAPL(s.axiomDataset, true))+ if err != nil {+ // Don't kill the stream on a single failed poll — log+ // and try again next tick. Persistent failures will+ // show as a stalled stream which the UI can detect.+ log.Printf("api: sandbox %s logs: tail poll failed: %v", sandboxID, err)+ continue+ }+ for _, ev := range newRows {+ if !ev.Time.After(cursor) {+ continue+ }+ writeSSEEvent(c.Response(), ev)+ cursor = ev.Time+ }+ if len(newRows) > 0 {+ c.Response().Flush()+ }+ case <-keepalive.C:+ writeSSEComment(c.Response(), "keepalive")+ c.Response().Flush()+ }+ }+}++// logQuery is the validated form of incoming query params, ready to+// render into APL.+type logQuery struct {+ sandboxID string+ since time.Time+ until time.Time+ text string // already-escaped+ sources []string // already-validated against allowedSources+ limit int+ tail bool+}++func parseLogQuery(sandboxID string, sandboxStarted time.Time, qs url.Values) (logQuery, error) {+ q := logQuery{+ sandboxID: sandboxID,+ since: sandboxStarted,+ limit: 1000,+ tail: true,+ }++ if v := qs.Get("tail"); v != "" {+ b, err := strconv.ParseBool(v)+ if err != nil {+ return q, fmt.Errorf("tail must be true or false")+ }+ q.tail = b+ }++ if v := qs.Get("since"); v != "" {+ t, err := time.Parse(time.RFC3339, v)+ if err != nil {+ return q, fmt.Errorf("since must be RFC3339")+ }+ q.since = t+ }++ if v := qs.Get("until"); v != "" {+ t, err := time.Parse(time.RFC3339, v)+ if err != nil {+ return q, fmt.Errorf("until must be RFC3339")+ }+ q.until = t+ }++ if v := qs.Get("limit"); v != "" {+ n, err := strconv.Atoi(v)+ if err != nil || n <= 0 {+ return q, fmt.Errorf("limit must be a positive integer")+ }+ if n > 10000 {+ n = 10000+ }+ q.limit = n+ }++ if v := qs.Get("q"); v != "" {+ // Disallow newlines + control chars; double-escape quotes.+ // APL string literals are double-quoted; embedded `"` is+ // escaped as `\"`. The set we strip here is the surface that+ // could break out of a string literal; everything else passes+ // through unchanged so search behaviour matches user intent.+ if strings.ContainsAny(v, "\r\n\x00") {+ return q, fmt.Errorf("q must not contain control characters")+ }+ q.text = strings.ReplaceAll(v, `"`, `\"`)+ }++ if v := qs.Get("source"); v != "" {+ for _, src := range strings.Split(v, ",") {+ src = strings.TrimSpace(src)+ if src == "" {+ continue+ }+ if _, ok := allowedSources[src]; !ok {+ return q, fmt.Errorf("source %q not allowed", src)+ }+ q.sources = append(q.sources, src)+ }+ }++ return q, nil+}++// toAPL renders the query into a KQL/APL string. The sandbox_id filter+// is the FIRST predicate after the dataset and is unconditionally+// applied — there is no path that omits it. Every interpolated string+// is either: a server-derived constant (sandboxID, dataset name), a+// validated value from allowedSources, or pre-escaped (text search).+//+// `tail` selects an open-ended (no until) variant.+func (q logQuery) toAPL(dataset string, tail bool) string {+ var b strings.Builder+ fmt.Fprintf(&b, "['%s']\n", dataset)+ fmt.Fprintf(&b, " | where sandbox_id == \"%s\"\n", q.sandboxID)+ if !q.since.IsZero() {+ fmt.Fprintf(&b, " | where _time >= datetime(\"%s\")\n", q.since.UTC().Format(time.RFC3339Nano))+ }+ if !tail && !q.until.IsZero() {+ fmt.Fprintf(&b, " | where _time <= datetime(\"%s\")\n", q.until.UTC().Format(time.RFC3339Nano))+ }+ if q.text != "" {+ fmt.Fprintf(&b, " | where line contains \"%s\"\n", q.text)+ }+ if len(q.sources) > 0 {+ quoted := make([]string, len(q.sources))+ for i, s := range q.sources {+ quoted[i] = fmt.Sprintf("\"%s\"", s)+ }+ fmt.Fprintf(&b, " | where source in (%s)\n", strings.Join(quoted, ", "))+ }+ fmt.Fprintf(&b, " | sort by _time asc\n")+ if !tail {+ fmt.Fprintf(&b, " | limit %d\n", q.limit)+ }+ return b.String()+}++// logEvent is the on-the-wire shape we re-emit to SSE clients. Mirrors+// the agent-side schema but only includes fields the UI needs to+// render a row. Unknown extra fields from Axiom are tolerated.+type logEvent struct {+ Time time.Time `json:"_time"`+ Source string `json:"source"`+ Line string `json:"line"`+ SandboxID string `json:"sandbox_id,omitempty"`+ Path string `json:"path,omitempty"`+ ExecID string `json:"exec_id,omitempty"`+ Command string `json:"command,omitempty"`+ Argv []string `json:"argv,omitempty"`+ ExitCode *int `json:"exit_code,omitempty"`+}++// queryAxiom POSTs an APL query and parses the response.+//+// Axiom's APL endpoint is /v1/datasets/_apl/query (with format=tabular+// off, default), and the response shape is:+//+// {+// "matches": [{"data": {<event fields>}}, ...]+// }+//+// We don't need format=tabular; the default object form is what we want.+func (s *Server) queryAxiom(ctx context.Context, apl string) ([]logEvent, error) {+ body, _ := json.Marshal(map[string]any{"apl": apl})+ req, err := http.NewRequestWithContext(ctx, "POST",+ "https://api.axiom.co/v1/datasets/_apl/query?format=legacy",+ bytes.NewReader(body))+ if err != nil {+ return nil, err+ }+ req.Header.Set("Authorization", "Bearer "+s.axiomQueryToken)+ req.Header.Set("Content-Type", "application/json")++ httpClient := &http.Client{Timeout: 15 * time.Second}+ resp, err := httpClient.Do(req)+ if err != nil {+ return nil, err+ }+ defer resp.Body.Close()+ if resp.StatusCode >= 400 {+ raw, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("axiom %d: %s", resp.StatusCode, string(raw))+ }++ var parsed struct {+ Matches []struct {+ Data json.RawMessage `json:"data"`+ } `json:"matches"`+ }+ if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {+ return nil, fmt.Errorf("decode axiom response: %w", err)+ }++ out := make([]logEvent, 0, len(parsed.Matches))+ for _, m := range parsed.Matches {+ var ev logEvent+ if err := json.Unmarshal(m.Data, &ev); err != nil {+ continue // skip malformed, don't fail the whole batch+ }+ out = append(out, ev)+ }+ return out, nil+}++// writeSSEEvent writes one event in SSE wire format. Errors here mean+// the client disconnected; the caller's ctx will see Done shortly.+func writeSSEEvent(w io.Writer, ev logEvent) {+ data, err := json.Marshal(ev)+ if err != nil {+ return+ }+ fmt.Fprintf(w, "data: %s\n\n", data)+}++func writeSSEComment(w io.Writer, comment string) {+ fmt.Fprintf(w, ": %s\n\n", comment)+}diff --git a/internal/api/sandbox_logs_test.go b/internal/api/sandbox_logs_test.gonew file mode 100644index 00000000..8c536f5b--- /dev/null+++ b/internal/api/sandbox_logs_test.go@@ -0,0 +1,135 @@+package api++import (+ "net/url"+ "strings"+ "testing"+ "time"+)++// TestAPL_AlwaysFiltersSandboxID is the load-bearing security test:+// no matter what the user supplies in query params, the rendered APL+// must filter on sandbox_id == the URL-path value. This is the+// invariant that makes the auth model work — break this and a user+// who can hit /api/sandboxes/X/logs but only owns Y could read X's+// logs by sneaking past the filter.+func TestAPL_AlwaysFiltersSandboxID(t *testing.T) {+ cases := []struct {+ name string+ qs url.Values+ }{+ {"empty", url.Values{}},+ {"with text", url.Values{"q": {"hello"}}},+ {"with sources", url.Values{"source": {"exec_stdout,exec_stderr"}}},+ {"with limit", url.Values{"limit": {"500"}}},+ {+ "injection attempt — quote in q",+ url.Values{"q": {`" or sandbox_id != "anything`}},+ },+ }++ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ q, err := parseLogQuery("sb-target", time.Now().Add(-time.Hour), tc.qs)+ if err != nil {+ t.Fatalf("parse: %v", err)+ }+ apl := q.toAPL("oc-sandbox-logs", false)+ if !strings.Contains(apl, `where sandbox_id == "sb-target"`) {+ t.Errorf("APL missing sandbox_id filter:\n%s", apl)+ }+ // Verify the filter line is the FIRST predicate after the+ // dataset, not buried somewhere a search OR could subvert.+ lines := strings.Split(strings.TrimSpace(apl), "\n")+ if len(lines) < 2 {+ t.Fatalf("APL too short:\n%s", apl)+ }+ if !strings.Contains(lines[1], `where sandbox_id == "sb-target"`) {+ t.Errorf("sandbox_id filter not first predicate:\n%s", apl)+ }+ })+ }+}++// TestAPL_RejectsBadSource: source values not in allowedSources are a+// 400 at parse time, never interpolated.+func TestAPL_RejectsBadSource(t *testing.T) {+ for _, bad := range []string{"system", "stdout", "exec_stdout'); drop --", ""} {+ if bad == "" {+ continue // empty values are stripped, not rejected+ }+ t.Run(bad, func(t *testing.T) {+ _, err := parseLogQuery("sb-x", time.Now(), url.Values{"source": {bad}})+ if err == nil {+ t.Errorf("expected error for source=%q", bad)+ }+ })+ }+}++// TestAPL_RejectsControlCharsInQ: newlines / NULs in `q` would let a+// user break out of the string literal and append APL of their own.+// They must be rejected at parse time.+func TestAPL_RejectsControlCharsInQ(t *testing.T) {+ for _, bad := range []string{"hello\nworld", "x\x00y", "\r"} {+ _, err := parseLogQuery("sb-x", time.Now(), url.Values{"q": {bad}})+ if err == nil {+ t.Errorf("expected error for q=%q", bad)+ }+ }+}++// TestAPL_QuoteEscapeInQ: legitimate double quotes in `q` are escaped+// to `\"` so the APL parses as a single string literal.+func TestAPL_QuoteEscapeInQ(t *testing.T) {+ q, err := parseLogQuery("sb-x", time.Now(), url.Values{"q": {`hello "world"`}})+ if err != nil {+ t.Fatalf("parse: %v", err)+ }+ apl := q.toAPL("d", false)+ if !strings.Contains(apl, `where line contains "hello \"world\""`) {+ t.Errorf("expected escaped quotes in APL:\n%s", apl)+ }+}++// TestAPL_TailVariantOmitsLimitAndUntil: the live-tail render path+// shouldn't bound by `until` or apply a row cap — every poll wants+// every new event since the cursor.+func TestAPL_TailVariantOmitsLimitAndUntil(t *testing.T) {+ q, err := parseLogQuery("sb-x", time.Now().Add(-time.Hour), url.Values{+ "until": {time.Now().Format(time.RFC3339)},+ "limit": {"50"},+ })+ if err != nil {+ t.Fatalf("parse: %v", err)+ }+ tailAPL := q.toAPL("d", true)+ if strings.Contains(tailAPL, "_time <=") {+ t.Errorf("tail APL must not bound by until:\n%s", tailAPL)+ }+ if strings.Contains(tailAPL, "limit") {+ t.Errorf("tail APL must not apply limit:\n%s", tailAPL)+ }++ // And the historical render path DOES include them.+ histAPL := q.toAPL("d", false)+ if !strings.Contains(histAPL, "_time <=") {+ t.Errorf("historical APL missing until:\n%s", histAPL)+ }+ if !strings.Contains(histAPL, "limit") {+ t.Errorf("historical APL missing limit:\n%s", histAPL)+ }+}++// TestAPL_LimitClamp: caller-supplied limit > 10000 is silently+// clamped to 10000 (we don't 400 here — the cap is a defense, not a+// contract).+func TestAPL_LimitClamp(t *testing.T) {+ q, err := parseLogQuery("sb-x", time.Now(), url.Values{"limit": {"99999"}})+ if err != nil {+ t.Fatalf("parse: %v", err)+ }+ if q.limit != 10000 {+ t.Errorf("expected limit clamped to 10000, got %d", q.limit)+ }+}diff --git a/internal/config/config.go b/internal/config/config.goindex 9cd751d9..7497551d 100644--- a/internal/config/config.go+++ b/internal/config/config.go@@ -124,9 +124,11 @@ type Config struct {// Axiom — log shipping for sandbox session logs.// AxiomIngestToken empty = log shipping disabled (kill-switch).- // Only the worker needs these; the agent receives them via the- // ConfigureLogship RPC at sandbox boot.+ // Worker uses AxiomIngestToken + AxiomDataset to deliver via the+ // ConfigureLogship RPC at sandbox boot. Server uses+ // AxiomQueryToken + AxiomDataset to serve the read API.AxiomIngestToken string+ AxiomQueryToken stringAxiomDataset string// AWS Secrets Manager — if set, secrets are fetched at startup using IAM credentials.@@ -235,6 +237,7 @@ func Load() (*Config, error) {SegmentWriteKey: os.Getenv("SEGMENT_WRITE_KEY"),AxiomIngestToken: os.Getenv("AXIOM_INGEST_TOKEN"),+ AxiomQueryToken: os.Getenv("AXIOM_QUERY_TOKEN"),AxiomDataset: envOrDefault("AXIOM_DATASET", "oc-sandbox-logs"),SecretsARN: os.Getenv("OPENSANDBOX_SECRETS_ARN"),