normalizeLexicalPath function
Collapses ., .., empty (duplicate-slash) and trailing segments of
path against the root /, staying at the root when .. climbs above
it — the sandbox shells' normalizer semantics (/a/../.. → /).
Implementation
String normalizeLexicalPath(String path) {
final segments = <String>[];
for (final part in path.split('/')) {
if (part.isEmpty || part == '.') continue;
if (part == '..') {
if (segments.isNotEmpty) segments.removeLast();
continue;
}
segments.add(part);
}
return '/${segments.join('/')}';
}