Skip to content

Commit 222a973

Browse files
authored
fix(vibe): keep user comments that follow a managed hook block (#5109)
* fix(vibe): keep user comments that follow a managed hook block A managed [[hooks]] block ran until the next table header or the end of the file, so the comments after it were removed with it on every events refresh and teardown. A comment there usually introduces the user hook that follows it. End the block after its last line of TOML and the blank lines right after it. Lines inside a multiline string or array still belong to the value. Refs #5079 Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) * test(vibe): cover the comment boundaries of a managed hook block The exact-byte removal test covered one comment right after a managed block. Add the two boundaries the comment-state logic decides: blank lines after a kept comment stay with it, and a comment followed by more keys of the managed block is removed with the block. Refs #5079 Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) * test(vibe): end the multiline-value control on the string; document cleanup The control passed even without the multiline-string guard, because the array after the string extended the managed block anyway. Put the string last so a missing guard leaves its closing lines behind and the test fails. Add a Runtime Events note to the Vibe guide: cleanup touches only the entries Spec Kit wrote, an entry ends after its last TOML line and the blank lines after it, and the file is deleted when only comments and blank lines remain. Refs #5079 Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) * test(vibe): run the comment-boundary cases with CRLF too The exact-byte removal cases ran only with LF. Run them with CRLF as well; they fail if the scanner keeps the CR in a line's content. The refresh test now tears down with the integration and manifest returned by the refresh call. Refs #5079 Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) * docs(vibe): say that comments go with a deleted hooks file The Runtime Events note said cleanup deletes .vibe/hooks.toml when only comments and blank lines remain, but not that those comments are lost with it, or that Codex's config.toml follows the same rule. Refs #5079 Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) * test(vibe): pin teardown when only a trailing comment remains The trailing-comment case always kept a user hook in the file, so no test covered the documented comments-only case. Add one: the comment survives a refresh, and teardown deletes .vibe/hooks.toml. Refs #5079 Assisted-by: Claude Code (model: claude-opus-5-5, autonomous)
1 parent 7b61682 commit 222a973

3 files changed

Lines changed: 140 additions & 2 deletions

File tree

‎docs/integrations/vibe.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ unsupported handlers are skipped with a warning.
6767
`--integration-options="--events false"` disables Spec Kit event handlers.
6868
See [runtime events](cli.md#runtime-events).
6969

70+
Spec Kit refreshes and removes only the `[[hooks]]` entries it wrote, which
71+
carry `speckit_marker = true`. Each entry ends after its last TOML line and the
72+
blank lines right after it, so a comment that follows a Spec Kit entry, such as
73+
a note above your own hook, stays in the file. If removing Spec Kit's entries
74+
leaves only comments and blank lines, Spec Kit deletes `.vibe/hooks.toml`, and
75+
those comments are deleted with it. Spec Kit applies the same rule to Codex's
76+
`.codex/config.toml`.
77+
7078
## Workflow Dispatch
7179

7280
Workflow steps use `vibe -p <prompt>` and request `--output json` for structured

‎src/specify_cli/integrations/vibe/__init__.py‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ def _managed_hook_spans(source: str) -> list[tuple[int, int]] | None:
395395
headers. A parser validity gate and the scanner both return ``None``
396396
for malformed or ambiguous input so callers can leave the source
397397
byte-for-byte untouched.
398+
399+
A block ends after its last line of TOML and the blank lines right
400+
after it. A standalone comment that follows stays with the content
401+
after it, such as the user hook it introduces (#5079).
398402
"""
399403
try:
400404
tomllib.loads(source)
@@ -403,6 +407,8 @@ def _managed_hook_spans(source: str) -> list[tuple[int, int]] | None:
403407

404408
spans: list[tuple[int, int]] = []
405409
current_start: int | None = None
410+
current_end = 0
411+
current_after_comment = False
406412
current_is_hooks = False
407413
current_has_marker = False
408414
multiline: str | None = None
@@ -486,7 +492,7 @@ def _managed_hook_spans(source: str) -> list[tuple[int, int]] | None:
486492
)
487493
if header_path is not None:
488494
if current_start is not None and current_has_marker:
489-
spans.append((current_start, offset))
495+
spans.append((current_start, current_end))
490496
current_start = offset
491497
current_is_hooks = is_array and header_path == ("hooks",)
492498
current_has_marker = False
@@ -515,12 +521,22 @@ def _managed_hook_spans(source: str) -> list[tuple[int, int]] | None:
515521
if array_depth < 0:
516522
return None
517523

524+
# Lines inside a multiline string or array are part of the value,
525+
# even when they look like comments.
526+
if line_starts_in_array or line_has_multiline_string or code.strip(" \t"):
527+
current_end = offset + len(line)
528+
current_after_comment = False
529+
elif content.strip(" \t"):
530+
current_after_comment = True
531+
elif not current_after_comment:
532+
current_end = offset + len(line)
533+
518534
offset += len(line)
519535

520536
if multiline is not None or array_depth:
521537
return None
522538
if current_start is not None and current_has_marker:
523-
spans.append((current_start, len(source)))
539+
spans.append((current_start, current_end))
524540
return spans
525541

526542
@classmethod

‎tests/integrations/test_integration_vibe.py‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,39 @@ def test_merge_and_teardown_preserve_user_hooks(self, tmp_path):
334334
remaining = self._parse(tmp_path)["hooks"]
335335
assert [h["name"] for h in remaining] == ["deny-rm-rf"]
336336

337+
@pytest.mark.parametrize(
338+
"user_after",
339+
(
340+
"# NOTE: keep guard-bash first\n"
341+
"[[hooks]]\n"
342+
'name = "user-hook"\n'
343+
'type = "pre_tool"\n'
344+
'command = "guard-bash"\n',
345+
"# notes about these hooks\n",
346+
),
347+
ids=("comment-before-user-hook", "trailing-comment"),
348+
)
349+
def test_refresh_and_teardown_keep_comments_after_a_managed_hook(
350+
self, tmp_path, user_after
351+
):
352+
"""A comment after a managed block is user content, not part of the block (#5079)."""
353+
path = tmp_path / ".vibe" / "hooks.toml"
354+
path.parent.mkdir(parents=True)
355+
path.write_bytes(
356+
b'[[hooks]]\nname = "deny-rm-rf"\ntype = "pre_tool"\ncommand = "guard-rm"\n'
357+
)
358+
events = {"pre_tool_use": [{"command": "speckit.tdd.validate"}]}
359+
integration, manifest = self._install(tmp_path, events)
360+
path.write_bytes(path.read_bytes() + ("\n" + user_after).encode("utf-8"))
361+
362+
integration, manifest = self._install(tmp_path, events)
363+
assert user_after in path.read_text(encoding="utf-8")
364+
365+
remove_integration_events(integration, tmp_path, manifest)
366+
remaining = path.read_text(encoding="utf-8")
367+
assert user_after in remaining
368+
assert "speckit_marker" not in remaining
369+
337370
@pytest.mark.parametrize("newline", ("\n", "\r\n"), ids=("lf", "crlf"))
338371
def test_merge_preserves_user_physical_line_bytes(self, tmp_path, newline):
339372
"""A native Windows write must not expand retained CRLF to CRCRLF."""
@@ -484,6 +517,20 @@ def test_teardown_deletes_file_without_user_content(self, tmp_path):
484517
remove_integration_events(integration, tmp_path, manifest)
485518
assert not (tmp_path / ".vibe" / "hooks.toml").exists()
486519

520+
def test_teardown_deletes_file_when_only_comments_remain(self, tmp_path):
521+
"""A trailing comment survives refresh, but a file left with only
522+
comments is deleted, as for Codex's config.toml (#5079)."""
523+
path = tmp_path / ".vibe" / "hooks.toml"
524+
events = {"pre_tool_use": [{"command": "speckit.tdd.validate"}]}
525+
integration, manifest = self._install(tmp_path, events)
526+
path.write_bytes(path.read_bytes() + b"\n# notes about these hooks\n")
527+
528+
integration, manifest = self._install(tmp_path, events)
529+
assert "# notes about these hooks" in path.read_text(encoding="utf-8")
530+
531+
remove_integration_events(integration, tmp_path, manifest)
532+
assert not path.exists()
533+
487534

488535
class TestVibeTomlNoOpRemoval:
489536
"""Unowned Vibe hooks.toml files survive no-op cleanup byte-for-byte."""
@@ -569,6 +616,73 @@ def test_removal_recognizes_valid_marker_with_toml_line_endings(self, tmp_path,
569616
assert removed_file is False
570617
assert path.read_bytes() == user.encode()
571618

619+
@pytest.mark.parametrize(
620+
("managed", "user"),
621+
(
622+
(
623+
'[[hooks]]\nname = "managed"\nspeckit_marker = true\n\n',
624+
"# NOTE: keep guard-bash first\n"
625+
"[[hooks]]\n"
626+
'name = "user-hook"\n'
627+
'command = "guard-bash"\n',
628+
),
629+
# Blank lines after a kept comment stay with it.
630+
(
631+
'[[hooks]]\nname = "managed"\nspeckit_marker = true\n\n',
632+
'# NOTE: keep guard-bash first\n\n\n[[hooks]]\nname = "user-hook"\n',
633+
),
634+
# A comment followed by more keys of the managed block is inside it.
635+
(
636+
'[[hooks]]\nname = "managed"\n# inside\n\nspeckit_marker = true\n\n',
637+
'[[hooks]]\nname = "user-hook"\n',
638+
),
639+
),
640+
ids=(
641+
"comment-before-user-hook",
642+
"blank-lines-after-comment",
643+
"comment-between-managed-keys",
644+
),
645+
)
646+
@pytest.mark.parametrize("newline", ("\n", "\r\n"), ids=("lf", "crlf"))
647+
def test_removal_ends_a_managed_hook_at_its_last_toml_line(
648+
self, tmp_path, managed, user, newline
649+
):
650+
"""The blank separator goes with the managed block; a comment after it stays (#5079)."""
651+
managed = managed.replace("\n", newline)
652+
user = user.replace("\n", newline)
653+
path, _ = self._user_hooks_file(tmp_path, (managed + user).encode())
654+
655+
removed_file = get_integration("vibe").remove_vibe_event_hooks(
656+
tmp_path, ensure_safe_destination=lambda _path: None
657+
)
658+
659+
assert removed_file is False
660+
assert path.read_bytes() == user.encode()
661+
662+
def test_removal_still_takes_comment_like_lines_inside_managed_values(self, tmp_path):
663+
"""Lines inside a managed multiline string or array belong to its block."""
664+
managed = (
665+
"[[hooks]]\n"
666+
'name = "managed"\n'
667+
"speckit_marker = true\n"
668+
"args = [\n"
669+
" # part of the array\n"
670+
' "a",\n'
671+
"]\n"
672+
'command = """\n'
673+
"# part of the command\n"
674+
'"""\n'
675+
)
676+
user = '[[hooks]]\nname = "user-hook"\n'
677+
path, _ = self._user_hooks_file(tmp_path, (managed + user).encode())
678+
679+
removed_file = get_integration("vibe").remove_vibe_event_hooks(
680+
tmp_path, ensure_safe_destination=lambda _path: None
681+
)
682+
683+
assert removed_file is False
684+
assert path.read_bytes() == user.encode()
685+
572686
def test_removal_preserves_marker_inside_multiline_strings(self, tmp_path):
573687
integration = get_integration("vibe")
574688
for name, delimiter in (("basic", '"""'), ("literal", "'''")):

0 commit comments

Comments
 (0)