Use ChatGPT Codex for launcher questions

This commit is contained in:
rope 2026-09-17 17:49:24 +01:00
parent af95242b60
commit 4c6d610263
2 changed files with 40 additions and 94 deletions

View file

@ -69,45 +69,20 @@
# Instant-answer backend for the quickshell launcher on the desktops:
# `printf '%s' "question" | ssh mediaserver qs-ask`.
#
# Runs through Claude Code so it draws on fred's existing subscription —
# no API key, no separate billing. The trade is latency: Claude Code boots
# a Node process per question, so this takes ~6s against the raw API's
# ~1s. The launcher therefore shows its Wikipedia answer first and lets
# this one supersede it when it lands.
# Runs through Codex CLI using fred's ChatGPT subscription. Sign in on
# the server as fred with `codex login` once; no API key is needed.
# The launcher shows Wikipedia first and replaces it when this answers.
#
# Reads the question from stdin — passing it as an ssh argv element would
# send it through the remote shell for a second round of word splitting.
#
# Every flag here is either isolation or trimming the invocation:
# --safe-mode no CLAUDE.md, skills, hooks, plugins, MCP or
# custom agents. Critical for correctness, not
# just speed: fred's ~/.claude/CLAUDE.md tells
# Claude to answer in caveman mode, which would
# otherwise leak into launcher answers. Auth
# keeps working normally, which is what makes
# the subscription usable here.
# --no-session-persistence writes no transcript, so launcher questions
# never appear in /resume or any project history
# --tools "" no tools at all: nothing to load, and it
# cannot touch the filesystem or network
# --disable-slash-commands no skill resolution
# --strict-mcp-config ignore every MCP config (none supplied)
# --system-prompt replaces the full Claude Code system prompt
# with one line, rather than appending to it
# --permission-mode dontAsk never block waiting on a prompt nobody sees
# --model claude-haiku-4-5 smallest model — a one-line fact needs no more
#
# NOT --bare, which looks like the right flag and is a trap: it skips even
# more, but its auth is "strictly ANTHROPIC_API_KEY or apiKeyHelper (OAuth
# and keychain are never read)" — i.e. it cannot use the subscription.
# Luna at low reasoning effort is enough for a short fact. The CLI runs
# read-only, without web search, shell access, user config, exec rules,
# or session persistence.
#
# STDOUT is the launcher's channel: the answer, or nothing at all if the
# call fails or the model doesn't know — silence is what the launcher
# reads as "keep the Wikipedia answer". Every reason for that silence goes
# to STDERR instead, which the launcher discards and a human running
# `qs-ask` by hand can read — and to the journal, since over ssh nobody
# ever sees that STDERR. `journalctl -t qs-ask` is the only way to tell a
# blank card caused by UNKNOWN from one caused by a failed call.
# call fails or the model doesn't know — silence means "keep Wikipedia".
# Reasons go to STDERR and the journal (`journalctl -t qs-ask`).
(pkgs.writeShellScriptBin "qs-ask" ''
# Every line carries this script's pid, because the launcher fires a
# fresh call per debounce and concurrent runs otherwise interleave with
@ -124,45 +99,25 @@
[ -z "$q" ] && { log "no question on stdin"; exit 0; }
log "ask: $q"
# Claude Code derives its project state (transcripts, auto-memory) from
# the working directory, so run from a dedicated empty one. Combined with
# --no-session-persistence and --safe-mode, a launcher question leaves no
# trace in the real projects' history or memory.
# Keep repository instructions and project files out of this invocation.
work="$HOME/.cache/qs-ask"
${pkgs.coreutils}/bin/mkdir -p "$work" || { log "cannot create $work"; exit 0; }
cd "$work" || { log "cannot enter $work"; exit 0; }
# MAX_THINKING_TOKENS=0 is the single biggest win here, worth ~2.4x.
# Measured: Haiku defaults to extended thinking, and it spent 225 output
# tokens and 1.9s deliberating before emitting an 80-character answer
# about the height of a tower. With thinking off: 43 output tokens,
# time-to-first-text 2657ms -> 1122ms, wall clock 5.7s -> 2.4s, same
# answer. A one-line lookup has nothing to reason about.
#
# --name is not cosmetic: without it Claude Code fires a second,
# concurrent `source=generate_session_title` request to invent a title
# for a session --no-session-persistence then throws away. It costs no
# wall time (both requests dispatch ~1ms apart), but it spent 507 input
# tokens against 153 for the real question. Naming the session up front
# removes it: one request instead of two, 660 input tokens -> 153.
#
# For the record, two things that are NOT the bottleneck, both measured:
# process startup (0.12s boot + 28ms to fire the request, so keeping a
# warm process saves nothing) and prompt size (184 input tokens).
ans=$(printf '%s' "$q" | ${pkgs.coreutils}/bin/env MAX_THINKING_TOKENS=0 \
${pkgs.claude-code}/bin/claude -p \
--safe-mode \
--no-session-persistence \
--tools "" \
--disable-slash-commands \
--strict-mcp-config \
--permission-mode dontAsk \
--output-format text \
--model claude-haiku-4-5 \
--name qs-ask \
--system-prompt 'Answer the question in one or two short sentences, under 240 characters. Lead with the specific fact asked for, including units. The query is often bare keywords rather than a question ("shadow hunter knife classic wow"); treat that as a request for the key facts about that thing, not as unanswerable. No preamble, no caveats, no markdown, no follow-up offers. Reply with exactly UNKNOWN only when you genuinely do not know, or the answer depends on live data you do not have.' \
ans=$(printf 'Answer the question in one or two short sentences, under 240 characters. Lead with the specific fact asked for, including units. The query is often bare keywords rather than a question ("shadow hunter knife classic wow"); treat that as a request for the key facts about that thing. No preamble, caveats, markdown, or follow-up offers. Do not use tools. Reply with exactly UNKNOWN only when you genuinely do not know, or the answer depends on live data you do not have.\n\nQuestion: %s\n' "$q" | \
${pkgs.coreutils}/bin/timeout 30s ${pkgs.codex}/bin/codex exec \
--model gpt-5.6-luna \
--config model_reasoning_effort=low \
--config web_search=disabled \
--disable shell_tool \
--sandbox read-only \
--skip-git-repo-check \
--ephemeral \
--ignore-user-config \
--ignore-rules \
- \
2>/dev/null) \
|| { log "claude exited non-zero (auth expired? run 'claude' once to log in)"; exit 0; }
|| { log "codex exited non-zero (check 'codex login status' as fred)"; exit 0; }
ans=$(printf '%s' "$ans" | ${pkgs.coreutils}/bin/tr -d '\r' | ${pkgs.gnused}/bin/sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')

View file

@ -344,19 +344,16 @@ in
'https://en.wikipedia.org/w/api.php?action=query&format=json&generator=search&gsrlimit=3&prop=extracts&exintro&explaintext&redirects=1'
'';
# Launcher LLM answers, via the mediaserver. The API key lives on the
# server ONLY — the desktops just pipe a question over the same SSH
# path the server monitor already uses, so no laptop ever holds a
# credential and there's one place to rotate it. The remote end is
# `qs-ask` in hosts/FredOS-Mediaserver.nix.
# Launcher LLM answers, via the mediaserver. Codex uses fred's ChatGPT
# login there; desktops only pipe questions over the existing SSH path.
# The remote end is `qs-ask` in hosts/FredOS-Mediaserver.nix.
#
# The query goes over STDIN, not as an argv element: `ssh host cmd arg`
# re-parses arg through the remote shell, so a question containing
# quotes would break. stdin sidesteps remote quoting entirely.
#
# Prints nothing when the key is missing, the server is unreachable, the
# call fails, or the model says it doesn't know — each of those falls
# through to the Wikipedia answer.
# Prints nothing when login is missing, the server is unreachable, the
# call fails, or the model says it doesn't know; Wikipedia then remains.
askFetchScript = pkgs.writeShellScript "ask-fetch" ''
q="$1"
[ -z "$q" ] && exit 0
@ -367,8 +364,7 @@ in
'';
# Launcher weather. Neither of the other two answer paths can serve
# this: qs-ask runs with no tools at all, so Claude has no live data and
# correctly refuses, and Wikipedia answers "weather in kristiansand"
# this: qs-ask has no live data, and Wikipedia answers "weather in kristiansand"
# with the city's founding date. open-meteo needs no key — geocode the
# place name, then read current conditions at those coordinates.
#
@ -2025,8 +2021,8 @@ in
// the slow paths count: Wikipedia lands inside the debounce
// and would just flicker the spinner.
readonly property bool answerPending: askProc.running || wxProc.running
readonly property string pendingSource: wxProc.running ? "open-meteo" : "Claude"
// Claude answered the current query, so the slower-arriving
readonly property string pendingSource: wxProc.running ? "open-meteo" : "ChatGPT"
// ChatGPT answered the current query, so the slower-arriving
// Wikipedia result must not overwrite it.
property bool askWon: false
property var answerCache: ({})
@ -2116,10 +2112,8 @@ in
}
}
// Claude, tried first. The script is the feature gate: with
// no API key it exits immediately and prints nothing, which
// is the same signal as a failed call — so the Wikipedia
// path stays the default with zero configuration.
// ChatGPT, tried alongside Wikipedia. Without a Codex login
// the script prints nothing and Wikipedia remains visible.
Process {
id: askProc
stdout: StdioCollector {
@ -2172,7 +2166,7 @@ in
return;
}
answerQuery = q;
// Weather takes the open-meteo path alone: Claude has no
// Weather takes the open-meteo path alone: ChatGPT has no
// network to look it up with, Wikipedia answers with the
// city's founding date, and the cache has no notion of
// age so it must not serve a temperature twice.
@ -2191,10 +2185,7 @@ in
return;
}
// Both at once. Wikipedia fills the card in well under a
// second; Claude takes ~6s through Claude Code and
// supersedes it on arrival. Racing them beats chaining:
// Claude-then-fallback would leave the card blank for
// six seconds on every question.
// second; ChatGPT supersedes it when Codex finishes.
askWon = false;
killAsk();
askProc.command = [Commands.askFetch, q];
@ -2218,7 +2209,7 @@ in
fact: text.slice(0, 400),
lead: "",
url: "", // nothing to open
source: "Claude"
source: "ChatGPT"
};
cacheAnswer(res);
// Typing may have moved on while the call was out.
@ -2365,7 +2356,7 @@ in
: "https://en.wikipedia.org/wiki/" + encodeURIComponent(title.replace(/ /g, "_")),
source: "Wikipedia"
};
// Claude already answered this query — don't overwrite
// ChatGPT already answered this query — don't overwrite
// the better answer in the cache or on screen.
if (askWon) return;
cacheAnswer(res);
@ -2801,7 +2792,7 @@ in
anchors.rightMargin: 14
spacing: 4
// Article title — Wikipedia only; a Claude
// Article title — Wikipedia only; a ChatGPT
// answer has no page behind it.
Row {
spacing: 8
@ -2842,12 +2833,12 @@ in
// Always say who answered — the two sources
// carry very different trust. The spinner
// runs for as long as Claude's ~6s call is
// runs for as long as the Codex call is
// out, which is the only cue that a
// Wikipedia answer already on screen is
// still provisional — and that a card that
// never changes was Claude declining, not
// Claude being slow.
// never changes was ChatGPT declining or
// the call failing.
Row {
spacing: 5
SIcon {