-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchannel.go
More file actions
252 lines (232 loc) · 6.31 KB
/
Copy pathchannel.go
File metadata and controls
252 lines (232 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"fmt"
"sort"
"sync"
"time"
)
// Channel is a persistent, 2-party message log keyed by the unordered pair of
// peer names. No turn FSM, no session ID — either side may tell at any time;
// read pulls the oldest unread message from the caller's mailbox. Messages
// are durable in the git-backed workspace under peers/<a>--<b>/.
type Channel struct {
PeerA string
PeerB string
seq int
log []Message
mailbox map[string][]Message
waiter map[string]chan waitResult
mu sync.Mutex
}
type waitResult struct {
msg *Message
err string
code int
}
// channelKey returns the canonical (sorted) key for a peer-pair channel.
func channelKey(a, b string) string {
if a > b {
a, b = b, a
}
return a + "\x00" + b
}
// channelDir returns the workspace-relative directory for a peer-pair
// channel's transcripts: "peers/<lo>--<hi>".
func channelDir(a, b string) string {
if a > b {
a, b = b, a
}
return "peers/" + safePathSegment(a) + "--" + safePathSegment(b)
}
func newChannel(a, b string) *Channel {
if a > b {
a, b = b, a
}
return &Channel{
PeerA: a,
PeerB: b,
mailbox: make(map[string][]Message),
waiter: make(map[string]chan waitResult),
}
}
func (c *Channel) peerOf(name string) (string, bool) {
switch name {
case c.PeerA:
return c.PeerB, true
case c.PeerB:
return c.PeerA, true
default:
return "", false
}
}
// tell appends a message and delivers it to the recipient's mailbox (or to a
// blocked read waiter, or to a cross-channel read-any waiter). Returns
// immediately; never blocks.
func (c *Channel) tell(s *State, from, body string) Response {
c.mu.Lock()
peer, ok := c.peerOf(from)
if !ok {
c.mu.Unlock()
return errorResponse(CodeError, "channel_not_participant", "use a registered peer channel", "not a participant of this channel", map[string]any{
"from": from,
})
}
c.seq++
msg := Message{
Seq: c.seq,
From: from,
TS: time.Now(),
Body: body,
}
c.log = append(c.log, msg)
s.enqueueWrite(
fmt.Sprintf("%s/%06d-%s.md", channelDir(c.PeerA, c.PeerB), msg.Seq, safePathSegment(from)),
renderChannelMsg(msg),
fmt.Sprintf("peer msg %d %s→%s", msg.Seq, from, peer),
)
if ch, ok := c.waiter[peer]; ok {
ch <- waitResult{msg: &msg}
delete(c.waiter, peer)
} else if s.deliverAny(peer, "peer", other(c, peer), msg) {
// delivered to cross-channel read-any
} else {
c.mailbox[peer] = append(c.mailbox[peer], msg)
if s.queue != nil {
_ = s.queue.mailboxAppend(peer, "peer", from, msg.Seq, from, msg.TS, msg.Body)
}
}
c.mu.Unlock()
return Response{OK: true, Data: map[string]any{"seq": msg.Seq, "peer": peer}}
}
// read consumes and returns the oldest message in the caller's mailbox. If
// the mailbox is empty and timeout > 0, blocks up to timeout for a new
// message. timeout == 0 returns immediately with an empty result.
func (c *Channel) read(s *State, from string, timeout time.Duration) Response {
c.mu.Lock()
_, ok := c.peerOf(from)
if !ok {
c.mu.Unlock()
return errorResponse(CodeError, "channel_not_participant", "use a registered peer channel", "not a participant of this channel", map[string]any{
"from": from,
})
}
if q, ok := c.mailbox[from]; ok && len(q) > 0 {
msg := q[0]
c.mailbox[from] = q[1:]
if s.queue != nil {
_ = s.queue.mailboxDeleteOne(from, "peer", other(c, from), msg.Seq)
}
c.mu.Unlock()
return msgResponse(&msg)
}
if timeout <= 0 {
c.mu.Unlock()
return Response{OK: true, Data: map[string]any{}}
}
ch := make(chan waitResult, 1)
c.waiter[from] = ch
c.mu.Unlock()
select {
case r := <-ch:
if r.err != "" {
reason := "read_failed"
retry := ""
if r.code == CodePeerClosed {
reason = "peer_closed"
retry = "re-register and retry read"
}
return errorResponse(r.code, reason, retry, r.err, map[string]any{
"from": from,
})
}
return msgResponse(r.msg)
case <-time.After(timeout):
c.mu.Lock()
delete(c.waiter, from)
c.mu.Unlock()
return Response{OK: true, Data: map[string]any{}}
}
}
// peek returns all pending messages without consuming.
func (c *Channel) peek(from string) Response {
c.mu.Lock()
defer c.mu.Unlock()
_, ok := c.peerOf(from)
if !ok {
return errorResponse(CodeError, "channel_not_participant", "use a registered peer channel", "not a participant of this channel", map[string]any{
"from": from,
})
}
q := c.mailbox[from]
out := make([]any, 0, len(q))
for _, m := range q {
out = append(out, map[string]any{
"seq": m.Seq,
"from": m.From,
"body": m.Body,
"ts": m.TS.Format(time.RFC3339),
})
}
return Response{OK: true, Data: map[string]any{"peer": other(c, from), "messages": out}}
}
// releaseWaiter signals an in-flight read with peer_closed. Called when an
// agent's lease expires so their own hanging read returns immediately.
func (c *Channel) releaseWaiter(name, reason string) {
c.mu.Lock()
defer c.mu.Unlock()
if ch, ok := c.waiter[name]; ok {
ch <- waitResult{err: reason, code: CodePeerClosed}
delete(c.waiter, name)
}
}
func msgResponse(m *Message) Response {
return Response{OK: true, Data: map[string]any{
"seq": m.Seq,
"from": m.From,
"body": m.Body,
"ts": m.TS.Format(time.RFC3339),
}}
}
func other(c *Channel, from string) string {
if from == c.PeerA {
return c.PeerB
}
return c.PeerA
}
func renderChannelMsg(m Message) []byte {
body := m.Body
if len(body) == 0 || body[len(body)-1] != '\n' {
body += "\n"
}
return []byte(fmt.Sprintf("---\nseq: %d\nfrom: %s\nts: %s\n---\n\n%s",
m.Seq, m.From, m.TS.Format(time.RFC3339), body))
}
// listChannels returns channels the named agent participates in, sorted by
// key for deterministic output.
func (s *State) listChannels(name string) []*Channel {
s.mu.Lock()
defer s.mu.Unlock()
out := make([]*Channel, 0)
for _, c := range s.channels {
if c.PeerA == name || c.PeerB == name {
out = append(out, c)
}
}
sort.Slice(out, func(i, j int) bool {
return channelKey(out[i].PeerA, out[i].PeerB) < channelKey(out[j].PeerA, out[j].PeerB)
})
return out
}
// getOrCreateChannel resolves or implicitly creates the peer-pair channel.
// Caller must ensure `from` and `peer` are registered agents.
func (s *State) getOrCreateChannel(from, peer string) *Channel {
key := channelKey(from, peer)
s.mu.Lock()
defer s.mu.Unlock()
if c, ok := s.channels[key]; ok {
return c
}
c := newChannel(from, peer)
s.channels[key] = c
return c
}