Skip to content

An open-source, faster Devin alternative

TypeScript

fix(logship): stopped-sandbox event duplication + oc logs 404 (#247)

Mohamed Habibd2358c5a9e17
diff --git a/cmd/oc/internal/commands/logs.go b/cmd/oc/internal/commands/logs.go
index 25bfdd15..97360aca 100644
--- a/cmd/oc/internal/commands/logs.go
+++ b/cmd/oc/internal/commands/logs.go
@@ -79,7 +79,9 @@ Examples:
qs.Set("limit", strconv.Itoa(limit))
}
- path := "/api/sandboxes/" + url.PathEscape(sandboxID) + "/logs"
+ // client.New() already appends /api to baseURL — use the bare
+ // path here, matching the convention in exec/sandbox commands.
+ path := "/sandboxes/" + url.PathEscape(sandboxID) + "/logs"
if encoded := qs.Encode(); encoded != "" {
path += "?" + encoded
}
diff --git a/internal/api/sandbox_logs.go b/internal/api/sandbox_logs.go
index 53f0231c..149ec6a9 100644
--- a/internal/api/sandbox_logs.go
+++ b/internal/api/sandbox_logs.go
@@ -159,6 +159,26 @@ func (s *Server) getSandboxLogs(c echo.Context) error {
return nil
}
+ // If the sandbox is not running, no new events will arrive — skip
+ // the Axiom poll loop entirely. But keep the SSE connection open
+ // with keepalive comments so the browser EventSource doesn't
+ // interpret a server-side close as a disconnect and auto-reconnect
+ // (which would re-fetch the historical batch and visibly duplicate
+ // events: see the 3× duplication report after #243).
+ if session.Status != "running" {
+ keepalive := time.NewTicker(15 * time.Second)
+ defer keepalive.Stop()
+ for {
+ select {
+ case <-ctx.Done():
+ return nil
+ case <-keepalive.C:
+ writeSSEComment(c.Response(), "keepalive")
+ c.Response().Flush()
+ }
+ }
+ }
+
// Live tail. Cursor starts at the last historical event's _time.
// Tail polls strictly require `ev.Time.After(cursor)`, so events
// at exactly the cursor are skipped — no double-emit of historical
@@ -233,16 +253,22 @@ type logQuery struct {
// applySessionState narrows a parsed query based on the sandbox's
// lifecycle. For stopped/error/hibernated sandboxes:
-// - tail is forced false (no new events will arrive)
-// - until is capped at stoppedAt + 30s grace (avoid polling into a void
-// of empty Axiom windows)
+// - until is capped at stoppedAt + 30s grace (the in-VM shipper
+// finishes flushing within a few seconds of process exit; 30s
+// covers ingest latency comfortably)
+//
+// q.tail is preserved — it reflects the client's explicit intent
+// (URL ?tail=false / CLI --no-tail). The handler reads session.Status
+// separately to decide whether to actually poll Axiom; for non-running
+// sandboxes it holds the SSE connection open with keepalives instead
+// of closing, so the browser EventSource doesn't auto-reconnect and
+// re-fetch the historical batch (which visibly duplicated events).
//
// Pure function (no DB dep) so the lifecycle behaviour is unit-testable.
func applySessionState(q logQuery, status string, stoppedAt *time.Time) logQuery {
if status == "running" {
return q
}
- q.tail = false
if stoppedAt != nil {
bound := stoppedAt.Add(30 * time.Second)
if q.until.IsZero() || q.until.After(bound) {
diff --git a/internal/api/sandbox_logs_test.go b/internal/api/sandbox_logs_test.go
index 31d8bfd7..fc33f94e 100644
--- a/internal/api/sandbox_logs_test.go
+++ b/internal/api/sandbox_logs_test.go
@@ -227,15 +227,26 @@ func TestApplySessionState_RunningPassthrough(t *testing.T) {
}
}
-// TestApplySessionState_StoppedForcesHistorical: any non-running status
-// flips tail off. New events can't arrive once the sandbox is gone.
-func TestApplySessionState_StoppedForcesHistorical(t *testing.T) {
- for _, status := range []string{"stopped", "error", "hibernated", "migrating"} {
+// TestApplySessionState_PreservesTail: q.tail reflects the client's
+// explicit intent (URL ?tail=false / CLI --no-tail) and is NOT
+// rewritten by applySessionState. The handler reads session.Status
+// separately to decide whether to actually poll Axiom; on non-running
+// sandboxes it holds the SSE connection open with keepalives.
+//
+// History: an earlier version flipped q.tail=false for non-running
+// sandboxes. The handler then `return nil`-ed after the historical
+// batch, closing the SSE stream. Browsers' EventSource auto-reconnects
+// on close → server re-sent the same historical batch → events
+// visibly duplicated in the UI (one copy per reconnect). Reverted.
+func TestApplySessionState_PreservesTail(t *testing.T) {
+ for _, status := range []string{"running", "stopped", "error", "hibernated", "migrating"} {
t.Run(status, func(t *testing.T) {
- q := logQuery{tail: true}
- got := applySessionState(q, status, nil)
- if got.tail {
- t.Errorf("status=%s: tail = true, want false", status)
+ for _, clientTail := range []bool{true, false} {
+ q := logQuery{tail: clientTail}
+ got := applySessionState(q, status, nil)
+ if got.tail != clientTail {
+ t.Errorf("status=%s clientTail=%v: tail = %v, want preserved", status, clientTail, got.tail)
+ }
}
})
}