# =============================================================================
# DRIVE CAP — what moves the drive's own read cap between 14x and 40x?
# =============================================================================
#
#   Run it:   Tools -> Advanced -> Run tests..., then Drive cap on the
#             pre-built tab, with the reference disc in.
#   Costs:    about 45 to 65 minutes. Three rips of one track each, and a
#             whole-disc rip you stop by closing the console (section J);
#             the rest are probes that rip nothing, and three tray cycles.
#
# -----------------------------------------------------------------------------
# WHY THIS EXISTS (round 44: the fork's lap 1 S17, the owner's answer of
# 2026-12-02, "All three kinds (Recommended)"; TASKS R255)
# -----------------------------------------------------------------------------
# The BDR-209D declares a read cap of its own (MODE SENSE page 2Ah, GET
# PERFORMANCE): 14x or 40x, never changing within a run, and the reference
# disc's hard tracks read right far more often at 40x. Placed on the owner's
# Bazzite journal (2026-12-02): it rose to 40x across the two deep sleeps of
# 2026-11-25; it fell to 14x after `drivespeed.txt`'s -S rips of 2026-11-09 and
# again on 2026-12-01 after the damaged disc's 3.5-hour error-heavy read, with no
# sleep or boot in between. Each phase below tests one candidate and is followed
# by a probe that rips nothing (`cyanrip -I`), read by `record-drive-speed`:
#
#   C  nothing running, twice, five minutes apart: does it drift?
#   D  cd-paranoia -A (the cache probe every Full run and drive setup sends)
#   E  track 3, then track 5, ripped alone: does reading hard tracks lower it?
#   F  a rip of track 5 SIGKILLed 30 s in, as drive setup's free_drive did on
#      2026-11-25 (the cyanrip verb's timeout kills its process group)
#   G  the tray opened and closed three times with the disc in: does reloading
#      the disc reset it?
#   H  this boot's kernel messages about the drive (a SATA reset between two
#      probes), then `compare-drive-speed` over every probe and rip record
#   J  BY HAND, LAST: you close this console while a whole-disc rip reads,
#      and the bundle shows how soon after it the rip's log was signed (PP-13)
#
# BY HAND, AFTER THIS RUN (the owner's part of S17): a suspend and resume
# (`systemctl suspend`, wake it after a minute), a reboot, and a cold power-off,
# each followed by one Disc census run (Run tests, pre-built tab). Each census
# probes the cap and compares it with the last probe in the history, which says
# whether the event in between changed it.
#
# C2 POINTERS AND THE TRAY LOCK (round 44: the fork's `.37`, their lap 3 S17 and
# S18; our lap 4 S10). The rips of E and F add `(read-c2)`, each sector read with
# its C2 error pointers into the -j record, a measurement, never a verdict, and
# never sent by an app rip; F's rip adds `(lock-tray)` too (HW-26). Each expands
# only for a build whose contract lists it (`.37`), else to nothing, said beside
# the step; `(read-c2)` only beside `(diagnostics)`, which it needs.
#
# NO SPEED IS SET HERE (`-S` is `drivespeed.txt`'s question). Pioneer's settings
# reply is a read (READ BUFFER; the owner's word of 2026-11-05, TASKS R137).
#
# A MEASUREMENT, NOT A VERDICT. Steps record; what can go red is a tray that
# will not move, or a comparison that read the cap fewer than twice.
# -----------------------------------------------------------------------------

log =============================================================
log DRIVE CAP - what moves the drive's own read cap
log about 40-60 minutes
log =============================================================

set debug_logging on
expect debug_logging on
snapshot atstart

# --- A. IDENTITY -----------------------------------------------------------

log --- A. identity ---
cyanrip --version
expect-exit 0
expect-cyanrip platterpus-fork

# --- B. THE DRIVE AND THE DISC ---------------------------------------------

log --- B. drive offset and disc ---
set-drive-offset
expect-drive-offset
abort-if-failed the read offset for this drive is not known — set the drive up once (Tools, Setup and Updates, Set up drive), then run again
pick-release d14a7546-815b-43c6-8af6-35cff6cee1d0 600
expect-tracks 5+
abort-if-failed no disc with five tracks is loaded — put the reference disc in, then run again
cyanrip-timeout 900
snapshot discready

# --- C. THE CAP AS FOUND, THEN WITH NOTHING RUNNING ------------------------

log --- C. the cap as found, then twice more with nothing running ---
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed start
wait 300
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed idle1
wait 300
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed idle2

# --- D. THE CACHE PROBE -----------------------------------------------------

log --- D. cd-paranoia -A, then the cap ---
cache-probe
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed aftercacheprobe

# --- E. THE HARD TRACKS, ALONE ----------------------------------------------

log --- E. track 3 alone, then track 5 alone, each followed by the cap ---
cyanrip -N -U -W -d (device) -s (offset) -o flac -r 5 -l 3 -D dcthree (consumer) (diagnostics) (read-c2)
record-accuracy capthree dcthree
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed afterthree
cyanrip -N -U -W -d (device) -s (offset) -o flac -r 5 -l 5 -D dcfive (consumer) (diagnostics) (read-c2)
record-accuracy capfive dcfive
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed afterfive

# --- F. A RIP KILLED MID-READ ----------------------------------------------
# The rip holds the tray with `(lock-tray)` (the fork's HW-26). cyanrip releases
# the lock on every exit it runs, but a SIGKILL runs none, so what the drive and
# the kernel do with the lock then is theirs: section G's first open shows it.

log --- F. a rip of track 5 killed 30 s in, then the cap ---
cyanrip-timeout 30
cyanrip -N -U -W -d (device) -s (offset) -o flac -r 5 -l 5 -D dckilled (consumer) (diagnostics) (lock-tray) (read-c2)
cyanrip-timeout 900
wait 30
expect-drive-free 60
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed afterkill

# --- G. THE TRAY, THREE TIMES ----------------------------------------------
# `held-is-measured`: if F's SIGKILL left the tray locked, a tray that will not
# OPEN here is recorded as info, a measurement of HW-26, not a FAIL; each later
# probe still runs and records the cap. A tray that opens and will not CLOSE is
# still a FAIL: the disc is out, and nothing after it measures what this script
# set up. Locked through all three cycles, G cannot test a reload; that is said.

log --- G. the tray opened and closed with the disc in, three times ---
cycle-tray 1 held-is-measured
wait 120
pick-release d14a7546-815b-43c6-8af6-35cff6cee1d0 600
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed aftertray1
cycle-tray 1 held-is-measured
wait 120
pick-release d14a7546-815b-43c6-8af6-35cff6cee1d0 600
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed aftertray2
cycle-tray 1 held-is-measured
wait 120
pick-release d14a7546-815b-43c6-8af6-35cff6cee1d0 600
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed aftertray3

# --- H. THE KERNEL'S LOG, AND THE COMPARISON -------------------------------

log --- H. the kernel's messages about the drive, and where the cap changed ---
record-kernel-log drivecap
compare-drive-speed

# --- I. LEAVE THE RIG AS WE FOUND IT ---------------------------------------

log --- I. restoring ---
set debug_logging off
expect-drive-offset
expect-dialog none
snapshot atend

log =============================================================
log ONE STEP LEFT, AND IT IS YOURS: section J below asks you to
log close this console while a rip reads. Once it has stopped,
log send the ONE .tar.gz named on the "SEND THIS ONE FILE"
log line in the app log - it is under
log ~/.local/share/platterpus/bundles/
log Then, by hand: suspend and wake the PC, run Disc census; reboot,
log run Disc census; power it off fully, run Disc census. Send each.
log =============================================================

# --- J. BY HAND: CLOSE THIS CONSOLE WHILE A RIP READS (the fork's PP-13) ----
# The fork's footer is to be written within 8 s of the script console closing
# mid-rip (our round 44 lap 2 S26): we read the time the console closed from the
# app log ("script console closing with a run in flight") and the footer from the
# rip's own log in the bundle. Closing the console ends the script, so this is
# the LAST section: nothing after it could run.
#
# WHAT CLOSING THE CONSOLE DOES (read from the code, 2026-12-03). The window's
# close button stops the run (`ScriptConsoleDialog.closeEvent` ->
# `ScriptRunner.stop`); Esc does not (`reject` ignores it while a run is in
# flight). Every step not reached is recorded as skipped. A rip the `rip` step
# started is cancelled through the window's own Cancel
# (`ScriptRunner._cancel_own_rip`), so it gets what every Cancel gets: the
# wrapper's SIGTERM, the device-scoped rescue 5 s later, and the wait for the
# rip's log; and the session waits for that rip to stop before it puts the
# settings back and packs the bundle (`_acceptance_rip_at_end`).
#
# WHY AN APP RIP, AND NOT ONE OF SECTION E'S `cyanrip` STEPS (lap 2 S26 said
# "the track-3 rip"). Closing the console during a `cyanrip` step SIGKILLs the
# step's host process group and SIGTERMs the reader in the container as exit
# work (`_free_the_jobs_drive`), but the session does not wait for that reader:
# it packs at once, so the bundle could hold the log before its footer, and the
# check would read our packing, not the fork's footer. The app rip is the path
# the session waits for. A whole-disc rip gives you minutes to close it in.
#
# IF YOU DO NOT CLOSE IT, the rip finishes, the line after it says PP-13 did not
# run, and the run ends as any other: nothing is lost but this one check.

log --- J. BY HAND: close this console while the rip reads (PP-13) ---
select-tracks all
album drivecap console close (run) (ripper)
rip
wait 30
log >>> NOW: CLOSE THIS SCRIPT CONSOLE with its window's close button (the X), not Esc, while the rip is still reading. Any moment in the next few minutes will do.
log >>> The run then ends, the rip is cancelled, and the results are packed once it has stopped. Send the bundle as the DONE lines above say.
wait-for-rip 3600
# Reached only if the console stayed open: the rip then ran to its end, and
# like every rip a script waits for, it must have finished whole.
expect-rip-complete
log PP-13 NOT RUN: the rip finished with the console still open. Nothing else is lost; send the bundle as above.
