···11+# crit (https://crit.md): agents run `crit` to open their diff/plan for review
22+# in the browser and block until it is finished. Each review session is its
33+# own daemon on its own port, reachable over retiolum as
44+# http://<host>.r:<port>.
55+#
66+# SECURITY: crit has no authentication, and comments left in a review are fed
77+# to the agent as instructions. Whoever can reach a daemon can steer an agent
88+# running here, so the ports are only open on retiolum.
99+{
1010+ self,
1111+ config,
1212+ pkgs,
1313+ ...
1414+}:
1515+let
1616+ firstPort = 7700;
1717+ lastPort = 7719;
1818+ crit-unwrapped = self.inputs.llm-agents.packages.${pkgs.system}.crit;
1919+2020+ # crit binds 127.0.0.1 on a random port by default. Bind all interfaces on a
2121+ # free port from the firewalled range instead and advertise the retiolum URL.
2222+ # Only the invocation that starts a daemon binds the port; reconnecting to a
2323+ # running session uses the port and URL recorded in ~/.crit/sessions.
2424+ crit = pkgs.symlinkJoin {
2525+ name = "crit";
2626+ paths = [
2727+ (pkgs.writeShellScriptBin "crit" ''
2828+ if [ -z "''${CRIT_PORT:-}" ]; then
2929+ for p in $(${pkgs.coreutils}/bin/seq ${toString firstPort} ${toString lastPort}); do
3030+ if [ -z "$(${pkgs.iproute2}/bin/ss -Htln "sport = :$p")" ]; then
3131+ export CRIT_PORT=$p
3232+ break
3333+ fi
3434+ done
3535+ fi
3636+ if [ -n "''${CRIT_PORT:-}" ]; then
3737+ export CRIT_PUBLIC_URL="''${CRIT_PUBLIC_URL:-http://${config.networking.hostName}.r:$CRIT_PORT}"
3838+ else
3939+ echo "crit: ports ${toString firstPort}-${toString lastPort} all busy, using a random port (not reachable over retiolum)" >&2
4040+ fi
4141+ export CRIT_HOST="''${CRIT_HOST:-0.0.0.0}"
4242+ export CRIT_ALLOW_UNAUTHENTICATED_NETWORK=1
4343+ exec ${crit-unwrapped}/bin/crit "$@"
4444+ '')
4545+ crit-unwrapped
4646+ ];
4747+ };
4848+in
4949+{
5050+ environment.systemPackages = [ crit ];
5151+5252+ networking.firewall.interfaces.retiolum.allowedTCPPortRanges = [
5353+ {
5454+ from = firstPort;
5555+ to = lastPort;
5656+ }
5757+ ];
5858+}