Remove terminal log replay

This commit is contained in:
npc0-hue
2026-08-25 22:25:09 +08:00
parent 725b71b6b6
commit 7ebe3bb3dd
9 changed files with 40 additions and 136 deletions
+8 -76
View File
@@ -4,8 +4,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
@@ -15,14 +13,11 @@ import (
)
const (
defaultLogEventHistoryLimit = 0
maxLogEventHistoryLimit = 10000
logEventHeartbeatInterval = 15 * time.Second
managedLogSessionIDPrefix = "log-session:"
logEventHeartbeatInterval = 15 * time.Second
managedLogSessionIDPrefix = "log-session:"
)
// serverLogEvents streams platform-accepted live append events for the terminal drawer.
// Callers can opt into a bounded current-session replay with historyLimit.
func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeMethodNotAllowed(w, http.MethodGet)
@@ -47,12 +42,11 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
header.Set("X-Accel-Buffering", "no")
w.WriteHeader(http.StatusOK)
historyLimit := parseLogEventHistoryLimit(r.URL.Query().Get("historyLimit"))
active := supervisedLogSession{}
if liveEligible {
active = activeSupervisedLogSession(streams)
}
emittedThrough, err := h.writeCurrentLogSession(w, instance.ID, active, historyLimit)
emittedThrough, err := h.writeCurrentLogSession(w, instance.ID, active)
if err != nil {
_ = writeSSEJSON(w, "error", "", map[string]string{"message": err.Error()})
flusher.Flush()
@@ -83,7 +77,7 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
continue
}
active = supervisedLogSession{}
emittedThrough, err = h.writeCurrentLogSession(w, instance.ID, active, historyLimit)
emittedThrough, err = h.writeCurrentLogSession(w, instance.ID, active)
if err != nil {
return
}
@@ -102,7 +96,7 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
continue
}
active = next
emittedThrough, err = h.writeCurrentLogSession(w, instance.ID, active, historyLimit)
emittedThrough, err = h.writeCurrentLogSession(w, instance.ID, active)
if err != nil {
return
}
@@ -125,7 +119,7 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
}
if !sameSupervisedLogSession(active, next) {
active = next
emittedThrough, err = h.writeCurrentLogSession(w, instance.ID, active, historyLimit)
emittedThrough, err = h.writeCurrentLogSession(w, instance.ID, active)
if err != nil {
return
}
@@ -216,34 +210,17 @@ func (session supervisedLogSession) hasStream(streamID string) bool {
return false
}
func (h *coreHandlers) writeCurrentLogSession(w http.ResponseWriter, serverInstanceID string, active supervisedLogSession, historyLimit int) (map[string]uint64, error) {
func (h *coreHandlers) writeCurrentLogSession(w http.ResponseWriter, serverInstanceID string, active supervisedLogSession) (map[string]uint64, error) {
emittedThrough := make(map[string]uint64, len(active.streams))
if err := writeSSEJSON(w, "session", "", dto.LogStreamEventsSessionResponse{ServerInstanceID: serverInstanceID, LogSessionID: active.sessionID, SessionStartedAt: active.startedAt, StreamCount: len(active.streams), ServerTime: time.Now().UTC()}); err != nil {
return nil, err
}
for _, stream := range active.streams {
if historyLimit == 0 {
emittedThrough[stream.ID] = stream.LatestSeq
}
emittedThrough[stream.ID] = stream.LatestSeq
if err := writeSSEJSON(w, "stream", "", dto.LogStreamFromDomain(stream)); err != nil {
return nil, err
}
}
if historyLimit == 0 || len(active.streams) == 0 {
return emittedThrough, nil
}
history, err := h.loadLogEventHistory(active.streams, historyLimit)
if err != nil {
return nil, err
}
for _, event := range history {
if err := writeSSEJSON(w, "log", logEventID(event), dto.LogStreamEventFromDomain(event)); err != nil {
return nil, err
}
if event.Entry.Seq > emittedThrough[event.Stream.ID] {
emittedThrough[event.Stream.ID] = event.Entry.Seq
}
}
return emittedThrough, nil
}
@@ -314,51 +291,6 @@ func (h *coreHandlers) loadLiveLogSnapshot(serverInstanceID string) ([]domain.Lo
return current, true, nil
}
func (h *coreHandlers) loadLogEventHistory(streams []domain.LogStream, limit int) ([]domain.LogStreamEvent, error) {
history := make([]domain.LogStreamEvent, 0, limit)
for _, stream := range streams {
afterSeq := uint64(0)
if stream.LatestSeq > uint64(limit) {
afterSeq = stream.LatestSeq - uint64(limit)
}
cursor, err := h.core.QueryLogStream(domain.LogStreamCursorQuery{LogStreamID: stream.ID, AfterSeq: afterSeq, Limit: limit})
if err != nil {
return nil, err
}
for _, entry := range cursor.Entries {
history = append(history, domain.LogStreamEvent{ServerInstanceID: stream.ServerInstanceID, Stream: stream, Entry: entry, LatestSeq: cursor.LatestSeq})
}
}
sort.SliceStable(history, func(i, j int) bool {
left, right := history[i], history[j]
if !left.Entry.Timestamp.Equal(right.Entry.Timestamp) {
return left.Entry.Timestamp.Before(right.Entry.Timestamp)
}
if left.Entry.Seq != right.Entry.Seq {
return left.Entry.Seq < right.Entry.Seq
}
return left.Stream.ID < right.Stream.ID
})
if len(history) > limit {
history = history[len(history)-limit:]
}
return history, nil
}
func parseLogEventHistoryLimit(value string) int {
if strings.TrimSpace(value) == "" {
return defaultLogEventHistoryLimit
}
limit, err := strconv.Atoi(value)
if err != nil || limit < 0 {
return defaultLogEventHistoryLimit
}
if limit > maxLogEventHistoryLimit {
return maxLogEventHistoryLimit
}
return limit
}
func writeSSEJSON(w http.ResponseWriter, eventName string, id string, value any) error {
payload, err := json.Marshal(value)
if err != nil {