mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(wrap): rewrite Serena's dashboard key at the root mapping indent
A uniformly indented root mapping is still the top-level mapping. Matching only column 0 left that key in place and appended a column-0 copy, which YAML rejects. Keep the root indent and leave a deeper key alone. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+34
-12
@@ -2269,6 +2269,24 @@ def _serena_inline_comment(rest: str) -> str:
|
||||
return " " + body
|
||||
|
||||
|
||||
def _serena_root_indent(text: str) -> str:
|
||||
"""Return the indentation of the root mapping.
|
||||
|
||||
YAML allows every root key to share an indent, so column 0 is not the
|
||||
test. The root indent is the shortest leading whitespace on a non-empty,
|
||||
non-comment line. A deeper line belongs to a nested block.
|
||||
"""
|
||||
root: str | None = None
|
||||
for line in text.splitlines():
|
||||
stripped = line.lstrip(" \t")
|
||||
if not stripped or stripped.startswith("#"):
|
||||
continue
|
||||
indent = line[: len(line) - len(stripped)]
|
||||
if root is None or len(indent) < len(root):
|
||||
root = indent
|
||||
return root or ""
|
||||
|
||||
|
||||
def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
"""Disable Serena's browser dashboard auto-open in ``~/.serena/serena_config.yml``.
|
||||
|
||||
@@ -2310,11 +2328,13 @@ def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
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.
|
||||
The edit only touches the key at the root mapping's indent. YAML allows
|
||||
that mapping to be indented as a whole, so column 0 is not the test: the
|
||||
root indent is the shortest indent of a non-comment line. A deeper
|
||||
occurrence belongs to a nested mapping (e.g. an ``extension:`` block that
|
||||
reuses 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
|
||||
|
||||
@@ -2340,15 +2360,17 @@ def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
# (keeping any inline comment) and drop redundant copies, so configs an
|
||||
# older Headroom already broke are repaired rather than re-broken.
|
||||
#
|
||||
# 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.
|
||||
# Match the key only at the root mapping's indent, and keep that indent
|
||||
# when rewriting or appending. A root mapping indented as a whole is still
|
||||
# the top-level mapping; appending a column-0 key beside it makes the file
|
||||
# unparsable. A deeper occurrence is a different mapping.
|
||||
#
|
||||
# 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]*(.*)$")
|
||||
indent = _serena_root_indent(text)
|
||||
dashboard = re.compile(rf"^{re.escape(indent)}{re.escape(key)}:[ \t]*(.*)$")
|
||||
lines: list[str] = []
|
||||
rewritten = False
|
||||
for line in text.splitlines(keepends=True):
|
||||
@@ -2362,15 +2384,15 @@ def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
rewritten = True
|
||||
comment = _serena_inline_comment(match.group(1))
|
||||
ending = "\n" if line.endswith(("\n", "\r")) else ""
|
||||
lines.append(f"{key}: false{comment}{ending}")
|
||||
lines.append(f"{indent}{key}: false{comment}{ending}")
|
||||
if rewritten:
|
||||
new = "".join(lines)
|
||||
else:
|
||||
appended.append(f"{key}: false")
|
||||
appended.append(f"{indent}{key}: false")
|
||||
|
||||
# Repair a config left by an affected Headroom version (see #2674 above).
|
||||
if not re.search(r"^\s*projects\s*:", new, re.MULTILINE):
|
||||
appended.append("projects: []")
|
||||
appended.append(f"{indent}projects: []")
|
||||
|
||||
if appended:
|
||||
body = new.rstrip("\n")
|
||||
|
||||
@@ -209,12 +209,39 @@ 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.
|
||||
def test_serena_dashboard_disabled_keeps_indented_root_mapping(tmp_path, monkeypatch) -> None:
|
||||
"""A root mapping indented as a whole is still the top-level mapping.
|
||||
|
||||
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.
|
||||
Rewriting only a column-0 key misses this setting and appends another
|
||||
at column 0. YAML then rejects the file: expected a document start, but
|
||||
found a block mapping start.
|
||||
"""
|
||||
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: true\n")
|
||||
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
|
||||
text = cfg.read_text()
|
||||
assert " web_dashboard_open_on_launch: false\n" in text
|
||||
assert "\nweb_dashboard_open_on_launch:" not in text
|
||||
parsed = yaml.safe_load(text)
|
||||
assert parsed["web_dashboard_open_on_launch"] is False
|
||||
assert parsed["projects"] == []
|
||||
|
||||
first = text
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_ignores_nested_key(tmp_path, monkeypatch) -> None:
|
||||
"""A key nested under another mapping is not Serena's setting.
|
||||
|
||||
The root key in this file is at column 0. The indented copy belongs to
|
||||
``extension`` and must not be rewritten or dropped as a duplicate.
|
||||
"""
|
||||
import yaml
|
||||
|
||||
@@ -241,6 +268,36 @@ def test_serena_dashboard_disabled_ignores_nested_key(tmp_path, monkeypatch) ->
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_ignores_nested_key_under_indented_root(
|
||||
tmp_path, monkeypatch
|
||||
) -> None:
|
||||
"""A deeper key stays nested when the root mapping itself is indented."""
|
||||
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
|
||||
assert " web_dashboard_open_on_launch: false\n" in text
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user