@@ -1371,44 +1371,52 @@ func (m *Mysis) extractLatestToolLoop(memories []*store.Memory) []*store.Memory
13711371 return result
13721372 }
13731373
1374-// getContextMemories returns memories for LLM context: system prompt + recent messages.
1375-// This keeps context small for faster inference while preserving essential information.
1376-// Compacts repeated snapshot tool results to prefer recent state.
1374+// getContextMemories returns memories for LLM context using loop-based composition.
1375+// Composes context as: [system prompt] + [chosen prompt source] + [most recent tool loop].
1376+// This ensures stable, bounded context and eliminates orphaned tool sequencing.
13771377 func (m *Mysis) getContextMemories() ([]*store.Memory, error) {
1378- a := m
1379- // Get recent memories (limited for performance)
1380- recent, err := a.store.GetRecentMemories(a.id, constants.MaxContextMessages)
1378+ // Get all recent memories
1379+ allMemories, err := m.store.GetRecentMemories(m.id, constants.MaxContextMessages)
13811380 if err != nil {
13821381 return nil, err
13831382 }
13841383
1385- // Apply compaction to remove redundant snapshot tool results
1386- compacted := a.compactSnapshots(recent)
1384+ // Build context: [system] + [prompt source] + [latest tool loop]
1385+ result := make([]*store.Memory, 0, 10) // Pre-allocate for typical size
13871386
1388- // Remove orphaned tool messages (results without corresponding tool calls)
1389- // This ensures OpenAI Chat Completions API compliance
1390- compacted = a.removeOrphanedToolMessages(compacted)
1387+ // Step 1: Add system prompt (if available)
1388+ system, err := m.store.GetSystemMemory(m.id)
1389+ if err == nil && system != nil {
1390+ result = append(result, system)
1391+ }
13911392
1392- // Remove orphaned tool calls (assistant tool calls without corresponding tool results)
1393- // This prevents OpenCode Zen API crashes when context window cuts off tool results
1394- compacted = a.removeOrphanedToolCalls(compacted)
1393+ // Step 2: Select and add prompt source by priority
1394+ // Priority: commander direct → last commander broadcast → last swarm broadcast → nudge
1395+ promptSource := m.selectPromptSource(allMemories)
13951396
1396- // Always try to fetch the system prompt and prepend it if not already first
1397- system, err := a.store.GetSystemMemory(a.id)
1398- if err != nil {
1399- // No system prompt found - this is okay, just use compacted memories
1400- return compacted, nil
1397+ if promptSource == nil {
1398+ // No prompt source found - generate synthetic nudge
1399+ // Note: Nudge counter increment happens in caller (handleLLMResponse)
1400+ // We create a temporary memory that is NOT stored in database
1401+ nudgeContent := "Continue your mission. Check notifications and coordinate with the swarm."
1402+ nudgeMemory := &store.Memory{
1403+ Role: store.MemoryRoleUser,
1404+ Source: store.MemorySourceSystem,
1405+ Content: nudgeContent,
1406+ SenderID: "",
1407+ CreatedAt: time.Now(),
1408+ }
1409+ result = append(result, nudgeMemory)
1410+ } else {
1411+ result = append(result, promptSource)
14011412 }
14021413
1403- // Check if system prompt is already the first message
1404- if len(compacted) > 0 && compacted[0].ID == system.ID {
1405- return compacted, nil
1414+ // Step 3: Extract and add the most recent tool loop (if any)
1415+ toolLoop := m.extractLatestToolLoop(allMemories)
1416+ if len(toolLoop) > 0 {
1417+ result = append(result, toolLoop...)
14061418 }
14071419
1408- // Prepend system prompt to compacted memories
1409- result := make([]*store.Memory, 0, len(compacted)+1)
1410- result = append(result, system)
1411- result = append(result, compacted...)
14121420 return result, nil
14131421 }
14141422
@@ -1778,7 +1786,12 @@ func (m *Mysis) shouldNudge(now time.Time) bool {
17781786 defer a.mu.Unlock()
17791787
17801788 if a.activityState == ActivityStateTraveling || a.activityState == ActivityStateCooldown {
1781- if !a.activityUntil.IsZero() && !now.Before(a.activityUntil) {
1789+ if !a.activityUntil.IsZero() {
1790+ if now.Before(a.activityUntil) {
1791+ // Activity not finished yet - don't nudge
1792+ return false
1793+ }
1794+ // Activity finished - transition to idle
17821795 a.activityState = ActivityStateIdle
17831796 a.activityUntil = time.Time{}
17841797 }