| 1 | package persistentshell |
| 2 | |
| 3 | import "context" |
| 4 | |
| 5 | type ctxKey struct{} |
| 6 | |
| 7 | // WithManager attaches m to ctx so a nested run (a sub-agent, for example) |
| 8 | // owns a private persistent shell distinct from its parent. |
| 9 | func WithManager(ctx context.Context, m *Manager) context.Context { |
| 10 | if ctx == nil { |
| 11 | ctx = context.Background() |
| 12 | } |
| 13 | if m == nil { |
| 14 | return ctx |
| 15 | } |
| 16 | return context.WithValue(ctx, ctxKey{}, m) |
| 17 | } |
| 18 | |
| 19 | // FromContext returns the Manager attached by WithManager, or nil. |
| 20 | func FromContext(ctx context.Context) *Manager { |
| 21 | if ctx == nil { |
| 22 | return nil |
| 23 | } |
| 24 | m, _ := ctx.Value(ctxKey{}).(*Manager) |
| 25 | return m |
| 26 | } |
| 27 |