Skip to content

Commit 334cf1f

Browse files
committed
Show one pinned lane dot per worker todo item
1 parent b3d8789 commit 334cf1f

7 files changed

Lines changed: 223 additions & 23 deletions

File tree

‎crates/jcode-app-core/src/server/background_tasks.rs‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ pub(super) async fn dispatch_swarm_todo_progress(
368368
Some((completed, total))
369369
};
370370
let mut items = compact_todo_items(&event.todos);
371+
let statuses: Vec<String> = event.todos.iter().map(|todo| todo.status.clone()).collect();
371372

372373
let swarm_id = {
373374
let mut members = swarm_members.write().await;
@@ -386,11 +387,15 @@ pub(super) async fn dispatch_swarm_todo_progress(
386387
{
387388
new.tool_intents = old.tool_intents.clone();
388389
}
389-
if member.todo_progress == progress && member.todo_items == items {
390+
if member.todo_progress == progress
391+
&& member.todo_items == items
392+
&& member.runtime.todo_statuses == statuses
393+
{
390394
return; // no change, skip the broadcast
391395
}
392396
member.todo_progress = progress;
393397
member.todo_items = items;
398+
member.runtime.todo_statuses = statuses;
394399
member.swarm_id.clone()
395400
};
396401
if let Some(swarm_id) = swarm_id {
@@ -625,6 +630,61 @@ mod tests {
625630
use super::*;
626631
use crate::bus::{BatchProgress, ToolEvent, ToolStatus};
627632

633+
#[tokio::test]
634+
async fn swarm_todo_statuses_include_items_outside_detail_window() {
635+
let (event_tx, _) = tokio::sync::mpsc::unbounded_channel();
636+
let member = SwarmMember {
637+
session_id: "worker".into(),
638+
event_tx,
639+
event_txs: HashMap::new(),
640+
working_dir: None,
641+
swarm_id: None,
642+
swarm_enabled: true,
643+
status: "running".into(),
644+
detail: None,
645+
task_label: None,
646+
friendly_name: None,
647+
report_back_to_session_id: None,
648+
latest_completion_report: None,
649+
role: "agent".into(),
650+
joined_at: std::time::Instant::now(),
651+
last_status_change: std::time::Instant::now(),
652+
is_headless: true,
653+
output_tail: None,
654+
todo_progress: None,
655+
todo_items: Vec::new(),
656+
runtime: Default::default(),
657+
};
658+
let members = Arc::new(RwLock::new(HashMap::from([("worker".into(), member)])));
659+
let swarms = Arc::new(RwLock::new(HashMap::new()));
660+
let mut event = crate::bus::TodoEvent {
661+
session_id: "worker".into(),
662+
todos: (0..20)
663+
.map(|i| crate::todo::TodoItem {
664+
id: i.to_string(),
665+
content: format!("Step {i}"),
666+
status: "pending".into(),
667+
..Default::default()
668+
})
669+
.collect(),
670+
};
671+
dispatch_swarm_todo_progress(&event, &members, &swarms).await;
672+
{
673+
let guard = members.read().await;
674+
let worker = &guard["worker"];
675+
assert_eq!(worker.todo_items.len(), SWARM_TODO_ITEMS_CAP);
676+
assert_eq!(worker.runtime.todo_statuses.len(), 20);
677+
}
678+
// Same counts and same capped detail snapshot, but a different status
679+
// outside the window must still reach clients.
680+
event.todos[19].status = "in_progress".into();
681+
dispatch_swarm_todo_progress(&event, &members, &swarms).await;
682+
assert_eq!(
683+
members.read().await["worker"].runtime.todo_statuses[19],
684+
"in_progress"
685+
);
686+
}
687+
628688
fn tool(id: &str, intent: &str, status: ToolStatus) -> ToolEvent {
629689
ToolEvent {
630690
session_id: "worker".into(),

‎crates/jcode-app-core/src/server/swarm.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ async fn broadcast_swarm_status_now(
800800
todo_progress: m.todo_progress,
801801
todo_items: m.todo_items.clone(),
802802
runtime: crate::protocol::SwarmMemberRuntime {
803+
todo_statuses: m.runtime.todo_statuses.clone(),
803804
model: m.runtime.model.clone(),
804805
provider: m.runtime.provider.clone(),
805806
auth_method: m.runtime.auth_method.clone(),

‎crates/jcode-app-core/src/server/swarm_persistence.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ fn hydrate_member_todos(member: &mut SwarmMember) {
446446
let completed = todos.iter().filter(|t| t.status == "completed").count() as u32;
447447
member.todo_progress = Some((completed, todos.len() as u32));
448448
member.todo_items = super::background_tasks::compact_todo_items(&todos);
449+
member.runtime.todo_statuses = todos.iter().map(|todo| todo.status.clone()).collect();
449450
}
450451

451452
pub(super) fn load_runtime_state() -> LoadedSwarmRuntimeState {

‎crates/jcode-protocol/src/lib.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,9 @@ pub struct SwarmMemberStatus {
659659

660660
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
661661
pub struct SwarmMemberRuntime {
662+
/// Full ordered todo statuses, independent of the capped detail snapshot.
663+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
664+
pub todo_statuses: Vec<String>,
662665
#[serde(default, skip_serializing_if = "Option::is_none")]
663666
pub model: Option<String>,
664667
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -687,7 +690,8 @@ pub struct SwarmMemberRuntime {
687690

688691
impl SwarmMemberRuntime {
689692
fn is_empty(&self) -> bool {
690-
self.model.is_none()
693+
self.todo_statuses.is_empty()
694+
&& self.model.is_none()
691695
&& self.provider.is_none()
692696
&& self.auth_method.is_none()
693697
&& self.effort.is_none()

‎crates/jcode-tui/src/tui/app/todos_view.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ fn hash_todo_card_agents(agents: &[crate::tui::ui::TodoCardAgent]) -> u64 {
470470
agent.session_id.hash(&mut hasher);
471471
agent.status.hash(&mut hasher);
472472
agent.activity.hash(&mut hasher);
473+
agent.todo_statuses.hash(&mut hasher);
473474
// Minute granularity: the band shows "3m", so finer ages would only
474475
// force redraws with no visible change.
475476
agent
@@ -657,6 +658,15 @@ fn now_ms() -> u64 {
657658
mod tests {
658659
use super::*;
659660

661+
#[test]
662+
fn worker_item_status_changes_invalidate_pinned_payload() {
663+
let mut agent = crate::tui::ui::TodoCardAgent::default();
664+
agent.todo_statuses = vec!["pending".into(), "in_progress".into()];
665+
let before = hash_todo_card_agents(&[agent.clone()]);
666+
agent.todo_statuses.swap(0, 1);
667+
assert_ne!(before, hash_todo_card_agents(&[agent]));
668+
}
669+
660670
/// Plan-level intent used by the view tests.
661671
fn plan() -> crate::todo::TodoPlan {
662672
crate::todo::TodoPlan {

‎crates/jcode-tui/src/tui/ui_todo_stages.rs‎

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
//! Layout comes from [`crate::todo::stages::stage_layout`], the same function
44
//! `run_plan` uses, so the card always matches execution order. The header row
55
//! labels each phase with its pips (`phase 1 ●●● phase 2 ○○`). Below it each
6-
//! lane (todo group) is one row: its status strip, its name, then its titles
7-
//! left to right. Lanes run in parallel within a phase, titles along a row in
6+
//! lane (todo group) is one row: its worker's todo-status strip, its name,
7+
//! then its coordinator todo titles left to right. If worker todos are not
8+
//! available, the strip falls back to the coordinator items. Lanes run in
9+
//! parallel within a phase, titles along a row in
810
//! sequence. No divider lines or rails. A list with no stages renders exactly
911
//! like a plain checklist.
1012
//!
@@ -29,6 +31,9 @@ pub(crate) struct TodoCardAgent {
2931
/// Seconds since the worker's last tool event or token.
3032
#[serde(default, skip_serializing_if = "Option::is_none")]
3133
pub activity_age_secs: Option<u64>,
34+
/// Ordered statuses of the worker's own todos, not the coordinator's lane tasks.
35+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
36+
pub todo_statuses: Vec<String>,
3237
}
3338

3439
impl TodoCardAgent {
@@ -63,6 +68,15 @@ impl TodoCardAgent {
6368
activity,
6469
age_secs: member.status_age_secs,
6570
activity_age_secs: member.runtime.activity_age_secs,
71+
todo_statuses: if member.runtime.todo_statuses.is_empty() {
72+
member
73+
.todo_items
74+
.iter()
75+
.map(|item| item.status.clone())
76+
.collect()
77+
} else {
78+
member.runtime.todo_statuses.clone()
79+
},
6680
}
6781
}
6882

@@ -245,7 +259,11 @@ fn current_stage(
245259
}
246260

247261
fn pip_span(todo: &crate::todo::TodoItem) -> Span<'static> {
248-
let (glyph, color) = match todo.status.as_str() {
262+
status_pip_span(&todo.status)
263+
}
264+
265+
fn status_pip_span(status: &str) -> Span<'static> {
266+
let (glyph, color) = match status {
249267
"completed" => ("●", rgb(100, 180, 100)),
250268
"in_progress" => ("●", asap_color()),
251269
_ => ("○", rgb(90, 90, 105)),
@@ -458,27 +476,54 @@ fn lane_name_spans(
458476
name
459477
}
460478

461-
/// A lane's status strip (`✓●○`), padded to `strip_width` so the names after
462-
/// it line up across lanes.
463-
fn lane_strip_spans(
464-
lane_todos: &[&crate::todo::TodoItem],
465-
strip_width: usize,
466-
live: bool,
467-
) -> Vec<Span<'static>> {
468-
// The lane's active step spins while its worker is running, so a frozen
469-
// dot means a frozen worker.
470-
let mut strip: Vec<Span<'static>> = lane_todos
479+
fn lane_item_statuses<'a>(
480+
lane_todos: &[&'a crate::todo::TodoItem],
481+
agents: &'a [TodoCardAgent],
482+
) -> Vec<(&'a str, bool)> {
483+
let mut seen = std::collections::HashSet::new();
484+
let mut statuses = Vec::new();
485+
for todo in lane_todos {
486+
let agent = todo
487+
.assigned_to
488+
.as_deref()
489+
.and_then(|key| agents.iter().find(|agent| agent.matches(key)))
490+
.filter(|agent| !agent.todo_statuses.is_empty());
491+
if let Some(agent) = agent {
492+
// Several coordinator todos may point at the same worker.
493+
if seen.insert(agent.session_id.as_str()) {
494+
statuses.extend(
495+
agent
496+
.todo_statuses
497+
.iter()
498+
.map(|status| (status.as_str(), is_live_status(&agent.status))),
499+
);
500+
}
501+
} else {
502+
statuses.push((
503+
todo.status.as_str(),
504+
lane_live_agent(&[todo], agents).is_some(),
505+
));
506+
}
507+
}
508+
statuses
509+
}
510+
511+
/// One dot per worker todo, falling back to coordinator todos without a worker list.
512+
fn lane_strip_spans(statuses: &[(&str, bool)], strip_width: usize) -> Vec<Span<'static>> {
513+
let strip: Vec<Span<'static>> = statuses
471514
.iter()
472-
.map(|todo| {
473-
let mut span = pip_span(todo);
474-
if live && todo.status == "in_progress" {
515+
.map(|(status, live)| {
516+
let mut span = status_pip_span(status);
517+
if *live && *status == "in_progress" {
475518
span.content = LIVE_DOT_SENTINEL.into();
476519
}
477520
span
478521
})
479522
.collect();
523+
let mut strip =
524+
super::truncate_line_with_ellipsis_to_width(&Line::from(strip), strip_width).spans;
480525
strip.push(Span::raw(
481-
" ".repeat(strip_width.saturating_sub(lane_todos.len()) + 2),
526+
" ".repeat(strip_width.saturating_sub(width_of(&strip)) + 2),
482527
));
483528
strip
484529
}
@@ -710,9 +755,10 @@ pub(crate) fn push_stage_lines(
710755
};
711756
let strip_width = lanes
712757
.iter()
713-
.map(|(_, lane)| lane.items.len())
758+
.map(|(_, lane)| lane_item_statuses(&items_of(lane), agents).len())
714759
.max()
715-
.unwrap_or(0);
760+
.unwrap_or(0)
761+
.min((inner_width / 3).max(1));
716762
let name_width = lanes
717763
.iter()
718764
.map(|(_, lane)| {
@@ -732,8 +778,8 @@ pub(crate) fn push_stage_lines(
732778
// dimmed (with its titles and dots) while the phase has not started.
733779
let later = current.is_some_and(|current| phase > current);
734780
let items = items_of(lane);
735-
let live = lane_live_agent(&items, agents).is_some();
736-
let mut row = lane_strip_spans(&items, strip_width, live);
781+
let statuses = lane_item_statuses(&items, agents);
782+
let mut row = lane_strip_spans(&statuses, strip_width);
737783
if name_width > 0 {
738784
let name = lane_name_spans(
739785
lane.group.as_deref(),

‎crates/jcode-tui/src/tui/ui_todo_stages_tests.rs‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,84 @@ fn agent(name: &str, status: &str, activity: Option<&str>, age: Option<u64>) ->
4343
activity: activity.map(str::to_string),
4444
age_secs: age,
4545
activity_age_secs: age,
46+
todo_statuses: Vec::new(),
47+
}
48+
}
49+
50+
#[test]
51+
fn lane_dots_use_worker_items_once_and_fall_back_without_items() {
52+
let mut first = assigned(todo("1", "Parser task", "in_progress", Some(1)), "fox");
53+
first.group = Some("Parser".into());
54+
let mut second = assigned(todo("2", "Another task", "pending", Some(1)), "fox");
55+
second.group = Some("Parser".into());
56+
let mut worker = agent("fox", "running", None, None);
57+
worker.todo_statuses = vec![
58+
"completed".into(),
59+
"in_progress".into(),
60+
"pending".into(),
61+
"pending".into(),
62+
];
63+
let items = [&first, &second];
64+
let agents = [worker];
65+
let statuses = lane_item_statuses(&items, &agents);
66+
assert_eq!(
67+
statuses,
68+
vec![
69+
("completed", true),
70+
("in_progress", true),
71+
("pending", true),
72+
("pending", true)
73+
]
74+
);
75+
let strip = lane_strip_spans(&statuses, 4);
76+
assert_eq!(strip[0].content, "●");
77+
assert_eq!(strip[1].content, LIVE_DOT_SENTINEL);
78+
assert_eq!(strip[2].content, "○");
79+
assert_eq!(strip[3].content, "○");
80+
let rows = render(&[first.clone(), second.clone()], &agents, 120);
81+
let row = rows
82+
.iter()
83+
.find(|row| row.contains("Parser") && row.contains('🦊'))
84+
.unwrap();
85+
assert!(row.contains("○○"), "{rows:?}");
86+
assert_eq!(
87+
lane_item_statuses(&items, &[]),
88+
vec![("in_progress", false), ("pending", false)]
89+
);
90+
}
91+
92+
#[test]
93+
fn worker_dots_prefer_complete_status_snapshot() {
94+
let mut member: crate::protocol::SwarmMemberStatus =
95+
serde_json::from_value(serde_json::json!({
96+
"session_id": "worker", "status": "running",
97+
"todo_items": [{"content": "visible detail", "status": "in_progress"}],
98+
"runtime": {"todo_statuses": ["completed", "completed", "in_progress", "pending"]}
99+
}))
100+
.unwrap();
101+
let snapshot = TodoCardAgent::from_member(&member);
102+
assert_eq!(
103+
snapshot.todo_statuses,
104+
["completed", "completed", "in_progress", "pending"]
105+
);
106+
member.runtime.todo_statuses.clear();
107+
assert_eq!(
108+
TodoCardAgent::from_member(&member).todo_statuses,
109+
["in_progress"]
110+
);
111+
let old: TodoCardAgent = serde_json::from_value(serde_json::json!({
112+
"session_id": "old-worker", "status": "ready"
113+
}))
114+
.unwrap();
115+
assert!(old.todo_statuses.is_empty());
116+
}
117+
118+
#[test]
119+
fn worker_dot_strip_fits_narrow_width() {
120+
let statuses = vec![("pending", false); 100];
121+
for width in 1..40 {
122+
let strip = lane_strip_spans(&statuses, width);
123+
assert!(width_of(&strip) <= width + 2);
46124
}
47125
}
48126

0 commit comments

Comments
 (0)