\section{Diagram Optimization via Categorical Rewriting}

Cells do not merely execute metabolic pathways---they \emph{optimize} them.
Flux Balance Analysis identifies rate-limiting enzymes, allosteric regulation
redirects flux through cheaper branches, and pathway rewiring eliminates
dead-end metabolites. The result is not a different pathway, but a more
efficient execution of the same input-output behavior. In this section we
show that wiring diagrams admit analogous optimizations: cost annotations
make resource consumption explicit, static analysis identifies structural
opportunities, and rewriting rules transform diagrams while preserving
observational equivalence.

\subsection{Cost-Annotated Diagrams}

We extend the WAgent operad (Section~4) with resource annotations.
Define a \emph{resource cost} monoid $(\mathbb{R}_{\geq 0}^3, +, \mathbf{0})$
where each element is a triple $(\text{atp}, \text{latency}, \text{memory})$.
A cost-annotated wiring diagram enriches the category of wiring diagrams
over this monoid: each module $m$ carries a cost $c(m) \in \mathbb{R}_{\geq 0}^3$,
and each wire $w$ carries a transmission cost $c(w) \in \mathbb{Z}_{\geq 0}$
(measured in ATP units), embedded into the resource vector as
$c_W^\uparrow(w) = (c(w), 0, 0)$ when added to path costs.

\begin{definition}[Cost-Annotated Wiring Diagram]\label{def:cost-annotated-diagram}
A \emph{cost-annotated wiring diagram} is a tuple $(M, W, c_M, c_W)$ where:
\begin{itemize}[leftmargin=*]
\item $M$ is a finite set of modules, each with typed input/output ports;
\item $W \subseteq \{(m_s.p_s, m_d.p_d) \mid m_s, m_d \in M\}$ is a set of wires;
\item $c_M : M \to \mathbb{R}_{\geq 0}^3$ assigns resource costs to modules;
\item $c_W : W \to \mathbb{Z}_{\geq 0}$ assigns transmission costs to wires.
\end{itemize}
The \emph{path cost} of a directed path $m_1 \xrightarrow{w_1} m_2 \xrightarrow{w_2} \cdots \xrightarrow{w_{n-1}} m_n$
is $\sum_{i=1}^{n} c_M(m_i) + \sum_{i=1}^{n-1} c_W^\uparrow(w_i)$.
\end{definition}

Biologically, $c_M$ corresponds to the ATP cost of enzyme catalysis,
while $c_W$ models the energetic cost of metabolite transport between
cellular compartments.

\subsection{Rewriting Rules as Endofunctors}

Each optimization pass is an endofunctor $F : \mathbf{WDiag} \to \mathbf{WDiag}$
on the category of wiring diagrams that preserves input-output behavior.
Two diagrams $D_1, D_2$ are \emph{behaviorally equivalent} if, for all
admissible input assignments under the deterministic execution model fixed
in Section~\ref{sec:coalgebra}, they produce the same observable outputs. In the current
implementation this notion is checked over finite supplied input suites
rather than by a general coinductive procedure.

\begin{theorem}[Optimization Soundness]\label{thm:optimization-soundness}
Let $F$ be an optimization pass. If $F$ only removes wires that provably
never transmit data (dead wire elimination) or reorders modules within
the same topological layer \emph{and those modules are pairwise observationally independent} (equivalently: no shared writable state and no order-sensitive side effects), then $F(D)$ is
behaviorally equivalent to $D$ for all diagrams $D$.
\end{theorem}

\paragraph{Assumptions.}
Execution is deterministic under the fixed input schedule, dead wires are semantically impossible because their type/optic constraints can never accept runtime values, and reordered modules neither share writable state nor perform order-sensitive side effects.

\begin{proof}[Proof sketch]
The theorem follows by checking the two rewrite primitives.

For dead-wire elimination, a removed wire can never transmit any value by hypothesis. Therefore no execution trace ever uses that wire, so removing it leaves all downstream observations unchanged.

For within-layer reordering, observational independence means the reordered modules do not read or write shared mutable state and do not produce side effects whose meaning depends on order. Hence swapping their execution order preserves each module's local output as well as every downstream module's observed inputs. Since the graph is otherwise unchanged, the overall observable output is unchanged.

Any implemented optimization pass is a composition of these local rewrites. Because each local rewrite preserves observable behavior, the composite pass preserves observable behavior as well.
\end{proof}

\paragraph{Operational takeaway.}
For agent-systems readers, this is the compiler-style rule: remove edges that can never fire, and only reorder steps that are truly independent. Once modules share mutable memory or side effects, optimization becomes a semantic change rather than a free speedup.

\noindent Three concrete passes are implemented:

\begin{enumerate}[leftmargin=*]
\item \textbf{Dead Wire Elimination.} A wire carrying a PrismOptic
whose accepted types have no intersection with the source port's
DataType can never transmit. Removing it does not change behavior.
This corresponds to pruning vestigial metabolic branches---enzymes
that never encounter their substrate are downregulated.

\item \textbf{Parallel Grouping.} Modules with no mutual dependencies
form a \emph{parallel group} and can execute concurrently. This is
identified via topological layering of the dependency DAG, subject to
the disjoint-state assumption of parallel composition from Section~4.
Biologically,
independent metabolic pathways (e.g., glycolysis and fatty acid oxidation)
operate simultaneously in different cellular compartments.

\item \textbf{Cost-Order Scheduling.} Within each parallel group,
modules are sorted by ascending cost. When the parallel group satisfies
the same observational-independence assumption, cheaper modules may execute first,
allowing early termination or budget reallocation if expensive modules
would exceed available ATP. This mirrors cellular enzyme kinetics:
low-$K_m$ (high-affinity, low-cost) enzymes are preferentially activated.
\end{enumerate}

\subsection{Critical Path Analysis}

Because path costs are vector-valued, critical-path analysis requires a
monotone scalarization $\lambda : \mathbb{R}_{\geq 0}^3 \to \mathbb{R}_{\geq 0}$
(for example, the latency projection $\lambda(a,\ell,m)=\ell$ for wall-clock
analysis, or a weighted sum when ATP and latency are jointly optimized).
The \emph{critical path} under scalarization $\lambda$ is the longest
scalarized path through the dependency DAG:

\[
\text{CP}_{\lambda}(D) = \arg\max_{P \in \text{Paths}(D)}
\left(\sum_{m \in P} \lambda(c_M(m)) + \sum_{w \in P} \lambda(c_W^\uparrow(w))\right)
\]

Under the latency projection, the critical path determines a lower bound
on execution time under maximal parallelism---no schedule can complete
faster than that latency-critical path. Under other scalarizations, it
identifies the bottleneck for the chosen objective. In metabolic terms, this is the rate-limiting pathway: the
bottleneck whose throughput constrains the entire system. Identifying
it enables targeted optimization (e.g., caching expensive modules,
splitting them into cheaper sub-diagrams).

\subsection{Resource-Aware Execution}

The \texttt{ResourceAwareExecutor} gates module execution on MetabolicState,
implementing the biological principle that pathway activity is regulated
by cellular energy availability:

\begin{itemize}[leftmargin=*]
\item \textbf{STARVING}: Only essential modules execute; non-essential
modules are skipped. This mirrors the starvation response where cells
shut down biosynthetic pathways to conserve ATP for survival functions.

\item \textbf{CONSERVING}: Expensive non-essential modules are deferred.
Cheap modules execute normally. This corresponds to metabolic triage
under moderate stress.

\item \textbf{NORMAL/FEASTING}: Full execution with parallel scheduling.
Independent module groups execute concurrently via thread pools. This
mirrors the fed state where abundant ATP enables all pathways.
\end{itemize}

\noindent A \texttt{BudgetOptic} provides wire-level cost capping:
once cumulative transmission cost through a wire exceeds a threshold,
further data flow is blocked. This implements pathway-level budget
caps analogous to allosteric feedback inhibition.

\subsection{Relation to Abbott \& Zardini}

Abbott and Zardini~\cite{abbott2025flashattention} derive FlashAttention
``on a napkin'' using Neural Circuit Diagrams~\cite{abbott2024neural}---a
diagrammatic scheme for representing deep learning algorithms with explicit
memory hierarchy awareness. Their core contribution is a performance model
for IO transfer costs across GPU memory levels (SRAM $\leftrightarrow$ HBM),
with cost $H^*(a, M) = \sum_t \alpha_t(a) \cdot M^{-\beta_t}$. Two
concrete optimization techniques drive the derivation: \emph{stream
partitioning}, which proves that SoftMax-Contraction is streamable via
an accumulator maintaining running max and sum; and \emph{group
partitioning}, which tiles query blocks across GPU cores to minimize
HBM transfers.

Our approach shares their insight that diagrammatic representations are
not merely notation but formal objects admitting cost-reducing
transformations. Both build on the broader lineage of categorical
methods in deep learning~\cite{fong2019seven}. The frameworks diverge
in three respects:

\begin{enumerate}[leftmargin=*]
\item \textbf{Optimization target.} Abbott \& Zardini optimize
\emph{IO transfer costs} across a fixed GPU memory hierarchy---bandwidth
between SRAM and HBM is the scarce resource. Operon optimizes
\emph{agent execution} under metabolic resource constraints (ATP budgets,
latency caps, memory limits). Same categorical insight---diagram structure
reveals optimization opportunities---but different cost models.

\item \textbf{Dynamic state.} Their functional equivalence ($\equiv$)
preserves input-output mappings: two diagrams are equivalent when they
produce the same outputs for the same inputs, differing only in resource
profile. Our coalgebraic observational semantics (Section~\ref{sec:coalgebra}) is aimed at
\emph{stateful} systems and tracks observable behavior across transitions
under a chosen input schedule, not just terminal outputs.

\item \textbf{Scope of composition.} Neural Circuit Diagrams represent
\emph{algorithm pipelines}---fixed dataflow graphs where boxes are
functions composed via typed wire columns. Operon's wiring diagrams
represent \emph{agent networks} where modules may be acquired at runtime
(plasmid registry), conditionally routed (prism optics), and subject to
resource-gated execution. The diagram itself is a dynamic object, not a
static specification.
\end{enumerate}

\noindent In short, Abbott \& Zardini demonstrate that diagrammatic
reasoning scales to deriving hardware-efficient algorithms; Operon extends
the same principle from algorithm-level optimization to system-level
orchestration of stateful, resource-constrained agents.
