mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(wrap): keep a flush Serena comment and ignore nested keys
A `#` glued to the colon never matched, so the rewrite left that line in place and appended a second key. Match only a column-0 key, keep the first `#` as the comment, and drop non-comment trailing tokens such as a YAML type tag. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+43
-9
@@ -2247,6 +2247,28 @@ def _setup_headroom_mcp(
|
||||
click.echo(line)
|
||||
|
||||
|
||||
def _serena_inline_comment(rest: str) -> str:
|
||||
"""Return the inline comment to keep when rewriting a Serena setting.
|
||||
|
||||
``rest`` is everything after the colon. The first ``#`` starts the comment,
|
||||
including when it sits flush against the colon or the old value: this
|
||||
setting is always rewritten to ``false``, so a ``#`` that strict YAML would
|
||||
treat as part of the scalar is kept as a comment instead. Whitespace that
|
||||
already separates the ``#`` is preserved; a glued ``#`` gets one inserted
|
||||
space. Every other token, such as a YAML type tag, is discarded with the
|
||||
old value.
|
||||
"""
|
||||
hash_at = rest.find("#")
|
||||
if hash_at < 0:
|
||||
return ""
|
||||
prefix, body = rest[:hash_at], rest[hash_at:]
|
||||
if prefix.strip(" \t") == "":
|
||||
return prefix + body if prefix else " " + body
|
||||
if prefix[-1] in " \t":
|
||||
return prefix[len(prefix.rstrip(" \t")) :] + body
|
||||
return " " + body
|
||||
|
||||
|
||||
def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
"""Disable Serena's browser dashboard auto-open in ``~/.serena/serena_config.yml``.
|
||||
|
||||
@@ -2287,6 +2309,12 @@ def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
"key absent" appended a second copy of the key, and Serena loads this file
|
||||
with ruamel in duplicate-key-strict mode, so every later startup died with
|
||||
``DuplicateKeyError`` before the MCP handshake ("Connection closed").
|
||||
|
||||
The edit only ever touches a *top-level* (column-0) occurrence of the key.
|
||||
An indented one belongs to a different mapping (e.g. a nested
|
||||
``extension:`` block that happens to reuse the name) and is left alone —
|
||||
both when rewriting the value and when dropping duplicates, so it can
|
||||
never be mistaken for, or merged with, Serena's real setting.
|
||||
"""
|
||||
import re
|
||||
|
||||
@@ -2311,24 +2339,30 @@ def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
# append a second copy of it in that case. Rewrite the value in place
|
||||
# (keeping any inline comment) and drop redundant copies, so configs an
|
||||
# older Headroom already broke are repaired rather than re-broken.
|
||||
# ``(?!#)\S*`` treats an empty value and a comment-only value the same;
|
||||
# ``(.*)`` keeps the comment text intact, including any colons in it.
|
||||
dashboard = re.compile(rf"^([ \t]*){re.escape(key)}:[ \t]*((?!#)\S*)(.*)$")
|
||||
#
|
||||
# Anchored with no leading whitespace: only a column-0 occurrence of the
|
||||
# key is this top-level setting. An indented one belongs to a different
|
||||
# mapping and must not be rewritten or deduplicated against.
|
||||
#
|
||||
# The pattern only recognizes the key line. ``_serena_inline_comment``
|
||||
# decides what to keep from the remainder, so a ``#`` flush against the
|
||||
# colon still matches and is preserved instead of leaving the line in
|
||||
# place and appending a second copy of the key.
|
||||
dashboard = re.compile(rf"^{re.escape(key)}:[ \t]*(.*)$")
|
||||
lines: list[str] = []
|
||||
rewritten = False
|
||||
for line in text.splitlines(keepends=True):
|
||||
match = dashboard.match(line)
|
||||
body = line.rstrip("\r\n")
|
||||
match = dashboard.match(body)
|
||||
if match is None:
|
||||
lines.append(line)
|
||||
continue
|
||||
if rewritten:
|
||||
continue
|
||||
rewritten = True
|
||||
indent, comment = match.group(1), match.group(3)
|
||||
if comment and not comment[0].isspace():
|
||||
comment = " " + comment
|
||||
ending = "\n" if line.endswith("\n") else ""
|
||||
lines.append(f"{indent}{key}: false{comment}{ending}")
|
||||
comment = _serena_inline_comment(match.group(1))
|
||||
ending = "\n" if line.endswith(("\n", "\r")) else ""
|
||||
lines.append(f"{key}: false{comment}{ending}")
|
||||
if rewritten:
|
||||
new = "".join(lines)
|
||||
else:
|
||||
|
||||
@@ -144,6 +144,36 @@ def test_serena_dashboard_disabled_keeps_comment_on_unset_value(tmp_path, monkey
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_keeps_comment_glued_to_colon(tmp_path, monkeypatch) -> None:
|
||||
"""A ``#`` with no space after the colon is still the comment.
|
||||
|
||||
``web_dashboard_open_on_launch:# note`` does not match a pattern that
|
||||
requires the value token to be non-comment text, so the line used to be
|
||||
left in place and a second copy of the key was appended.
|
||||
"""
|
||||
import yaml
|
||||
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
cfg = tmp_path / ".serena" / "serena_config.yml"
|
||||
cfg.parent.mkdir(parents=True)
|
||||
cfg.write_text(
|
||||
"projects: []\n"
|
||||
"web_dashboard_open_on_launch:# note: leave unset for now\n"
|
||||
"gui_log_window: false\n"
|
||||
)
|
||||
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
|
||||
text = cfg.read_text()
|
||||
assert text.count("web_dashboard_open_on_launch") == 1
|
||||
assert "web_dashboard_open_on_launch: false # note: leave unset for now" in text
|
||||
assert yaml.safe_load(text)["web_dashboard_open_on_launch"] is False
|
||||
|
||||
first = text
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_repairs_duplicate_keys(tmp_path, monkeypatch) -> None:
|
||||
"""Configs already broken by an older Headroom must come back valid.
|
||||
|
||||
@@ -179,6 +209,62 @@ def test_serena_dashboard_disabled_repairs_duplicate_keys(tmp_path, monkeypatch)
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_ignores_nested_key(tmp_path, monkeypatch) -> None:
|
||||
"""Only the column-0 key is Serena's setting; an indented one is not.
|
||||
|
||||
A line edit that matches the key name regardless of indentation can
|
||||
rewrite an unrelated nested mapping's key, and can mistake it for a
|
||||
duplicate of the real top-level setting and delete the real one.
|
||||
"""
|
||||
import yaml
|
||||
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
cfg = tmp_path / ".serena" / "serena_config.yml"
|
||||
cfg.parent.mkdir(parents=True)
|
||||
cfg.write_text(
|
||||
"extension:\n"
|
||||
" web_dashboard_open_on_launch: true\n"
|
||||
"web_dashboard_open_on_launch: true\n"
|
||||
"projects: []\n"
|
||||
)
|
||||
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
|
||||
text = cfg.read_text()
|
||||
assert " web_dashboard_open_on_launch: true\n" in text, "nested key must be left untouched"
|
||||
parsed = yaml.safe_load(text)
|
||||
assert parsed["web_dashboard_open_on_launch"] is False
|
||||
assert parsed["extension"]["web_dashboard_open_on_launch"] is True
|
||||
|
||||
first = text
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_drops_non_comment_trailing_content(
|
||||
tmp_path, monkeypatch
|
||||
) -> None:
|
||||
"""Trailing content that is not a comment must not survive the rewrite.
|
||||
|
||||
E.g. an explicit YAML type tag (``!!bool true``) has a value token plus a
|
||||
second, space-separated token. Preserving that second token as if it were
|
||||
a comment turns the rewritten line into ``false true``, which YAML parses
|
||||
as the string ``"false true"`` instead of the boolean ``False``.
|
||||
"""
|
||||
import yaml
|
||||
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
cfg = tmp_path / ".serena" / "serena_config.yml"
|
||||
cfg.parent.mkdir(parents=True)
|
||||
cfg.write_text("projects: []\nweb_dashboard_open_on_launch: !!bool true\n")
|
||||
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
|
||||
text = cfg.read_text()
|
||||
assert "web_dashboard_open_on_launch: false\n" in text
|
||||
assert yaml.safe_load(text)["web_dashboard_open_on_launch"] is False
|
||||
|
||||
|
||||
def test_serena_config_is_never_created_by_headroom(tmp_path, monkeypatch) -> None:
|
||||
"""Headroom must NOT pre-empt Serena's own config bootstrap (#2674).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user