fix(m0): keep the game in front for the whole frames window and record the game_frames results

On the game PC the game went behind another window right after Jarvis
started and stayed there for three windows, drawing at its background rate
(about 58 fps). The kit only checked the window in front at the end of a
window, so the window in which the user clicked back into the game counted
and showed yielding as a 47 % drop in 1 % low.

- FrontWatch checks every second from the start of a window to its end and
  spoils the window if the game was behind anything at any check.
- Before a window the kit asks the user, with a beep, to go back to the
  game and waits up to two minutes.
- What was in front is kept only as a category (a Jarvis process, a console
  window, Explorer, another app), never an app name.

Results: with the game in front, a busy Jarvis cost 0.2 % of the average
fps and 3.3 % of the 1 % low with no frames over 50 ms, so by the
pre-registered rule nothing is added and yielding is not adopted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 09:35:38 +09:00
parent cec52165b2
commit 8f2470af2a
6 changed files with 202 additions and 18 deletions

View File

@@ -9,6 +9,7 @@ from __future__ import annotations
import json
import sys
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
@@ -1795,20 +1796,23 @@ GAME_PID = 7000
def _run_frames_stage(tmp_path, monkeypatch, fake_server, admin=True, front=(GAME_PID, "game.exe"), pr=None,
capture_err=None, stt=None):
capture_err=None, stt=None, in_capture=None):
from m0lib import frames
pr = _FakePriorities() if pr is None else pr
captured = []
monkeypatch.setattr(frames, "is_admin", lambda: admin)
monkeypatch.setattr(frames, "ensure_presentmon", lambda work: Path("pm.exe"))
monkeypatch.setattr(frames, "ask_for_a_game", lambda: True)
monkeypatch.setattr(frames, "foreground_process", lambda: front)
monkeypatch.setattr(frames, "foreground_process", front if callable(front) else (lambda: front))
monkeypatch.setattr(frames, "get_priority", pr.get)
monkeypatch.setattr(frames, "set_priority", pr.set)
monkeypatch.setattr(frames, "SETTLE_S", 0)
monkeypatch.setattr(frames, "BACK_SETTLE_S", 0)
def capture(exe, pid, seconds, out_csv, log):
captured.append((pid, dict(pr.now)))
if in_capture:
in_capture(len(captured))
return frames.parse_csv(_pm_csv([16.0] * 4000, pid=pid)), capture_err
monkeypatch.setattr(frames, "capture", capture)
@@ -1862,6 +1866,72 @@ def test_a_yield_that_lowered_nothing_does_not_count_as_a_yielding_window(tmp_pa
assert r["status"] == "partial" and any("only 3 of 4" in e["error"] for e in r["errors"])
def test_a_window_where_the_game_left_the_front_midway_is_not_counted_even_if_it_was_back_by_the_end(
tmp_path, monkeypatch, fake_server):
# On the game PC the game went behind another window after Jarvis started and the user clicked
# back into it during a window: that window looked valid at its end but was not.
from m0lib import frames
monkeypatch.setattr(frames, "FRONT_EVERY_S", 0.01)
front = {"now": (GAME_PID, "game.exe")}
def in_capture(n):
if n == 3: # the first busy window
front["now"] = (4321, "someapp.exe")
time.sleep(0.2)
front["now"] = (GAME_PID, "game.exe")
r, *_ = _run_frames_stage(tmp_path, monkeypatch, fake_server, front=lambda: front["now"],
in_capture=in_capture)
rows = {x["key"]: x for x in r["frame_rows"]}
assert not rows["busy"]["game"]["valid"]
assert rows["busy"]["game"]["away_from_front"]["to"] == ["another app"]
assert "someapp" not in json.dumps(r, ensure_ascii=False) # what else was open stays private
assert all(x["game"]["valid"] for k, x in rows.items() if k != "busy")
assert r["comparison"]["busy"]["windows"] == 1
def test_when_the_game_is_not_in_front_before_a_window_the_kit_asks_and_waits_for_it(
tmp_path, monkeypatch, fake_server):
# Starting Jarvis puts a console window in front: the kit says so, waits until the user is back
# in the game, and measures then, so the window counts.
from m0lib import frames
monkeypatch.setattr(frames, "FRONT_EVERY_S", 0.01)
stt = _FakeStt()
away = {"polls": 0}
def front():
if "stt_loaded" in stt.events and away["polls"] < 4:
away["polls"] += 1
return (1234, "windowsterminal.exe")
return (GAME_PID, "game.exe")
r, *_ = _run_frames_stage(tmp_path, monkeypatch, fake_server, front=front, stt=stt)
rows = r["frame_rows"]
assert all(x["game"]["valid"] for x in rows)
lost = [x for x in rows if x.get("front_lost_before")]
assert [x["key"] for x in lost] == ["jarvis_idle"]
assert lost[0]["front_lost_before"]["to"] == "a console window" and lost[0]["front_lost_before"]["back"]
def test_a_game_that_never_comes_back_to_the_front_spoils_the_window_but_not_the_stage(
tmp_path, monkeypatch, fake_server):
from m0lib import frames
monkeypatch.setattr(frames, "FRONT_EVERY_S", 0.01)
monkeypatch.setattr(frames, "FRONT_WAIT_S", 0.05)
calls = {"n": 0}
def front():
calls["n"] += 1
return (GAME_PID, "game.exe") if calls["n"] == 1 else (1, "explorer.exe")
r, *_ = _run_frames_stage(tmp_path, monkeypatch, fake_server, front=front)
rows = r["frame_rows"]
assert len(rows) == 7 and not any(x["game"]["valid"] for x in rows)
assert rows[0]["front_lost_before"]["to"] == "Explorer (desktop or taskbar)"
assert rows[0]["front_lost_before"]["back"] is False
assert all(r["comparison"][k]["windows"] == 0 for k in ("off", "busy", "busy_yield"))
def test_a_failed_capture_is_not_counted(tmp_path, monkeypatch, fake_server):
r, *_ = _run_frames_stage(tmp_path, monkeypatch, fake_server, capture_err="PresentMon exited with timeout")
assert not any(x["game"]["valid"] for x in r["frame_rows"])