Files
clide/lib/kernel/src/ipc/isolate_client.dart
T
Jeroen SchweitzerandClaude Opus 4.6 45e8132a41
test / unit + widget + golden + a11y (push) Failing after 28s
test / integration_test (xvfb) (push) Has been skipped
test / bundle smoke (xvfb 5s) (push) Has been skipped
test / daemon subprocess + web WASM smoke (push) Has been skipped
async recvFd + backend terminal test (sandbox blocks ptyc exec)
PtySession.spawn() now runs recvFd in a child isolate via
Isolate.spawn so the blocking FFI call doesn't stall the backend
isolate's event loop.

Terminal testmode test spawns a real backend isolate, opens a project,
and sends pane.spawn via IPC — the exact same path the full app uses.
Currently fails: ptyc successfully starts but its fork+execvp is
blocked by the macOS sandbox ("Operation not permitted"). The SBPL
allows /bin/zsh exec from the app process, but ptyc's child process
may not inherit the exec permission, or the PTYC_SOCK_FD is not
inherited by the ptyc child (Dart Process.start fd inheritance on
macOS).

IsolateClient.events getter exposed for test access.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-26 15:55:29 +02:00

73 lines
2.1 KiB
Dart

/// IPC client that sends requests to a backend isolate via SendPort.
///
/// Replaces [InProcessClient] for production use. The backend isolate
/// owns the [DaemonDispatcher] and all subprocess/file-I/O services.
/// Requests and responses travel as serialized Maps over SendPort,
/// reusing the existing IPC protocol (IpcRequest/IpcResponse/IpcEvent).
library;
import 'dart:async';
import 'dart:isolate';
import 'package:clide/clide.dart';
import 'package:clide/kernel/src/events/bus.dart';
import 'package:clide/kernel/src/events/types.dart';
import 'package:clide/kernel/src/ipc/client.dart';
import 'package:clide/kernel/src/log.dart';
class IsolateClient extends DaemonClient {
IsolateClient({
required Logger log,
required DaemonBus events,
required SendPort backendPort,
}) : _backendPort = backendPort,
_events = events,
super(socketPath: '', log: log, events: events);
final SendPort _backendPort;
final DaemonBus _events;
/// The event bus that receives events from the backend.
DaemonBus get events => _events;
final Map<String, Completer<IpcResponse>> _pending = {};
int _nextId = 0;
/// Called by [Backend] to feed incoming messages from the backend isolate.
void handleMessage(Map<String, Object?> msg) {
final type = msg['type'] as String?;
switch (type) {
case 'response':
final resp = IpcResponse.fromJson(msg);
final c = _pending.remove(resp.id);
if (c != null && !c.isCompleted) c.complete(resp);
case 'event':
final evt = IpcEvent.fromJson(msg);
_events.emit(DaemonEvent(
subsystem: evt.subsystem,
kind: evt.kind,
data: evt.data,
ts: evt.timestamp,
));
}
}
@override
bool get isConnected => true;
@override
Future<void> start() async {}
@override
Future<void> stop() async {}
@override
Future<IpcResponse> request(String cmd, {Map<String, Object?> args = const {}}) {
final id = '${_nextId++}';
final req = IpcRequest(id: id, cmd: cmd, args: args);
final c = Completer<IpcResponse>();
_pending[id] = c;
_backendPort.send(req.toJson());
return c.future;
}
}