toys: add oq, a non-interactive one-shot query wrapper

Submits a prompt to b/new, waits for completion, prints result to
stdout, then removes the job. Mirrors --noninteractive mode of
conventional agent CLIs. Supports all b/new spec fields as flags;
-parallel N prints N results separated by === headers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Levi Neely 2026-04-16 19:27:40 +02:00
parent 3e310c16e5
commit aed7c0adf0
1 changed files with 88 additions and 0 deletions

88
toys/oq Executable file
View File

@ -0,0 +1,88 @@
#!/usr/bin/env bash
# oq: non-interactive ollie query via b/ namespace.
#
# Usage: oq [flags] [prompt...]
# echo "prompt" | oq [flags]
#
# Flags:
# -name base name for the job(s); suffixed with -0, -1, ...
# -cwd working directory (default: pwd)
# -agent agent config name
# -backend backend override
# -model model override
# -output output format hint (e.g. json)
# -parallel run the same prompt N times in parallel (default: 1)
# -keep do not remove job(s) after printing result
set -euo pipefail
MOUNT=${OLLIE_9MOUNT:-$HOME/mnt/ollie}
die() { printf 'oq: %s\n' "$*" >&2; exit 1; }
name=; cwd=; agent=; backend=; model=; output=; parallel=1; keep=0
while [ $# -gt 0 ]; do
case $1 in
-name) name=$2; shift ;;
-cwd) cwd=$2; shift ;;
-agent) agent=$2; shift ;;
-backend) backend=$2; shift ;;
-model) model=$2; shift ;;
-output) output=$2; shift ;;
-parallel) parallel=$2; shift ;;
-keep) keep=1 ;;
--) shift; break ;;
-*) die "unknown flag: $1" ;;
*) break ;;
esac
shift
done
# Prompt from remaining args or stdin.
if [ $# -gt 0 ]; then
prompt="$*"
elif ! [ -t 0 ]; then
prompt=$(cat)
else
die "prompt required: pass as arguments or pipe via stdin"
fi
[ -z "$prompt" ] && die "empty prompt"
cwd=${cwd:-$(pwd)}
[ -z "$name" ] && name=$(date +%s%N)-$$
{
printf 'name=%s\n' "$name"
printf 'cwd=%s\n' "$cwd"
[ -n "$agent" ] && printf 'agent=%s\n' "$agent"
[ -n "$backend" ] && printf 'backend=%s\n' "$backend"
[ -n "$model" ] && printf 'model=%s\n' "$model"
[ -n "$output" ] && printf 'output=%s\n' "$output"
printf 'parallel=%s\n' "$parallel"
printf -- '---\n'
printf '%s\n' "$prompt"
} > "$MOUNT/b/new" || die "failed to submit job"
rc=0
for i in $(seq 0 $((parallel - 1))); do
id="${name}-${i}"
B="$MOUNT/b/$id"
while true; do
status=$(tr -d '[:space:]' < "$B/status" 2>/dev/null) || { sleep 0.05; continue; }
case "$status" in
done*) break ;;
failed*) rc=1; break ;;
*) sleep 0.05 ;;
esac
done
[ "$parallel" -gt 1 ] && printf '=== %s ===\n' "$id"
cat "$B/result" 2>/dev/null
[ "$keep" -eq 0 ] && rm -r "$B" 2>/dev/null || true
done
exit $rc