[Canonical page](<https://docs.dhtmlx.com/planning-engine/js/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. To turn them on, pass an [`analytics`](<https://docs.dhtmlx.com/planning-engine/js/api/types/common.md#analyticsoptions>) object with two flags, `criticalPath` and `slack`. The same object goes into three places:

- The options of [`schedule`](<https://docs.dhtmlx.com/planning-engine/js/api/schedule.md>) and [`project.solve`](<https://docs.dhtmlx.com/planning-engine/js/api/project-solve.md>). The analytics describe the freshly scheduled dates.
- The options of [`project.preview`](<https://docs.dhtmlx.com/planning-engine/js/api/project-preview.md>). The analytics describe the previewed dates.
- The options of [`project.analyze`](<https://docs.dhtmlx.com/planning-engine/js/api/project-analyze.md>). 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, `result.analytics` holds the answer. With both flags off, the result has no `analytics` field. See [`ScheduleResult`](<https://docs.dhtmlx.com/planning-engine/js/api/types/schedule-result.md#analytics>) for its shape.

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. Slack is the gap between the two:

- **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, and 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: its target starts exactly when the link allows. `analytics.criticalPath` lists the critical task and link ids. `analytics.slack` holds one `{ task, total, free }` entry per task.

A few rules shape the output:

- Slack is in `config.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. It's critical when any of its non-summary descendants is.
- With `config.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 `"invalid"`, and both lists are absent.

## Requesting analytics

### Getting analytics with the schedule

Set the flags in the solve options to get analytics together with the scheduled dates.

```js
import { schedule } from "plen-js";

const result = schedule(
  {
    tasks: [
      { id: "design", start_date: "2026-08-10 08:00", duration: 2 },
      { id: "build", start_date: "2026-08-10 08:00", duration: 3 },
      { id: "docs", start_date: "2026-08-10 08:00", duration: 1 },
    ],
    links: [
      { id: "l1", source: "design", target: "build", type: "fs" },
      { id: "l2", source: "design", target: "docs", type: "fs" },
    ],
  },
  { analytics: { criticalPath: true, slack: true } },
);

const { criticalPath, slack } = result.analytics;
console.log(`critical tasks: ${criticalPath.tasks.join(", ")}`);
console.log(`critical links: ${criticalPath.links.join(", ")}`);
for (const s of slack) {
  console.log(`${s.task}: total ${s.total} free ${s.free}`);
}
```

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/js/api/project-solve.md>) takes the same options. The flags are part of the solve cache key, so `project.solve(null, { analytics: { slack: true } })` on a solved project recomputes from the accepted dates. It doesn't return the cached result without analytics.

### Checking a change before you commit it

Set the flags in the preview options to see how an edit would change the critical path.

```js
import { Project } from "plen-js";

const project = new Project();
project.solve({
  tasks: [
    { id: "design", start_date: "2026-08-10 08:00", duration: 2 },
    { id: "build", start_date: "2026-08-10 08:00", duration: 3 },
    { id: "docs", start_date: "2026-08-10 08:00", duration: 1 },
  ],
  links: [
    { id: "l1", source: "design", target: "build", type: "fs" },
    { id: "l2", source: "design", target: "docs", type: "fs" },
  ],
});

// What if the docs take five days instead of one?
const result = project.preview(
  { tasks: { update: [{ id: "docs", duration: 5 }] } },
  { analytics: { criticalPath: true, slack: true } },
);

const { criticalPath, slack } = result.analytics;
console.log(`critical: ${criticalPath.tasks.join(", ")}`);
for (const s of slack) {
  console.log(`${s.task}: total ${s.total} free ${s.free}`);
}
```

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/js/live-projects/previewing-changes.md>).

## Analyzing without scheduling

### Analyzing your own dates

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

```js
import { Project } from "plen-js";

// "build" starts a day after "design" ends. A solve would pull it back to Wednesday.
const data = {
  tasks: [
    { id: "design", start_date: "2026-08-10 08:00", duration: 2 },
    { id: "build", start_date: "2026-08-13 08:00", duration: 3 },
  ],
  links: [{ id: "l1", source: "design", target: "build", type: "fs" }],
};

const project = new Project();
const result = project.analyze(data, {
  analytics: { criticalPath: true, slack: true },
});

console.log(result.updates.length); // 0
const { criticalPath, slack } = result.analytics;
console.log(`critical tasks: ${criticalPath.tasks.join(", ")}`);
for (const s of slack) {
  console.log(`${s.task}: total ${s.total} free ${s.free}`);
}
```

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` never changes the project. When you pass data, it validates and reads the data but doesn't install it. Invalid data throws an [`InputError`](<https://docs.dhtmlx.com/planning-engine/js/api/types/errors.md#inputerror>), as described in [Error handling](<https://docs.dhtmlx.com/planning-engine/js/error-handling.md>). The result has no updates and no planner conflicts, and it has no `baselineId`.

### Analyzing the project's current dates

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

```js
import { Project } from "plen-js";

const project = new Project();
project.solve({
  tasks: [
    { id: "design", start_date: "2026-08-10 08:00", duration: 2 },
    { id: "build", start_date: "2026-08-10 08:00", duration: 3 },
    { id: "docs", start_date: "2026-08-10 08:00", duration: 1 },
  ],
  links: [
    { id: "l1", source: "design", target: "build", type: "fs" },
    { id: "l2", source: "design", target: "docs", type: "fs" },
  ],
});

const options = { analytics: { criticalPath: true } };

// Solved and clean: the result names the baseline it read.
const clean = project.analyze(null, options);
console.log(clean.baselineId !== undefined); // true

// Commit an edit without solving.
project.apply({ tasks: { update: [{ id: "docs", duration: 5 }] } });

const dirty = project.analyze(null, options);
console.log(dirty.baselineId); // undefined
console.log(`critical: ${dirty.analytics.criticalPath.tasks.join(", ")}`);
```

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

## Reading the result

Check [`status`](<https://docs.dhtmlx.com/planning-engine/js/api/types/schedule-result.md#analytics>) first. Then read the lists you asked for. A flag you left off means its list is absent, even when the status is `"available"`.

```js
import { schedule } from "plen-js";

const result = schedule(
  {
    config: { use_progress: true },
    tasks: [
      { id: "phase", type: "summary" },
      { id: "spec", parent: "phase", start_date: "2026-08-10 08:00", duration: 1, progress: 1 },
      { id: "review", parent: "phase", start_date: "2026-08-10 08:00", duration: 2 },
      { id: "release", start_date: "2026-08-10 08:00", duration: 1 },
    ],
    links: [{ id: "l1", source: "review", target: "release", type: "fs" }],
  },
  { analytics: { criticalPath: true, slack: true } },
);

const { analytics } = result;
if (analytics.status === "invalid") {
  console.log(`${result.cycles.length} dependency cycle(s), no analytics`);
} else {
  console.log(`critical: ${analytics.criticalPath.tasks.join(", ")}`);
  for (const s of analytics.slack) {
    console.log(`${s.task}: total ${s.total} free ${s.free}`);
  }
}
```

The output shows each rule from [How analytics work](<https://docs.dhtmlx.com/planning-engine/js/scheduling/critical-path-and-slack.md#how-analytics-work>):

```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`. `analytics` then comes back as `{ status: "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/js/scheduling/reading-results.md>).
