run method

  1. @override
Future<BdResult> run(
  1. List<String> args, {
  2. Duration? timeout,
  3. String? stdin,
})

Runs bd <args>. Throws BdTimeoutException if the call exceeds timeout (the implementation kills the process tree); never throws for a non-zero exit — that is reported via BdResult.exitCode for the caller to route through BdException.fromOutput.

stdin, when provided, is written to the child's stdin and the stream is closed (EOF). bd batch reads its line-oriented script this way — one spawn, one Dolt transaction (ADR-0001 D4).

Implementation

@override
Future<BdResult> run(List<String> args, {Duration? timeout, String? stdin}) {
  _eventLog?.add('bd:${args.isEmpty ? '' : args.first}');
  calls.add(List<String>.unmodifiable(args));
  stdins.add(stdin);
  final sub = args.isNotEmpty ? args.first : '';
  if (sub == 'dep' && args.length > 1 && args[1] == 'list') {
    return Future<BdResult>.value(
      BdResult(
        exitCode: 0,
        stdout: jsonEncode({
          'schema_version': 1,
          'data': [
            for (final dependency in exportDependencies) dependency.toJson(),
          ],
        }),
        stderr: '',
      ),
    );
  }
  if (sub == 'list') {
    final typeIndex = args.indexOf('-t');
    final type = typeIndex >= 0 ? IssueType(args[typeIndex + 1]) : null;
    final statusIndex = args.indexOf('--status');
    final status = statusIndex >= 0
        ? BeadStatus(args[statusIndex + 1])
        : null;
    final externalRefIndex = args.indexOf('--external-ref');
    final externalRef = externalRefIndex >= 0
        ? args[externalRefIndex + 1]
        : null;
    final metadataFields = <String, String>{};
    for (var i = 0; i < args.length - 1; i++) {
      if (args[i] != '--metadata-field') continue;
      final assignment = args[i + 1];
      final separator = assignment.indexOf('=');
      if (separator < 0) continue;
      metadataFields[assignment.substring(0, separator)] = assignment
          .substring(separator + 1);
    }
    final matches = exportBeads
        .where((bead) {
          if (type != null && bead.issueType != type) return false;
          if (status != null && bead.status != status) return false;
          if (externalRef != null && bead.externalRef != externalRef) {
            return false;
          }
          return metadataFields.entries.every(
            (entry) => bead.metadata[entry.key] == entry.value,
          );
        })
        .toList(growable: false);
    return Future<BdResult>.value(
      BdResult(
        exitCode: 0,
        stdout: jsonEncode({
          'schema_version': 1,
          'data': [for (final bead in matches) bead.toJson()],
        }),
        stderr: '',
      ),
    );
  }
  if (sub == 'export') {
    // Legacy tripwire payload retained for tests that deliberately exercise
    // an unsupported command; production readers never reach this branch.
    final depsByIssue = <String, List<Map<String, dynamic>>>{};
    for (final dep in exportDependencies) {
      (depsByIssue[dep.issueId] ??= <Map<String, dynamic>>[]).add(
        dep.toJson(),
      );
    }
    final jsonl = exportBeads
        .map((b) {
          final json = b.toJson();
          final deps = depsByIssue[b.id];
          if (deps != null) json['dependencies'] = deps;
          return jsonEncode(json);
        })
        .join('\n');
    return Future<BdResult>.value(
      BdResult(exitCode: 0, stdout: jsonl, stderr: ''),
    );
  }
  if (sub == 'create' && args.length > 1 && args[1] == '--graph') {
    // `bd create --graph <plan-file> [--ephemeral] --json` — a graph-apply
    // pour reports a `key → id` MAP (`data.ids`), never a single `data.id`.
    return Future<BdResult>.value(
      BdResult(
        exitCode: 0,
        stdout: jsonEncode({
          'schema_version': 1,
          'data': {'ids': graphApplyIds},
        }),
        stderr: '',
      ),
    );
  }
  final data = switch (sub) {
    'create' => '{"id":"$_createdId"}',
    _ => '{"id":"${args.length >= 2 ? args[1] : ''}"}',
  };
  return Future<BdResult>.value(
    BdResult(
      exitCode: 0,
      stdout: '{"schema_version":1,"data":$data}',
      stderr: '',
    ),
  );
}