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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Kanban decomposer — Hermes `kanban_decompose.py` profile routing parity.

use std::path::Path;
use std::sync::Arc;

use lingshu_state::{KanbanDb, KanbanDecomposeChild, KanbanStatus, kanban_db_path_for_board, list_board_slugs};
use edgequake_llm::LLMProvider;
use serde::Deserialize;
use serde_json::Value;

use crate::auxiliary_model::resolve_side_task_provider_and_model;
use crate::config::{AppConfig, KanbanDecomposerConfig};
use crate::kanban_profiles::{
    format_roster_for_prompt, install_root, list_profile_roster, normalize_assignee_choice,
    resolve_default_assignee, resolve_orchestrator_profile, valid_assignee_names,
};

const SYSTEM_PROMPT: &str = r#"You are the Kanban decomposer for Lingshu.

A user dropped a rough idea into the Triage column. Break it into a small graph of concrete child tasks and route each one to the best-matching profile from the available roster.

You will be given:
  - The original task title and body
  - The list of available profiles (each with name + description)
  - The fallback "default_assignee" used when no profile fits

Output a single JSON object:

Fan-out (2–6 parallel/sequenced tasks):
{
  "fanout": true,
  "rationale": "<one sentence>",
  "tasks": [
    {
      "title": "<imperative title, <= 80 chars>",
      "body": "<detailed spec for the worker>",
      "assignee": "<profile name from the roster, or null for default>",
      "parents": [<int>, ...]
    }
  ]
}

Single task (no useful decomposition):
{
  "fanout": false,
  "rationale": "<one sentence>",
  "title": "<tightened title>",
  "body": "<concrete spec>",
  "assignee": "<profile name from the roster, or null for default>"
}

Rules:
- "parents" are 0-based indices into the same "tasks" list (data dependencies).
- Prefer parallelism — independent tasks get empty parents.
- Pick assignees from the roster by matching DESCRIPTION (not just name).
- Each child body must stand alone for a fresh worker.
- No preamble, no code fences — JSON only.
"#;

#[derive(Debug, Clone)]
pub struct DecomposeOutcome {
    pub task_id: String,
    pub ok: bool,
    pub reason: String,
    pub fanout: bool,
    pub child_ids: Option<Vec<String>>,
}

#[derive(Debug, Deserialize)]
struct FanoutResponse {
    #[serde(default)]
    fanout: bool,
    #[serde(default)]
    rationale: String,
    #[serde(default)]
    tasks: Vec<LlmTask>,
    #[serde(default)]
    title: Option<String>,
    #[serde(default)]
    body: Option<String>,
    #[serde(default)]
    assignee: Option<String>,
}

#[derive(Debug, Deserialize)]
struct LlmTask {
    title: String,
    #[serde(default)]
    body: Option<String>,
    #[serde(default)]
    assignee: Option<String>,
    #[serde(default)]
    parents: Vec<usize>,
}

struct DecomposeContext {
    default_assignee: String,
    orchestrator: String,
    valid_names: std::collections::HashSet<String>,
}

fn decompose_context(app_cfg: &AppConfig, install_root: &Path) -> DecomposeContext {
    DecomposeContext {
        default_assignee: resolve_default_assignee(&app_cfg.kanban, install_root),
        orchestrator: resolve_orchestrator_profile(&app_cfg.kanban, install_root),
        valid_names: valid_assignee_names(install_root),
    }
}

fn extract_json_object(raw: &str) -> Option<Value> {
    let stripped = raw
        .trim()
        .trim_start_matches("```json")
        .trim_start_matches("```")
        .trim_end_matches("```")
        .trim();
    let start = stripped.find('{')?;
    let end = stripped.rfind('}')?;
    if end <= start {
        return None;
    }
    serde_json::from_str(&stripped[start..=end]).ok()
}

fn llm_tasks_to_children(tasks: Vec<LlmTask>, ctx: &DecomposeContext) -> Vec<KanbanDecomposeChild> {
    tasks
        .into_iter()
        .map(|t| KanbanDecomposeChild {
            title: t.title.trim().chars().take(200).collect(),
            body: t.body.map(|b| b.trim().to_string()).filter(|b| !b.is_empty()),
            assignee: Some(normalize_assignee_choice(
                t.assignee.as_deref(),
                &ctx.default_assignee,
                &ctx.valid_names,
            )),
            parents: t.parents,
        })
        .collect()
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn decompose_task(
    db: &KanbanDb,
    task_id: &str,
    provider: Arc<dyn LLMProvider>,
    main_model: &str,
    decomposer_cfg: &KanbanDecomposerConfig,
    auxiliary_model: Option<&str>,
    app_cfg: &AppConfig,
    install_root: &Path,
) -> DecomposeOutcome {
    let ctx = decompose_context(app_cfg, install_root);
    let roster = list_profile_roster(install_root);

    let task = match db.get_task(task_id) {
        Ok(Some(t)) => t,
        Ok(None) => {
            return DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: "task not found".into(),
                fanout: false,
                child_ids: None,
            };
        }
        Err(e) => {
            return DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: format!("db error: {e}"),
                fanout: false,
                child_ids: None,
            };
        }
    };

    if task.status != "triage" {
        return DecomposeOutcome {
            task_id: task_id.to_string(),
            ok: false,
            reason: "task is not in triage".into(),
            fanout: false,
            child_ids: None,
        };
    }

    let (side_provider, _side_model) = resolve_side_task_provider_and_model(
        decomposer_cfg.model.as_deref(),
        auxiliary_model,
        provider,
        main_model,
        "kanban decomposer",
    );

    let body = task.body.as_deref().unwrap_or("(empty)");
    let user = format!(
        "Task id: {}\nTitle: {}\nBody:\n{body}\n\n\
         Available profiles (assignees you may pick from):\n{}\n\n\
         Default assignee (used when no profile fits a task): {}",
        task.id,
        task.title,
        format_roster_for_prompt(&roster),
        ctx.default_assignee,
    );
    let messages = vec![
        edgequake_llm::ChatMessage::system(SYSTEM_PROMPT),
        edgequake_llm::ChatMessage::user(&user),
    ];
    let options = edgequake_llm::CompletionOptions {
        max_tokens: Some(decomposer_cfg.max_tokens as usize),
        temperature: Some(0.3),
        ..Default::default()
    };

    let raw = match side_provider
        .chat_with_tools(&messages, &[], None, Some(&options))
        .await
    {
        Ok(r) => r.content,
        Err(e) => {
            return DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: format!("LLM error: {e}"),
                fanout: false,
                child_ids: None,
            };
        }
    };

    let Some(val) = extract_json_object(&raw) else {
        return DecomposeOutcome {
            task_id: task_id.to_string(),
            ok: false,
            reason: "failed to parse decomposer JSON".into(),
            fanout: false,
            child_ids: None,
        };
    };

    let parsed: FanoutResponse = match serde_json::from_value(val) {
        Ok(p) => p,
        Err(e) => {
            return DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: format!("invalid decomposer shape: {e}"),
                fanout: false,
                child_ids: None,
            };
        }
    };

    if !parsed.fanout {
        let title = parsed
            .title
            .filter(|t| !t.trim().is_empty())
            .unwrap_or_else(|| task.title.clone());
        let body = parsed.body.as_deref().or(task.body.as_deref());
        let assignee = if task.assignee.is_some() {
            None
        } else {
            Some(normalize_assignee_choice(
                parsed.assignee.as_deref(),
                &ctx.default_assignee,
                &ctx.valid_names,
            ))
        };
        match db.specify_triage_task(
            task_id,
            &title,
            body,
            assignee.as_deref(),
            Some("decomposer"),
        ) {
            Ok(true) => DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: true,
                reason: parsed.rationale,
                fanout: false,
                child_ids: None,
            },
            Ok(false) => DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: "task moved out of triage before specify".into(),
                fanout: false,
                child_ids: None,
            },
            Err(e) => DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: format!("specify failed: {e}"),
                fanout: false,
                child_ids: None,
            },
        }
    } else {
        let children = llm_tasks_to_children(parsed.tasks, &ctx);
        if children.is_empty() {
            return DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: "fanout=true but tasks list empty".into(),
                fanout: true,
                child_ids: None,
            };
        }
        match db.decompose_triage_task(
            task_id,
            Some(&ctx.orchestrator),
            &children,
            Some("decomposer"),
        ) {
            Ok(Some(ids)) => DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: true,
                reason: format!(
                    "{} — decomposed into {} children",
                    parsed.rationale,
                    ids.len()
                ),
                fanout: true,
                child_ids: Some(ids),
            },
            Ok(None) => DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: "task moved out of triage before decompose".into(),
                fanout: true,
                child_ids: None,
            },
            Err(e) => DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: format!("decompose rejected: {e}"),
                fanout: true,
                child_ids: None,
            },
        }
    }
}

/// List triage task ids on one board db.
pub fn list_triage_ids(db: &KanbanDb) -> Result<Vec<String>, lingshu_types::AgentError> {
    Ok(db
        .list_tasks(Some(KanbanStatus::Triage), 200)?
        .into_iter()
        .map(|t| t.id)
        .collect())
}

/// Auto-decompose up to `per_tick` triage tasks across all boards.
pub async fn run_auto_decompose_tick(
    home: &Path,
    provider: Arc<dyn LLMProvider>,
    main_model: &str,
    cfg: &AppConfig,
) -> usize {
    if !cfg.kanban.enabled || !cfg.kanban.auto_decompose {
        return 0;
    }
    let root = install_root();
    let per_tick = cfg.kanban.auto_decompose_per_tick.max(1);
    let mut attempted = 0usize;
    let mut decomposed = 0usize;

    for slug in list_board_slugs(home) {
        if attempted >= per_tick as usize {
            break;
        }
        let path = kanban_db_path_for_board(home, Some(&slug));
        let Ok(db) = KanbanDb::open(&path) else {
            continue;
        };
        let triage_ids = list_triage_ids(&db).unwrap_or_default();
        for task_id in triage_ids {
            if attempted >= per_tick as usize {
                break;
            }
            attempted += 1;
            let outcome = decompose_task(
                &db,
                &task_id,
                provider.clone(),
                main_model,
                &cfg.auxiliary.kanban_decomposer,
                cfg.auxiliary.model.as_deref(),
                cfg,
                &root,
            )
            .await;
            if outcome.ok {
                decomposed += 1;
                tracing::info!(
                    task_id = %outcome.task_id,
                    fanout = outcome.fanout,
                    reason = %outcome.reason,
                    "kanban: auto-decomposed triage task"
                );
            } else {
                tracing::debug!(
                    task_id = %outcome.task_id,
                    reason = %outcome.reason,
                    "kanban: auto-decompose skipped/failed"
                );
            }
        }
    }
    decomposed
}

/// Synchronous decompose for slash commands and HTTP API.
pub async fn decompose_task_by_id(
    home: Option<&Path>,
    task_id: &str,
    provider: Arc<dyn LLMProvider>,
    main_model: &str,
    cfg: &AppConfig,
) -> DecomposeOutcome {
    let home = lingshu_state::kanban_home(home);
    let path = lingshu_state::kanban_db_path_for_board(&home, None);
    let root = install_root();
    let db = match KanbanDb::open(&path) {
        Ok(db) => db,
        Err(e) => {
            return DecomposeOutcome {
                task_id: task_id.to_string(),
                ok: false,
                reason: format!("open db: {e}"),
                fanout: false,
                child_ids: None,
            };
        }
    };
    decompose_task(
        &db,
        task_id,
        provider,
        main_model,
        &cfg.auxiliary.kanban_decomposer,
        cfg.auxiliary.model.as_deref(),
        cfg,
        &root,
    )
    .await
}

pub fn decompose_outcome_json(outcome: &DecomposeOutcome) -> serde_json::Value {
    serde_json::json!({
        "ok": outcome.ok,
        "task_id": outcome.task_id,
        "reason": outcome.reason,
        "fanout": outcome.fanout,
        "child_ids": outcome.child_ids,
    })
}

/// Format a decompose outcome for slash command replies.
pub fn format_decompose_outcome(outcome: &DecomposeOutcome) -> String {
    if outcome.ok {
        if outcome.fanout {
            let ids = outcome
                .child_ids
                .as_ref()
                .map(|v| v.join(", "))
                .unwrap_or_default();
            format!("✅ Decomposed `{}` → {ids}\n{}", outcome.task_id, outcome.reason)
        } else {
            format!(
                "✅ Specified `{}` (single task, triage → todo)\n{}",
                outcome.task_id, outcome.reason
            )
        }
    } else {
        format!("❌ Decompose `{}` failed: {}", outcome.task_id, outcome.reason)
    }
}

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

    #[test]
    fn extract_json_strips_fences() {
        let raw = "```json\n{\"fanout\": false, \"title\": \"x\"}\n```";
        let v = extract_json_object(raw).expect("json");
        assert_eq!(v["fanout"], false);
    }
}