Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/agent/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,20 @@ func (al *AgentLoop) ProcessDirectWithChannel(

// ProcessHeartbeat processes a heartbeat request without session history.
// Each heartbeat is independent and doesn't accumulate context.
// If the agent is already processing another turn, the heartbeat is skipped.
func (al *AgentLoop) ProcessHeartbeat(
ctx context.Context,
content, channel, chatID string,
) (string, error) {
// Skip heartbeat if the agent is busy with another turn
if ts := al.getAnyActiveTurnState(); ts != nil {
logger.InfoCF("heartbeat", "Skipping heartbeat β€” agent is busy",
map[string]any{
"session_key": ts.sessionKey,
})
return "HEARTBEAT_SKIPPED", nil
}

if err := al.ensureHooksInitialized(ctx); err != nil {
return "", err
}
Expand Down
111 changes: 111 additions & 0 deletions pkg/agent/loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2132,3 +2132,114 @@ func TestFilterClientWebSearch_EmptyInput(t *testing.T) {
t.Fatalf("len(result) = %d, want 0", len(result))
}
}

func TestProcessHeartbeat_SkipsWhenAgentBusy(t *testing.T) {
al, _, _, _, cleanup := newTestAgentLoop(t)
defer cleanup()

// Simulate an active turn by storing a turnState directly
busyTS := &turnState{
sessionKey: "user-chat",
agentID: "main",
phase: TurnPhaseRunning,
startedAt: time.Now(),
}
al.activeTurnStates.Store(busyTS.sessionKey, busyTS)
defer al.activeTurnStates.Delete(busyTS.sessionKey)

resp, err := al.ProcessHeartbeat(context.Background(), "heartbeat prompt", "telegram", "chat123")
if err != nil {
t.Fatalf("ProcessHeartbeat returned error: %v", err)
}
if resp != "HEARTBEAT_OK" {
t.Fatalf("expected HEARTBEAT_OK when busy, got %q", resp)
}
}

func TestProcessHeartbeat_RunsWhenIdle(t *testing.T) {
al, _, _, _, cleanup := newTestAgentLoop(t)
defer cleanup()

// Verify no active turns exist
if ts := al.getAnyActiveTurnState(); ts != nil {
t.Fatal("expected no active turns in fresh AgentLoop")
}

// ProcessHeartbeat should proceed (not return early with HEARTBEAT_OK skip)
// and, with the mock provider used in newTestAgentLoop, return a deterministic
// mock response.
resp, err := al.ProcessHeartbeat(context.Background(), "heartbeat prompt", "cli", "direct")

// Must not skip due to busy check.
if resp == "HEARTBEAT_OK" {
t.Fatal("ProcessHeartbeat skipped despite no active turns")
}

// Given the mock provider, the heartbeat call should succeed with a fixed response.
if err != nil {
t.Fatalf("expected no error from ProcessHeartbeat when idle, got: %v", err)
}
if resp != "Mock response" {
t.Fatalf("expected mock provider response %q, got %q", "Mock response", resp)
}
}

func TestProcessHeartbeat_SkipsForAnyActiveSession(t *testing.T) {
al, _, _, _, cleanup := newTestAgentLoop(t)
defer cleanup()

// Register multiple active turns to cover the sync.Map iteration
for _, key := range []string{"session-a", "session-b"} {
ts := &turnState{
sessionKey: key,
agentID: "main",
phase: TurnPhaseRunning,
startedAt: time.Now(),
}
al.activeTurnStates.Store(key, ts)
defer al.activeTurnStates.Delete(key)
}

resp, err := al.ProcessHeartbeat(context.Background(), "heartbeat prompt", "telegram", "chat123")
if err != nil {
t.Fatalf("ProcessHeartbeat returned error: %v", err)
}
if resp != "HEARTBEAT_OK" {
t.Fatalf("expected HEARTBEAT_OK when agent has active turns, got %q", resp)
}
}

func TestProcessHeartbeat_ProceedsAfterTurnClears(t *testing.T) {
al, _, _, _, cleanup := newTestAgentLoop(t)
defer cleanup()

// Simulate a turn that starts and then completes
busyTS := &turnState{
sessionKey: "user-chat",
agentID: "main",
phase: TurnPhaseRunning,
startedAt: time.Now(),
}
al.activeTurnStates.Store(busyTS.sessionKey, busyTS)

// First call β€” should skip
resp, err := al.ProcessHeartbeat(context.Background(), "heartbeat prompt", "cli", "direct")
if err != nil {
t.Fatalf("first ProcessHeartbeat returned error: %v", err)
}
if resp != "HEARTBEAT_OK" {
t.Fatalf("expected skip on first call, got %q", resp)
}

// Clear the turn (simulating user conversation ending)
al.activeTurnStates.Delete(busyTS.sessionKey)

// Second call β€” should proceed (not skip)
resp2, err2 := al.ProcessHeartbeat(context.Background(), "heartbeat prompt", "cli", "direct")
if err2 != nil {
t.Fatalf("second ProcessHeartbeat returned error: %v", err2)
}
if resp2 != "Mock response" {
t.Fatalf("expected heartbeat to proceed and return %q, got %q", "Mock response", resp2)
}
}
Loading