OLLAMA_HOST Resolution Strategy — ALAI Fleet (MC #8476)
OLLAMA_HOST Resolution Strategy — ALAI Fleet
Owner: Skillforge |/ Lexicon
MC: #8476
Verified: 2026-07-28 |from live files and BookStack API
MC:Canonical references read:
~/system/tools/ollama-host.sh~/system/architecture/distributed-ai-factory-plan.md§4 (OLLAMA_HOST Abstraction)~/.zshrcfor shell integration verification
Purpose
ALAI tools and agents need a portable way to reach Ollama from different factory hosts. The wrapper ~/system/tools/ollama-host.sh resolves the best reachable Ollama endpoint once, exports it as OLLAMA_HOST, and lets Node/JS callers consume it via process.env.OLLAMA_HOST instead of hardcoding localhost:11434.
The architecture-plan intent is: ANVIL local → Tailscale ANVIL 100.103.49.98 → FORGE 10.0.0.2 → graceful warning/fallback. The live script adds two operational safeguards: explicit override support and a five-minute cache.
1.Resolution WhyStrategy ThisImplemented Existsby ollama-host.sh
ALAIActual agentlive scriptsorder callin Ollama~/system/tools/ollama-host.sh:
-
inferenceExplicit
(classifiers,environmentembeddings,override
sessionIfsummarization).OLLAMA_HOSTDifferentismachinesalreadyreachset,Ollamathedifferently:script honors it and does not probe. -
Fresh cache short-circuit
If/tmp/ollama-host.cacheexists and is younger than 300 seconds:- cached URL other than
NONE→ export it asOLLAMA_HOST - cached value
NONE→ warn and leaveOLLAMA_HOSTunset
- cached URL other than
-
ANVIL hostname match
(MacIfStudio,the hostname containsmakinja-sin-mac-studio/ hostnameormakinja.local),—theOllamascriptrunsassumeslocallyit is running on ANVIL and sets:OLLAMA_HOST=http://localhost:11434. -
Remote/clientLocalhostsprobe
(e.g.Probeab-mac)http://localhost:11434/api/version—withmustareachone-second timeout. If it responds, set:OLLAMA_HOST=http://localhost:11434 -
Tailscale ANVIL probe
Probe ANVIL over Tailscaleat100.103.49.98:11434, or fall back toFORGE(10.0.0.2:11434, LAN-only) if ANVIL is unreachable. Neither reachable— script must degrade gracefully (warn, do not hang, do not crash agents that havewith aClaudetwo-secondAPI fallback).-
FORGE LAN probe
Probe FORGE(on LANonly)with—acurlone-second timeout:http://10.0.0.2:11434/api/version,version1stimeoutIf
(fast-failsitonresponds,non-LANset:hosts,OLLAMA_HOST=http://10.0.0.2:11434e.g.remote/cloud -
7.Graceful degrade
—If none of the endpoints responds, write"NONE"NONEto the cache, printoneaWARNINGwarning to stderr, and leave$OLLAMA_HOSTunset.AgentsThewithwarninganames the failed endpoints and states that Ollama-dependent agents will fail gracefully while Claude APIfallback continue; Ollama-onlyagentsfailcontinueexplicitlynormally.downstream
ollama-host.sh centralizes this so scripts don't hardcode an endpoint. Instead they read
$OLLAMA_HOST (or, in JS, process.env.OLLAMA_HOST), which the wrapper resolves and
exports once per shell session.
2. Resolution Order (as implemented in ollama-host.sh)
The live script's actual order is cache-first, then hostname, then a network probe
ladder — this is slightly more defensive than the original architecture-plan pseudocode
(§4 of distributed-ai-factory-plan.md), which omits the cache and the local-probe step.
Documenting the real script here:timeout:
1. Explicit override — if $OLLAMA_HOST is already set in the environment, use it
as-is. No probing. (Lets a human or CI job pin an endpoint.)
2. Cache check — if /tmp/ollama-host.cache exists and is < 5 min old:
- cached value present and != "NONE" → export it, done
- cached value == "NONE" → warn, leave unset, done
3. Hostname match — if `hostname` contains "makinja-sin-mac-studio" or
"makinja.local" (i.e. we ARE ANVIL) → localhost:11434
4. Local probe — curl localhost:11434/api/version, 1s timeout
(catches "Ollama running locally for some other reason")
5. Tailscale ANVIL — curl http://100.103.49.98:11434/api/version,version
2s
If 6.it responds, set:
OLLAMA_HOST=http://100.103.49.98:11434
Every successful resolution (steps 3–6) writes its result to the cache file before
exporting, so the next shell in the same 5-minute window skips the probe ladder
entirely (step 2 short-circuits).
3.Cache Cache: /tmp/ollama-host.cacheBehavior
Path:Cache file:/tmp/ollama-host.cache- TTL:
300seconds / five minutes (flat_OLLAMA_CACHE_TTL=300) - Value
file,format:singleoneline:lineeithercontaining a resolved URLlikesuch ashttp://100.103.49.98:11434, or the literalstringNONE). TTL:Why it exists:300avoidssecondsrepeated(5curlminutes),timeoutsenforcedonbyeverycomparingshellorstatmtimesubprocessagainststart- Platform
+%s
datestat -f %m stat -c %Y
Successful onendpoint bothresolution ANVILwrites (macOS)the andURL anyto Linuxthe cache. A failed resolution writes NONE, so the host thatdoes sourcesnot repeat the same file.
NONEollama_flush_cache()
ollama_flush_cache()The wrapper exports this function:
ollama_flush_cache() {
rm -f "$_OLLAMA_CACHE_FILE"
echo "[ollama-host] Cache cleared. Run 'source ~/system/tools/ollama-host.sh' to re-resolve."
}
DeletesUse /tmp/ollama-host.cache and prints a reminder to re-source. Call this manually
after:
Tailscale reconnects (e.g.it aftersleep/wakeTailscale reconnects, after Ollama starts/restarts, after ANVIL/FORGE network state changes, orVPN flap) and ANVIL becomes reachable againOllama is (re)started on ANVIL or FORGEYou changed$OLLAMA_HOSTby hand and want the wrapper to re-probe instead of trustingafter a stalecachedNONEfailure
Itresult is exported as a shell function (export -f ollama_flush_cache) so it's callable from
any subshell in the sourcing session, not just interactively.suspected.
ThereThe isscript also exports ollama_available(), a one-line guard scripts can call before an Ollama-only
code path::
ollama_available() {
[[ -n "${OLLAMA_HOST:-}" ]] && [[ "${OLLAMA_HOST}" != "NONE" ]]
}
Use this before an Ollama-only path when a script needs an explicit availability guard.
4.How ConsumptionRefactored inJS/Node Files Consume the Result
Refactored Node/JS Agentcode Scripts
Scripts that were refactored off hardcoded endpoints readreads the resolvedenvironment value viaexported by the wrapper:
const host = process.env.OLLAMA_HOST || 'http://localhost:11434';
or equivalent variants. The important rule is: do not hardcode a fleet endpoint inside each agent/tool; read process.env.OLLAMA_HOST (theand shell wrapper exports it, so any JS process spawned from a
shell that sourcedlet ollama-host.sh inheritsresolve it).
Verified counts (2026-07-28, grep -rl over ~/system, .js/.sh/.ts/.mjs):routing.
| |
| File-count note |
The task ⚠️ Correction tofor MC #8476
description:thesays task text (written 2026-04-20, the day
ollama-host.sh was implemented) states "“52 refactored files now read process.env.OLLAMA_HOST".” ACurrent live grepverification todayon counts2026-07-28 149found:
| Scope | Verified count | Evidence file |
|---|---|---|
Source-like files ~/system *.js, *.mjs, *.ts, *.sh) matched with rg --no-ignore |
38 | /tmp/verify-8476/process-env-ollama-host-files-rg-no-ignore.txt |
All matched files under ~/system including backups/docs/evidence/context bundles |
50 | /tmp/verify-8476/process-env-ollama-host-all-files-rg-no-ignore.txt |
The historical “52” figure is therefore documented as task context, but was not reproduced by the current filesystem scan. Do not 52.treat This52 runbook reportsas the verifiedcurrent live count ratherunless thanseparate thedated originalimplementation estimate;
the 52 figure appears to be a stale same-day estimate that predates further refactor work,
not a target to reconcile against. No historical record (evidence/reports) of a "52 files"
sign-off was found via discover.js search, so the original number cannot be
reconstructed or verified — only the current state.
Not every hardcoded referenceevidence is necessarilyfound.
Shell someIntegration are(~/.zshrc)
The source line expected by ollama-host.sh itself (the probe URLs), health-probe/monitoring scripts that intentionally target a
specific host, or docs/comments. A hardcode count is a signal for follow-up, not an
automatic defect list — each hit needs eyeballing before "refactor" is filed as a task.
5. Shell Integration
Expected integration (per ollama-host.sh header comment):is:
[ -f ~/system/tools/ollama-host.sh ] && source ~/system/tools/ollama-host.sh
addedCurrent toverification ~/.zshrc.
⚠️this Verified gap (2026-07-28):machine: ~/.zshrc onwas thisread machineand (ANVIL) currently hascontains no
reference to ollama-host.sh, OLLAMA_HOST, or "ollama" at all (grep -in ollama ~/.zshrcreturns
nothing; file last modified 2026-05-18). The wrapper is NOT currently auto-sourced on
interactive shell start. It must be invoked explicitly (source ~/system/tools/ollama-host.sh) or sourced by whatever launches an agent process today.
This is a real gap, not a documentation omission — filed as a follow-up rather than
silently assumed fixed. See §6.
6. Follow-ups Identified While Writingline. This Runbook
runbook zshrc source line missing— addrecords theone-linerequiredsourceline;guardaddingfrom §5it to~/.zshrcsoiseveryainteractivefollow-upshelloperationalresolveschange, not performed by this documentation task.
Operational Checklist
When a host cannot reach Ollama:
- Check the currently resolved value:
echo "$OLLAMA_HOSTOLLAMA_HOST"automatically - Clear
ofstalerelyingcacheonandeachre-resolve:agent-launch path toollama_flush_cache sourceit~/system/tools/ollama-host.shindependently. "52Probefiles"thefigureexpectedstaleendpoints—manually:treatcurlas-sfhistorical/unverifiable;--max-time1491 http://localhost:11434/api/version curl -sf --max-time 2 http://100.103.49.98:11434/api/version curl -sf --max-time 1 http://10.0.0.2:11434/api/version- If ANVIL should be reachable over Tailscale but is
thenot,currentcheckverifiedTailscalecountACL/bindingofwork fromdistributed-ai-factory-plan.md§4.
instead
Evidence Captured for This Runbook
Verification artifacts for MC #8476 were saved under
/tmp/verify-8476/, including:- BookStack shelf/book/page API metadata
- exported BookStack markdown
process.env.OLLAMA_HOSTconsumers.matchRe-baseline if this runbook is used as a compliance reference.listsHardcodeQA/GOTCHAaudit— a full-tree count of remaininglocalhost:11434/127.0.0.1:11434references (excludingollama-host.sh's own probe code) would tell CodeCraft how much refactor work is actually left; not completed here due to~/system's size (23G) making a full recursive grep slow — a scoped follow-up task should target it.Tailscale ACL— per architecture plan §4, ANVIL's Ollama port was not reachable from other Tailscale peers as of the plan's writing. This runbook does not re-verify that ACL state; treat §4's Tailscale ACL recommendation as still open unless confirmed otherwise.artifacts
- Check the currently resolved value: