Something went wrong. Try again.
Read-only, events-only MCP server for GitHub
Something went wrong. Try again.
github-events-mcp cursor.go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168package main
import ( "bytes" "crypto/sha256" "encoding/base64" "encoding/hex" "encoding/json" "errors" "io" "sort" "time")
const replayWindow = 7 * 24 * time.Hourconst indexOverlap = 2 * time.Minuteconst maxSeenIDs = 10000
// maxCursorBytes is the largest events/poll cursor albedo accepts, measured on the// base64url wire form (albedo extensions/mcp/src/albedo_ext_mcp/events_wire.gleam,// max_cursor_bytes). A larger cursor is refused and the subscription is paused, so// every cursor this server returns must fit.const maxCursorBytes = 4096
type cursorState struct { Version int `json:"v"` Subscription string `json:"s"` Start time.Time `json:"start"` At time.Time `json:"at"` Seen map[string]time.Time `json:"seen"` Upstream string `json:"upstream,omitempty"` UpstreamSince time.Time `json:"upstreamSince,omitzero"` ReadTo time.Time `json:"readTo,omitzero"` Drain string `json:"drain,omitempty"`}
func hashParts(parts ...string) string { encoded, _ := json.Marshal(parts) sum := sha256.Sum256(encoded) return hex.EncodeToString(sum[:])}
func subscriptionKey(name string, args Arguments) string { encoded, _ := json.Marshal(args) return hashParts(name, string(encoded))}
func cursorBytes(c cursorState) string { encoded, _ := json.Marshal(c) return base64.RawURLEncoding.EncodeToString(encoded)}
// encodeCursor keeps the cursor under albedo's size limit. When the state does not// fit, the oldest seen identities are dropped: a dropped identity may at worst be// delivered twice, and albedo deduplicates up to 128 event ids, so a duplicate is// always preferable to a cursor albedo refuses and a paused subscription. The newest// identities survive because they are the ones still inside the overlap window.func encodeCursor(c cursorState) string { if encoded := cursorBytes(c); len(encoded) <= maxCursorBytes { return encoded } ids := make([]string, 0, len(c.Seen)) for id := range c.Seen { ids = append(ids, id) } sort.Slice(ids, func(i, j int) bool { if a, b := c.Seen[ids[i]], c.Seen[ids[j]]; !a.Equal(b) { return a.After(b) } return ids[i] < ids[j] }) keep := func(n int) cursorState { trimmed := c trimmed.Seen = make(map[string]time.Time, n) for _, id := range ids[:n] { trimmed.Seen[id] = c.Seen[id] } return trimmed } // Dropping an identity only shrinks the payload, so the largest fitting prefix // of the newest identities is a binary search. low, high := 0, len(ids) for low < high { mid := low + (high-low+1)/2 if len(cursorBytes(keep(mid))) <= maxCursorBytes { low = mid } else { high = mid - 1 } } return cursorBytes(keep(low))}
// pruneSeen drops seen identities the next fetch can no longer re-deliver: it filters// events before deduplication, so any identity older than the fetch watermark is dead// weight that only grows the cursor.func pruneSeen(c cursorState, since time.Time) cursorState { for id, at := range c.Seen { if at.Before(since) { delete(c.Seen, id) } } return c}
func decodeCursor(encoded, key string, now time.Time) (cursorState, error) { var c cursorState if len(encoded) > 2<<20 { return c, errors.New("cursor too large") } raw, err := base64.RawURLEncoding.DecodeString(encoded) if err != nil { return c, errors.New("invalid cursor") } decoder := json.NewDecoder(bytes.NewReader(raw)) decoder.DisallowUnknownFields() if err := decoder.Decode(&c); err != nil { return c, errors.New("invalid cursor") } if err := decoder.Decode(new(any)); err != io.EOF { return c, errors.New("invalid cursor") } if c.Version != 1 || c.Subscription != key || c.At.IsZero() || c.Start.IsZero() || c.At.Before(c.Start) || c.At.After(now.Add(time.Minute)) || len(c.Seen) > maxSeenIDs || len(c.Upstream) > 100<<10 || (!c.UpstreamSince.IsZero() && c.UpstreamSince.After(c.At)) || (c.Drain != "" && len(c.Drain) != 64) || (!c.ReadTo.IsZero() && (c.ReadTo.Before(c.At) || c.ReadTo.After(now.Add(time.Minute)))) { return c, errors.New("invalid cursor") } for id, at := range c.Seen { if len(id) != 64 || at.Before(c.Start) || at.After(maxTime(c.At, c.ReadTo)) { return c, errors.New("invalid cursor") } } if c.Seen == nil { c.Seen = make(map[string]time.Time) } return c, nil}
func newCursor(key string, now time.Time) cursorState { return cursorState{Version: 1, Subscription: key, Start: now, At: now, Seen: make(map[string]time.Time)}}
func cursorFloor(c cursorState, now time.Time, maxAge *int64) (cursorState, time.Time, bool) { floor := now.Add(-replayWindow) if maxAge != nil { floor = maxTime(floor, now.Add(-time.Duration(min(*maxAge, int64(replayWindow/time.Millisecond)))*time.Millisecond)) } truncated := c.At.Before(floor) if truncated { c.Start, c.At, c.Seen = floor, floor, make(map[string]time.Time) c.Upstream, c.ReadTo, c.Drain, c.UpstreamSince = "", time.Time{}, "", time.Time{} } // Even without a gap, maxAge may narrow the overlap window. since := maxTime(c.Start, c.At.Add(-indexOverlap)) since = maxTime(since, floor) return c, since, truncated}
func maxTime(a, b time.Time) time.Time { if a.After(b) { return a } return b}