[Canonical page](<https://docs.dhtmlx.com/planning-engine/rust/scheduling/critical-path-and-slack/>)
[Full documentation index](<https://docs.dhtmlx.com/planning-engine/llms.txt>)


# Critical path and slack

This guide shows how to find the tasks that decide the project's finish date and how much room every other task has. You can get these numbers from a scheduling run or from dates you already have, without scheduling.

## How analytics work

Analytics are off by default. Ask for them with an [`AnalyticsOptions`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/analytics-options.md>) value, which has two flags: `critical_path` and `slack`. The same value goes into three places:

- `SolveOptions.analytics`, for [`schedule`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/schedule.md>) and [`Project::solve`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/project.md#solve>). The analytics describe the freshly scheduled dates.
- `PreviewOptions.analytics`, for [`Project::preview`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/project.md#preview>). The analytics describe the previewed dates.
- `AnalyzeOptions.analytics`, for [`Project::analyze`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/project.md#analyze>). The analytics describe dates you supply, or the project's current dates, as they are. Nothing gets scheduled.

When you set at least one flag, `ScheduleResult.analytics` holds an [`Analytics`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/results/analytics.md>) value. With both flags off, the field is `None`.

The engine computes the numbers from two plans. The first plan is the dates being analyzed. For the second, the engine schedules the same tasks backward from the latest finish, which gives each task its latest possible start. The gap between the two is the slack:

- **Total slack** is how far a task can slip before the project's finish date moves. It's the latest start minus the planned start.
- **Free slack** is how far a task can slip before it pushes any successor. It's the smallest margin over the task's outgoing links, never more than the total slack. A task with no successors has free slack equal to its total slack.

A task is **critical** when its total slack is zero or less. A link is critical when both of its tasks are critical and the link is binding, so its target starts exactly when the link allows. [`Critical`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/results/critical.md>) lists the critical task and link ids, and `slack` holds one [`TaskSlack`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/results/task-slack.md>) per task.

A few rules shape the output:

- Slack is in the configured `duration_unit` (days by default) and counts working time on each task's calendar. A weekend between two tasks doesn't add slack.
- Only leaf tasks and milestones get a slack entry. A summary has no slack, and it's critical when any of its non-summary descendants is.
- With `use_progress`, completed tasks (`progress == 1`) report zero total and free slack, but they're never critical.
- A dependency cycle makes the numbers meaningless. `status` is then [`AnalyticsStatus::Invalid`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/results/analytics-status.md>), and both lists are `None`.

## Getting critical path and slack

### Requesting analytics

Set the flags in [`SolveOptions`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/solve-options.md>) to get analytics together with the schedule.

```rust
use plen_core::{
    schedule, AnalyticsOptions, DateTime, LinkData, LinkType, ProjectData, SolveOptions, TaskData,
};

fn task(id: &str, start: DateTime, duration: f64) -> TaskData {
    let mut task = TaskData::new(id);
    task.start_date = Some(start);
    task.duration = Some(duration);
    task
}

fn fs(id: &str, source: &str, target: &str) -> LinkData {
    LinkData {
        id: id.into(),
        source: source.into(),
        target: target.into(),
        link_type: LinkType::Fs,
        lag: 0.0,
        lag_unit: None,
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let monday = DateTime::from_ymd_hm(2026, 8, 10, 8, 0).ok_or("invalid date")?;
    let data = ProjectData {
        tasks: vec![
            task("design", monday, 2.0),
            task("build", monday, 3.0),
            task("docs", monday, 1.0),
        ],
        links: vec![fs("l1", "design", "build"), fs("l2", "design", "docs")],
        ..Default::default()
    };

    let options = SolveOptions {
        analytics: AnalyticsOptions {
            critical_path: true,
            slack: true,
        },
        ..Default::default()
    };
    let result = schedule(data, &options)?;

    let analytics = result.analytics.ok_or("analytics not requested")?;
    if let Some(critical) = &analytics.critical_path {
        for id in &critical.tasks {
            println!("critical task: {id}");
        }
        for id in &critical.links {
            println!("critical link: {id}");
        }
    }
    for s in analytics.slack.iter().flatten() {
        println!("{}: total {} free {}", s.task, s.total, s.free);
    }
    Ok(())
}
```

Both `build` and `docs` wait for `design`. `build` takes three days and `docs` takes one, so `design` and `build` form the critical path, joined by link `l1`. `docs` reports `total 2 free 2`: it can slip two working days before the project end moves.

[`Project::solve`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/project.md#solve>) takes the same `SolveOptions`. The flags are part of the solve cache key, so `solve(None, ..)` with analytics turned on recomputes from the accepted dates instead of returning a cached result without them.

### Checking a change before you commit it

Set the flags in [`PreviewOptions`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/preview-options.md>) to see how an edit would change the critical path.

```rust
use plen_core::{
    AnalyticsOptions, DateTime, LinkData, LinkType, Patch, PreviewOptions, Project, ProjectData,
    SchedulingChanges, SolveOptions, TaskData, TaskUpdate,
};

fn task(id: &str, start: DateTime, duration: f64) -> TaskData {
    let mut task = TaskData::new(id);
    task.start_date = Some(start);
    task.duration = Some(duration);
    task
}

fn fs(id: &str, source: &str, target: &str) -> LinkData {
    LinkData {
        id: id.into(),
        source: source.into(),
        target: target.into(),
        link_type: LinkType::Fs,
        lag: 0.0,
        lag_unit: None,
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let monday = DateTime::from_ymd_hm(2026, 8, 10, 8, 0).ok_or("invalid date")?;
    let data = ProjectData {
        tasks: vec![
            task("design", monday, 2.0),
            task("build", monday, 3.0),
            task("docs", monday, 1.0),
        ],
        links: vec![fs("l1", "design", "build"), fs("l2", "design", "docs")],
        ..Default::default()
    };

    let mut project = Project::default();
    project.solve(Some(data), &SolveOptions::default())?;

    // What if the docs take five days instead of one?
    let mut longer = TaskUpdate::new("docs");
    longer.duration = Patch::Value(5.0);
    let mut changes = SchedulingChanges::default();
    changes.tasks.update.push(longer);

    let options = PreviewOptions {
        analytics: AnalyticsOptions {
            critical_path: true,
            slack: true,
        },
        ..Default::default()
    };
    let result = project.preview(&changes, &options)?;

    let analytics = result.analytics.ok_or("analytics not requested")?;
    if let Some(critical) = &analytics.critical_path {
        let ids: Vec<String> = critical.tasks.iter().map(|id| id.to_string()).collect();
        println!("critical: {}", ids.join(", "));
    }
    for s in analytics.slack.iter().flatten() {
        println!("{}: total {} free {}", s.task, s.total, s.free);
    }
    Ok(())
}
```

With five days of docs, the critical path moves to `design, docs`, and `build` now has two days of slack. The project itself doesn't change. For more on previewing, see [Previewing changes](<https://docs.dhtmlx.com/planning-engine/rust/live-projects/previewing-changes.md>).

### Analyzing without scheduling

Use [`Project::analyze`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/project/project.md#analyze>) when you already have dates, for example from your own database or a gantt chart, and want the critical path for those exact dates.

```rust
use plen_core::{
    AnalyticsOptions, AnalyzeOptions, DateTime, LinkData, LinkType, Project, ProjectData, TaskData,
};

fn task(id: &str, start: DateTime, duration: f64) -> TaskData {
    let mut task = TaskData::new(id);
    task.start_date = Some(start);
    task.duration = Some(duration);
    task
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let monday = DateTime::from_ymd_hm(2026, 8, 10, 8, 0).ok_or("invalid date")?;
    let thursday = DateTime::from_ymd_hm(2026, 8, 13, 8, 0).ok_or("invalid date")?;

    // "build" starts a day after "design" ends. A solve would pull it back to Wednesday.
    let data = ProjectData {
        tasks: vec![task("design", monday, 2.0), task("build", thursday, 3.0)],
        links: vec![LinkData {
            id: "l1".into(),
            source: "design".into(),
            target: "build".into(),
            link_type: LinkType::Fs,
            lag: 0.0,
            lag_unit: None,
        }],
        ..Default::default()
    };

    let project = Project::default();
    let options = AnalyzeOptions {
        analytics: AnalyticsOptions {
            critical_path: true,
            slack: true,
        },
    };
    let result = project.analyze(Some(&data), &options)?;
    assert!(result.updates.is_empty());

    let analytics = result.analytics.ok_or("analytics not requested")?;
    if let Some(critical) = &analytics.critical_path {
        for id in &critical.tasks {
            println!("critical task: {id}");
        }
    }
    for s in analytics.slack.iter().flatten() {
        println!("{}: total {} free {}", s.task, s.total, s.free);
    }
    Ok(())
}
```

Only `build` is critical. `design` has one day of total and free slack, because `build` starts a day later than the link requires. Link `l1` isn't critical: it isn't binding, and `design` isn't critical.

`analyze` takes `&self` and never changes the project. With `Some(&data)` it validates and reads the data but doesn't install it. Invalid data fails with [`Error::Input`](<https://docs.dhtmlx.com/planning-engine/rust/api/core/errors/error.md>), as in [Error handling](<https://docs.dhtmlx.com/planning-engine/rust/error-handling.md>). The result has no `updates` and no planner conflicts, and its `baseline_id` is `None`.

### Analyzing the project's current dates

Pass `None` to analyze the dates the project holds now, including edits that haven't been solved yet.

```rust
use plen_core::{
    AnalyticsOptions, AnalyzeOptions, DateTime, LinkData, LinkType, Patch, Project, ProjectData,
    SchedulingChanges, SolveOptions, TaskData, TaskUpdate,
};

fn task(id: &str, start: DateTime, duration: f64) -> TaskData {
    let mut task = TaskData::new(id);
    task.start_date = Some(start);
    task.duration = Some(duration);
    task
}

fn fs(id: &str, source: &str, target: &str) -> LinkData {
    LinkData {
        id: id.into(),
        source: source.into(),
        target: target.into(),
        link_type: LinkType::Fs,
        lag: 0.0,
        lag_unit: None,
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let monday = DateTime::from_ymd_hm(2026, 8, 10, 8, 0).ok_or("invalid date")?;
    let data = ProjectData {
        tasks: vec![
            task("design", monday, 2.0),
            task("build", monday, 3.0),
            task("docs", monday, 1.0),
        ],
        links: vec![fs("l1", "design", "build"), fs("l2", "design", "docs")],
        ..Default::default()
    };

    let mut project = Project::default();
    project.solve(Some(data), &SolveOptions::default())?;

    let options = AnalyzeOptions {
        analytics: AnalyticsOptions {
            critical_path: true,
            slack: false,
        },
    };

    // Solved and clean: the result names the baseline it read.
    let clean = project.analyze(None, &options)?;
    assert!(clean.baseline_id.is_some());

    // Commit an edit without solving.
    let mut longer = TaskUpdate::new("docs");
    longer.duration = Patch::Value(5.0);
    let mut changes = SchedulingChanges::default();
    changes.tasks.update.push(longer);
    project.apply(&changes)?;

    let dirty = project.analyze(None, &options)?;
    assert!(dirty.baseline_id.is_none());
    if let Some(critical) = dirty.analytics.and_then(|a| a.critical_path) {
        let ids: Vec<String> = critical.tasks.iter().map(|id| id.to_string()).collect();
        println!("critical: {}", ids.join(", "));
    }
    Ok(())
}
```

After the edit, the critical path is `design, docs`. `analyze` doesn't change the project's solved status, so the project still needs `solve(None, ..)` before `preview` works again. The `baseline_id` is set only when the project is solved and has no unsolved edits. That's how you tell whether the analytics describe the accepted baseline.

### Reading the result

Check `status` first. Then read the lists you asked for: a flag you left off gives `None` for its list even when the status is `Available`.

```rust
use plen_core::{
    schedule, AnalyticsOptions, AnalyticsStatus, DateTime, LinkData, LinkType, ProjectData,
    SolveOptions, TaskData, TaskType,
};

fn task(id: &str, start: DateTime, duration: f64) -> TaskData {
    let mut task = TaskData::new(id);
    task.start_date = Some(start);
    task.duration = Some(duration);
    task
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let monday = DateTime::from_ymd_hm(2026, 8, 10, 8, 0).ok_or("invalid date")?;

    let mut phase = TaskData::new("phase");
    phase.task_type = TaskType::Summary;

    let mut spec = task("spec", monday, 1.0);
    spec.parent = Some("phase".into());
    spec.progress = 1.0;

    let mut review = task("review", monday, 2.0);
    review.parent = Some("phase".into());

    let mut data = ProjectData {
        tasks: vec![phase, spec, review, task("release", monday, 1.0)],
        links: vec![LinkData {
            id: "l1".into(),
            source: "review".into(),
            target: "release".into(),
            link_type: LinkType::Fs,
            lag: 0.0,
            lag_unit: None,
        }],
        ..Default::default()
    };
    data.config.use_progress = true;

    let options = SolveOptions {
        analytics: AnalyticsOptions {
            critical_path: true,
            slack: true,
        },
        ..Default::default()
    };
    let result = schedule(data, &options)?;

    let analytics = result.analytics.ok_or("analytics not requested")?;
    if analytics.status == AnalyticsStatus::Invalid {
        println!("{} dependency cycle(s), no analytics", result.cycles.len());
        return Ok(());
    }
    if let Some(critical) = &analytics.critical_path {
        let ids: Vec<String> = critical.tasks.iter().map(|id| id.to_string()).collect();
        println!("critical: {}", ids.join(", "));
    }
    for s in analytics.slack.iter().flatten() {
        println!("{}: total {} free {}", s.task, s.total, s.free);
    }
    Ok(())
}
```

The output shows each rule from the concept section:

```text
critical: review, phase, release
spec: total 0 free 0
review: total 0 free 0
release: total 0 free 0
```

- `phase` is critical because its child `review` is. It has no slack entry, since summaries never get one.
- `spec` is complete, so it reports zero slack. It still isn't on the critical path.
- The critical task list follows the scheduling graph order, not input order. Look tasks up by id instead of relying on their position.

If the links form a cycle, the solve returns unchanged dates and lists each cycle in `result.cycles`. The analytics then come back as `Invalid` with no lists. Fix the cycle and run again. For the rest of the result, see [Reading results](<https://docs.dhtmlx.com/planning-engine/rust/scheduling/reading-results.md>).
