lingshu-core 0.10.0

Agent core: conversation loop, prompt builder, context compression, model routing
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Ralph-loop orchestration — judge + auto-continuation decisions.

use std::sync::Arc;

use lingshu_types::AgentError;
use edgequake_llm::LLMProvider;

use crate::config::{GoalJudgeConfig, GoalsConfig};
use crate::goal_judge::{
    DEFAULT_MAX_CONSECUTIVE_PARSE_FAILURES, resolve_goal_judge_provider_and_model, run_goal_judge,
};
use crate::goals::{GoalState, GoalStatus, GoalStore};

pub const CONTINUATION_PROMPT_TEMPLATE: &str = "\
[Continuing toward your standing goal]\n\
Goal: {goal}\n\n\
Continue working toward this goal. Take the next concrete step. \
If you believe the goal is complete, state so explicitly and stop. \
If you are blocked and need input from the user, say so clearly and stop.";

pub const CONTINUATION_PROMPT_WITH_SUBGOALS_TEMPLATE: &str = "\
[Continuing toward your standing goal]\n\
Goal: {goal}\n\n\
Additional criteria the user added mid-loop:\n\
{subgoals_block}\n\n\
Continue working toward the goal AND all additional criteria. Take \
the next concrete step. If you believe the goal and every \
additional criterion are complete, state so explicitly and stop. \
If you are blocked and need input from the user, say so clearly \
and stop.";

/// Prefix shared by synthetic Ralph-loop continuation user messages.
pub const GOAL_CONTINUATION_PREFIX: &str = "[Continuing toward your standing goal]\nGoal:";

/// Return true for synthetic Ralph-loop continuation prompts.
pub fn is_goal_continuation_text(text: &str) -> bool {
    text.starts_with(GOAL_CONTINUATION_PREFIX)
}

/// Return true if the text looks like a slash command.
pub fn looks_like_slash_command(text: &str) -> bool {
    text.trim_start().starts_with('/')
}

/// True when the queue contains a non-slash user payload (Hermes queue peek).
pub fn prompt_queue_has_real_user_message(queue: &[String]) -> bool {
    queue
        .iter()
        .any(|item| !looks_like_slash_command(item) && !is_goal_continuation_text(item))
}

/// Remove synthetic goal continuations from a FIFO prompt queue.
pub fn drain_goal_continuations_from_queue(queue: &mut Vec<String>) -> usize {
    let before = queue.len();
    queue.retain(|item| !is_goal_continuation_text(item));
    before - queue.len()
}

/// Compact status-bar chip for the active Ralph loop (TUI / gateway surfaces).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GoalStatusChip {
    pub label: String,
    pub status: GoalStatus,
}

/// Build a one-line status chip from persisted goal state.
pub fn compact_status_chip(state: &GoalState) -> Option<GoalStatusChip> {
    if state.is_empty() || state.status == GoalStatus::Cleared {
        return None;
    }
    let goal = state
        .goal_text
        .as_deref()
        .map(str::trim)
        .filter(|t| !t.is_empty())?;
    let short = crate::safe_truncate(goal, 22);
    let turns = format!("{}/{}", state.turns_used, state.max_turns);
    let sub = if state.subgoals.is_empty() {
        String::new()
    } else {
        format!(" · {} sub", state.subgoals.len())
    };

    let (label, status) = match state.status {
        GoalStatus::Active => (format!("{turns}{sub} · {short}"), GoalStatus::Active),
        GoalStatus::Paused => (format!("{turns}{sub} · {short}"), GoalStatus::Paused),
        GoalStatus::Done => (format!("✓ goal done · {short}"), GoalStatus::Done),
        GoalStatus::Cleared => return None,
    };
    Some(GoalStatusChip { label, status })
}

/// Map a post-turn judge decision to a short flash string for the status bar.
pub fn goal_flash_from_decision(decision: &GoalContinuationDecision) -> Option<String> {
    if decision.verdict == "done" || decision.status == GoalStatus::Done {
        return Some("✓ goal complete".into());
    }
    if decision.status == GoalStatus::Paused {
        return Some("⏸ goal paused".into());
    }
    if decision.should_continue {
        return Some("↻ goal continuing".into());
    }
    None
}

/// Decision returned after a turn completes and the goal judge runs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GoalContinuationDecision {
    pub should_continue: bool,
    pub continuation_prompt: Option<String>,
    pub message: String,
    pub verdict: String,
    pub status: GoalStatus,
}

impl GoalContinuationDecision {
    pub fn inactive() -> Self {
        Self {
            should_continue: false,
            continuation_prompt: None,
            message: String::new(),
            verdict: "inactive".into(),
            status: GoalStatus::Cleared,
        }
    }
}

pub fn status_line(state: &GoalState) -> String {
    let Some(goal) = state
        .goal_text
        .as_deref()
        .map(str::trim)
        .filter(|t| !t.is_empty())
    else {
        return "No active goal. Set one with /goal <text>.".into();
    };

    let turns = format!("{}/{} turns", state.turns_used, state.max_turns);
    let sub = if state.subgoals.is_empty() {
        String::new()
    } else {
        format!(
            ", {} subgoal{}",
            state.subgoals.len(),
            if state.subgoals.len() == 1 { "" } else { "s" }
        )
    };

    match state.status {
        GoalStatus::Active => format!("⊙ Goal (active, {turns}{sub}): {goal}"),
        GoalStatus::Paused => {
            let extra = state
                .paused_reason
                .as_deref()
                .filter(|r| !r.is_empty())
                .map(|r| format!("{r}"))
                .unwrap_or_default();
            format!("⏸ Goal (paused, {turns}{sub}{extra}): {goal}")
        }
        GoalStatus::Done => format!("✓ Goal done ({turns}{sub}): {goal}"),
        GoalStatus::Cleared => "No active goal. Set one with /goal <text>.".into(),
    }
}

pub fn render_subgoals_list(state: &GoalState) -> String {
    if state.is_empty() {
        return "(no active goal)".into();
    }
    if state.subgoals.is_empty() {
        return "(no subgoals — use /subgoal <text> to add criteria)".into();
    }
    state
        .subgoals
        .iter()
        .enumerate()
        .map(|(idx, sub)| {
            let marker = if sub.done { "[x]" } else { "[ ]" };
            format!("  {}. {marker} {}", idx + 1, sub.text)
        })
        .collect::<Vec<_>>()
        .join("\n")
}

pub fn next_continuation_prompt(state: &GoalState) -> Option<String> {
    if state.status != GoalStatus::Active {
        return None;
    }
    let goal = state.goal_text.as_deref()?.trim();
    if goal.is_empty() {
        return None;
    }
    if state.subgoals.is_empty() {
        return Some(CONTINUATION_PROMPT_TEMPLATE.replace("{goal}", goal));
    }
    let subgoals_block = state
        .subgoals
        .iter()
        .enumerate()
        .map(|(idx, sub)| {
            let marker = if sub.done { "[x]" } else { "[ ]" };
            format!("- {}. {marker} {}", idx + 1, sub.text)
        })
        .collect::<Vec<_>>()
        .join("\n");
    Some(
        CONTINUATION_PROMPT_WITH_SUBGOALS_TEMPLATE
            .replace("{goal}", goal)
            .replace("{subgoals_block}", &subgoals_block),
    )
}

/// Run the goal judge after a turn and update persisted loop state.
#[allow(clippy::too_many_arguments)]
pub async fn evaluate_goal_after_turn(
    goal_store: Arc<dyn GoalStore>,
    session_id: &str,
    last_response: &str,
    interrupted: bool,
    _goals_cfg: &GoalsConfig,
    judge_cfg: &GoalJudgeConfig,
    auxiliary_model: Option<&str>,
    main_provider: Arc<dyn LLMProvider>,
    main_model: &str,
) -> Result<GoalContinuationDecision, AgentError> {
    let mut state = goal_store.active(session_id)?;
    if state.is_empty() || state.status != GoalStatus::Active {
        return Ok(GoalContinuationDecision::inactive());
    }

    if interrupted {
        goal_store.pause(session_id, "user-interrupted (Ctrl+C)")?;
        return Ok(GoalContinuationDecision {
            should_continue: false,
            continuation_prompt: None,
            message: "⏸ Goal paused — turn was interrupted. Use /goal resume to continue, or /goal clear to stop.".into(),
            verdict: "skipped".into(),
            status: GoalStatus::Paused,
        });
    }

    if last_response.trim().is_empty() {
        return Ok(GoalContinuationDecision {
            should_continue: false,
            continuation_prompt: None,
            message: String::new(),
            verdict: "skipped".into(),
            status: GoalStatus::Active,
        });
    }

    state.turns_used = state.turns_used.saturating_add(1);

    let goal_text = state.goal_text.clone().unwrap_or_default();
    let (judge_provider, judge_model) = resolve_goal_judge_provider_and_model(
        judge_cfg,
        auxiliary_model,
        main_provider,
        main_model,
    );
    let verdict = run_goal_judge(
        &judge_provider,
        &judge_model,
        &goal_text,
        last_response,
        &state,
        judge_cfg,
    )
    .await;

    state.last_verdict = Some(if verdict.done {
        "done".into()
    } else {
        "continue".into()
    });
    state.last_reason = Some(verdict.reason.clone());

    if verdict.parse_failed {
        state.consecutive_parse_failures = state.consecutive_parse_failures.saturating_add(1);
    } else {
        state.consecutive_parse_failures = 0;
    }

    if verdict.done {
        state.status = GoalStatus::Done;
        goal_store.save_loop_state(session_id, &state)?;
        return Ok(GoalContinuationDecision {
            should_continue: false,
            continuation_prompt: None,
            message: format!("✓ Goal achieved: {}", verdict.reason),
            verdict: "done".into(),
            status: GoalStatus::Done,
        });
    }

    if state.consecutive_parse_failures >= DEFAULT_MAX_CONSECUTIVE_PARSE_FAILURES {
        state.status = GoalStatus::Paused;
        state.paused_reason = Some(format!(
            "judge model returned unparseable output {} turns in a row",
            state.consecutive_parse_failures
        ));
        goal_store.save_loop_state(session_id, &state)?;
        return Ok(GoalContinuationDecision {
            should_continue: false,
            continuation_prompt: None,
            message: format!(
                "⏸ Goal paused — the judge model ({} turns) isn't returning the required JSON verdict. \
                 Route the judge to a stricter model in ~/.lingshu/config.yaml:\n\
                   auxiliary:\n\
                     goal_judge:\n\
                       model: google/gemini-3-flash-preview\n\
                 Then /goal resume to continue.",
                state.consecutive_parse_failures
            ),
            verdict: "continue".into(),
            status: GoalStatus::Paused,
        });
    }

    if state.turns_used >= state.max_turns {
        state.status = GoalStatus::Paused;
        state.paused_reason = Some(format!(
            "turn budget exhausted ({}/{})",
            state.turns_used, state.max_turns
        ));
        goal_store.save_loop_state(session_id, &state)?;
        return Ok(GoalContinuationDecision {
            should_continue: false,
            continuation_prompt: None,
            message: format!(
                "⏸ Goal paused — {}/{} turns used. Use /goal resume to keep going, or /goal clear to stop.",
                state.turns_used, state.max_turns
            ),
            verdict: "continue".into(),
            status: GoalStatus::Paused,
        });
    }

    goal_store.save_loop_state(session_id, &state)?;
    let continuation = next_continuation_prompt(&state);
    Ok(GoalContinuationDecision {
        should_continue: continuation.is_some(),
        continuation_prompt: continuation,
        message: format!(
            "↻ Continuing toward goal ({}/{}): {}",
            state.turns_used, state.max_turns, verdict.reason
        ),
        verdict: "continue".into(),
        status: GoalStatus::Active,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::goals::SubGoal;

    #[test]
    fn status_line_formats_active_goal() {
        let state = GoalState {
            goal_text: Some("Ship it".into()),
            status: GoalStatus::Active,
            turns_used: 2,
            max_turns: 20,
            ..Default::default()
        };
        let line = status_line(&state);
        assert!(line.contains("active"));
        assert!(line.contains("Ship it"));
    }

    #[test]
    fn continuation_prompt_includes_subgoals() {
        let state = GoalState {
            goal_text: Some("Build API".into()),
            status: GoalStatus::Active,
            subgoals: vec![SubGoal {
                id: 1,
                text: "write tests".into(),
                done: false,
            }],
            ..Default::default()
        };
        let prompt = next_continuation_prompt(&state).expect("prompt");
        assert!(prompt.contains("write tests"));
        assert!(prompt.contains("Additional criteria"));
    }

    #[test]
    fn is_goal_continuation_text_detects_synthetic_prompt() {
        let prompt = next_continuation_prompt(&GoalState {
            goal_text: Some("Ship".into()),
            status: GoalStatus::Active,
            ..Default::default()
        })
        .expect("prompt");
        assert!(is_goal_continuation_text(&prompt));
        assert!(!is_goal_continuation_text("/goal status"));
    }

    #[test]
    fn prompt_queue_peek_ignores_slash_only_entries() {
        let queue = vec!["/subgoal add tests".into(), "fix the bug".into()];
        assert!(prompt_queue_has_real_user_message(&queue));

        let slash_only = vec!["/goal status".into(), "/subgoal clear".into()];
        assert!(!prompt_queue_has_real_user_message(&slash_only));
    }

    #[test]
    fn drain_goal_continuations_preserves_user_messages() {
        let cont = CONTINUATION_PROMPT_TEMPLATE.replace("{goal}", "Ship");
        let mut queue = vec![cont.clone(), "real user msg".into(), cont];
        let removed = drain_goal_continuations_from_queue(&mut queue);
        assert_eq!(removed, 2);
        assert_eq!(queue, vec!["real user msg".to_string()]);
    }

    #[test]
    fn compact_status_chip_formats_active_goal() {
        let chip = compact_status_chip(&GoalState {
            goal_text: Some("Refactor payment module".into()),
            status: GoalStatus::Active,
            turns_used: 3,
            max_turns: 20,
            subgoals: vec![SubGoal {
                id: 1,
                text: "tests".into(),
                done: false,
            }],
            ..Default::default()
        })
        .expect("chip");
        assert!(chip.label.contains("⊙ 3/20"));
        assert!(chip.label.contains("1 sub"));
        assert!(chip.label.contains("Refactor payment"));
    }

    #[test]
    fn goal_flash_from_decision_maps_verdicts() {
        let cont = goal_flash_from_decision(&GoalContinuationDecision {
            should_continue: true,
            continuation_prompt: None,
            message: String::new(),
            verdict: "continue".into(),
            status: GoalStatus::Active,
        });
        assert_eq!(cont.as_deref(), Some("↻ goal continuing"));
    }
}