Skip to content

Commit 1ee4553

Browse files
install agents --tool claude adds the statusLine to an existing settings.json
_configure_claude copied the shipped settings template only when ~/.claude/settings.json did not exist, so every Claude Code user who already had any setting never received the status line at all, and only the stale entry an earlier uninstall left behind made a reinstall look as if it had worked. Lane B (plan 2, B4) made uninstall remove that owned entry, which turned the silent gap into a visible one: install -> uninstall -> install ended with no statusLine (its FNF-1). _add_claude_statusline is the mirror image of lane B's _remove_claude_statusline: a learner's own statusLine is kept untouched; otherwise the member is inserted as bytes before the closing brace, indented like the file's last member, every other byte stays, and the edit is parsed back and must equal the original plus the entry before it is written. A file that cannot be read as a JSON object is never edited. RED: 3 new contract tests fail without the src change (existing-file add, install/uninstall/ install round trip, byte-preserving add on _configure_claude directly); 72 pass with it. The no-reflow property is asserted on _configure_claude alone because install_claude_stop_hook, which runs after it, still reserialises the whole file (FOUND-NOT-FIXED lane B #2, separate).
1 parent 7fe6cd6 commit 1ee4553

2 files changed

Lines changed: 143 additions & 1 deletion

File tree

‎packages/studyloop/src/studyloop/installers.py‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,64 @@ def _remove_claude_statusline(settings: Path, statusline: Path) -> int:
18161816
return 1
18171817

18181818

1819+
def _add_claude_statusline(settings: Path, template: Path) -> int:
1820+
"""Add StudyLoop's ``statusLine`` to an existing ``settings.json`` that has none.
1821+
1822+
Install used to copy the shipped template only when the file did not exist,
1823+
so a learner with any Claude Code setting of their own never received the
1824+
status line, and only a stale entry left by an earlier uninstall made a
1825+
reinstall look as if it had worked. The mirror image of
1826+
:func:`_remove_claude_statusline`: a learner's own ``statusLine`` is kept
1827+
untouched; otherwise the member is INSERTED as bytes before the closing
1828+
brace, every other byte stays, and the edit is parsed back and must equal
1829+
the original plus the entry before it is written. Returns 1 when added,
1830+
0 when the file already has a ``statusLine`` or cannot be read as a JSON
1831+
object (a file that cannot be understood is never edited).
1832+
"""
1833+
import json
1834+
1835+
try:
1836+
raw = settings.read_bytes().decode("utf-8")
1837+
loaded = json.loads(raw)
1838+
entry = json.loads(template.read_text(encoding="utf-8"))["statusLine"]
1839+
except (OSError, UnicodeDecodeError, json.JSONDecodeError, KeyError, TypeError):
1840+
return 0
1841+
if not isinstance(loaded, dict) or "statusLine" in loaded:
1842+
return 0
1843+
root = _json_root_object_span(raw)
1844+
if root is None:
1845+
raise InstallError(f"Cannot add StudyLoop's statusLine to {settings} without rewriting it")
1846+
members = _json_member_spans(raw, root) or []
1847+
close = root[1] - 1 # index of the closing brace
1848+
member = f'"statusLine": {json.dumps(entry)}'
1849+
if members:
1850+
# Join with the separator the file already uses between its last
1851+
# member and the closing brace (", " on one line, ",\n " indented).
1852+
last_value_end = members[-1][3]
1853+
gap = raw[last_value_end:close]
1854+
# Indent the new member like the last one: the whitespace that opens
1855+
# the last member's own line (the gap before the brace holds the
1856+
# brace's indentation, which is one level shallower).
1857+
last_key_start = members[-1][1]
1858+
line_start = raw.rfind("\n", 0, last_key_start) + 1
1859+
indent = raw[line_start:last_key_start]
1860+
between = (",\n" + indent) if "\n" in gap else ", "
1861+
# ``gap`` is re-added after the member so the brace keeps its own line
1862+
# and indentation.
1863+
updated = raw[:last_value_end] + between + member + gap + raw[close:]
1864+
else:
1865+
updated = raw[: root[0] + 1] + member + raw[close:]
1866+
expected = {**loaded, "statusLine": entry}
1867+
try:
1868+
edited_ok = json.loads(updated) == expected
1869+
except json.JSONDecodeError:
1870+
edited_ok = False
1871+
if not edited_ok:
1872+
raise InstallError(f"Cannot add StudyLoop's statusLine to {settings} without rewriting it")
1873+
settings.write_bytes(updated.encode("utf-8"))
1874+
return 1
1875+
1876+
18191877
def _configure_claude(repo_root: Path, *, uninstall: bool) -> int:
18201878
claude_home = _HOME / ".claude"
18211879
statusline = claude_home / "study-statusline.sh"
@@ -1835,9 +1893,12 @@ def _configure_claude(repo_root: Path, *, uninstall: bool) -> int:
18351893
shutil.copy2(repo_root / "agents/claude/study-statusline.sh", statusline)
18361894
statusline.chmod(0o755)
18371895
changed += 1
1896+
template = repo_root / "agents/claude/settings.json"
18381897
if not settings.exists():
1839-
shutil.copy2(repo_root / "agents/claude/settings.json", settings)
1898+
shutil.copy2(template, settings)
18401899
changed += 1
1900+
else:
1901+
changed += _add_claude_statusline(settings, template)
18411902
return changed
18421903

18431904

‎packages/studyloop/tests/test_uninstall_contract.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,84 @@ def test_status_line_removal_refuses_an_edit_it_cannot_verify(sandbox: Sandbox)
441441
sandbox.uninstall()
442442

443443
assert settings.read_text(encoding="utf-8") == planted
444+
445+
446+
def test_install_adds_the_status_line_to_an_existing_settings_file(sandbox: Sandbox) -> None:
447+
"""A learner who already had settings never got the status line at all.
448+
449+
Found while merging lane B (2026-10-22): install copied StudyLoop's template
450+
only when ``~/.claude/settings.json`` did not exist, so every Claude Code
451+
user with any setting of their own was silently skipped, and the leftover
452+
entry an earlier uninstall used to leave behind was the only thing that
453+
made a reinstall look as if it worked.
454+
"""
455+
settings = sandbox.home / ".claude/settings.json"
456+
learner = {"theme": "dark", "hooks": {"Stop": [_USER_STOP_GROUP]}}
457+
settings.write_text(json.dumps(learner, indent=2) + "\n", encoding="utf-8")
458+
459+
sandbox.install()
460+
461+
loaded = json.loads(settings.read_text(encoding="utf-8"))
462+
assert loaded["statusLine"] == _OUR_STATUS_LINE
463+
assert loaded["theme"] == "dark"
464+
# Install also merges its own Stop hook beside the learner's; the
465+
# learner's group must still be there, first.
466+
assert loaded["hooks"]["Stop"][0] == _USER_STOP_GROUP
467+
468+
469+
def test_install_then_uninstall_then_install_restores_the_status_line(sandbox: Sandbox) -> None:
470+
"""The round trip lane B's removal made honest: a reinstall must mean installed."""
471+
settings = sandbox.home / ".claude/settings.json"
472+
settings.unlink()
473+
sandbox.install()
474+
sandbox.uninstall()
475+
assert "statusLine" not in json.loads(settings.read_text(encoding="utf-8"))
476+
477+
sandbox.install()
478+
479+
assert json.loads(settings.read_text(encoding="utf-8"))["statusLine"] == _OUR_STATUS_LINE
480+
481+
482+
def test_install_never_replaces_a_learners_own_status_line(sandbox: Sandbox) -> None:
483+
"""A learner's status line is theirs; install adds nothing beside it.
484+
485+
Only the ``statusLine`` member is asserted: install also merges the
486+
session-export Stop hook into this file (and reflows it while doing so,
487+
FOUND-NOT-FIXED lane B #2), which is a separate contract.
488+
"""
489+
settings = sandbox.home / ".claude/settings.json"
490+
planted = json.dumps({"statusLine": _LEARNER_STATUS_LINE, "theme": "dark"}, indent=2) + "\n"
491+
settings.write_text(planted, encoding="utf-8")
492+
493+
sandbox.install()
494+
495+
loaded = json.loads(settings.read_text(encoding="utf-8"))
496+
assert loaded["statusLine"] == _LEARNER_STATUS_LINE
497+
assert loaded["theme"] == "dark"
498+
499+
500+
def test_install_adds_the_status_line_without_reflowing_the_learners_file(
501+
sandbox: Sandbox,
502+
) -> None:
503+
"""The adder itself inserts one member; every other byte of the file stays.
504+
505+
Checked on ``_configure_claude`` directly: the session-export Stop-hook
506+
installer that runs after it still reserialises the whole file
507+
(FOUND-NOT-FIXED lane B #2), so the end-to-end install cannot yet promise
508+
this. Hand formatting and a non-ASCII value are the tell-tales a
509+
reserialising writer would destroy.
510+
"""
511+
settings = sandbox.home / ".claude/settings.json"
512+
before = '{\n "theme": "caf\u00e9",\n "model": "opus"\n}\n'
513+
settings.write_text(before, encoding="utf-8")
514+
515+
installers._configure_claude(sandbox.repo, uninstall=False)
516+
517+
text = settings.read_text(encoding="utf-8")
518+
assert ' "theme": "caf\u00e9",\n' in text
519+
assert ' "model": "opus"' in text
520+
assert json.loads(text)["statusLine"] == _OUR_STATUS_LINE
521+
522+
installers._configure_claude(sandbox.repo, uninstall=True)
523+
524+
assert settings.read_text(encoding="utf-8") == before

0 commit comments

Comments
 (0)