feat(agent): implement graceful finish vs hard abort for SubTurn lifecycle
Problem: When parent turn finishes early, all child SubTurns receive "context canceled" error,because child context was derived from parent context. Solution: Implement a lifecycle management system that distinguishes between: - Graceful finish (Finish(false)): signals parentEnded, children continue - Hard abort (Finish(true)): immediately cancels all children Changes: - turn_state.go: - Add parentEnded atomic.Bool to signal parent completion - Add parentTurnState reference for IsParentEnded() checks - Modify Finish(isHardAbort bool) to distinguish abort types - subturn.go: - Add Critical bool to SubTurnConfig (Critical SubTurns continue after parent ends) - Add Timeout time.Duration for SubTurn self-protection - Use independent context (context.Background()) instead of derived context - SubTurns check IsParentEnded() to decide whether to continue or exit - loop.go: - Call Finish(false) for normal completion (graceful) - Add IsParentEnded() check in LLM iteration loop - steering.go: - HardAbort calls Finish(true) to immediately cancel children Behavior: - Normal finish: parentEnded=true, children continue, orphan results delivered - Hard abort: all children cancelled immediately via context - Critical SubTurns: continue running after parent finishes gracefully - Non-Critical SubTurns: can exit gracefully when IsParentEnded() returns true
This commit is contained in:
1 parent
e05d2620e1
commit
f8defe3ae1
5 files changed
+284
-62
No files matched your search
+19
-2
@@ -1073,10 +1073,12 @@ func (al *AgentLoop) runAgentLoop(
|
||||
}
|
||||
}
|
||||
|
||||
// Signal completion to rootTS so it knows it is finished, terminating any active sub-turns.
|
||||
// Signal completion to rootTS so it knows it is finished.
|
||||
// Only call Finish() if this is a root turn (not a SubTurn recursively calling runAgentLoop).
|
||||
// Use isHardAbort=false for normal completion (graceful finish).
|
||||
// This allows Critical SubTurns to continue running and deliver orphan results.
|
||||
if isRootTurn {
|
||||
rootTS.Finish()
|
||||
rootTS.Finish(false)
|
||||
}
|
||||
|
||||
// If last tool had ForUser content and we already sent it, we might not need to send final response
|
||||
@@ -1211,6 +1213,21 @@ func (al *AgentLoop) runLLMIteration(
|
||||
for iteration < agent.MaxIterations || len(pendingMessages) > 0 {
|
||||
iteration++
|
||||
|
||||
// Check if parent turn has ended (graceful finish).
|
||||
// This is only relevant for SubTurns (turnState with parentTurnState != nil).
|
||||
// If parent ended and this SubTurn is not Critical, exit gracefully.
|
||||
if ts := turnStateFromContext(ctx); ts != nil && ts.IsParentEnded() {
|
||||
logger.InfoCF("agent", "Parent turn ended, SubTurn continues or exits", map[string]any{
|
||||
"agent_id": agent.ID,
|
||||
"iteration": iteration,
|
||||
"turn_id": ts.turnID,
|
||||
})
|
||||
// For now, we continue running. The Critical flag check is handled
|
||||
// at SubTurnConfig level in spawnSubTurn. Here we just log and continue.
|
||||
// If this SubTurn should exit gracefully, it would have been cancelled
|
||||
// by its own timeout or the caller would have handled it.
|
||||
}
|
||||
|
||||
// Inject pending steering messages into the conversation context
|
||||
// before the next LLM call.
|
||||
if len(pendingMessages) > 0 {
|
||||
|
||||
Reference in new issue
Block a user