Skip to content

fix(OPT-3447): keep the KVM guest clock synchronized with the host - #27

Merged
Optale-ops merged 2 commits into
mainfrom
fix/opt3447-manifest-time
Nov 18, 2026
Merged

Optale-ops merged 2 commits into
mainfrom
fix/opt3447-manifest-time

Conversation

@Optale-ops

@Optale-ops Optale-ops commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

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 signs iat with host time, and the guest rejects iat > 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.sh is 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.
  • The script finds the guest's KVM virtual PTP clock 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 _chrony with -F 1; chronyd creates its own socket directory there.
  • It waits up to 15 s for the first sync before running /sandbox_api/entrypoint.sh. After that it watches the clock: if chronyd exits, or chronyc tracking reports anything but Normal six times in a row (60 s), the API stops and the runner restarts.
  • With no KVM PTP clock the guest refuses to start. That needs KVM on a TSC clocksource. The fallback is LAUNCHER_EXEC=/sandbox_api/entrypoint.sh (compose) or workerSandbox.launcher.guestCommand (Helm), documented in the README.
  • chrony is added and the script copied in all three guest image constructors (api/Dockerfile, launcher/Dockerfile, docker/Dockerfile.worker-sandbox).
  • The manifest tolerance is unchanged. Direct NsJail and Lambda startup are unchanged. Jails mount neither /run nor /dev/ptp*.
  • Adapted from upstream Keep KVM sandbox clocks synchronized with the host LibreChat-AI/code-interpreter#379 (open), which was proven on a live KVM guest with libkrun 1.19.0: lag slope went from +88 ppm to -0.02 ppm. The AX41 runs the same libkrun 1.19.0, with libkrunfw 5.5.0 (CONFIG_PTP_1588_CLOCK_KVM=y), and its host clocksource is tsc.

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 --check passes on all three Dockerfiles. I built the sandbox-build stage of api/Dockerfile: chrony 4.6.1, /sandbox_api/guest-clock.sh and /run/codeapi-clock (0700) are in the image, chronyd -p accepts the generated config, and the wrapper refuses to start outside a KVM guest.
  • Live KVM boot on KS-7 (kernel 6.8, TSC), head 37c1286, sandbox-runner-true image built from this branch:
    • The launcher reports Guest entrypoint: /sandbox_api/guest-clock.sh. There is one chronyd version 4.6.1 starting and one Guest clock synchronized with the host through /dev/ptp0 per boot (no nested copy), and it comes before Starting NsJail sandbox API..., about 6 s after chronyd starts.
    • Guest offset (host docker timestamp minus guest pino time) was 0.000-0.004 s right after boot and still 0.001 s two minutes later.
    • A manifest signed with host time passed the manifest check and reached sandbox execution. The run itself then failed with 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 403 Execution manifest is not valid yet (not_yet_valid), which is the production symptom.
    • Kill case: a test wrapper killed chronyd 30 s after boot. The log showed 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.
    • Logs: optale-artifacts/agent-upgrade-0.19/build/opt3447-manifest-time/kvm-proof-37c1286-run{1,2}.log.
  • Not proven on KS-7: the 60 s sync-loss monitor against a real PTP outage (the lifecycle test covers it with a stub chronyc), and a full-stack code round trip.

AX41 verification after rollout

  1. sandbox-runner log shows Guest clock synchronized with the host through /dev/ptpN before Starting NsJail sandbox API....
  2. Guest offset, using the pino time vs 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).
  3. not_yet_valid count in the sandbox-runner log stays at 0. A real round trip returns 200 with stdout 2.

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
Optale-ops marked this pull request as ready for review November 18, 2026 16:43
@Optale-ops
Optale-ops merged commit 1372b60 into main Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant