diff --git a/CHANGELOG.md b/CHANGELOG.md index 6def3e00d52..b15bf6853fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- Web: on reattachment to a running engine, the last unfinished log line stays buffered until the engine completes it, so a split context-window line is recorded once and supplies startup telemetry; truncating the log clears that fragment (#3813). Thanks to @AIFlowML. + - Web: settings files whose JSON root is an array or a scalar now fall back to defaults at startup instead of raising, and the original file is left untouched; a valid settings object still loads its known fields (#3808). Thanks to @AIFlowML. - Tests: the Desktop helper timing test drives the exact echo, typing and keypress deadlines with a fake clock instead of real process startup, while still checking that the timed-out helper exits and a fresh one starts (#3802). Thanks to @AIFlowML. diff --git a/web/tensorfold_web/engine.py b/web/tensorfold_web/engine.py index 1c131c2c98a..3cdd0828256 100644 --- a/web/tensorfold_web/engine.py +++ b/web/tensorfold_web/engine.py @@ -319,7 +319,9 @@ def say(line: str, **kw: bool) -> None: if size > 1024**2: # only the tail: the file may be up to LOG_MAX_BYTES f.seek(size - 1024**2) f.readline() # from the next whole line - for line in f.read().split("\n")[:-1][-LOG_LINES:]: + replayed = f.read().split("\n") + buf = replayed.pop() + for line in replayed[-LOG_LINES:]: say(_redrawn(line), earlier=True) say(f"[web] found the engine a previous TensorFold Web started (pid {child.pid}); it kept running") elif f is not None: @@ -344,6 +346,7 @@ def say(line: str, **kw: bool) -> None: # POSIX O_APPEND permits truncation; review inherited offsets before supporting Windows engines. if os.path.getsize(log) > LOG_MAX_BYTES: os.truncate(log, 0) + buf = "" f.seek(0) time.sleep(0.2) if f is not None: diff --git a/web/tests/test_engine_survives.py b/web/tests/test_engine_survives.py index 6541bb8a0d6..f7ee5737669 100644 --- a/web/tests/test_engine_survives.py +++ b/web/tests/test_engine_survives.py @@ -112,3 +112,51 @@ def test_a_kill_after_a_stop_that_took_too_long_is_a_stop_too(tmp_path): popen.kill() eng._pump(child, log, False) assert popen.returncode == -9 and eng.exit_code == 0 and eng.status().status != "failed" + + +@pytest.mark.parametrize(("initial", "appended", "expected", "tokens"), [ + ("", "new\n", ["new"], None), + ("old\n", "new\n", ["old", "new"], None), + ("ready\n[tensorfold] context window ", "32,768 tokens\n", + ["ready", "[tensorfold] context window 32,768 tokens"], 32768), +]) +def test_replay_keeps_unfinished_line(tmp_path, initial, appended, expected, tokens): + eng = Engine(Settings()) + log = tmp_path / "engine.log" + log.write_text(initial, encoding="utf-8") + + class Child: + pid, popen, returncode = 101, None, 0 + + def alive(self): + with log.open("a", encoding="utf-8") as writer: + writer.write(appended) + return False + + eng.proc = Child() + eng._pump(eng.proc, log, replay=True) + assert [line.split(" ", 1)[1] for line in eng.log if "[web]" not in line] == expected + assert eng.startup.get("context_tokens") == tokens + + +def test_log_truncation_discards_replayed_partial_line(tmp_path, monkeypatch): + eng = Engine(Settings()) + log = tmp_path / "engine.log" + log.write_text("unfinished old", encoding="utf-8") + monkeypatch.setattr(engine_mod, "LOG_MAX_BYTES", 1) + + class Child: + pid, popen, returncode = 101, None, 0 + calls = 0 + + def alive(self): + self.calls += 1 + if self.calls == 1: + return True + log.write_text("fresh\n", encoding="utf-8") + return False + + monkeypatch.setattr(engine_mod.time, "sleep", lambda _: None) + eng.proc = Child() + eng._pump(eng.proc, log, replay=True) + assert [line.split(" ", 1)[1] for line in eng.log if "[web]" not in line] == ["fresh"]