-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_run.go
More file actions
267 lines (239 loc) · 7.58 KB
/
Copy pathagent_run.go
File metadata and controls
267 lines (239 loc) · 7.58 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package agentgo
import (
"context"
"errors"
"fmt"
)
// buildConfig constructs a LoopConfig from the agent's settings. Must be called with lock held.
func (a *Agent) buildConfig(continuing bool) LoopConfig {
skipInitialSteering := a.skipNextInitialSteeringPoll
a.skipNextInitialSteeringPoll = false
initialState := a.stateLocked()
initialState.Progress = activateRunProgress(initialState.Progress, continuing)
return LoopConfig{
Model: a.model,
MaxTurns: a.maxTurns,
MaxRetries: a.maxRetries,
MaxToolErrors: a.maxToolErrors,
ThinkingLevel: a.thinkingLevel,
InitialState: initialState,
ContextManager: a.contextManager,
ToolResultMessageFactory: a.toolResultFactory,
CommitContext: func(msgs []AgentMessage, usage *ContextUsage) error {
a.mu.Lock()
defer a.mu.Unlock()
a.messages = copyMessages(msgs)
a.syncContextManagerLocked()
return nil
},
CommitMessage: a.messageCommitter,
ToolGate: a.toolGate,
GetSteeringMessages: func() []AgentMessage {
a.mu.Lock()
defer a.mu.Unlock()
if skipInitialSteering {
skipInitialSteering = false
return nil
}
return dequeue(&a.steeringQ)
},
GetFollowUpMessages: func() []AgentMessage {
a.mu.Lock()
defer a.mu.Unlock()
return dequeue(&a.followUpQ)
},
ModelMiddlewares: a.modelMiddlewares,
ToolMiddlewares: a.toolMiddlewares,
MaxToolConcurrency: a.maxToolConcurrency,
ShouldEmitAbortMarker: a.wantAbortMarker.Load,
OnMessage: a.onMessage,
BeforeTurn: a.beforeTurn,
AfterTurn: a.afterTurn,
StopGuard: a.stopGuard,
LengthRecoveryPrompt: a.lengthRecoveryPrompt,
AbortMarkerText: a.abortMarkerText,
AbortMarkerToolText: a.abortMarkerToolText,
CacheLastMessage: a.cacheLastMessage,
PromptCacheKey: a.promptCacheKey,
}
}
// consumeLoop projects loop events into the stateful Agent view. It must not
// create AgentMessages: new entries come only from committed EventMessageEnd
// events, while Loop-owned state snapshots may replace the projected history.
func (a *Agent) consumeLoop(runCtx context.Context, kind RunKind, events <-chan Event) {
// Capture this run's done channel and cancel up front. A new run can take
// over before this loop's defer runs — an auto-continue may start it from
// this run's EventAgentEnd listener — reassigning a.done/a.cancel to that
// run. Holding our own copies keeps the defer from touching the newer run.
a.mu.Lock()
myDone := a.done
myCancel := a.cancel
a.mu.Unlock()
defer func() {
a.mu.Lock()
// Release this run's derived ctx now that the loop has drained — chiefly
// the propagation goroutine context starts when the caller passes a
// cancellable parent. myCancel is this run's own, so this never touches a
// newer run's ctx.
if myCancel != nil {
myCancel()
}
// Reset shared run-state only if no newer run has taken over. An
// auto-continue can start the next run from this run's EventAgentEnd
// listener (which fires before this defer), reassigning a.done/a.cancel/
// isRunning to that run — stomping them here would abort it.
if a.done == myDone {
a.isRunning = false
a.streamMessage = nil
a.pendingToolCalls = make(map[string]struct{})
a.cancel = nil
a.wantAbortMarker.Store(false)
}
a.mu.Unlock()
close(myDone)
}()
for ev := range events {
if ev.Type == EventAgentEnd {
a.consumeAgentEnd(runCtx, kind, ev)
continue
}
a.mu.Lock()
switch ev.Type {
case EventAgentStart:
if ev.State != nil {
a.applyLoopStateLocked(*ev.State)
}
// Message lifecycle
case EventMessageStart:
a.streamMessage = ev.Message
case EventMessageUpdate:
a.streamMessage = ev.Message
case EventMessageEnd:
a.streamMessage = nil
if ev.Message != nil {
a.messages = append(a.messages, ev.Message)
a.syncContextManagerLocked()
// Accumulate usage from assistant messages
if msg, ok := ev.Message.(Message); ok && msg.Usage != nil {
a.totalUsage.Add(msg.Usage)
}
}
// Tool execution lifecycle
case EventToolExecStart:
if ev.ToolID != "" {
a.pendingToolCalls[ev.ToolID] = struct{}{}
}
case EventToolExecEnd:
delete(a.pendingToolCalls, ev.ToolID)
// Turn end
case EventModelResponse:
if msg, ok := ev.Message.(Message); ok {
if errStr, _ := msg.Metadata["error_message"].(string); errStr != "" {
a.lastError = errStr
}
}
case EventTurnEnd:
if ev.State != nil {
a.applyLoopStateLocked(*ev.State)
}
// Execution errors are runtime facts, not conversation messages. A model
// may still return a terminal Message with StopReasonError; the Loop
// commits that through EventMessageEnd like any other model outcome.
case EventError:
// Clear the externally-visible streaming view: listeners may
// call State() from this EventError callback, and a stale
// streamMessage would surface a never-completing partial that the
// agent has already abandoned. Cleared here, before listeners run
// (notifications fire after the lock is released below).
a.streamMessage = nil
if ev.Err != nil && !errors.Is(ev.Err, context.Canceled) {
a.lastError = ev.Err.Error()
}
}
// Copy listeners to avoid holding lock during callback
listeners := make([]func(Event), len(a.listeners))
copy(listeners, a.listeners)
a.mu.Unlock()
for _, fn := range listeners {
if fn != nil {
fn(ev)
}
}
}
}
// consumeAgentEnd crosses from the AgentLoop terminal event back into the
// stateful Agent boundary. AfterRun deliberately executes here, outside the
// Loop goroutine, after final state projection and before terminal listeners
// may start another run.
func (a *Agent) consumeAgentEnd(runCtx context.Context, kind RunKind, ev Event) {
a.runMu.Lock()
a.mu.Lock()
if ev.State != nil {
a.applyLoopStateLocked(*ev.State)
}
a.isRunning = false
a.streamMessage = nil
a.pendingToolCalls = make(map[string]struct{})
hook := a.afterRun
snapshot := a.snapshotLocked()
a.mu.Unlock()
var hookErr error
if hook != nil {
summary := RunSummary{EndReason: EndReasonError}
if ev.Summary != nil {
summary = *ev.Summary
}
if err := callAfterRun(context.WithoutCancel(runCtx), hook, AfterRunContext{
Kind: kind,
Snapshot: snapshot,
Summary: summary,
Err: ev.Err,
}); err != nil {
hookErr = fmt.Errorf("after run: %w", err)
ev.Err = errors.Join(ev.Err, hookErr)
summary.EndReason = EndReasonError
ev.Summary = &summary
}
}
a.mu.Lock()
if hookErr != nil {
a.lastError = ev.Err.Error()
finalState := a.stateLocked()
ev.State = &finalState
}
listeners := make([]func(Event), len(a.listeners))
copy(listeners, a.listeners)
a.mu.Unlock()
a.runMu.Unlock()
if hookErr != nil {
notifyListeners(listeners, Event{Type: EventError, Err: hookErr})
}
notifyListeners(listeners, ev)
}
func callAfterRun(ctx context.Context, hook AfterRunHook, run AfterRunContext) (err error) {
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("panic: %v", recovered)
}
}()
return hook(ctx, run)
}
func notifyListeners(listeners []func(Event), event Event) {
for _, listener := range listeners {
if listener != nil {
listener(event)
}
}
}
func (a *Agent) applyLoopStateLocked(state AgentState) {
a.messages = copyMessages(state.Messages)
a.totalUsage = state.TotalUsage
a.runProgress = cloneRunProgress(state.Progress)
a.syncContextManagerLocked()
}
func (a *Agent) syncContextManagerLocked() {
if a.contextManager == nil {
return
}
a.contextManager.Sync(copyMessages(a.messages))
}