First, you might be looking for this call:
Blockly.getMainworkspace().addChangeListener(Blockly.Events.disableOrphans);
Though that will disable the repeat block in your example, since it's assuming that there would be some sort of 'start' block at the top of the stack.
To answer your question directly, here's some code:
const blocks = Blockly.getMainWorkspace().getTopBlocks(false);
for (const block of blocks) {
if (block.outputConnection) {
// This block is unreachable as described in this thread.
}
}
Note that the current generators will actually generate code for disconnected value blocks -- they aren't unreachable. In many cases they are harmless, but they might have side effects.
Here's an example: