Evaluating Alternatives#
This guide shows how to compare several possible edits in one raw evaluate call and pick the best one, for example to decide which resource should take a task. It covers building candidates, scoring them with and without leveling, reading the metrics, handling candidates that fail, keeping long evaluations off the main thread and committing the winner.
How evaluation works#
evaluate(candidates_json, options_json) is a method of a solved JsonProject. candidates_json is a JSON array of candidates. Each candidate is an object with an id and a changes field. changes holds a SchedulingChanges payload, the same JSON you'd pass to apply. One candidate is one alternative: "give task B to dev2", "split the review between two people", "add a link". The call scores every candidate against the project's accepted baseline and returns the scores in an envelope. The project isn't modified: evaluate is read-only, like preview.
Every candidate goes through the same run on a disposable copy of the project:
- Copy the reference state. That's the accepted baseline, or its cleared copy with
clearFirst(see Options). applythe candidate's changes.solvewithout data and measure the scheduled stage.- When the options set
level, runlevelwith those options and measure the leveled stage.
You'd run the same sequence to commit the candidate for real. Each candidate starts from the reference state, never from the previous candidate. Both stages are compared with the one baseline.
The result of the envelope has three fields:
baseline: the metrics of the reference state itself. All its deltas are 0.candidates: one entry per candidate, in input order. An entry holds the candidate'sid,scheduledmetrics,leveledmetrics (only withlevel) and acost. A failed candidate has anerrorinstead.baselineId: the token of the accepted baseline the candidates were scored against.
Each stage is described by a Metrics object: the project finish and the finish of each focus task, resource overload, conflicts, cycles and a single cost number. Every measurement is an absolute value plus a delta against the baseline, so you can show either one.
Errors come in two levels. Problems with the call itself return an error envelope:
- no solved baseline:
kind: "project",code: "no-baseline". This includes a project that had anapplysince its lastsolve. - a stale
baselineIdin the options or in any candidate'schanges:kind: "project",code: "stale-baseline". - an unknown focus task, leveling options that don't fit the baseline, or
level: true:kind: "validation". - malformed JSON or an unknown field in a candidate or in the options:
kind: "parse".
Everything else fails only its own candidate. The candidate gets an error and no metrics, and the other candidates are scored as usual. See Error handling for the envelope itself.
evaluate reports scores, not dates. The result has no task updates and no overallocation windows. Once you've picked a candidate, commit it with apply and level, or look at its dates with preview.
Candidates#
All examples on this page use the same small project: tasks A and B, two working days each, both starting on Monday, August 10, 2026, and both assigned to dev1. The resource dev2 is idle, so dev1 is overloaded for two days.
Build each alternative as a candidate object and pass the array to evaluate. An empty change set ({}) is a useful candidate too: it scores "leave the plan as it is" next to the other options.
const = ;
// Tasks A and B, two working days each, both on dev1 in the same week.
const = ;
const = ;
const = new;
baseline: cost 2
stay: cost 2
dev2: cost 0
Without leveling, only the scheduled stage is scored. stay keeps both tasks on dev1 and costs 2, two resource-days of overload. dev2 removes the overload and costs 0.
The candidate id is echoed back so you can match results to inputs. Ids aren't checked for uniqueness, and results keep input order anyway. A candidate object takes only id and changes; any other field fails the whole call with a parse error. See the evaluate reference.
Options#
The options object has four fields, all optional:
level: aLevelingOptionsobject levels each candidate after scheduling it and adds the leveled stage.{}uses the default leveling options.falseornull(the default) scores the scheduled stage only.trueisn't accepted: it returns avalidationerror.focus: one task id or an array of ids. These are the tasks to report finish dates and focus overload for. There's no implicit "modified task", so list the tasks your candidates are about.baselineId: a baseline token. The call fails withstale-baselinewhen it no longer names the accepted baseline. Each candidate'schanges.baselineIdis checked the same way.clearFirst: score against a copy with the in-scopeleveling_delayandpausescleared.
Leveling changes the picture. Without it, an overloaded plan looks bad on overload. With it, the overload turns into delay, which is what the user gets after committing and leveling:
const = ;
// Tasks A and B, two working days each, both on dev1 in the same week.
const = ;
const = ;
const = new;
stay: project ends 2026-08-14 08:00 (+2 days), cost 2
A ends 2026-08-12 08:00 (+0)
B ends 2026-08-14 08:00 (+2)
dev2: project ends 2026-08-12 08:00 (+0 days), cost 0
A ends 2026-08-12 08:00 (+0)
B ends 2026-08-12 08:00 (+0)
For stay, leveling resolves the overload by pushing B two days later, so the cost is now the delay. For dev2 there's nothing to level.
level runs with the options exactly as you pass them. Its own clearFirst defaults to true, so each candidate is re-leveled from zero. That creates a trap after an earlier level run. The stored delays are already in the baseline finish, and they hide the overload the current assignments cause. A candidate that removes the overload then shows a negative finish delta, because leveling from zero drops the old delays.
The top-level clearFirst in the evaluate options fixes this. It replaces the reference state with a copy that has the in-scope delays and pauses cleared, solved again. The baseline metrics and every candidate run then use that copy. The scope is level.scope.tasks when you give it, otherwise every task. It's independent of level.clearFirst, so it also works without level.
const = ;
// Tasks A and B, two working days each, both on dev1 in the same week.
const = ;
const = ;
const = new;
baseline overload: 2
dev2: finish delta 0
With clearFirst, the baseline reports 2 resource-days of overload and the dev2 candidate a finish delta of 0. Without it, the baseline has no overload and the same candidate shows a finish delta of -2. Both views are valid. Use clearFirst when you'll commit with level and its default clearFirst: true, which re-levels from zero.
A few details:
baselineId, candidate tokens,leveloptions and focus ids are all checked against the accepted state before anything is cleared.clearFirstadds no new errors.- A candidate's own
changes.baselineIdis dropped before its trial run, because the cleared copy has a different token. The result'sbaselineIdis still the accepted state's. - When there's nothing to clear in scope, the output is the same as with
clearFirst: false.
The full option list is on the evaluate reference. For the leveling options themselves, see Leveling resources.
Metrics and cost#
Every stage reports the same Metrics fields:
| Field | What it measures |
|---|---|
finish.project |
Latest end over scheduled tasks. null when the stage has no tasks. |
finish.tasks |
One { id, finish } entry per focus task, in focus order. |
overload.total |
Overload over all resources. |
overload.resources |
Overload per resource, for every resource that's non-zero in the stage or the baseline. |
overload.focus |
Per focus task: the overload per resource in the buckets that intersect the task. |
conflicts, cycles |
How many conflicts and cycles the stage has. |
cost |
One number for ranking, lower is better. |
Dates come as a shift: { date, delta }. delta is the working time from the baseline finish in config.duration_unit, positive when later. A task's delta is measured on its own calendar, the project's on the global calendar. date is a YYYY-MM-DD HH:mm string, like every date on the raw surface (see Wire format). Counts and overload come as an amount: { value, delta }.
Overload is measured the same way the leveler sees it. For each time bucket, it's how far a resource's load goes over its capacity, summed over buckets. The unit is capacity × buckets, so on a day basis 2 means "two resource-days over". One bucket size (basis) applies to the baseline and every stage of every candidate: level.basis when you give it, otherwise the one derived from config.duration_unit. loadProfile shows the same per-resource sum as excess at that basis.
Focus overload shows where the trouble is. At the scheduled stage it's the overload around the task. At the leveled stage it's what leveling couldn't resolve there. A milestone has zero length and covers no bucket.
The leveled stage (LeveledMetrics) carries the same fields, flattened into one object, plus converged, rounds and windows. windows is the number of overallocation windows left after leveling.
cost is one fixed heuristic:
cost = max(0, finish.project.delta)
+ overload.total.value
+ 1000 × (conflicts.value + cycles.value)
When the basis follows duration_unit, a day of delay and a resource-day of overload weigh the same. An earlier finish doesn't earn a bonus. The 1000 penalty keeps any broken schedule, with conflicts or cycles, behind every valid one. The candidate's own cost is the cost of the last stage present: leveled when you level, scheduled otherwise. The baseline carries its own cost.
If the heuristic doesn't fit your product, rank on the raw values instead. This function prints the per-resource overload change of each candidate's scheduled stage, given the parsed result of an evaluate envelope:
For the project above, the dev2 candidate prints dev2: dev1 overload 0 (-2 vs baseline). The row for dev1 is there because dev1 was overloaded in the baseline, even though it's zero in the stage. Resources come in stage order, then resources that exist only in the baseline. See the Metrics reference.
Failed and cyclic candidates#
A candidate can fail without failing the call. Three cases need handling:
- Rejected.
applyrejects an operation in the change set (the result reports only the first rejection), orsolveorlevelfails on the candidate's input, for example on a leveling scope id the candidate removed. The entry haserror: { path, message }and noscheduled,leveledorcost. - Cyclic. The candidate creates a dependency cycle.
scheduledis measured on the unchanged dates the cyclic solve returns, andcycles.valueis above 0. Leveling stops at round 0 on a cyclic plan, soleveledis absent. The cycle penalty pushes the cost over 1000. - Focus task removed. When the candidate removes a focus task, that task's
finishand its focus overloadresourcesarenull. The rest of the metrics are normal.
const = ;
// Tasks A and B, two working days each, both on dev1 in the same week.
const = ;
const = ;
const = new;
missing rejected at assignments.update[0]: unknown assignment id
cycle is cyclic, leveled: false
cycle cost 1002
removed removes focus task B
removed cost 0
The rejected entry in the envelope looks like this:
Rank only candidates that have a cost. The cyclic candidate has one, but the penalty keeps it at the bottom. Its 1002 is the cycle penalty plus the two resource-days of overload dev1 still has.
Call-level errors still come back as an error envelope with ok: false, and no candidate is scored. For example, focusing on a task the project doesn't have:
const = ;
A stale token returns kind: "project" with code: "stale-baseline". See Error handling.
Sequential only#
Each candidate runs on its own copy, so candidates are independent of each other. The WebAssembly build still scores them one at a time, in input order, on the thread that calls evaluate.
Candidates are scored one after another.
evaluate is synchronous and CPU-bound, like every raw export. A large evaluation blocks the calling thread until it returns. Keep it off the UI thread or the request loop by running it in a Web Worker or a Node.js worker thread. Each worker loads its own module instance and holds its own JsonProject:
// evaluate-worker.js
const = ;
const = ;
// Each message carries the project data, the candidates and the options as JSON text.
;
// main.js
const = ;
const = ;
const = ;
// One candidate per resource that could take task B.
const = .;
const = new;
;
;
The worker prints dev1: cost 2 and dev2: cost 0. Only JSON text crosses between the threads, so the envelope can go back unparsed. For the worker setup in the browser build, see Run in a worker.
Committing the winner#
evaluate never changes the project. To commit the chosen candidate, run its changes for real: apply the same change set, then level with the same leveling options, step included. With other options, the committed plan isn't the one that was scored. If you scored without level, follow apply with solve(undefined, …) instead.
const = ;
// Tasks A and B, two working days each, both on dev1 in the same week.
const = ;
const = ;
const = new;
committed dev2 (cost 0)
Results come back in input order, so the index of a scored entry is the index of its candidate. Setting baselineId to the token from the result makes apply refuse the commit when another edit landed in between. In that case, evaluate again.
If you scored with clearFirst: true in the evaluate options, commit with level and its own clearFirst left at the default true. The commit then re-levels from zero, like the scoring did.
To handle a rejected apply, see Recover from rejected changes. To generate candidates automatically instead of writing them by hand, use suggestAssignments, which scores its steps the same way. See Suggesting assignments.