maybeAwaitReady function

Future<ArmingOutcome> maybeAwaitReady({
  1. required GlintSession session,
  2. required String glintId,
  3. required bool awaitReady,
  4. required int ceilingMs,
  5. String? toolLabel,
})

If awaitReady is true, polls until glintId is hittable. Returns ArmingReady on success (caller proceeds with the action and tags armed metadata onto the response), ArmingFailed with a ready-to- return envelope on miss, or ArmingSkipped when awaitReady is false.

Implementation

Future<ArmingOutcome> maybeAwaitReady({
  required GlintSession session,
  required String glintId,
  required bool awaitReady,
  required int ceilingMs,
  String? toolLabel,
}) async {
  if (!awaitReady) return const ArmingSkipped();
  final result = await session.readinessGate.awaitReady(glintId: glintId, ceilingMs: ceilingMs);
  switch (result) {
    case ReadyResult():
      return ArmingReady(attempts: result.attempts, elapsedMs: result.elapsedMs);
    case BusyResult():
      return ArmingFailed(StructuredResponse.appBusy(result.timeout,
          summary: '${toolLabel ?? "action"} on $glintId: the app stayed busy '
              '(${result.attempts} polls, ${result.elapsedMs}ms)'));
    case NotFoundResult():
      final hint = didYouMean(result.suggestions);
      return ArmingFailed(StructuredResponse.error(
        summary: '${toolLabel ?? "action"} on $glintId: target never appeared '
            '(${result.attempts} polls, ${result.elapsedMs}ms'
            '${result.staleScreen ? ", screen static" : ""})',
        errorKind: GlintErrorKind.unresolvedTarget,
        detail: result.staleScreen
            ? 'the screen stopped changing and still has no glintId="$glintId" — '
                'the id is stale or belongs to another screen'
            : 'no scene poll within $ceilingMs ms saw glintId="$glintId"',
        nextSteps: [
          if (hint != null) hint,
          'verify the glintId via `get_scene`',
          if (!result.staleScreen) 'raise `readyTimeoutMs` if the target arrives slowly',
        ],
      ));
    case NeverReadyResult():
      return ArmingFailed(StructuredResponse.error(
        summary: '${toolLabel ?? "action"} on $glintId: present but never '
            'hittable (${result.attempts} polls, ${result.elapsedMs}ms)',
        errorKind: GlintErrorKind.targetNeverReady,
        detail: result.detail,
        nextSteps: const [
          'check if a modal, absorber, or overlay covers the target',
          'raise `readyTimeoutMs` if the target settles slowly',
        ],
      ));
  }
}