Files
iverson3yaandClaude Opus 5.5 208fb6c811 Add a frame-hold verification skill and TLA+ model shared by Claude Code and Codex
- Add GVFG_SDK/sdk/gvfg/formal: a TLA+ model of one channel's frame-hold protocol (read, release, stop, start and signal-change recovery) and six TLC configs: the shipped code with the reader joined before stop/start and with it running across them, and four mutations that must fail.
- Add .agents/skills/verify-gvfg-frame-hold: check.ps1 runs every config against its expected result, prints counterexamples with -Trace, and reports when the covered code moves past the sync commit; f1-hardware-repro.ps1 stages a stop and start during a read on a real card.
- Add a Claude Code entry in .claude/skills that points to the shared copy, and notes on which steps must run outside the Codex Windows sandbox.

Validation: git diff --cached --check passed. check.ps1 met all six expectations and reported the model in step with 1d89392. f1-hardware-repro.ps1 reproduced the stuck channel against b47f27b and passed against 1d89392 on a GIGABYTE SDI VFG100. TLC needs Java 17+ and tla2tools 1.7.4; check.ps1 -Install fetches the jar into the git-ignored formal/tools. Not yet run from Codex.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 16:46:15 +08:00

12 KiB

name, description
name description
verify-gvfg-frame-hold Model-check the GVFG capture frame-hold protocol (gvfg_read_channel_frame / gvfg_release_channel_frame against stop, start and signal-change recovery) with TLA+/TLC, and reproduce the stop->start race on real hardware. Use it before and after any change to gvfg_capture.cpp's channel read/release/stop/start, or to GigabyteCaptureSession's wait_frame, release_frame, start_stream, stop_stream, handle_signal_event or restart_stream_after_signal_change.

Verify the GVFG frame-hold protocol

What is verified

Property. While an application holds a frame whose data is the backend capture buffer: (1) HeldFrameIsCurrent: the buffer still belongs to the driver stream that is running now, so no recovery restart and no stop/start has replaced it; (2) ReleaseWillSucceed: the application's single mandated gvfg_release_channel_frame() will succeed.

Harm if broken. In zero-copy mode, the application reads a buffer that the driver is refilling, which gives torn or foreign video. In every mode, the release returns GVFG_ESTATE and the channel stays "frame held", so every later read fails with "previous frame has not been released" until the app stops the channel.

Model. The model is GVFG_SDK/sdk/gvfg/formal/CaptureFrameHold.tla. It has three threads: the app's video reader (read, OSD branch, deliver, release), the app's lifecycle thread (stop and start, serialized as the customer docs require), and the session event thread (format change or plug-in, followed by the recovery restart). Each action is one mutex-protected section, or one atomic access, in the code, and its comment names the function. A ghost epoch counts GvfgStartCapture calls, so "same buffer" means "same epoch".

Tool. TLA+ with TLC 1.7.4 (pinned and SHA256-checked). The question is whether events can happen in a bad order across threads, and an exhaustive model checker over the protocol answers that. The C++ cannot be model checked directly, because it sits on Win32 events and the vendor GvfgSdk. A small hardware program (scripts/f1_hardware_repro.cpp) stages the one interleaving that matters most on a real card.

Scope and bounds

  • One channel. The configs use at most 4 stop/start calls, 2 signal events and 4 read attempts. At those bounds MC_Contract covers 45,655 distinct states and MC_ConcurrentReader 237,899. A one-off run at 6/3/6 also passed, with 361,278 and 2,822,739 states.
  • Out of scope: audio frames, unplug events (they bump the generation but never restart), the vendor driver itself (GvfgGetVideoFrame*, GvfgStart/StopCapture are taken as atomic and correct), the OSD processor's internals (only its keep-hold versus early-release branch is modelled), multi-channel handles, open/close/destroy, and timeouts (a timed-out wait is the same as a failed wait here).
  • A Pass covers this model under these bounds, not the whole SDK.

Run it

powershell -NoProfile -ExecutionPolicy Bypass -File .agents\skills\verify-gvfg-frame-hold\scripts\check.ps1

It needs Java 17 or later (installed on the dev PC: Eclipse Temurin 21, from winget install --id EclipseAdoptium.Temurin.21.JDK -e) and tla2tools.jar 1.7.4. The script looks for the jar in $env:TLA2TOOLS_JAR, then GVFG_SDK/sdk/gvfg/formal/tools/ (git-ignored, inside the workspace so a sandboxed agent can read it), then %USERPROFILE%\.tools\tla. Pass -Install once to download it into formal/tools/. All six configs take a few seconds together. TLC runs with one worker, so the first violation it reports is the same on every run. Logs go to GVFG_SDK/sdk/gvfg/formal/out/<config>.log (git-ignored). Use -Only MC_Contract to run a single config.

Config What it is Expected
MC_Contract.cfg Shipped code. The app pauses or joins its reader before every stop/start (srcroot GvfgSource::stop does). Pass
MC_ConcurrentReader.cfg Shipped code. The reader keeps running across stop then start. Pass (F1 fixed)
MC_Mut_NoRestartWait.cfg Mutation: recovery restart no longer waits for frame_held_. Violation: HeldFrameIsCurrent
MC_Mut_NoRecoveryCheck.cfg Mutation: capture ignores recovery_pending_ and the generation. Violation: HeldFrameIsCurrent
MC_Mut_NoRunGeneration.cfg Mutation: delivery checks only running. This is the code before the F1 fix. Violation: HeldFrameIsCurrent
MC_Mut_NoRunningRecheck.cfg Mutation: delivery checks only the run generation. Violation: ReleaseWillSucceed

The script exits 0 only when every config matches its expected result and the covered code is unchanged since the sync commit. Exit 3 (STALE) means the model passed but nobody has confirmed it against the changed code. That is not a pass, so follow "Keeping it in step".

Read a violation with check.ps1 -Trace <config>. It reruns that config and prints the shortest counterexample, one row per step: the action, then the variables that step changed. Map each action to code with the table under "Code-to-model map". In Claude Code, the pstack statechart skill (/pstack:statechart) can also draw the trace, and on a pass it draws how the protocol moves between states. Use its current instructions, and do not copy its commands into this skill.

Running it from Codex

Codex reads this skill from .agents/skills/ (repo scope). Its Windows sandbox ([windows] sandbox = "elevated") runs commands as a separate low-privilege user on a private desktop.

  • check.ps1 and check.ps1 -Trace only need Java (under C:\Program Files) and the jar in formal/tools/, so they should run inside the sandbox. -Install needs network access.
  • f1-hardware-repro.ps1 needs the capture card and the MSVC toolchain. Run it outside the sandbox: approve the escalation when Codex asks, or use full access for that command.
  • The behaviour above follows the Codex docs. It has not been tried in Codex yet, so if a step fails in the sandbox, rerun that one command outside it before you suspect the code.

Hardware check for F1

On a PC with a GVFG card and a live signal on channel 0, with no viewer holding the card:

powershell -NoProfile -ExecutionPolicy Bypass -File .agents\skills\verify-gvfg-frame-hold\scripts\f1-hardware-repro.ps1 -GvfgBuildDir GVFG_SDK\build\<name>

It compiles f1_hardware_repro.cpp against that build's gvfg.lib. It attaches a frame processor through the exported gvfg_control_frame_processor (the hook OSD uses), which parks one read between capture and delivery. It then calls gvfg_stop_channel and gvfg_start_channel, and lets the read finish. Exit 0 means the read was rejected and the next read works. Exit 1 means F1 reproduced. Exit 2 means it could not be verified here, and exit 3 means the race could not be staged.

Finding F1: fixed in 1d89392

Before the fix. The trigger is gvfg_stop_channel() followed by gvfg_start_channel() while gvfg_read_channel_frame() sits between getting the frame and delivering it. The delivery check looked only at running, which start had set back to true. So the read handed out a frame from the stopped stream as valid. Its release then failed with GVFG_ESTATE ("Capture frame release rejected: no frame is held"), because stop/start had reset frame_held_. releaseFrame returns before clearing frameHeld, so every later read failed with "previous frame has not been released". On the dev PC this reproduced on hardware with the code before the fix (3.2.0 at b47f27b, built from git archive) and with the older 3.1.0 build.

The fix, in gvfg_capture.cpp (gvfg_channel_session_t):

  • start() increments runGeneration and sets running in one frameMutex section.
  • readFrame() records runGeneration when it marks the read in progress.
  • Delivery rejects the frame when running is false or the generation moved. It releases the backend hold if the session still has one, and returns GVFG_ESTATE with "capture stopped or restarted during the read".

Verified. MC_ConcurrentReader passes. MC_Mut_NoRunGeneration (the old check) still fails, which shows the check can fail. The hardware program exits 0 against the fixed build (GVFG_SDK/build/verify-f1) and 1 against b47f27b. srcroot's capture journey passes on the fixed SDK.

Still open, related. readAudioFrame has the same running-only check. A stop->start during an audio read can deliver one PCM chunk from before the restart. There is no backend hold for audio, so the channel does not get stuck. Audio is outside this model.

Telling a real bug from a modeling error

Map each step of the trace to the code named in the action's comment. Then ask whether the code really allows that step at that moment: are the mutexes the same, and is the check the same? If it does, the trace is a real interleaving. Report it in the code's words, as F1 does. If it does not, the model is wrong. Fix the action so it matches the code. Never weaken an invariant and never drop a thread to get a pass.

Code-to-model map (keep in step)

Code (gvfg_capture.cpp = C, gigabyte_capture_session.cpp = S) Model action / guard
C readFrame: if (!running) before the lock RBegin
C readFrame, first frameMutex section: held / in-progress checks, record runGeneration RBeginLock
S wait_frame: running_, frame_held_, generation snapshot RWaitCheck
S wait_frame: WaitForMultipleObjects (stop, recovery, frame) RWakeFail, RWakeFrame
S wait_frame capture section under capture_mutex_: recovery_pending_ / generation check RCapture, guard CaptureRejectsDuringRecovery
C readFrame OSD: keep the backend hold, or release_frame early ROsdKeep, ROsdEarlyRelease
C readFrame delivery under frameMutex: running recheck and runGeneration comparison RDeliver, guards DeliverRechecksRunning, DeliverChecksRunGeneration
C readFrame stoppedWhileWaiting release RStopRelease
C releaseFrame (calls S release_frame inside frameMutex) RRelease
C stop: running = false, releaseHeldFrameForStop, then S stop_stream UStop, UStopFrames, UStopBackend, UStopStream, UStopJoin
C start: S start_stream, then ++runGeneration; running = true under frameMutex UStartSession, UStartChannel
S handle_signal_event first block (generation++, recovery_pending_) ESignal
S restart decision (format compare) EDecide (nondeterministic)
S restart_stream_after_signal_change: wait for !frame_held_ || !running_ EWaitHold, guard RestartWaitsForRelease
S restart Stop/StartCapture under capture_mutex_ ERestart (epoch + 1)
S handle_signal_event end (recovery_pending_ = false) EFinish

Coverage record. The covered files are GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp (gvfg_channel_session_t: start, stop, readFrame, releaseFrame, releaseHeldFrameForStop), and GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.cpp with its .h (start_stream, stop_stream, wait_frame, release_frame, handle_signal_event, restart_stream_after_signal_change, wait_for_recovery). Sync commit: 1d89392 (the F1 fix, on master). The model was last confirmed against the code at this commit.

Keeping it in step

  1. check.ps1 prints STALE when a covered file differs from the sync commit, working tree included. Read the diff of the covered functions: git diff <sync commit> -- <file>.
  2. For each changed check, lock, or ordering, update the matching action in the table, or its guard in the Guards set of MC_Contract.cfg and MC_ConcurrentReader.cfg when the change removes a guarded check. Add any new step as a new action, with its function named in a comment.
  3. Run check.ps1. A violation in MC_Contract or MC_ConcurrentReader means the change breaks the property. Read the trace with check.ps1 -Trace <config>, and report it with the steps in code words. Do not loosen the model. When the change touches the read, stop or start path, also run the hardware check.
  4. Move the sync commit (here and in check.ps1) forward only after you have confirmed that the model describes the code at the new commit.