Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions bin/fm-jev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@
#
# Pauses. Two kinds, both in state/jev/disabled ("<day>\t<why>" or, for a timed
# pause, "<day>\t<why>\t<until-epoch>"). A DAY pause holds until the next local
# calendar day: on OpenRouter an API/transport error, a response without a
# calendar day: on OpenRouter an API error that came back as an HTTP response, a response without a
# usage.cost, or reaching the daily spend cap (USD, default 1; override with a
# decimal number in config/jev-daily-cap, summed from the usage.cost each
# response reports); on either backend an invalid config/jev-endpoint, which is
# a configuration mistake to surface, not a transient condition to retry. A
# TIMED pause covers timeouts, on either backend: the Nth consecutive timeout
# (config/jev-timeout-streak, default 3; any successful classification resets
# TIMED pause covers timeouts and transport failures (any curl failure with no
# HTTP response, such as a DNS or connection error), on either backend: the Nth
# consecutive one (config/jev-timeout-streak, default 3; any successful classification resets
# the count, kept in state/jev/timeout-streak) pauses classification for
# config/jev-timeout-pause minutes (default 60), then Jev tries again by
# itself. A further pause with no success in between doubles (60, 120, 240 ...
Expand All @@ -123,12 +124,12 @@
# skipped by a pause is logged with why=disabled.
#
# The local backend has no per-request cost and no monotonic quota to protect,
# so apart from the timed timeout pause and an invalid endpoint none of its own
# failure reasons ever pauses it: a 1-minute load average at or above
# config/jev-max-load (default 8; checked before each request) or an
# API/transport error each only skip that one row (why=load/api-error), and the
# very next drain tries again. A timeout also skips the rest of that same
# drain's rows (why=timeout) without dialing, so a stuck server costs one
# so apart from the timed pause (timeouts and transport failures) and an invalid
# endpoint none of its own failure reasons ever pauses it: a 1-minute load
# average at or above config/jev-max-load (default 8; checked before each
# request) or an HTTP API error each only skip that one row
# (why=load/api-error), and the very next drain tries again. A timeout also
# skips the rest of that same drain's rows (why=timeout) without dialing, so a stuck server costs one
# timeout per drain rather than one per row (and counts once toward the
# streak), and the classify lock is never held long enough for detached drains
# to pile up behind it. The wake itself is untouched in every case because
Expand Down Expand Up @@ -397,7 +398,7 @@ jev_clock() { # <epoch> -> HH:MM local time (GNU date, then BSD date)
}

# state/jev/disabled is "<day>\t<why>" (a pause until the next local day: the
# spend cap, an API/transport error or a missing cost on OpenRouter, an
# spend cap, an HTTP API error or a missing cost on OpenRouter, an
# invalid config/jev-endpoint) or "<day>\t<why>\t<until-epoch>" (a timed pause
# that clears itself). A "<day>\ttimeout" record without an until time was
# written when one timeout paused the whole day; that reason no longer pauses,
Expand Down Expand Up @@ -704,7 +705,7 @@ jev_classify_row() { # <id> <reason-masked> <status-masked> <backend> <spend-be
if [ "$rc" -ne 0 ]; then
rm -f "$resp"
jev_log_skip "$id" api-error "transport error (curl exit $rc)" "$reason" "$status"
[ "$backend" = local ] || jev_pause "$day" api-error
jev_timeout_note "$day"
return 0
fi
case "$code" in
Expand Down
6 changes: 3 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,12 @@ Only the wake's reason line and the worker's last status line leave the machine,
For the local backend nothing leaves the machine at all (no egress); masking is kept only for prompt-length hygiene, not data governance.

Each request, on either backend, has a 5 second timeout by default; `config/jev-timeout` optionally overrides it with a positive number of seconds (an invalid value keeps the default and `bin/fm-jev.sh status` says so).
A timeout pauses classification only when several happen in a row, on either backend: `config/jev-timeout-streak` (positive whole number, default 3) sets how many consecutive timeouts, and any successful classification resets the count.
A timeout or a transport failure (any request that gets no HTTP response, such as a DNS or connection error) pauses classification only when several happen in a row, on either backend: `config/jev-timeout-streak` (positive whole number, default 3) sets how many consecutive ones, and any successful classification resets the count.
The pause is short and clears itself: `config/jev-timeout-pause` (positive whole number of minutes, default 60) sets its length, after which Jev tries again.
A further pause with no success in between doubles in length (60, 120, 240 minutes and so on), and no pause outlasts the local day.
A single slow request therefore only skips its own wake; invalid values in either file keep the default and `bin/fm-jev.sh status` says so.
The OpenRouter backend also has a daily spend cap (USD, default 1; override with a decimal number in `config/jev-daily-cap`), summed from each response's `usage.cost`; reaching it, an API/transport error, or a response without a cost pauses classification until the next local day, unchanged.
The local backend has no per-request cost and no monotonic quota to protect, so apart from the timeout pause above none of its own failure reasons pauses it: a 1-minute load average at or above `config/jev-max-load` (default 8, checked before each request) or an API/transport error each only skip that one row (logged with `why=load`/`api-error`), and the very next drain tries again.
The OpenRouter backend also has a daily spend cap (USD, default 1; override with a decimal number in `config/jev-daily-cap`), summed from each response's `usage.cost`; reaching it, an API error returned as an HTTP response, or a response without a cost pauses classification until the next local day, unchanged.
The local backend has no per-request cost and no monotonic quota to protect, so apart from the timeout pause above none of its own failure reasons pauses it: a 1-minute load average at or above `config/jev-max-load` (default 8, checked before each request) or an API error each only skip that one row (logged with `why=load`/`api-error`), and the very next drain tries again.
A timeout also skips the rest of that same drain's rows (logged with `why=timeout`) without dialing, so a stuck server costs one timeout per drain rather than one per row, counts once toward the streak, and detached drains never pile up behind it.
A pause is never silent: entering one, for any reason and of either kind, appends one `check` wake naming the reason and when it ends (`check: jev-pause: ...`), once per pause episode, so the supervising session tells the captain.
The first successful classification after the pause ends appends one `check: jev-resume: ...` wake, but only for a pause that was announced.
Expand Down
25 changes: 23 additions & 2 deletions tests/fm-jev.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ case "${FAKE_CURL_MODE:-ok}" in
timeout) exit 28 ;;
hang) sleep 3; exit 28 ;;
refused) exit 7 ;;
dns) exit 6 ;;
http500) printf '{"error":{"message":"boom"}}' > "$out"; printf '500' ;;
nocost) printf '{"answers":{"handling":{"type":"choice","choice":"absorbable","confidence":0.9}},"usage":{}}' > "$out"; printf '200' ;;
ok)
Expand Down Expand Up @@ -450,7 +451,7 @@ test_pause_state_command_prints_only_while_paused() {

test_api_errors_pause_until_the_next_day() {
local mode home
for mode in http500 refused nocost; do
for mode in http500 nocost; do
home=$(jev_case "api-$mode" with-key)
queue_row "$home" 1 heartbeat heartbeat heartbeat
queue_row "$home" 2 stale default:w1:p1 'stale: default:w1:p1'
Expand All @@ -459,7 +460,26 @@ test_api_errors_pause_until_the_next_day() {
[ "$(cut -f2 "$home/state/jev/disabled")" = api-error ] || fail "$mode did not pause Jev"
[ "$(calls "$home")" = 1 ] || fail "$mode: Jev kept calling after an API error"
done
pass "HTTP errors, transport errors and a missing cost pause Jev until the next day"
pass "HTTP errors and a missing cost pause Jev until the next day"
}

test_transport_errors_count_toward_the_timed_pause() {
local home now until
home=$(jev_case transport1 with-key)
timeout_drain "$home" 1 1 dns
jev_events "$home" 'select(.why == "api-error")' | grep -q . || fail "a transport error was not logged"
assert_absent "$home/state/jev/disabled" "one transport error paused Jev"
assert_contains "$(in_home "$home" "$JEV" status)" 'timeout streak: 1 of 3' "the transport error did not count in the streak"
home=$(jev_case transport3 with-key)
now=$(date +%s)
timeout_drain "$home" 1 3 refused
[ "$(calls "$home")" = 3 ] || fail "transport errors did not each dial"
[ "$(cut -f2 "$home/state/jev/disabled")" = timeout ] || fail "three transport errors did not start the timed pause"
until=$(pause_until "$home")
[ -n "$until" ] && [ "$until" -gt "$now" ] || fail "the timed pause carries no end time"
timeout_drain "$home" 4 1 refused
[ "$(calls "$home")" = 3 ] || fail "a paused Jev still dialed"
pass "transport errors count in the timeout streak and start the timed pause, never a day pause"
}

test_daily_cap_pauses_after_the_spend_is_reached() {
Expand Down Expand Up @@ -1128,6 +1148,7 @@ test_every_pause_is_announced_once_through_a_check_wake
test_resume_is_announced_only_for_an_announced_pause
test_pause_state_command_prints_only_while_paused
test_api_errors_pause_until_the_next_day
test_transport_errors_count_toward_the_timed_pause
test_daily_cap_pauses_after_the_spend_is_reached
test_local_backend_takes_priority_needs_no_key_and_records_the_answering_model
test_local_backend_uncertain_maps_to_doubt
Expand Down