Hermes Agent 137113b0a2 junction: pf.conf-style policy router for model endpoints (C99/POSIX)
Rules are ordered and first-match-wins: model glob, path glob, required context,
hook/retry overrides, pass <endpoint>[,<endpoint>] [as <model>] or block.
Endpoints carry shell pre-hooks (exit != 0 = not yet, retried with backoff after an
optional wake command), an offline window after a failed connect, and two timeouts
(idle + total) so a thinking model logs still_working instead of looking hung.

tests/run.sh: 14 checks against C stubs (failing, gated, slow, dead endpoints).
2026-10-29 13:24:31 +00:00

junction

A policy router for OpenAI-compatible model endpoints. C99 + POSIX, no dependencies beyond libc.

junction sits in front of N model servers and decides per request which one serves it — and what to do when none of them wants to. No product name, no vendor strings, no branding anywhere in the code: the only name in the sources is junction itself, so branding can be layered on later without touching logic.

What it does that a static proxy does not

  • Ordered rules, like a firewall. req ... pass ... / block, first match wins. Match on model glob, path glob and required context.
  • Shell pre-hooks per endpoint. A hook that exits non-zero means "not available yet" — the endpoint is retried with backoff for a bounded time, optionally after running a wake command. Hooks get the request in their environment.
  • Unreachable means offline, immediately. A failed TCP connect is not a retry case: the endpoint is marked offline and skipped in milliseconds.
  • Honest timeouts. Two of them per endpoint: idle ("nothing arrived for N seconds") and total. Silence is logged as still_working and the wait continues; it never looks like a hang and it never abandons a model that is legitimately thinking.
  • Context-aware. A request needing more context than an endpoint offers is refused for that endpoint instead of being silently truncated.
  • Failover with cooldown, and a 503 that names why each endpoint was skipped.

Build / run

make                      # -> ./junction
make test                 # stub endpoints, no network, no models (~30 s)
sudo make install         # /usr/local/bin + /usr/local/share/junction

./junction --config junction.conf [--port 8090]
curl -s localhost:8090/status | python3 -m json.tool   # (only for pretty-printing)

Configuration — pf.conf style

Keyword, arguments, braces. # starts a comment, double quotes group words, unknown keywords are a hard error with a line number. Full example: config.example.

listen 0.0.0.0:8090
chars_per_token 4
default_max_tokens 4096
connect_timeout_ms 1500
offline_hold_s 20

endpoint peer1 {
    base http://192.0.2.10:8081/v1
    kind remote
    context 64000
    priority 20
    timeout 600
    idle_timeout 60
    cooldown 120
    hook /usr/local/share/junction/hooks/check-http.sh
}

endpoint workstation {
    base http://192.0.2.20:8080/v1
    context 128000
    priority 10
    timeout 1800
    idle_timeout 30
    hook_retry 180          # wait up to 3 min for the hooks to pass
    hook_backoff 5
    wake /usr/local/share/junction/hooks/wake-wol.sh
    hook /usr/local/share/junction/hooks/check-http.sh
}

endpoint cloud {
    base https://api.example.com/v1
    context 200000
    priority 90
    api_key_env EXAMPLE_API_KEY
}

# rules — first match wins
req need >64000 pass workstation, cloud
req for "*coder*" retry 300 pass workstation as "Qwen3.8-Flash-Next-GSQ-RCO-IQ1_M"
req path "/v1/models" check none pass peer1
req for "blocked-*" block
req for "*" pass peer1, cloud

Rule syntax

req [for <model-glob>] [path <glob>] [need >N | <N | N]
    [check hooks|none] [retry <seconds>]
    ( pass <endpoint>[,<endpoint>...] [as <model>] | block )
  • for — model name glob (fnmatch), matches the model in the request body.
  • need — estimated tokens (chars/4 + max_tokens); >64000 means "needs more than 64k", so small endpoints are not even considered.
  • check hooks|none — override the endpoint's hook list for this rule.
  • retry <s> — override hook_retry for this rule ("wait up to N s until the box is ready").
  • pass a, b — try a, then b on failure.
  • as <model> — rewrite the model field before forwarding (the local box may serve a different name than the client asked for).
  • block — refuse with 403.

No matching rule at all → 503 no_matching_rule. That is deliberate: an unmatched request is a configuration gap, not something to quietly send to a cloud.

Endpoint keys

key meaning
base OpenAI-compatible base URL including /v1 (http:// only in this version)
context usable context window in tokens
kind free-form label (local, remote, cloud)
priority lower wins when a rule lists several
timeout total wall clock for one request (seconds)
idle_timeout silence before still_working is logged (seconds)
cooldown rest after a failed request (seconds)
hook shell command; exit 0 = usable, non-zero = not yet (repeatable)
hook_retry how long to keep retrying hooks
hook_backoff sleep between hook attempts
wake command run once before the hook retries begin
api_key_env env var whose value is sent as Authorization: Bearer ...

Hooks

Plain POSIX shell. Environment:

JUNCTION_BACKEND  JUNCTION_BASE  JUNCTION_PATH  JUNCTION_MODEL
JUNCTION_NEED_TOKENS  JUNCTION_ATTEMPT

Exit non-zero and print the reason on stdout — it lands in /status and in the 503 body. Examples in hooks/: check-http.sh, check-budget.sh (refuse below a threshold read from any JSON URL), wake-wol.sh (magic packet).

Control plane

Method Path Effect
GET /status endpoints (state, counters, last error), rules
POST /admin/pause/<name> stop routing there
POST /admin/resume/<name> clear pause + cooldown + offline window

Everything else is proxied to the endpoint the rules choose, including streaming responses (the upstream's framing is relayed byte for byte).

Known limits (v0.1, deliberately named)

  • One request at a time. The accept loop handles a request to completion before taking the next one. A slow model therefore queues everything behind it. This keeps the counters and the state machine honest; a thread pool is the next step.
  • http:// upstreams only. No TLS client yet — a cloud endpoint needs a local terminator (stunnel, nginx) in front.
  • Chunked request bodies are not supported (Content-Length only). Chunked responses are relayed fine.
  • Token estimate is chars/4, good enough to pick an endpoint, not to bill.

Tests

tests/run.sh

Builds stubs (C) for: a failing endpoint, a gated one (hook refuses until a file appears), a slow one (6 s silence), and a dead port. Checks offline detection, cooldown/failover, hook retry, still_working logging, block rules, catch-all, model rewrite, no_matching_rule, and two config-error cases. Pass = PASS.

S
Description
junction (from n95, ~/src/junction)
Readme
1.4 MiB
0 Stars 3 Watchers 0 Forks
Languages
C 64.5%
Shell 21.2%
Python 7.2%
JavaScript 2.9%
HTML 2.6%
Other 1.6%