Skip to content

Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.

Agent eval: platform smoke for installed builds

Bowen Xueed49071096c0
diff --git a/freevideo_engine/agent/private_file.py b/freevideo_engine/agent/private_file.py
new file mode 100644
index 0000000..4c80ccb
--- /dev/null
+++ b/freevideo_engine/agent/private_file.py
@@ -0,0 +1,101 @@
+"""Create a file that only the current user can open."""
+import logging
+import os
+
+WINDOWS = os.name == 'nt'
+
+
+def create(path):
+ """Exclusively create `path` for writing and return its descriptor.
+
+ Raises FileExistsError when the file is already there. On Windows the file is created with its
+ permissions already in place, so there is no moment at which another account could open it.
+ """
+ if WINDOWS:
+ try:
+ return _create_windows(path)
+ except FileExistsError:
+ raise
+ except Exception as error:
+ # A volume that keeps no permissions (FAT, some shares) still gets its file, as before.
+ logging.getLogger(__name__).warning('Could not restrict %s to this user: %s', path, error)
+ return os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
+
+
+def _create_windows(path):
+ import ctypes
+ import msvcrt
+ from ctypes import wintypes as w
+
+ advapi = ctypes.WinDLL('advapi32', use_last_error=True)
+ kernel = ctypes.WinDLL('kernel32', use_last_error=True)
+
+ class Attributes(ctypes.Structure):
+ _fields_ = [('length', w.DWORD), ('descriptor', ctypes.c_void_p), ('inherit', w.BOOL)]
+
+ advapi.OpenProcessToken.argtypes = [w.HANDLE, w.DWORD, ctypes.POINTER(w.HANDLE)]
+ advapi.OpenProcessToken.restype = w.BOOL
+ advapi.GetTokenInformation.argtypes = [w.HANDLE, ctypes.c_int, ctypes.c_void_p, w.DWORD, ctypes.POINTER(w.DWORD)]
+ advapi.GetTokenInformation.restype = w.BOOL
+ advapi.ConvertSidToStringSidW.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)]
+ advapi.ConvertSidToStringSidW.restype = w.BOOL
+ advapi.ConvertStringSecurityDescriptorToSecurityDescriptorW.argtypes = [
+ w.LPCWSTR, w.DWORD, ctypes.POINTER(ctypes.c_void_p), ctypes.c_void_p]
+ advapi.ConvertStringSecurityDescriptorToSecurityDescriptorW.restype = w.BOOL
+ kernel.GetCurrentProcess.argtypes = []
+ kernel.GetCurrentProcess.restype = w.HANDLE
+ kernel.CreateFileW.argtypes = [w.LPCWSTR, w.DWORD, w.DWORD, ctypes.POINTER(Attributes), w.DWORD, w.DWORD, w.HANDLE]
+ kernel.CreateFileW.restype = w.HANDLE
+ kernel.CloseHandle.argtypes = [w.HANDLE]
+ kernel.CloseHandle.restype = w.BOOL
+ kernel.LocalFree.argtypes = [ctypes.c_void_p]
+ kernel.LocalFree.restype = ctypes.c_void_p
+
+ def failed():
+ return ctypes.WinError(ctypes.get_last_error())
+
+ # The account this process runs as, as a SID string.
+ token = w.HANDLE()
+ if not advapi.OpenProcessToken(kernel.GetCurrentProcess(), 0x0008, ctypes.byref(token)): # TOKEN_QUERY
+ raise failed()
+ try:
+ size = w.DWORD(0)
+ advapi.GetTokenInformation(token, 1, None, 0, ctypes.byref(size)) # TokenUser: ask for the size
+ if not size.value:
+ raise failed()
+ user = ctypes.create_string_buffer(size.value)
+ if not advapi.GetTokenInformation(token, 1, user, size.value, ctypes.byref(size)):
+ raise failed()
+ finally:
+ kernel.CloseHandle(token)
+ text = ctypes.c_void_p()
+ # TOKEN_USER starts with the pointer to the SID.
+ if not advapi.ConvertSidToStringSidW(ctypes.cast(user, ctypes.POINTER(ctypes.c_void_p))[0], ctypes.byref(text)):
+ raise failed()
+ try:
+ sid = ctypes.wstring_at(text.value)
+ finally:
+ kernel.LocalFree(text)
+
+ # Protected (nothing inherited from the folder): this user, the system and administrators only.
+ descriptor = ctypes.c_void_p()
+ if not advapi.ConvertStringSecurityDescriptorToSecurityDescriptorW(
+ 'D:P(A;;FA;;;%s)(A;;FA;;;SY)(A;;FA;;;BA)' % sid, 1, ctypes.byref(descriptor), None):
+ raise failed()
+ try:
+ attributes = Attributes(ctypes.sizeof(Attributes), descriptor.value, False)
+ # GENERIC_WRITE, shared like os.open so a concurrent reader waits for the full line, CREATE_NEW, FILE_ATTRIBUTE_NORMAL
+ handle = kernel.CreateFileW(os.fspath(path), 0x40000000, 0x3, ctypes.byref(attributes), 1, 0x80, None)
+ error = ctypes.get_last_error()
+ finally:
+ kernel.LocalFree(descriptor)
+ if handle is None or handle == ctypes.c_void_p(-1).value:
+ if error in (80, 183): # ERROR_FILE_EXISTS, ERROR_ALREADY_EXISTS
+ raise FileExistsError(os.fspath(path))
+ raise ctypes.WinError(error)
+ try:
+ return msvcrt.open_osfhandle(handle, os.O_WRONLY | os.O_BINARY)
+ except Exception:
+ kernel.CloseHandle(handle)
+ os.unlink(path)
+ raise
diff --git a/freevideo_engine/agent/projects.py b/freevideo_engine/agent/projects.py
index 11d17f1..be4b590 100644
--- a/freevideo_engine/agent/projects.py
+++ b/freevideo_engine/agent/projects.py
@@ -11,6 +11,7 @@ from copy import deepcopy
import hashlib
import json
import logging
+import re
import secrets
import threading
import time
@@ -57,6 +58,17 @@ def _invalid(path, expected, received=None):
return spec.SpecError([schema.diagnostic(path, expected, received)])
+def _refused(project, error):
+ """The editor's own check names the part it refused; an Agent cannot take its next step without it."""
+ # The editor's reasons are fixed phrases ("Invalid clip", "Invalid source"); they repeat nothing supplied.
+ reason = str(error) if type(error) is ValueError and re.fullmatch(r'[A-Z][A-Za-z ]{2,60}', str(error)) else ''
+ row = schema.diagnostic('/project', 'a valid editor project' + (' (%s)' % (reason[:1].lower() + reason[1:]) if reason else ''),
+ project)
+ row['next'] = ('Correct that part of /project. To place clips, create the project with empty tracks, '
+ 'then add each clip with an insert operation.')
+ return spec.SpecError([row])
+
+
def _digest(scope, payload):
return hashlib.sha256(json.dumps(dict(scope=scope, body=payload), sort_keys=True, separators=(',', ':'),
ensure_ascii=True, allow_nan=False).encode()).hexdigest()
@@ -126,7 +138,7 @@ class Projects:
except OSError as error:
raise ProjectError('storage_unavailable') from error
except (ValueError, TypeError, KeyError, RecursionError) as error:
- raise _invalid('/project', 'a valid editor project') from error
+ raise _refused(project, error) from error
def _rejected(self, error, stored, path, received, *, operation=None):
if error['code'] in STATE_CODES:
diff --git a/freevideo_engine/agent/routes.py b/freevideo_engine/agent/routes.py
index de322c8..8dec0f3 100644
--- a/freevideo_engine/agent/routes.py
+++ b/freevideo_engine/agent/routes.py
@@ -22,6 +22,7 @@ import uuid
from . import API_VERSION, enabled
from . import assets
+from . import private_file
from . import spec as specs
from . import schema as contract, errors, access
from .jobs import ACTIVE, DONE, JobError, Jobs, chain_error, library_view
@@ -66,9 +67,8 @@ def ensure_token(root):
except FileNotFoundError:
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
value = secrets.token_urlsafe(32)
- # TODO Windows: set a current-user-only ACL; verify on Windows hardware.
try:
- descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
+ descriptor = private_file.create(path)
except FileExistsError:
pass
else: