verifyAgentSdkPayloadFor function
The post-install check: whether payloadDir — an installed AI server —
holds a tree the transport can actually run.
npm ci exiting 0 is not that proof. It succeeds with optional
dependencies skipped, which for this SDK means a tree with no CLI to spawn;
it succeeds for a Node whose platform the SDK ships nothing for; and an
install interrupted between npm and the marker leaves a tree that looks
complete. So the installer runs this before it calls anything installed.
What the SDK needs is read from the SDK's own manifest where it says so —
its entry point from main, its peers from peerDependencies — so an SDK
release that renames its entry or adds a peer is checked for what it now
requires rather than for what an earlier one did.
The installer states the installing Node's platform, which is the one the SDK will look for; a caller checking for this Dart host passes agentSdkHostPlatform.
expectedPlatform null means "any one CLI" — not a way to switch the check
off: a tree with no CLI at all is a problem either way.
Implementation
AgentSdkPayloadVerification verifyAgentSdkPayloadFor({
required String payloadDir,
required String? expectedPlatform,
}) {
final sep = Platform.pathSeparator;
final problems = <String>[];
final platforms = <String>[];
AgentSdkPayloadVerification result() => AgentSdkPayloadVerification(
payloadDir: payloadDir,
problems: List.unmodifiable(problems),
cliPlatforms: List.unmodifiable(platforms..sort()),
expectedPlatform: expectedPlatform,
);
if (!Directory(payloadDir).existsSync()) {
// Everything below would report the same fact five more times.
problems.add(
'the AI server is not installed at "$payloadDir" — press Install in '
'the AI notice, or run `dart run tom_core_agentic:install_agent_sdk_server`',
);
return result();
}
void requireFile(String path, String what) {
final file = File(path);
if (!file.existsSync()) {
problems.add('$what is missing: "$path"');
} else if (file.lengthSync() == 0) {
// A zero-length file is what an interrupted copy leaves behind, and it
// passes every existence check ever written.
problems.add('$what is empty: "$path"');
}
}
requireFile('$payloadDir$sep$agentSdkServerScriptName', 'the server script');
requireFile('$payloadDir${sep}package.json', "the server's manifest");
final modules = '$payloadDir${sep}node_modules';
final scope = '$modules$sep$_sdkScope';
final sdkDir = '$scope$sep$_sdkName';
final manifest = File('$sdkDir${sep}package.json');
if (!manifest.existsSync()) {
problems.add(
'the Agent SDK is not installed: "$sdkDir" — `npm ci` did not run '
'here, or the SDK package was renamed upstream',
);
return result();
}
Map<String, Object?> sdk;
try {
sdk = jsonDecode(manifest.readAsStringSync()) as Map<String, Object?>;
} on FormatException catch (e) {
problems.add('the Agent SDK\'s manifest is unreadable: ${e.message}');
return result();
}
// The importable half: what the server's `import` resolves to.
final main = sdk['main'];
if (main is! String || main.isEmpty) {
problems.add('the Agent SDK\'s manifest names no entry point (`main`)');
} else {
requireFile(
'$sdkDir$sep${main.replaceAll('/', sep)}',
"the Agent SDK's entry point",
);
}
// npm installs peers since v7, so a missing one is a tree installed by a
// tool that does not — and the SDK fails at import, not at install.
final peers = sdk['peerDependencies'];
if (peers is Map) {
for (final peer in peers.keys.cast<String>()) {
final dir = '$modules$sep${peer.replaceAll('/', sep)}';
if (!File('$dir${sep}package.json').existsSync()) {
problems.add('the Agent SDK\'s peer "$peer" is missing: "$dir"');
}
}
}
platforms.addAll(
Directory(scope)
.listSync()
.whereType<Directory>()
.map((d) => d.path.split(sep).last)
.where(_isPlatformPackage)
.map((name) => name.substring(_sdkName.length + 1)),
);
if (platforms.isEmpty) {
problems.add(
'no Claude Code CLI package under "$scope" — the SDK has nothing to '
'spawn; the tree was installed with optional dependencies omitted',
);
return result();
}
if (expectedPlatform != null) {
final allowed = {
for (final name in _keptPlatformPackages(expectedPlatform))
name.substring(_sdkName.length + 1),
};
final foreign = platforms.where((p) => !allowed.contains(p)).toList();
if (!platforms.any(allowed.contains)) {
problems.add(
'the install carries no CLI for "$expectedPlatform", only '
'"${(platforms..sort()).join('", "')}"',
);
} else if (foreign.isNotEmpty) {
// npm installs only the installing Node's own package; another one here
// means the tree did not come from this install.
problems.add(
'the install should carry only the "$expectedPlatform" CLI and also '
'carries "${(foreign..sort()).join('", "')}"',
);
}
}
for (final platform in platforms) {
final dir = '$scope$sep$_sdkName-$platform';
final binary = File(
'$dir$sep${platform.startsWith('win32-') ? 'claude.exe' : 'claude'}',
);
requireFile(binary.path, 'the Claude Code CLI for $platform');
// The bit, not the file: a native binary without `+x` fails at a depth no
// directory listing reveals.
if (!Platform.isWindows &&
binary.existsSync() &&
binary.statSync().mode & 0x49 == 0) {
problems.add(
'the Claude Code CLI for $platform is not executable: '
'"${binary.path}"',
);
}
}
return result();
}