Repository navigation
fix(OPT-3447): keep the KVM guest clock synchronized with the host - #27
Merged
Merged
Conversation
The libkrun guest reads the host clock at boot and then free-runs. On the AX41 it fell 30.5 s behind after 56 days (6 ppm, the host's NTP frequency correction), past the 30 s execution-manifest window, so about half of fresh manifests were rejected as not_yet_valid. Run chrony in the guest against the KVM virtual PTP clock (host CLOCK_REALTIME via the hypervisor), wait for the first sync before the sandbox API starts, and stop the API if the clock daemon dies. The launcher now runs api/src/guest-clock.sh as the default guest command; it then runs entrypoint.sh. Direct NsJail and Lambda startup are unchanged; the manifest tolerance is unchanged. Adapted from LibreChat-AI#379.
…chrony with seccomp - launcher passes no argv: libkrun appends it to exec_path, which made guest-clock.sh start a nested copy of itself. The script now refuses arguments; a test runs it as the launcher does. - stop the API when chronyd reports the clock unsynchronized for 60 s (PTP source lost), not only when chronyd exits. - chronyd drops to _chrony with seccomp level 1; it creates its own socket directory inside the root-owned clock tmpfs. - LAUNCHER_EXEC is passable through compose and Helm (workerSandbox.launcher.guestCommand); README documents the non-TSC failure mode and the entrypoint.sh fallback.
Optale-ops
marked this pull request as ready for review
November 18, 2026 16:43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Task: OPT-3447. figent code runs failed with
Error from sandbox: Execution manifest is not valid yet.Cause
The sandbox API, which verifies the manifests, runs inside the libkrun microVM in
sandbox-runner. The guest reads the host clock once at boot and then runs on its own counter with nothing keeping it in sync. On the AX41 the VM had been up since 23 Sep. By 18 Nov it was 30.36-30.51 s behind the host, and the gap grew about 6 ppm, which matches the host's timesyncd frequency correction of +6.101 ppm. The service-worker signsiatwith host time, and the guest rejectsiat > now + 30. With a 30.5 s lag, every manifest signed in the first half of a host second fails. That fits the ~50% rejection rate (09Z 2/5, 10Z 15/24, 11Z 6/10, 12Z 7/13). Restarting sandbox-runner at 12:26:18Z brought the offset down to 0.01 s, but the drift starts again from there. Evidence: optale-artifacts/foundation/OPT-3447/samples.md.Change
api/src/guest-clock.shis now the launcher's default guest command (LAUNCHER_EXEC). The launcher now passes no argv, because libkrun appends argv to the exec path; the script refuses arguments.KVM virtual PTPclock by name and writes a chrony config into a fresh root-owned tmpfs at/run/codeapi-clock(PHC refclock only,port 0,cmdport 0,makestep 0.1 1). It starts chronyd as_chronywith-F 1; chronyd creates its own socket directory there./sandbox_api/entrypoint.sh. After that it watches the clock: if chronyd exits, orchronyc trackingreports anything butNormalsix times in a row (60 s), the API stops and the runner restarts.LAUNCHER_EXEC=/sandbox_api/entrypoint.sh(compose) orworkerSandbox.launcher.guestCommand(Helm), documented in the README.api/Dockerfile,launcher/Dockerfile,docker/Dockerfile.worker-sandbox)./runnor/dev/ptp*.CONFIG_PTP_1588_CLOCK_KVM=y), and its host clocksource istsc.Tests
bash tests/guest_clock_lifecycle.sh(new, added to CI): API never starts before sync; sync failure, early daemon crash or missing KVM PTP clock means no API; exit status is passed through; daemon death stops the API; TERM cleans up within a bounded time; the PTP clock is picked by name; the config has no network source. Passes locally (bash 5.2.21).docker buildx build --checkpasses on all three Dockerfiles. I built thesandbox-buildstage ofapi/Dockerfile: chrony 4.6.1,/sandbox_api/guest-clock.shand/run/codeapi-clock(0700) are in the image,chronyd -paccepts the generated config, and the wrapper refuses to start outside a KVM guest.sandbox-runner-trueimage built from this branch:Guest entrypoint: /sandbox_api/guest-clock.sh. There is onechronyd version 4.6.1 startingand oneGuest clock synchronized with the host through /dev/ptp0per boot (no nested copy), and it comes beforeStarting NsJail sandbox API..., about 6 s after chronyd starts.tool-call socket proxy is not configured, because the standalone harness has no egress gateway; that has nothing to do with the clock. A manifest with iat = now + 45 s got 403Execution manifest is not valid yet(not_yet_valid), which is the production symptom.ERROR: guest clock daemon exited; stopping the sandbox API, guest-clock.sh exited 1 with the API gone, the VM exited and docker restarted it, and it synced again (Guest clock synchronized ...before the API) on every cycle.AX41 verification after rollout
Guest clock synchronized with the host through /dev/ptpNbeforeStarting NsJail sandbox API....timevs docker timestamp method from samples.md: about 0.0x s right after boot, and still about 0.0x s 24 h and 7 days later (without the fix it grows ~0.5 s/day).not_yet_validcount in the sandbox-runner log stays at 0. A real round trip returns 200 with stdout2.