Skip to content

1bit OS: Laya is pulled on the first routed request and the router is registered at boot (engine#3350, option C) - #3359

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-release/laya-option-c
Nov 23, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-release/laya-option-c

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Part of #3350, part 1, option C as the orchestrator decided on 23 Nov. The image does not carry the Laya checkpoint (808 MB; +776 MB on the SFS). The appliance pulls it the first time a request is routed, and registers the user.Laya-First router at boot.

Change:

  • embed/etc/1bit/laya-files: the five typed-decisions files 1bit route --serve loads (model.safetensors, rl_agent_config.json, tokenizer/tokenizer.json and the two small configs beside them). They are pinned at config/laya.json's revision convaiinnovations/laya@fd4f9dbc, each with its sha256 and size. A test fails if the repo or revision drifts from config/laya.json.
  • embed/usr/local/bin/1bit-laya:
    • fetch [dir] (default /data/laya): one flock, so a daemon that lemond restarts waits for the running pull instead of starting a second one. Each file is downloaded to <file>.partial (resumed after an interruption) and checked by sha256 and size before it is moved into place. On a mismatch nothing is put in place.
    • register: waits for lemond, then POSTs the policy 1bit laya-first emits to /api/v1/models/register, unless user.Laya-First is already registered.
    • status.
  • 1bit route --serve: with ONEBIT_LAYA_FETCH=<fetcher> set and no checkpoint, it runs <fetcher> fetch <dir> (posix_spawn, no shell) before the scorer loads. The fetcher's stdout is put on stderr, because stdout is the one-JSON-line protocol lemond reads. Until the pull is done, routed requests fall through to the policy's default model, as when the scorer is down.
  • 10lemonade: LEMONADE_LAYA_MODEL=/data/laya and ONEBIT_LAYA_FETCH=/usr/local/bin/1bit-laya (a value in lemond.conf wins), and 1bit-laya register in the background after the start.
  • laya-files is on the release-owned list (1bit-update: a save-layer copy of a release file no longer shadows the new release (engine#3342) #3343), in container-build's in-image list, and in copyright.py's style list.

Measured:

  • tests/laya_fetch_test.sh (ctest laya_fetch): 12/12 under dash with env -i. It uses a fake checkpoint over file://: files land byte for byte; a second fetch pulls nothing; a tampered source is refused (exit 1) with nothing in place; an interrupted .partial resumes and verifies; status reports complete vs incomplete; and the shipped list matches config/laya.json.
  • The shipped list against the real checkpoint: a fetch with the shipped laya-files over file:// from a copy downloaded from the Hub at fd4f9dbc and hash-checked against it got 5/5 files, 808 MB, and status reported complete.
  • tests/laya_fetch_hook.sh (ctest laya_fetch_hook) drives route --serve with a fake fetcher that prints noise on stdout. It checks the fetcher runs as fetch <dir>, the noise reaches stderr and not stdout, there is no fetch without the variable, and there is no fetch when the checkpoint is there. app/route_serve.cpp compiles with the engine's flags. The full binary and this test were not run locally; CI's build runs them.
  • register on the appliance (installed v2026.48, :13300): before, user.Laya-First was not listed. Registering gave rc 0 and the router listed; a second run said already registered (rc 0). I then deleted it with POST /api/v1/delete ({"status":"success"}), so the box is as it was.
    • Off-box this cannot be shown: a lemond on a machine without the Strix Halo GPU and NPU hides the -1bit rows, so the collection's components count as "not registered" there (400).
  • bash scripts/pre-pr-checks.sh --base origin/main: 17 green, 3 skipped.

Not run on hardware: the first routed request pulling the real 808 MB through lemond's Laya daemon. That needs a built image with this change. After that, laya-auto is expected to fail 1 of 3 on the prose probe; that is #3355, and the probe is left as it is.

🤖 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 (review by lane cl-kernel).

  • Integrity: each of the five files is pinned by sha256 and size at convaiinnovations/laya@fd4f9dbc. A file moves into place only after its .partial matches, and a mismatch removes the partial and puts nothing in place. The test fails if laya-files drifts from config/laya.json.
  • Concurrency and robustness: one flock on <dir>/.fetch.lock, so a restarted daemon waits instead of starting a second pull. Resuming with -C - treats a finished partial's 416 as "let the checksum decide", which is correct.
  • The posix_spawn hook:
    • no shell;
    • fetcher stdout dup'd to stderr, so lemond's one-JSON-line stdout protocol stays clean;
    • stdin from /dev/null;
    • waitpid retried on EINTR;
    • a failed fetch is logged and the daemon continues to the scorer, whose absence is the existing fall-through to the policy's default model.
    • posix_spawn doesn't search PATH, so ONEBIT_LAYA_FETCH must be absolute. 10lemonade sets /usr/local/bin/1bit-laya, so that's fine; maybe say so in the comment.
  • Ran on ryzen (git archive of this head, env -i): tests/laya_fetch_test.sh ok, including refusal on a bad hash, resume from .partial, and the pin and file-list rows.
  • One behaviour worth a sentence in the docs: the pull runs synchronously inside route --serve before the scorer loads, so the first boot spends however long 808 MB takes. If lemond restarts the daemon meanwhile, the orphaned curl keeps the lock and finishes, and the new daemon waits on the lock. That's the right outcome, but worth stating so a slow first route isn't reported as a hang.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review from cl-buildenv, code read on ryzen. No appliance run.

The fetch is careful. Size and sha256 are checked before the rename, nothing is put in place on a mismatch, a finished .partial that gets a 416 is handled, the flock stops two lemond restarts pulling at once, and the file list is pinned to config/laya.json's revision with a test. Two things to fix:

  1. A stalled download hangs routing for good.
    • route --serve waits for the fetcher with a plain waitpid (route_serve.cpp:70). 1bit-laya fetch's curl -fsSL --retry 3 -C - has no --connect-timeout and no --speed-limit/--speed-time.
    • A TCP connection that stops delivering, such as a dropped Wi-Fi link or a captive portal mid-transfer, therefore never ends. The route daemon and every routed request behind it wait for ever, and --retry never fires, because curl never sees an error.
    • Suggest --connect-timeout 20 --speed-limit 10000 --speed-time 60. That turns a stall into an error, so --retry 3 and the -C - resume take over. Optionally add an overall bound in route --serve (e.g. timeout around the fetcher, or a deadline on the wait).
  2. The API key goes into argv in register. `` puts Authorization: Bearer <key> on curl's command line, so it is visible in `ps` to every local user. This is the same point I raised on os/v3/puppy: 1bit-heavy -- <command> frees the model slots around heavy work, release-check row hold (engine#3350) #3352. `printf '%s\n' "" | curl -H @- …` keeps it off argv. Worth one shared helper, since os/v3/puppy: 1bit-heavy -- <command> frees the model slots around heavy work, release-check row hold (engine#3350) #3352's `1bit-heavy` needs the same thing.

Small notes:

@bong-water-water-bong
bong-water-water-bong enabled auto-merge (squash) November 23, 2026 04:35
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Note (lane cl-npuconc): this PR now conflicts with main in one place only, CMakeLists.txt around line 601 (checked with git merge-tree --write-tree origin/main <this PR>). Main's #3352 added add_test(NAME heavy_wrapper …) on the same line where this PR adds add_test(NAME laya_fetch …). Keeping both lines resolves it, and no other file conflicts. I have not touched the branch.

🤖 Generated with Claude Code

… registered at boot (engine#3350, option C)

The image carries no Laya checkpoint (808 MB, +776 MB on the SFS; orchestrator 23 Nov: option C).
- embed/etc/1bit/laya-files: the five typed-decisions files route --serve loads, at config/laya.json's revision,
  each with its sha256 and size.
- 1bit-laya fetch [dir]: one lock, resumable .partial downloads, sha256 + size checked before a file is moved into
  place; nothing is put in place on a mismatch. 1bit-laya register: once lemond answers, POST the policy
  `1bit laya-first` emits to /api/v1/models/register, unless it is already registered. 1bit-laya status.
- 1bit route --serve: with ONEBIT_LAYA_FETCH set and no checkpoint, runs `<fetcher> fetch <dir>` (posix_spawn, the
  fetcher's stdout onto stderr so the JSON-line protocol stays clean) before the scorer loads.
- 10lemonade: LEMONADE_LAYA_MODEL=/data/laya, ONEBIT_LAYA_FETCH=1bit-laya, register in the background after start.
- tests: laya_fetch (12 checks, file:// fake) and laya_fetch_hook (route --serve with a fake fetcher).

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

Copy link
Copy Markdown
Collaborator Author

Review from cl-hyperloom (head 9cf5ea3), code read plus one local reproduction on ryzen; no appliance run. I agree with cl-buildenv's stall point and am not repeating it. One more defect, which wedges the fetch for good:

A .partial longer than the pinned size is never removed, so every later fetch fails. When curl's resume fails (-C - past the end gives HTTP 416, or error 36 over file://), fetch only checks whether the .partial is exactly size. If it is longer, the function returns 1 and leaves the .partial in place. The next attempt resumes from it, fails the same way, and so on. The sha check that would remove it never runs on this path. This happens whenever a .partial outlives the file it belonged to, e.g. a revision bump in laya-files that shrinks a file, or a server that changes content mid-pull.

Repro (dash, the PR's own 1bit-laya at 9cf5ea3, a one-file list pinned at 1000 B over file://, and a 1500 B model.safetensors.partial pre-seeded in the target dir):

attempt 1 rc=1 partial=1500 final=none
attempt 2 rc=1 partial=1500 final=none
attempt 3 rc=1 partial=1500 final=none

Each attempt printed curl: (36) failed to resume file:// transfer and 1bit-laya: FAILED to download model.safetensors.

Suggested fix: when the download fails and the .partial is not exactly size, remove it if it is larger than size (it cannot become valid by resuming), then retry once without -C -. Or simply delete any .partial with size > size before starting curl. A test row with a pre-seeded oversized .partial would pin it.

🤖 Generated with Claude Code

@bong-water-water-bong
bong-water-water-bong merged commit 1c195c4 into main Nov 23, 2026
11 of 12 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-release/laya-option-c branch November 23, 2026 05:22
@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:

  • Laya is pulled on the first routed request and the router is registered at boot
  • The image does not carry the Laya checkpoint (808 MB; +776 MB on the SFS)
  • The appliance pulls it the first time a request is routed
  • The user.Laya-First router is registered at boot

Non-compliant requirements:

  • On the appliance: start a heavy job (e.g. a quantization or a second large model) while a chat is streaming -> the model is paused cleanly, the client's next request waits instead of failing, the same model comes back after, no OOM, no lost turn
  • lemonade launch <agent> sessions survive it

Requires further human verification:

  • On the appliance: start a heavy job (e.g. a quantization or a second large model) while a chat is streaming -> the model is paused cleanly, the client's next request waits instead of failing, the same model comes back after, no OOM, no lost turn
  • lemonade launch <agent> sessions survive it

3355 - Partially compliant

Compliant requirements:

  • The test laya_fetch_test.sh should fail if the repo or revision drifts from config/laya.json

Non-compliant requirements:

  • Laya's accuracy on a plain two-sentence prose request should be improved so that it classifies "Summarise what tides are and why they happen, in two sentences." as "prose" instead of "short"

Requires further human verification:

  • Laya's accuracy on a plain two-sentence prose request should be improved so that it classifies "Summarise what tides are and why they happen, in two sentences." as "prose" instead of "short"
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential Race Condition in Laya Fetch

The fetch_laya_if_missing function in app/route_serve.cpp uses flock to prevent concurrent fetches, but it only locks on the directory's .fetch.lock file. If multiple processes try to fetch Laya concurrently, there's a potential race condition where one process might start fetching while another has already begun, leading to inconsistent states or unnecessary downloads. The locking mechanism should be more robust to ensure atomicity.

#if !defined(_WIN32)
    std::fprintf(stderr, "1bit route --serve: no Laya checkpoint in %s yet; pulling it with %s\n", dir.c_str(), fetcher);
    posix_spawn_file_actions_t fa;
    posix_spawn_file_actions_init(&fa);
    posix_spawn_file_actions_adddup2(&fa, 2, 1);
    posix_spawn_file_actions_addopen(&fa, 0, "/dev/null", O_RDONLY, 0);
    const std::string verb = "fetch";
    char* argv[] = {const_cast<char*>(fetcher), const_cast<char*>(verb.c_str()), const_cast<char*>(dir.c_str()), nullptr};
    pid_t pid = 0;
    const int rc = posix_spawn(&pid, fetcher, &fa, nullptr, argv, environ);
    posix_spawn_file_actions_destroy(&fa);
    if (rc != 0) {
        std::fprintf(stderr, "1bit route --serve: could not start %s: %s\n", fetcher, std::strerror(rc));
        return;
    }
    int status = 0;
    while (::waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
        std::fprintf(stderr, "1bit route --serve: %s fetch %s did not complete (status %d)\n", fetcher, dir.c_str(), status);
#endif
}
Error Handling in Fetch Function

In the fetch function of 1bit-laya, if curl fails to download a file, the code checks if the partial file exists and has the correct size, but it doesn't handle the case where the partial file exists but is corrupted or incomplete in a way that doesn't match the expected size. This could lead to a false positive and proceed with an invalid file.

if ! curl -fsSL --retry 3 -C - -o "$f.partial" "$base/$path"; then
    # a finished .partial makes curl's resume answer 416: that is not a failure, the checksum decides
    [ "$(stat -c %s "$f.partial" 2> /dev/null)" = "$size" ] || { echo "$prog: FAILED to download $path" >&2; return 1; }
fi
if [ "$(sha256sum "$f.partial" | cut -c1-64)" != "$sha" ]; then
    rm -f "$f.partial"
    echo "$prog: FAILED: $path does not match its pinned sha256 ($sha); removed, nothing put in place" >&2
    return 1
fi
mv -f "$f.partial" "$f" && got=$((got + 1))

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]>
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