| 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 | |
| 21 | // WithQueue stamps q onto ctx for the remember/forget tools to find. |
| 22 | func WithQueue(ctx context.Context, q Queue) context.Context { |
| 23 | return context.WithValue(ctx, queueKey{}, q) |
| 24 | } |
| 25 | |
| 26 | // QueueFromContext returns the memory queue the agent stamped, if any. |
| 27 | func QueueFromContext(ctx context.Context) (Queue, bool) { |
| 28 | q, ok := ctx.Value(queueKey{}).(Queue) |
| 29 | return q, ok && q != nil |
| 30 | } |
| 31 | |
| 32 | // ClaimAutoMemoryWriteFromContext consumes a host-issued create-only grant. |
| 33 | // Manual/approved writes have no claim and retain the legacy update behavior. |
| 34 | func ClaimAutoMemoryWriteFromContext(ctx context.Context, args json.RawMessage) bool { |
| 35 | q, ok := QueueFromContext(ctx) |
| 36 | if !ok { |
| 37 | return false |
| 38 | } |
| 39 | claimer, ok := q.(autoMemoryWriteClaimer) |
| 40 | return ok && claimer.ClaimAutoMemoryWrite(args) |
| 41 | } |
| 42 |