@@ -86,13 +86,19 @@ def __init__(self, root, home, flavor, display):
8686 self .slug = f'living-code-{ flavor ["id" ]} '
8787 self .socket = home / 'terminal.sock'
8888 self .children = []
89+ self .terminal_process = None
90+ self .process_log = home / 'capture-process.log'
8991 self .env = {key : os .environ [key ] for key in ('PATH' , 'FONTCONFIG_FILE' , 'FONTCONFIG_PATH' , 'TERMINFO_DIRS' , 'LOCALE_ARCHIVE' , 'SSL_CERT_FILE' ) if key in os .environ }
9092 self .env .update (HOME = str (home ), XDG_CONFIG_HOME = str (home / '.config' ), XDG_CACHE_HOME = str (home / '.cache' ),
9193 XDG_DATA_HOME = str (home / '.local/share' ), XDG_STATE_HOME = str (home / '.local/state' ),
9294 XDG_RUNTIME_DIR = str (home / 'runtime' ), TMPDIR = str (home / 'tmp' ), DISPLAY = display ,
9395 LANG = 'C.UTF-8' , LC_ALL = 'C.UTF-8' , TERM = 'xterm-kitty' , COLORTERM = 'truecolor' ,
9496 SHELL = shutil .which ('bash' ) or '/bin/sh' , LIBGL_ALWAYS_SOFTWARE = '1' ,
9597 DBUS_SESSION_BUS_ADDRESS = 'unix:path=' + str (home / 'no-session-bus' ))
98+ if mesa := os .environ .get ('CAPTURE_MESA' ):
99+ # libglvnd must use the locked Mesa vendor, not /run/opengl-driver from a NixOS host.
100+ self .env .update (__EGL_VENDOR_LIBRARY_FILENAMES = f'{ mesa } /share/glvnd/egl_vendor.d/50_mesa.json' ,
101+ LIBGL_DRIVERS_PATH = f'{ mesa } /lib/dri' , GALLIUM_DRIVER = 'llvmpipe' )
96102 for name in ('.config' , '.cache' , '.local/share' , '.local/state' , 'runtime' , 'tmp' ):
97103 (home / name ).mkdir (parents = True , exist_ok = True )
98104 (home / 'runtime' ).chmod (0o700 )
@@ -144,8 +150,9 @@ def run(self, command, **kwargs):
144150 text = True , capture_output = True , timeout = 20 , ** kwargs ).stdout
145151
146152 def spawn (self , command ):
147- process = subprocess .Popen ([str (x ) for x in command ], cwd = self .work , env = self .env ,
148- stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL , start_new_session = True )
153+ with self .process_log .open ('ab' ) as output :
154+ process = subprocess .Popen ([str (x ) for x in command ], cwd = self .work , env = self .env ,
155+ stdout = output , stderr = subprocess .STDOUT , start_new_session = True )
149156 self .children .append (process )
150157 return process
151158
@@ -163,7 +170,8 @@ def terminal(self, command):
163170 args = ['kitty' , '--config' , 'NONE' , '--listen-on' , f'unix:{ self .socket } ' , '--title' , 'Living Code capture' , '--hold' ]
164171 for key , value in options .items ():
165172 args += ['--override' , f'{ key } ={ value } ' ]
166- return self .spawn (args + [str (x ) for x in command ])
173+ self .terminal_process = self .spawn (args + [str (x ) for x in command ])
174+ return self .terminal_process
167175
168176 def remote (self , * args ):
169177 return self .run (['kitty' , '@' , '--to' , f'unix:{ self .socket } ' , * args ])
@@ -174,6 +182,8 @@ def text(self, ansi=False):
174182 def wait_text (self , pattern , timeout = 30 ):
175183 deadline , last = time .monotonic () + timeout , ''
176184 while time .monotonic () < deadline :
185+ if self .terminal_process is not None and self .terminal_process .poll () is not None :
186+ raise RuntimeError (f'Native terminal exited with status { self .terminal_process .returncode } ' )
177187 try :
178188 last = self .text ()
179189 if re .search (pattern , last , re .I ):
@@ -221,6 +231,8 @@ def capture(root, force=False, only=None):
221231 return
222232 except (ValueError , KeyError , FileNotFoundError ):
223233 pass
234+ if not os .environ .get ('CAPTURE_MESA' ):
235+ raise RuntimeError ('Missing locked software renderer. Set CAPTURE_MESA in capture/flake.nix and run pnpm screenshots.' )
224236 for binary in ('Xvfb' , 'xdotool' , 'kitty' , 'magick' ):
225237 if not shutil .which (binary ):
226238 raise RuntimeError (f'Missing { binary } . Run pnpm screenshots through the pinned capture/flake.nix environment.' )
@@ -290,6 +302,10 @@ def capture(root, force=False, only=None):
290302 name = next (image ['path' ] for image in manifest ['images' ] if image ['flavor' ] == id )
291303 evidence [id ] = {'path' : name , 'sha256' : digest (image ), 'application' : version , 'paletteColors' : colors }
292304 print (f'{ manifest ["name" ]} { id } : captured the real application ({ len (colors )} palette colors)' , flush = True )
305+ except Exception :
306+ if context .process_log .exists ():
307+ print ('Native process diagnostics:\n ' + context .process_log .read_text (errors = 'replace' )[- 5000 :], file = sys .stderr )
308+ raise
293309 finally :
294310 try :
295311 if hasattr (app , 'cleanup' ):
0 commit comments