[Go SDK] Add real-time memory change subscriptions via WebSocket
Python and TypeScript SDKs can subscribe to memory changes in real-time using WebSocket connections. This enables reactive patterns where agents respond to state changes immediately.
What we need
Add memory event subscription capability to the Go SDK.
Developer Experience
// Subscribe to memory changes with pattern matching
subscription, err := agent.Memory.Watch(ctx, memory.WatchOptions{
Patterns: []string{"user_*", "config.*"},
Scope: memory.ScopeSession,
})
if err != nil {
log.Fatal(err)
}
defer subscription.Close()
// Process events as they arrive
for event := range subscription.Events() {
switch event.Type {
case memory.EventCreated:
fmt.Printf("New key: %s = %v\n", event.Key, event.Value)
case memory.EventUpdated:
fmt.Printf("Updated: %s = %v\n", event.Key, event.Value)
case memory.EventDeleted:
fmt.Printf("Deleted: %s\n", event.Key)
}
}
// Alternative: callback-based API
agent.Memory.OnChange(ctx, "user_*.preferences", func(event memory.ChangeEvent) {
fmt.Printf("User preferences changed: %s\n", event.Key)
})
Types to Add
type WatchOptions struct {
Patterns []string
Scope MemoryScope
ScopeID string
}
type ChangeEvent struct {
Key string
Value any
Type EventType // Created, Updated, Deleted
Scope MemoryScope
ScopeID string
Timestamp time.Time
ActorID string
}
type Subscription interface {
Events() <-chan ChangeEvent
Close() error
}
Pattern Matching
Support glob-style patterns:
user_*matchesuser_123,user_preferences, etc.config.*matchesconfig.theme,config.language, etc.*matches everything
Reference Implementation
- Python:
sdk/python/agentfield/memory_events.py-MemoryEventClient,@on_changedecorator - TypeScript:
sdk/typescript/src/memory/MemoryEventClient.ts-watchMemory()
Files to Create
sdk/go/agent/memory_events.go- WebSocket client and subscription management
Documentation (internal team)
This adds new real-time capabilities. Update the Go SDK documentation to include:
- Watch/subscription patterns and options
- Event types and their meanings
- Pattern matching syntax
- Best practices for long-running subscriptions
- Error handling and reconnection behavior
Hi! I’m interested in contributing Go SDK memory subscriptions for this issue. Is this still open for external contributions?
For an initial PR, I’d propose:
Memory.Watchwith scope, scope ID, and key-pattern filtering through the existing control-plane WebSocket endpoint.- Context cancellation, safe subscription closure, and bounded reconnect attempts.
- Explicit buffering and disconnect semantics, without promising lossless replay.
- Tests and a usage example, while keeping existing
MemoryBackendimplementations compatible.
Does this scope still match your expectations? Is anyone already working on it, or are there newer API decisions I should follow?
I'd like to pick this up. Before writing code I checked the current state on main (v0.1.140) against the issue body, and a few things have drifted since this was filed, so I want to confirm the API decisions first. @FriendlyPasser asked much the same in the comment above.
Server side is ready and unchanged in shape. routes_memory.go registers three endpoints, with patterns (comma-joined), scope, scope_id as query params:
GET /api/v1/memory/events/wsGET /api/v1/memory/events/sseGET /api/v1/memory/events/history
Go SDK has nothing yet (no memory_events.go, no Watch/OnChange), so this is genuinely unimplemented.
1. The event type in the issue doesn't match the wire format. The authoritative shape is control-plane/pkg/types/types.go:643:
type MemoryChangeEvent struct {
ID string
Type string // "memory_change"
Timestamp time.Time
Scope string
ScopeID string
Key string
Action string // "set" or "delete" only
Data json.RawMessage
PreviousData json.RawMessage
Metadata EventMetadata // AgentID, ActorID, WorkflowID
}
The issue proposes Type EventType with Created/Updated/Deleted, a flat Value, and a top-level ActorID. The server only emits set/delete (internal/handlers/memory.go:179,328), Data/PreviousData are raw JSON, and ActorID is nested under Metadata.
Which do you want for Go?
- (a) Mirror the server verbatim:
Action string,Data/PreviousDataasjson.RawMessage,Metadatanested. Honest about the wire, consistent with Python/TS, no invented information. - (b) Honour the issue's sketch and synthesise
CreatedvsUpdatedby checking whetherPreviousDatais empty. That's a guess at the boundary (asetwith no previous value could be a first write or a write of a previously-null value), and neither Python nor TS exposes the distinction today, so Go would be the odd one out.
I'd go with (a) and keep the Go type a faithful mirror, but it's your call since the issue text says otherwise.
2. This would add the Go SDK's first WebSocket dependency. sdk/go/go.mod currently has three direct deps (jsonschema, testify, yaml) and no WS client anywhere. The control plane already uses github.com/gorilla/websocket v1.5.3, so my default would be to match that rather than introduce a second WS library into the repo. Confirm that's acceptable, or say if you'd rather have coder/websocket or keep the SDK dependency-free (in which case SSE over net/http is the only realistic option, see below).
3. Pattern-matching semantics differ between the server and the Python SDK. The server filters with filepath.Match (memory_events.go:97,158), where * does not cross a separator and . is literal. Python's PatternMatcher rewrites * to .* as a regex without escaping ., so config.* there also matches configX. For Go I'd rely on server-side filtering and use filepath.Match for any client-side check, which keeps Go consistent with the server. Worth noting the Python divergence separately if you want it aligned.
4. SSE isn't mentioned in the issue but exists. Should the first PR ship WebSocket only, or Watch over WS with SSE as a fallback transport? I'd start with WebSocket alone and leave SSE for a follow-up unless you want both.
Proposed scope for the first PR, assuming (a) + gorilla:
sdk/go/agent/memory_events.go:Memory.Watch(ctx, WatchOptions) (Subscription, error)withEvents() <-chan MemoryChangeEventandClose() error, plusOnChange(ctx, patterns, handler)on top of it.- Server-side filtering via
patterns/scope/scope_id; context cancellation; bounded exponential reconnect capped at 30s to match the TS client; documented buffer bound and drop-on-slow-consumer behaviour, with no lossless-replay promise (history()is the backfill path). history()against/events/history.- Tests against an
httptestWebSocket server (no live control plane), covering filter wiring, event decode, reconnect, and cleanClose. - No changes to existing
MemoryBackendimplementations.
Happy to follow whatever you decide on points 1, 2 and 4. If @FriendlyPasser is already mid-implementation I'll back off, I just didn't see a PR linked.
This seems to have gone stale. @santoshkumarradha is this still a requirement? I'm happy to pick it up