返回 DeepSeek-Reasonix
queue.go
根目录 / internal / memory / queue.go
1 package memory
2
3 import (
4 "context"
5 "encoding/json"
6 )
7
8 // Queue receives a one-line note about a memory change a tool just made, so the
9 // controller can fold it into the current turn — taking effect this session
10 // without touching the cache-stable system prefix. The remember/forget tools
11 // read it from their call context the same way background tools read the job
12 // manager.
13 type Queue interface{ QueueMemory(note string) }
14
15 type autoMemoryWriteClaimer interface {
16 ClaimAutoMemoryWrite(args json.RawMessage) bool
17 }
18
19 type queueKey struct{}
20 type noQueue struct{}
21
22 // WithQueue stamps q onto ctx for the remember/forget tools to find.
23 func WithQueue(ctx context.Context, q Queue) context.Context {
24 return context.WithValue(ctx, queueKey{}, q)
25 }
26
27 // WithoutQueue shadows an ancestor queue. Child agents may persist memory but
28 // must not inject turn-tail notes into the parent's live conversation.
29 func WithoutQueue(ctx context.Context) context.Context {
30 return context.WithValue(ctx, queueKey{}, noQueue{})
31 }
32
33 // QueueFromContext returns the memory queue the agent stamped, if any.
34 func QueueFromContext(ctx context.Context) (Queue, bool) {
35 q, ok := ctx.Value(queueKey{}).(Queue)
36 return q, ok && q != nil
37 }
38
39 // ClaimAutoMemoryWriteFromContext consumes a host-issued create-only grant.
40 // Manual/approved writes have no claim and retain the legacy update behavior.
41 func ClaimAutoMemoryWriteFromContext(ctx context.Context, args json.RawMessage) bool {
42 q, ok := QueueFromContext(ctx)
43 if !ok {
44 return false
45 }
46 claimer, ok := q.(autoMemoryWriteClaimer)
47 return ok && claimer.ClaimAutoMemoryWrite(args)
48 }
49
49 lines GO