ollie/tools/route

118 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# ollie:parallel read
# ollie:prompt
# ## route
#
# Route a subtask to an appropriate model tier. Spawns a subagent that reports back.
#
# **Args**: `[instruction]` or `[instruction, "--tier=worker|expert"]` or `[instruction, "--backend=<name>", id]`
#
# ```
# call_tool: calls=[{tool: "route", args: ["implement the login page"]}]
# call_tool: calls=[{tool: "route", args: ["refactor auth module", "--tier=expert"]}]
# ```
# ollie:end
set -euo pipefail
SERVICE="org.ollie.SessionManager"
OBJ="/org/ollie/SessionManager"
IFACE="org.ollie.SessionManager"
die() { printf 'route: %s\n' "$*" >&2; exit 1; }
dbus_call() {
dbus-send --session --dest=$SERVICE --type=method_call --print-reply \
$OBJ "$IFACE.$1" "${@:2}" 2>/dev/null
}
dbus_string() {
dbus_call "$@" | awk -F'"' '/string "/{print $2; exit}'
}
[ -z "${OLLIE_SESSION_ID:-}" ] && die "OLLIE_SESSION_ID not set (route must run inside a session)"
# Parse args
tier="worker"
backend_override=""
instruction=""
id=""
for arg in "$@"; do
case "$arg" in
--tier=*) tier="${arg#--tier=}" ;;
--backend=*) backend_override="${arg#--backend=}" ;;
*) [ -z "$instruction" ] && instruction="$arg" || id="$arg" ;;
esac
done
[ -z "$instruction" ] && die "instruction required"
case "$tier" in
worker|expert) ;;
*) die "unknown tier: $tier (use worker or expert)" ;;
esac
# Resolve backend: explicit flag > parent session's config > OLLIE_BACKEND
if [ -n "$backend_override" ]; then
BACKEND="$backend_override"
else
parent_config=$(dbus_string GetConfig string:"$OLLIE_SESSION_ID")
BACKEND=$(printf '%s\n' "$parent_config" | grep -m1 '^backend=' | cut -d= -f2-)
BACKEND="${BACKEND:-${OLLIE_BACKEND:-openrouter}}"
fi
# Resolve model: OLLIE_TIER_{TIER}_{BACKEND} > legacy flat vars > hardcoded defaults
tier_upper=$(echo "$tier" | tr '[:lower:]' '[:upper:]')
backend_lower=$(echo "$BACKEND" | tr '[:upper:]' '[:lower:]')
tier_var="OLLIE_TIER_${tier_upper}_${backend_lower}"
MODEL="${!tier_var:-}"
if [ -z "$MODEL" ]; then
case "$tier" in
worker) MODEL="${OLLIE_CASCADE_MODEL:-mistralai/devstral-small}" ;;
expert) MODEL="${OLLIE_ESCALATE_MODEL:-anthropic/claude-sonnet-4-20250514}" ;;
esac
fi
# Agent config
case "$tier" in
worker) agent="worker" ;;
expert) agent="expert" ;;
esac
id="${id:-route-${tier}-$(date +%s%N)-$$}"
[ -n "$OLLIE_SESSION_ID" ] && id="${OLLIE_SESSION_ID}__${id}"
# Create session via D-Bus
session_id=$(dbus_string CreateSession \
string:"$(pwd)" string:"$BACKEND" string:"$MODEL" string:"$agent" string:"" string:"")
[ -z "$session_id" ] && die "failed to create session"
# Rename to our chosen ID
dbus_call RenameSession string:"$session_id" string:"$id" > /dev/null 2>&1
prompt="[parent_session_id=${OLLIE_SESSION_ID}]
[session_id=${id}]
Complete the following task. Read code, reason about implementation, and stay within scope.
When finished, report your result back to the parent session using PeerSubmit.
Format:
[from=${id}]
STATUS: done
SUMMARY: <what was accomplished>
On failure:
[from=${id}]
STATUS: error
REASON: <why>
After reporting the result, STOP. One report, then stop.
TASK:
${instruction}"
dbus_call Submit string:"$session_id" string:"$prompt" > /dev/null 2>&1 || die "failed to submit prompt"
printf 'session=%s\nbackend=%s\nmodel=%s\ntier=%s\n' "$id" "$BACKEND" "$MODEL" "$tier"