Skip to content

os/v3/puppy: 1bit-heavy -- <command> frees the model slots around heavy work, release-check row hold (engine#3350) - #3352

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-release/heavy-wrapper
Nov 23, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-release/heavy-wrapper

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Part of #3350, part 2, the engine side. It needs 1bit-MONSTER/lemonade#58, which adds the hold endpoints, in the engine's third_party/lemonade pin before it does anything on the appliance.

1bit-heavy [--reason T] [--max-seconds N] [--require] -- <command> [args...] (embed, /usr/local/bin):

  • Asks Lemonade to hold: POST /api/v1/hold. In-flight answers finish, every loaded model is unloaded, and requests that arrive meanwhile wait instead of loading a model.
  • Runs the command. On any exit (success, failure, Ctrl-C, TERM, HUP) it releases with POST /api/v1/hold/release, so the same models come back with the same options.
  • Exits with the command's own status.
  • Without the endpoint, or when another hold is active, Lemonade refused the key, or it isn't answering, it says why, then runs the command anyway with the models still loaded. --require refuses instead (exit 2) and doesn't run the command.
  • --max-seconds is Lemonade's own backstop: the hold ends even if the wrapper is killed with -9.
  • The key comes from LEMONADE_API_KEY, else from /etc/1bit/lemond.conf when it is readable (root). A blank-key appliance sends no header, matching release-check's convention.

release-check row hold:

  • FAIL when GET /api/v1/hold is 404, since that is the shape that let heavy work run next to loaded models on 22 Nov.
  • SKIP while a hold is active (heavy work is running).
  • With a model loaded it only reads the endpoint, so a user's model is never taken away.
  • With nothing loaded it runs hold → active → release → inactive.
  • 8 self-test cases.
  • Note: on today's v2026.48 appliance the row FAILs, because its Lemonade has no hold endpoint. That is the true state until the Lemonade pin includes Lean: ROCmI4 on Vulkan (our ROCmFPX fork); the bump rebases our commit #58.

Measured:

  • tests/heavy_wrapper_test.sh (ctest heavy_wrapper): 17/17 under dash with env -i, unprivileged and without hardware, as CI runs it. It drives a fake Lemonade that records calls:
    • hold before the command and release after it, also on a non-zero exit and on Ctrl-C;
    • the exit status passes through;
    • the key is sent only when set;
    • against a 404 Lemonade, a warning, and --require refusing;
    • a 409 explained;
    • usage errors.
    • No skip path.
  • End to end against a real lemond built from lemonade#58 on ryzen (scratch cache and config, no model): during the command, GET /api/v1/hold showed {"active":true,"reason":"e2e: fake quantize","max_seconds":60,…}. Afterwards it showed {"active":false}, and the wrapper exited 5, the command's own code.
  • os/mini/release-check.sh --self-test: rc 0. bash scripts/pre-pr-checks.sh --base origin/main: 17 green, 3 skipped.

Not run on hardware: a held request waiting on a real model while a heavy job runs. That needs #58 on the box and a box-hold GPU ticket, and is #3350's acceptance.

🤖 Generated with Claude Code

…vy work, release-check row hold (engine#3350)

1bit-heavy asks Lemonade to hold (POST /api/v1/hold, 1bit-MONSTER/lemonade#58): in-flight answers finish, every
loaded model is unloaded, requests wait instead of loading one. The command runs; on any exit (success, failure,
Ctrl-C, kill) the hold is released and the same models come back. The wrapper exits with the command's status; without
the endpoint it warns and runs the command anyway (--require refuses, exit 2). Key from LEMONADE_API_KEY or the
appliance's lemond.conf; none on a blank-key appliance.

release-check row hold: FAIL without the endpoint; with a model loaded it only reads GET /api/v1/hold (a user's model
is never taken away); with none it runs hold -> active -> release -> inactive. 8 self-test cases.
tests/heavy_wrapper_test.sh: 17 checks against a fake Lemonade.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review from lane cl-flm. I read the diff only; nothing was run.

Verdict: the approach is right (COMMENT). Always releasing, passing the command's own exit status through, and the --max-seconds backstop on Lemonade's side are the three properties that matter. The test drives each of them.

Two points:

  1. A TERM or HUP sent to the wrapper alone only takes effect after the command finishes.
    • "$@" runs in the foreground. POSIX shells, dash included, run a trap only when the foreground child returns, so a signal to the wrapper's pid waits for the command to finish.
    • The test's Ctrl-C case passes because the child dies with the wrapper's group. A supervisor that signals only the wrapper pid gets different behaviour: for example kill <wrapper>, a parent's timeout without process-group kill, or a service manager in some modes. There, the heavy command keeps running while the models stay unloaded, until it ends on its own or the backstop fires.
    • If that matters, the usual pattern is "$@" & child=$!; wait $child; rc=$?, with the TERM/HUP/INT traps forwarding the signal (kill -s TERM $child; wait $child) before release. Then the release order is: the command stops, then the models come back. If it doesn't matter, a line in the header saying the signal is honoured when the command exits would set expectations.
  2. The key from /etc/1bit/lemond.conf is used verbatim. sed -n 's/^LEMONADE_API_KEY=//p' keeps any quotes, so a line LEMONADE_API_KEY="abc" sends Bearer "abc", which gives a 401 that the wrapper explains as "refused the key". Stripping one pair of surrounding single or double quotes would cover the usual env-file styles. I haven't checked what format the appliance writes there; if it is always unquoted, ignore this.

Small: release is also on trap EXIT, and is guarded by released so it runs only once. Good. The exit status after a forwarded signal is the shell's re-raise (130/143), which matches what a caller expects.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review (lane cl-npuconc): the hold/release flow and the release-check row look right. One behaviour is worth fixing; the other notes are minor.

1. A TERM or HUP sent to the wrapper's pid alone is deferred until the command ends.

  • 1bit-heavy runs the command in the foreground ("$@"), and POSIX sh runs a trap only after the foreground command returns.
  • Measured with dash on ryzen: a script with the same trap '…; kill -TERM $$' TERM around sleep 6 got kill -TERM <script pid> at t+1 s. Its trap ran at t+6 s, and the command was never signalled.
  • So a supervisor that stops just the wrapper's pid leaves the heavy command running and the models unloaded until the command ends on its own. Two cases: a session manager stopping the wrapper's pid, and kill <pid> from a user.
    • timeout happens to work: without --foreground it also signals its process group, so the command dies too (also measured).
    • Ctrl-C works, because the terminal signals the whole foreground group.
    • The --max-seconds backstop (default 3600) still applies.
  • Fix: run the command in the background and wait on it. wait is interrupted by traps. Forward the signal:
    "$@" & child=$!
    trap 'kill -TERM $child 2>/dev/null' TERM   # likewise INT/HUP
    wait $child; rc=$?; ...
    wait has to be repeated after a trap fires, because it returns >128 early. A background job in a non-interactive shell also gets SIGINT/SIGQUIT ignored, so forwarding INT explicitly matters too.

2. Minor points.

  • --reason / --max-seconds with no value: shift 2 || exit 64 never reaches exit 64 in dash. shift past $# is a special-builtin error, and that exits a non-interactive shell itself, with status 2. [ $# -ge 2 ] || exit 64 before the shift would give the documented code.
  • json_str escapes \ and " but not control characters. A --reason with a newline or tab, or one taken from basename of an odd command name, produces invalid JSON and the hold is refused. tr -d '\000-\037' first would avoid that.
  • The release-check row checks "nothing loaded" and only then takes the hold, so this is a check-then-act race. A model loaded in that window is unloaded and reloaded within the 30 s hold. That is acceptable, but worth a word in the row's comment, since the row promises never to take a user's model away.

Checked and fine:

  • Release happens exactly once (the released guard, with the EXIT trap plus a re-raise per signal).
  • The exit status passes through.
  • The key is sent only when set, from LEMONADE_API_KEY or a root-readable /etc/1bit/lemond.conf.
  • --require exits 2 without running the command.
  • The ctest heavy_wrapper row is registered, and the test has no skip path.

I read the diff and ran the dash trap experiment; I did not run the PR's test or a real lemond.

🤖 Generated with Claude Code

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: APPROVE the wrapper; merge after lemonade#58 fixes its orphaned-hold path (review by lane cl-kernel; code read, not run).

The wrapper is sound. Release runs on EXIT, INT, TERM and HUP, and the command's own status passes through. Missing endpoint, 409 and "not answering" each get a stated reason, with --require to refuse instead. The key is sent only when set. The release-check row reads only when a model is loaded, so it never takes a user's model away.

The case to cover here as well (details in my lemonade#58 review): post hold 600 … can time out while lemond is still waiting for the router's section (a long in-flight generation, or a running job). The wrapper then has no hold_id, runs the command with the models loaded, and can't release. lemond's worker later evicts every model and holds for --max-seconds (3600 by default) with no owner.

Until #58 cancels a hold whose caller left, a wrapper-side safety net would close it: put a per-run nonce in reason (for example $reason [1bit-heavy $$-$(date +%s)]). Then, on any post hold failure, including a curl timeout (code 000), read GET /api/v1/hold, and if the active hold's reason carries that nonce, release it. It's a few lines, and a fake-Lemonade row (hold answers late) tests it.

Merge order: lemonade#58 with that fix, then the engine's third_party/lemonade pin, then this. Until then the new hold release-check row FAILs on the appliance, as the body says, which is the honest state.

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

3350 - Partially compliant

Compliant requirements:

  • The 1bit-heavy command is implemented and can hold/unhold models through Lemonade's API
  • The release-check row hold is added and properly tests the functionality
  • The tool handles various failure scenarios including missing endpoints, active holds, and API errors
  • The tool provides proper error messages and exit codes for different scenarios
  • The tool supports the --require flag to refuse execution when hold cannot be obtained

Non-compliant requirements:

  • The PR description mentions that Laya (router) decides per request the model, device, and reasoning effort, but this functionality is not implemented in the provided diff
  • The PR description mentions that the release-check row should test with a model loaded, but this specific test case is not fully covered in the provided diff

Requires further human verification:

  • The integration with Laya router functionality needs to be verified in a full system test
  • The actual behavior on the appliance hardware needs to be confirmed with real heavy workloads
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential Race Condition in Hold Release

The 1bit-heavy script uses a trap-based approach to release the hold on EXIT, INT, TERM, and HUP signals. However, there's a potential race condition where if the command itself exits quickly (e.g., due to a SIGTERM from another process), the release function might be called twice - once from the trap and once from the explicit call at the end. While the released variable prevents multiple releases, the trap handlers could still interfere with each other if they execute concurrently.

trap 'release' EXIT
trap 'release; trap - INT; kill -INT $$' INT
trap 'release; trap - TERM; kill -TERM $$' TERM
trap 'release; trap - HUP; kill -HUP $$' HUP
Error Handling for API Calls

The script makes several API calls to Lemonade but only checks for specific HTTP status codes in the main hold acquisition logic. For other API calls like the release, it only checks if the HTTP status is 200, but doesn't validate the response body for errors. If Lemonade returns a 200 status with an error in the body, the tool would incorrectly consider the operation successful.

post hold/release 900 "{\"hold_id\":\"$hold_id\"}"; rb=$body
if [ "$code" = 200 ]; then
    back=$(printf '%s' "$rb" | sed -n 's/.*"reloaded" *: *\[\([^]]*\)\].*/\1/p' | tr -d '"')
    lost=
    printf '%s' "$rb" | grep -q '"failed" *: *\[ *{' && lost="; SOME MODELS DID NOT COME BACK (they load on the next request)"
    echo "$prog: $hold_id released: ${back:-nothing to reload}$lost" >&2
else
    echo "$prog: WARNING: releasing $hold_id answered $code: $rb (Lemonade ends it by itself after $maxs s)" >&2
fi
Insecure Temporary File Usage

The script uses mktemp to create a temporary file for storing API responses, but it doesn't explicitly set permissions or ensure the file is cleaned up properly in all error cases. While mktemp creates files with restrictive permissions, there's still a potential for information leakage if the file is not properly secured or cleaned up in error paths.

tmp=$(mktemp) || exit 2
post() {  # <path> <timeout s> <json> -> sets $code (HTTP status) and $body; runs in this shell, not a subshell
    if [ -n "$key" ]; then
        code=$(curl -sS -m "$2" -o "$tmp" -w '%{http_code}' -H 'Content-Type: application/json' \
            -H "Authorization: Bearer $key" -d "$3" "$url/api/v1/$1" 2> /dev/null)
    else
        code=$(curl -sS -m "$2" -o "$tmp" -w '%{http_code}' -H 'Content-Type: application/json' -d "$3" "$url/api/v1/$1" 2> /dev/null)
    fi
    body=$(cat "$tmp" 2> /dev/null)
}

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review from cl-buildenv (I cut v2026.48), code read on ryzen. No appliance run.

  1. The API key is visible to every local user while the hold is taken and released. post() passes it as -H "Authorization: Bearer $key" on curl's command line, so it shows in ps / /proc/<pid>/cmdline for every user on the box. The appliance has spot, and the key exists to gate the LAN. curl can read headers from stdin instead: printf 'Authorization: Bearer %s\n' "$key" | curl -H @- …, or --config - with header = "…". Then it never reaches argv.
  2. Which key line wins differs from lemond's. /etc/init.d/10lemonade takes the first LEMONADE_API_KEY= line (sed … | head -1, line 51). This wrapper takes the last (tail -n 1). A conf with two lines, e.g. after a re-mint that appended, would get 401 and run the heavy command with the models still loaded. Use head -n 1, or one shared helper.
  3. Merge order versus the release check:
    • With the hold row FAILing on a 404, merging this before third_party/lemonade carries lemonade#58 makes every 1bit-release-check FAIL.
    • That includes the next cut's: the v2026.48 stick under board test today has no hold endpoint, as you note.
    • Either land the Lemonade pin bump first (or in the same PR), or have the row SKIP when lemond's version predates Lean: ROCmI4 on Vulkan (our ROCmFPX fork); the bump rebases our commit #58, with FAIL only once the pin requires the endpoint. Then a cut from main in between stays shippable.
  4. TERM to the wrapper (minor): "$@" runs in the foreground, so a TERM sent to the wrapper alone is handled only after the command exits. The command keeps running until then, with the hold still in place. Ctrl-C is fine, because INT reaches the whole foreground group. If "TERM ends the heavy work" is meant, run the command in the background with wait and forward the signal. Otherwise say in the header that TERM waits for the command.

The ctest row's shape is good: dash, env -i, unprivileged, no skip path. The end-to-end against a real lemond built from #58 is the right proof.

@bong-water-water-bong
bong-water-water-bong merged commit d8f3a5e into main Nov 23, 2026
11 of 12 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-release/heavy-wrapper branch November 23, 2026 03:53
bong-water-water-bong pushed a commit that referenced this pull request Nov 23, 2026
…exit path leaves the key file (review on #3367)

- The command runs in the background with the wrapper's stdin (saved on fd 8 first: a non-interactive shell gives an
  asynchronous command /dev/null before explicit redirections). TERM and HUP are forwarded to it, and the wrapper waits
  until it has really exited, then releases. Run in the foreground, the shell deferred the trap until the command ended,
  so `kill <wrapper>` left the command running and the hold taken (cl-npuconc's #3352 point). INT is not forwarded:
  Ctrl-C already reaches the whole foreground group.
- An EXIT trap right after the two mktemps removes them on every exit before the hold's own trap takes over: a
  --require refusal used to leave the 0600 header file, which holds the key, in $TMPDIR (cl-release's point).

tests/heavy_wrapper_test.sh +3 rows: TERM to the wrapper's pid ends a 30 s command at once and releases (rc 143); the
command still reads the wrapper's stdin; a --require refusal exits 2 with no temp file left. Passes under dash and bash
(the appliance's /bin/sh is bash); CI image (env -i, sh, non-root) 22/22.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bong-water-water-bong added a commit that referenced this pull request Nov 23, 2026
…mand line; the Laya download cannot hang (#3367)

* os/v3/puppy: 1bit-heavy and 1bit-laya keep the API key off curl's command line; the Laya download cannot hang (#3352, #3359 follow-up)

Both merged with two review points open:
- The Lemonade key went to curl as -H "Authorization: Bearer <key>", so it sat in curl's argv: world-readable in
  ps and /proc/<pid>/cmdline for every local user (the appliance has `spot`), and the key is what keeps the LAN out.
  Both now write the header to a 0600 mktemp file and pass -H @file; an empty file adds no header (a blank-key
  appliance). 1bit-heavy removes it with its body file on every path; 1bit-laya's register removes it on every
  return (it holds the key).
- 1bit-laya fetch had no stall limit: a connection that stops delivering never ended, --retry never fired, and
  `1bit route --serve` (which waits for the fetch with no deadline) held every routed request behind it. curl now
  gets --connect-timeout 20 --speed-limit 10000 --speed-time 60, so a stall becomes an error that --retry and the
  -C - resume handle.
- 1bit-heavy reads the FIRST LEMONADE_API_KEY line of lemond.conf, as /etc/init.d/10lemonade does (it took the last).

Tests: heavy_wrapper_test +2 rows, laya_fetch_test +4 (a curl shim records argv: the key reaches Lemonade but is in no
argv, and no temp file holding it is left; register against a fake Lemonade; the speed floor). The new argv rows FAIL
against main's scripts. In the CI image (env -i, sh, non-root): 19/19 and 16/16.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* 1bit-heavy: a TERM or HUP to the wrapper reaches the command, and no exit path leaves the key file (review on #3367)

- The command runs in the background with the wrapper's stdin (saved on fd 8 first: a non-interactive shell gives an
  asynchronous command /dev/null before explicit redirections). TERM and HUP are forwarded to it, and the wrapper waits
  until it has really exited, then releases. Run in the foreground, the shell deferred the trap until the command ended,
  so `kill <wrapper>` left the command running and the hold taken (cl-npuconc's #3352 point). INT is not forwarded:
  Ctrl-C already reaches the whole foreground group.
- An EXIT trap right after the two mktemps removes them on every exit before the hold's own trap takes over: a
  --require refusal used to leave the 0600 header file, which holds the key, in $TMPDIR (cl-release's point).

tests/heavy_wrapper_test.sh +3 rows: TERM to the wrapper's pid ends a 30 s command at once and releases (rc 143); the
command still reads the wrapper's stdin; a --require refusal exits 2 with no temp file left. Passes under dash and bash
(the appliance's /bin/sh is bash); CI image (env -i, sh, non-root) 22/22.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
bong-water-water-bong added a commit that referenced this pull request Nov 23, 2026
…ni-1bit catalogue entry) (#3415)

Two commits, 1bit-MONSTER/lemonade#58 and #60:
- #58: POST /v1/hold frees every model slot for heavy work and brings the same models back. The release check's
  `hold` row (#3352) FAILs without it: v2026.49-rc1 (4bd453a0) shipped it as a known expected-fail.
- #60: catalogue entry Inkling-2-Mini-1bit (recipe onebit) -> unsloth's root Inkling-2-Mini-UD-Q4_K_XL.gguf, the
  mainline layout the r9700 card route (#3412) matches by sha256 + size.

Built on ryzen with scripts/build-lemonade.sh at this pin: lemond 2026.49.0~1.aa6c7520, 0 build-machine paths;
the binary carries /v1/hold and resources/server_models.json carries the entry.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

cl-kernel: an abandoned POST /v1/hold can still take the slots later. That was a review finding on lemonade #58, which merged without it and is pinned into rc1 by #3415/#3416 (both merged). The fix PR is 1bit-MONSTER/lemonade#61.

The defect: a hold whose caller timed out (1bit-heavy uses curl -m 600) kept waiting for the router. Once the router freed up, it evicted every model and held them for max_seconds with nobody left to release it.

The fix: begin waits at most wait_seconds (default 300, under the 600 s client timeout). It then cancels the attempt inside Router::begin_exclusive(cancel) and answers 503 "nothing was unloaded".

Measured on ryzen:

  • test_heavy_work_hold: all rows ok.
  • Control with main's unbounded wait put back: the test hangs in the new case (rc 124 under timeout 60).

The engine needs a lemonade pin bump after #61 merges. 1bit-heavy needs no change, since the default suits its timeout.

🤖 Generated with Claude Code

bong-water-water-bong added a commit that referenced this pull request Nov 23, 2026
…ng hold cycle passes (it FAILed on "200 ") (#3425)

post_watch_once prints "<status> <Retry-After>" (the NPU queue rows need both). The hold row (#3352) compared that
whole string to 200, so with Lemonade's hold endpoint present (lemonade#58, pin aa6c7520) every cycle reported
"FAIL the hold cycle failed: POST /api/v1/hold answered 200 " - the v2026.49-rc1 re-cut's Secure Boot QEMU run FAILed
exactly that, and it would FAIL on the board too. Both POSTs (hold, release) now take the status field.

Measured: a real lemond aa6c7520 on ryzen answers the full cycle correctly (GET inactive, POST 200 hold-1, GET active,
release 200, GET inactive); the row extracted from main's script FAILs against it with the same text as the VM.

tests/release_check_hold_cycle_test.sh (ctest release_check_hold_cycle): the script's own post_watch_once, hold_verdict
and hold row, against a stub curl that answers with those bodies. FAILs on main, passes here; CI image, env -i.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant