scanServerHandles function

List<ServerHandle> scanServerHandles(
  1. String runDir, {
  2. String? underRoot,
})

The srv-*.json handles in runDir, newest first, optionally filtered to servers whose project root sits inside underRoot.

Containment rather than equality, deliberately: a server in a monorepo runs from repo/server_pkg/ and must still appear under the repo worktree. Reads files and nothing else — liveness is decided by connecting, and this is called from places (report, computeAll) that must not open sockets.

Implementation

List<ServerHandle> scanServerHandles(String runDir, {String? underRoot}) {
  List<FileSystemEntity> entries;
  try {
    entries = Directory(runDir).listSync();
  } on FileSystemException {
    return [];
  }
  var handles = <ServerHandle>[];
  for (var entity in entries) {
    var name = p.basename(entity.path);
    if (entity is! File ||
        !name.startsWith('srv-') ||
        !name.endsWith('.json')) {
      continue;
    }
    var handle = ServerHandle.tryRead(entity);
    if (handle == null) continue;
    if (underRoot != null &&
        !p.equals(underRoot, handle.projectRoot) &&
        !p.isWithin(underRoot, handle.projectRoot)) {
      continue;
    }
    handles.add(handle);
  }
  handles.sort((a, b) => b.startedAt.compareTo(a.startedAt));
  return handles;
}