Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(install): decode Windows Task Scheduler XML output before parsing (#3969) (#4064)
Luis Arayab78e07da7fad
diff --git a/headroom/install/supervisors.py b/headroom/install/supervisors.pyindex 3baae0171..e2691aa02 100644--- a/headroom/install/supervisors.py+++ b/headroom/install/supervisors.py@@ -405,12 +405,39 @@ def _parse_windows_task_xml(raw: bytes | str) -> ElementTree.Element:if isinstance(raw, bytes):try:return ElementTree.fromstring(raw)- except ElementTree.ParseError:- raw = raw.decode(errors="replace")+ except (ElementTree.ParseError, ValueError, LookupError):+ pass++ raw_bytes = raw+ raw_str = None+ if b"\x00" in raw_bytes:+ try:+ raw_str = raw_bytes.decode("utf-16")+ except UnicodeDecodeError:+ raw_str = raw_bytes.decode(errors="replace")+ else:+ declared_enc = None+ match = re.search(rb'<\?xml\s+[^>]*encoding=["\']([^"\']+)["\']', raw_bytes)+ if match:+ enc_name = match.group(1).decode("ascii", errors="ignore").lower()+ if "utf-16" not in enc_name and "utf-32" not in enc_name:+ declared_enc = enc_name+ if declared_enc:+ try:+ raw_str = raw_bytes.decode(declared_enc)+ except (LookupError, UnicodeDecodeError):+ pass+ if raw_str is None:+ try:+ raw_str = raw_bytes.decode("utf-8-sig")+ except UnicodeDecodeError:+ raw_str = raw_bytes.decode(errors="replace")+ else:+ raw_str = rawnormalized = re.sub(- r'<\?xml\s+version="1\.0"\s+encoding="[^"]+"\?>',- '<?xml version="1.0"?>',- raw,+ r'<\?xml\s+version="1\.0"\s+encoding="[^"]+"\?>\s*',+ "",+ raw_str,count=1,)return ElementTree.fromstring(normalized)@@ -444,7 +471,7 @@ def _register_windows_task(name: str, xml: str, *, expected_logon_type: str) ->try:task = _parse_windows_task_xml(query.stdout or b"")- except ElementTree.ParseError as exc:+ except (ElementTree.ParseError, ValueError, LookupError) as exc:raise _WindowsTaskRegistrationError(f"Could not parse registered Windows task {name!r}", task_created=True) from excdiff --git a/tests/test_install/test_supervisors.py b/tests/test_install/test_supervisors.pyindex deaa90178..2c6d6759a 100644--- a/tests/test_install/test_supervisors.py+++ b/tests/test_install/test_supervisors.py@@ -147,6 +147,112 @@ def test_parse_windows_task_xml_accepts_valid_bytes() -> None:assert task.find("LogonType").text == "S4U"+def test_parse_windows_task_xml_accepts_utf16_encoded_bytes() -> None:+ xml = (+ '<?xml version="1.0" encoding="UTF-16"?>\n'+ '<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'+ "<Principals><Principal><LogonType>S4U</LogonType></Principal></Principals>"+ "</Task>"+ )+ task = _parse_windows_task_xml(xml.encode("utf-16"))+ assert task is not None+ assert task.find(".//{*}LogonType").text == "S4U"+++def test_parse_windows_task_xml_accepts_utf16be_without_bom() -> None:+ xml = (+ '<?xml version="1.0" encoding="UTF-16"?>\n'+ '<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'+ "<Principals><Principal><LogonType>S4U</LogonType></Principal></Principals>"+ "</Task>"+ )+ task = _parse_windows_task_xml(xml.encode("utf-16-be"))+ assert task is not None+ assert task.find(".//{*}LogonType").text == "S4U"+++def test_parse_windows_task_xml_preserves_declared_windows_codepage() -> None:+ xml = (+ '<?xml version="1.0" encoding="windows-1252"?>\n'+ '<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'+ "<Name>Caf\xe9</Name>"+ "<Principals><Principal><LogonType>S4U</LogonType></Principal></Principals>"+ "</Task>"+ )+ task = _parse_windows_task_xml(xml.encode("windows-1252"))+ assert task is not None+ assert task.find(".//{*}Name").text == "Caf\xe9"+ assert task.find(".//{*}LogonType").text == "S4U"+++def test_parse_windows_task_xml_accepts_declared_multibyte_encoding() -> None:+ xml = (+ '<?xml version="1.0" encoding="shift_jis"?>\n'+ '<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'+ "<Name>\u30c6\u30b9\u30c8</Name>"+ "<Principals><Principal><LogonType>S4U</LogonType></Principal></Principals>"+ "</Task>"+ )+ task = _parse_windows_task_xml(xml.encode("shift_jis"))+ assert task is not None+ assert task.find(".//{*}Name").text == "\u30c6\u30b9\u30c8"+ assert task.find(".//{*}LogonType").text == "S4U"+++def test_parse_windows_task_xml_accepts_ansi_with_invalid_utf8() -> None:+ xml = (+ b'<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'+ b"<Name>Caf\xe9</Name>"+ b"<Principals><Principal><LogonType>S4U</LogonType></Principal></Principals>"+ b"</Task>"+ )+ task = _parse_windows_task_xml(xml)+ assert task is not None+ assert task.find(".//{*}LogonType").text == "S4U"+++def test_register_windows_task_cleans_up_on_unparseable_xml(monkeypatch) -> None:+ def fake_run(command: list[str], **kwargs):+ if command[1] == "/Query":+ return _LaunchctlResult(stdout=b"<<<not-xml>>>")+ return _LaunchctlResult()++ monkeypatch.setattr("headroom.install.supervisors.subprocess.run", fake_run)++ with pytest.raises(_WindowsTaskRegistrationError) as exc_info:+ _register_windows_task("headroom-test-task", "<Task />", expected_logon_type="S4U")++ assert exc_info.value.task_created is True+++def test_register_windows_task_cleans_up_on_unsupported_multibyte_error(monkeypatch) -> None:+ def fake_run(command: list[str], **kwargs):+ if command[1] == "/Query":+ return _LaunchctlResult(+ stdout=b'<?xml version="1.0" encoding="unsupported-codec"?><Task>'+ )+ return _LaunchctlResult()++ monkeypatch.setattr("headroom.install.supervisors.subprocess.run", fake_run)++ with pytest.raises(_WindowsTaskRegistrationError) as exc_info:+ _register_windows_task("headroom-test-task", "<Task />", expected_logon_type="S4U")++ assert exc_info.value.task_created is True+++def test_parse_windows_task_xml_accepts_utf8_bom_bytes() -> None:+ xml = (+ '<?xml version="1.0" encoding="UTF-16"?>\n'+ '<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'+ "<Principals><Principal><LogonType>S4U</LogonType></Principal></Principals>"+ "</Task>"+ )+ task = _parse_windows_task_xml(xml.encode("utf-8-sig"))+ assert task is not None+ assert task.find(".//{*}LogonType").text == "S4U"++def test_parse_windows_task_xml_accepts_text() -> None:task = _parse_windows_task_xml("<Task><LogonType>InteractiveToken</LogonType></Task>")