My question is long on background and motivation, and almost but not quite answered over at the nLab. I'll write up a bunch before asking my question (feel free to skip to the end or look at the title), so that (i) those who know more than me can see exactly what I do and do not understand (ii) those that know less than me might learn something (iii) to clarify my own thoughts.

## Background

Let $\mathcal S$ be a reasonably nice category: for example, I want it to at least have either all finite limits, or I want it to have a good theory of submersions; and I need some extra conditions (see comments), that I haven't fully thought through, but they should be satisfied in various "geometrical" categories like (your favorite convenient category of) Topological Spaces or Manifolds. A **span in $\mathcal S$** is a diagram $X \leftarrow M \rightarrow Y$, and if I can, I will require that the right map be a submersion. There is a two-category $\operatorname{Span}(\mathcal S)$ constructed in the usual way: 1-morphisms are spans, and 2-morphisms are maps of spans that cover the identity morphisms on the bases.

A **category object in $\mathcal S$** is a span $X = \{X_0 \leftarrow X_1 \rightarrow X_0\}$ and 2-morphisms $$ \{ X_0 \leftarrow X_0 \rightarrow X_0 \} \overset i \to \{X_0 \leftarrow X_1 \rightarrow X_0\}$$ $$ \{X_0 \leftarrow (X_1 \underset{X_0}\times X_1) \rightarrow X_0\} \overset m \to \{X_0 \leftarrow X_1 \rightarrow X_0\}$$ in $\operatorname{Span}(\mathcal S)$ making $X$ into an algebra object. (The domain of $m$ is the 1-composition of $X$ with itself in $\operatorname{Span}(\mathcal S)$, and the domain of $i$ is the 1-identity span on $X_0$.) Writing $\circ$ for the 1-composition in $\operatorname{Span}(\mathcal S)$, the requirement is that the various maps $X\circ X \circ X \to X$ formed from $m,i$ all agree.

A **functor in $\mathcal S$** $\{X_1 \rightrightarrows X_0\} \to \{Y_1 \rightrightarrows Y_0\}$ is a pair of maps $X_1 \to Y_1$ and $X_0 \to Y_0$ making some diagrams commute. But there tend not to be enough functors when $\mathcal S$ does not satisfy the axiom of choice. For example, if $\mathcal S$ is the category of manifolds, then certain categories (the groupoids, which I will define in a moment) are supposed to present stacks, but the functor that associates to each manifold $M$ the groupoid of functors $\{M \rightrightarrows M\} \to \{X_1 \rightrightarrows X_0\}$ into some groupoid $X$ does not satisfy the right descent axioms.

Instead, the usual thing to do is to define the notion of "right-principal bibundles". Let $X = \{X_1 \rightrightarrows X_0\}$ and $Y = \{Y_1 \rightrightarrows Y_0\}$ be categories. An **$X,Y$-bibundle** is a span $B = \{X_0 \leftarrow B_1 \rightarrow Y_0\}$ and 2-morphisms $X \circ B \to B$ and $B \circ Y \to B$, such that all the various 2-morphisms $X \circ X \circ B \circ Y \circ Y \to B$ agree (as above, $\circ$ is the 1-composition in $\operatorname{Span}(\mathcal S)$). The "tensor product over $Y$" gives a "composition" of bibundles which is associative up to a canonical associator that satisfies a pentagon.

Given a span $B = \{X_0 \leftarrow B_1 \rightarrow Y_0\}$, there is another span
$$ X_0 \leftarrow (B_1 \underset{X_0 \times Y_0}\times B_1) \rightarrow Y_0 $$
and a "diagonal" map from $B$ to this other span. Let $B$ be an $X,Y$-bibundle. Using the diagonal map, one can build a map
$$ B_1 \underset{Y_0}\times Y_1 \to B_1 \underset{X_0}\times B_1$$
which is actually a map of objects over $X_0 \times Y_0 \times Y_0$. On (generalized) elements, this map is $(b,y) \mapsto (b,by)$. The bibundle $B$ is **right-principal** if this map is an isomorphism. A category $X$ is a **groupoid** if it is right-principal as an $X,X$-bibundle. If $X,Y$ are groupoids, a functor $f: X \to Y$ determines an $X,Y$-bibundle where the middle object is $X_0 \underset {Y_0} \times Y_1$, and it is right-principal.

Then the point is that the 2-category whose objects are groupoids in $\mathcal S$ and whose one-morphisms are right-principal bibundles embeds as a full sub-2-category into the category of "stacks", which I will not define. For a precise version of this story, see for example C. Blohmann, Stacky Lie groups, 2007.

## Question

Any functor of categories in $\mathcal S$ determines a bibundle, but if the categories are not groupoids, then the bibundle is not (usually) right-principal; for example, the identity bibundle is not. I do want the bibundles of groupoids to be "morphisms" of categories, so I don't want to just take functors. On the other hand, as observed in Op. cit., if we don't demand some sort of "right-principality" condition, then the 2-category which allows all bibundles as 1-morphisms has neither products nor a terminal object. Hence my question is:

What is the correct notion of "bibundle" for (internal) categories that generalizes "right-principal bibundle of groupoids"?

The answer to my question is almost "anafunctor". An anafunctor seems to have a bit more structure than a bibundle, since it is an object not in $\operatorname{Span}(\mathcal S)$ but in $\operatorname{Span}(\text{categories in }\mathcal S)$. (Conversely, at least when $\mathcal S$ is the category of sets, bibundles wihout any conditions are the same as profunctors.) If I understood better how to go between anafunctors and bibundles, I would probably be happy.