Skip to content

Graph analysis and optimization

Graph analysis helps you inspect a definition before your application starts using it. It can identify structural problems, repeated conditions and combinations of built-in rules that deserve attention. Your application team then decides whether to change the definition and checks that the revised graph still makes the intended decisions.

The optimize command produces a report; it leaves the input file unchanged. Its structural exploration also tells you what it examined. Reading that scope is part of interpreting the result: an unexplored branch and an impossible choice are different findings.

The tools answer different questions, even when they inspect the same graph:

Tool or report section What it examines How your application team uses it
validate Whether the definition can be accepted by the graph loader and its validation rules. Fix invalid input before treating it as a usable graph.
GraphScanner / structure Nodes, connections and selected metadata checks. Locate structural findings and distinguish optional presentation information from blocking problems.
RulesInspector / semantic Parameters and relationships of supported built-in conditions. Investigate repeated predicates, invalid parameters or provably incompatible conditions.
Simulator / simulation and dynamic A bounded exploration of graph connections. Inspect the paths actually explored and whether exploration finished.

OptimizerRunner combines the latter three into one report. The CLI loads the JSON definition before running them, so a loader error can stop the command before an optimizer report exists. Start with Graph validation and diagnostics if loading already fails.

The rules inspector does not execute custom rule handlers. A finding that their semantics were not analyzed tells you to verify those rules through your application’s configured integration. It does not establish that they will pass or fail during execution.

We will reuse the learning-choice graph introduced in Anatomy of a graph. Node 10 offers a guide through edge 101, ending at node 20, and practice through edge 102, ending at node 30. Practice requires the context value practiceEnabled to be true.

The analysis example before the change uses graph code learning-path-analysis and repeats that same built-in condition twice on edge 102. Both predicates ask exactly the same question. Removing one is a candidate simplification, provided that we verify the resulting behavior.

Use the CLI already available in your PHP project, as described in Command-line workflows. The following commands assume the downloaded file is in the current directory:

Terminal window
php vendor/bin/execkernel validate analysis-learning-before.json --json

Validation succeeds: the graph has three nodes, two edges and two terminals. A redundant predicate does not make this definition invalid. Now ask for the analysis report:

Terminal window
php vendor/bin/execkernel optimize \
--graph=analysis-learning-before.json \
--mode=strict \
--max-paths=1000 \
--max-work=10000 > analysis-before-report.json

--graph selects the definition. --mode=strict selects the exit policy; it does not change the graph or the findings. The two bounds limit structural exploration, which we will interpret below. The shell saves standard output as JSON; choose a fresh report filename because > can replace an existing file.

The complete recorded report contains this semantic finding:

{
"code": "OPT-SEM-003",
"severity": "INFO",
"message": "Repeated identical built-in condition.",
"edgeId": 102,
"conditionIndex": 1,
"rule": "context_value",
"firstConditionIndex": 0
}

The indexes start at zero within the conditions attached to that edge. Here, condition 1 repeats condition 0 on edge 102. The location tells you what to inspect; INFO tells you that this is an observation to consider, not a blocking error.

The command exits with 0, while the summary contains infos: 1. A successful exit therefore does not mean that the report is empty. Your review should read the findings that matter to the change you are making.

The learning graph keeps its two routes. Edge 102 has the same practiceEnabled condition twice before the change and once after it; the informational finding points to that repetition.

The revised definition removes only the second identical condition. Its complete conditions array is now:

[
{
"edge_id": 102,
"rule": "context_value",
"params": {
"key": "practiceEnabled",
"operator": "==",
"value": true
}
}
]

This change keeps the requirement for practice. It does not enable practice for everyone or remove the application’s responsibility to supply the correct context. Run the same analysis against the revised file:

Terminal window
php vendor/bin/execkernel optimize \
--graph=analysis-learning-after.json \
--mode=strict \
--max-paths=1000 \
--max-work=10000 > analysis-after-report.json

The recorded result after the change has zero errors, warnings and informational findings. That confirms that the repeated-condition finding disappeared. To check the application’s behavior, we also executed edge 102 with both relevant context values:

Context supplied by the application Before After
practiceEnabled: false Choice rejected; state stays at node 10. Same rejection and state.
practiceEnabled: true Choice accepted; terminal 30 is returned. Same terminal and state.

For this built-in predicate, the repeated evaluation adds no new condition. The observed outputs support removing it. They do not establish a latency improvement; measure that separately if it matters. Do not apply the same reasoning blindly to custom handlers whose behavior or side effects your application must understand.

After reviewing the definition and relevant execution cases, follow Graph upgrade and publication pipeline to introduce the revision. An optimizer report does not publish a graph or migrate existing sessions.

Read structural exploration as structural evidence

Section titled “Read structural exploration as structural evidence”

Both complete reports contain these fields:

{
"analysis_kind": "structural_edge_unique_paths",
"complete": true,
"stop_reason": null,
"rules_evaluated": false,
"user_probabilities": false,
"paths": 2
}

The simulator walks the declared connections in a deterministic depth-first order. Within a path, it does not traverse the same edge again. It counts paths ending at terminals or at points where that exploration cannot continue. This bounds cyclic exploration without pretending to model an actual person’s choices.

The two paths in this example are the routes to terminals 20 and 30. They are counted even though practice may be unavailable for a particular session. The simulator does not evaluate the session’s context, call custom handlers or assign probabilities to choices.

Fields such as node_visits, terminal_hits and abandonment_rates describe this exploration. In particular, abandonment_rates refers to structural dead ends in the explored paths. It is not measured user abandonment. To answer questions about activity in your application, collect confirmed events through Flow Analytics Events.

Run the revised graph with room for only one completed path:

Terminal window
php vendor/bin/execkernel optimize \
--graph=analysis-learning-after.json \
--mode=strict \
--max-paths=1 \
--max-work=10000

This run exits with 0 and reports OPT-DYN-002 at INFO severity. Its relevant fields are:

{
"complete": false,
"stop_reason": "max_paths",
"paths": 1,
"node_visits": {"10": 1, "20": 1, "30": 0}
}

Node 30 has zero visits because the bound stopped the exploration before that branch. The result is not evidence that the node is unreachable. If complete exploration is a requirement in your review process, check simulation.complete explicitly; strict mode alone does not turn an informational partial-result finding into a failure.

--max-work limits the exploration’s work counter, which includes inspected edges and entered frames. It is not a millisecond timeout. Both bounds accept integers from 1 to 1000000; defaults are 1000 paths and 100000 work units. Select finite bounds appropriate to the graph and inspect stop_reason when they are reached.

For a successfully loaded graph, the report and the selected policy determine the result:

Findings Strict mode Permissive mode
At least one FATAL Exit 2. Exit 2.
ERROR, without a fatal finding Exit 1. Exit 0.
WARN, without errors Exit 0 by default; 1 if the configured warning tolerance is exceeded. Exit 0.
Only INFO, or no findings Exit 0. Exit 0.

Use --block-warnings to reject any warning in strict mode, or --max-warnings=N to allow a chosen count; the options are mutually exclusive. Input, usage and internal failures have their own CLI handling, described in CLI commands, options and exit codes.

An application can also construct OptimizerRunner and register an OptimizerRulePluginInterface implementation for additional checks. That plugin analyzes the graph and returns findings; your team defines what those findings mean. Keep the report, the proposed definition change and its behavior checks together so reviewers can assess the actual improvement.