requestCoherenceRefusal function

ModelCoherenceRefusal? requestCoherenceRefusal(
  1. RequestCoherenceInput input
)

G2: the request-build gate. Returns the refusal to surface instead of the request, or null when the quadruple is coherent enough to send.

Deliberately conservative: a key that matches NO configured family at all (a session-only explicit token, an OAuth blob, a synthetic test key) is never refused — only a key that provably belongs to a KNOWN other family while the active endpoint's own family resolves a different value (or nothing) is a mixed triple.

Implementation

ModelCoherenceRefusal? requestCoherenceRefusal(RequestCoherenceInput input) {
  final mismatch = input.mismatch;
  if (mismatch != null) {
    return ModelCoherenceRefusal(
      problem: mismatch.problem,
      fields: mismatch.fields,
      repair: '/provider ${input.modelProviderName} — then /model <id>',
    );
  }
  if (input.identityKind != input.wiringKind) {
    return ModelCoherenceRefusal(
      problem:
          'model claims provider "${input.modelProviderName}" (wire '
          '${input.identityKind}) but the session wiring speaks '
          '${input.wiringKind}',
      fields: const ['provider', 'baseUrl'],
      repair: '/provider ${input.modelProviderName} — then /model <id>',
    );
  }
  if (input.bindingError != null) {
    return ModelCoherenceRefusal(
      problem: input.bindingError!,
      fields: const ['provider', 'model', 'baseUrl'],
      repair: '/model <id> on a matching provider, or /provider <name>',
    );
  }
  if (!input.wiringKeyPresent ||
      input.explicitToken ||
      input.headerAuth ||
      input.activeFamilyResolvesSameKey) {
    return null;
  }
  if (!input.foreignFamilyResolvesSameKey) return null;
  return ModelCoherenceRefusal(
    problem:
        'the session key was resolved for a DIFFERENT provider family — '
        'it must not ride a request to ${input.baseUrl}',
    fields: const ['key', 'baseUrl'],
    repair:
        '/provider ${input.modelProviderName} to re-bind this endpoint\'s '
        'own key — then /model <id>',
  );
}