fault18: restore squashed lines to plain form

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Opus 5.5 committed 2026-12-03 10:14:20 -07:00
1 parent ee333dedb9
commit 13021cdde6
1 file changed
+24 -11
+24 -11
View File
@@ -139,17 +139,21 @@ def _append(text, new):
def _drops_text(p, limit=2000000): def _drops_text(p, limit=2000000):
"""Read a non-link drops log up to `limit` bytes, or None when absent; OSError if unreadable or oversize.""" """Read a non-link drops log up to `limit` bytes, or None when absent; OSError if unreadable or oversize."""
try: try:
with open(p, "rb", opener=lambda q, f: os.open(q, f | os.O_NOFOLLOW)) as f: raw = f.read(limit + 1) with open(p, "rb", opener=lambda q, f: os.open(q, f | os.O_NOFOLLOW)) as f:
except FileNotFoundError: return None raw = f.read(limit + 1)
if len(raw) > limit: raise OSError("%s is over %d bytes" % (os.path.join(*p.split(os.sep)[-2:]), limit)) except FileNotFoundError:
return None
if len(raw) > limit:
raise OSError("%s is over %d bytes" % (os.path.join(*p.split(os.sep)[-2:]), limit))
return raw.decode("utf-8", "replace") return raw.decode("utf-8", "replace")
def _check_writable(p, capped=True): def _check_writable(p, capped=True):
if not p: raise OSError("this folder is inside a git directory or a bare repository") if not p:
raise OSError("this folder is inside a git directory or a bare repository")
for t, label, folder in ((os.path.dirname(p), os.path.basename(os.path.dirname(p)), True), (p, os.path.join(*p.split(os.sep)[-2:]), False)): for t, label, folder in ((os.path.dirname(p), os.path.basename(os.path.dirname(p)), True), (p, os.path.join(*p.split(os.sep)[-2:]), False)):
why = (("a symlink" if os.path.islink(t) else "not a directory" if os.path.lexists(t) and not os.path.isdir(t) else "") if folder why = (("a symlink" if os.path.islink(t) else "not a directory" if os.path.lexists(t) and not os.path.isdir(t) else "") if folder
else ("" if (w := store.regular(t)[0]) == "over 64,000 bytes" and not capped else w)) or ("read-only" if os.path.exists(t) and not os.access(t, os.W_OK) else "") else ("" if not capped and store.regular(t)[0] == "over 64,000 bytes" else store.regular(t)[0])) or ("read-only" if os.path.exists(t) and not os.access(t, os.W_OK) else "")
if why: if why:
raise OSError("%s is %s" % (label, why)) raise OSError("%s is %s" % (label, why))
if not os.path.exists(os.path.dirname(p)) and not os.access(os.path.dirname(os.path.dirname(p)), os.W_OK): if not os.path.exists(os.path.dirname(p)) and not os.access(os.path.dirname(os.path.dirname(p)), os.W_OK):
@@ -485,7 +489,9 @@ def organize_record(answer_file, cwd):
if proj and (not pp or os.path.islink(os.path.dirname(pp))): if proj and (not pp or os.path.islink(os.path.dirname(pp))):
return _migrate(cwd)[1].removeprefix("sisi: ") return _migrate(cwd)[1].removeprefix("sisi: ")
with _locks(USER, *filter(None, [pp])): with _locks(USER, *filter(None, [pp])):
if msg := proj and _move(pp, _legacy(cwd)): return msg.removeprefix("sisi: ") msg = proj and _move(pp, _legacy(cwd))
if msg:
return msg.removeprefix("sisi: ")
had = {USER: lines(up), PROJECT: scope_lines(PROJECT, cwd, migrate=False) if proj else []} had = {USER: lines(up), PROJECT: scope_lines(PROJECT, cwd, migrate=False) if proj else []}
if (want := _organized(("locks", "organize.json")).get("snap:" + _mark_key(PROJECT, cwd))) and _snap(had[USER], had[PROJECT]) != want: if (want := _organized(("locks", "organize.json")).get("snap:" + _mark_key(PROJECT, cwd))) and _snap(had[USER], had[PROJECT]) != want:
return "the rules changed since `sisi rules organize` ran; run `sisi rules organize` again" return "the rules changed since `sisi rules organize` ran; run `sisi rules organize` again"
@@ -508,20 +514,26 @@ def organize_record(answer_file, cwd):
why_bad = ("expected N = M" if not m else "unknown problem number" if not (1 <= n <= nprobs and 1 <= k <= nprobs) why_bad = ("expected N = M" if not m else "unknown problem number" if not (1 <= n <= nprobs and 1 <= k <= nprobs)
else "cycle" if rn == k else "already merged" if k in amap and retro._alias_root(amap, k) != rn else "") else "cycle" if rn == k else "already merged" if k in amap and retro._alias_root(amap, k) != rn else "")
bad_same.append((raw_l, why_bad)) if why_bad else amap.__setitem__(k, rn) bad_same.append((raw_l, why_bad)) if why_bad else amap.__setitem__(k, rn)
if not any(got.values()) and not core and any(had.values()): return "the answer holds no rules at all" if not any(got.values()) and not core and any(had.values()):
return "the answer holds no rules at all"
try: try:
seen, op, dp, ts = _organized(), store.data("rules", "organized.json"), store.data(DROPS), time.strftime("%Y-%m-%dT%H:%M:%S") seen, op, dp, ts = _organized(), store.data("rules", "organized.json"), store.data(DROPS), time.strftime("%Y-%m-%dT%H:%M:%S")
seen["retros"] = len(store.read(store.data("retro.jsonl")).splitlines()) seen["retros"] = len(store.read(store.data("retro.jsonl")).splitlines())
seen.update({_mark_key(USER): len(_retro(got[USER])), **({_mark_key(PROJECT, cwd): len(_retro(got[PROJECT]))} if proj else {})}) seen.update({_mark_key(USER): len(_retro(got[USER])), **({_mark_key(PROJECT, cwd): len(_retro(got[PROJECT]))} if proj else {})})
seen.pop("rules/projects/%s.md" % store.project_key(cwd), None) seen.pop("rules/projects/%s.md" % store.project_key(cwd), None)
change = {up: (_text(up), "".join(l + "\n" for l in got[USER])), op: (_text(op), json.dumps(seen))} change = {up: (_text(up), "".join(l + "\n" for l in got[USER])), op: (_text(op), json.dumps(seen))}
if proj: before = _text(pp); change[pp] = (before, "".join(l + "\n" for l in got[PROJECT]) if got[PROJECT] or before is not None else None) if proj:
if core: change[cp] = (cb := _text(cp), "".join(l + "\n" for l in dict.fromkeys(_parse(cb or "") + core))) before = _text(pp)
change[pp] = (before, "".join(l + "\n" for l in got[PROJECT]) if got[PROJECT] or before is not None else None)
if core:
change[cp] = (cb := _text(cp), "".join(l + "\n" for l in dict.fromkeys(_parse(cb or "") + core)))
if recs: if recs:
_check_writable(dp, capped=False) _check_writable(dp, capped=False)
old = _drops_text(dp) old = _drops_text(dp)
change[dp] = (old, _append(old, [json.dumps({"ts": ts, "rule": r, "problem": pn, "reason": why}, sort_keys=True) for r, pn, why in recs])) change[dp] = (old, _append(old, [json.dumps({"ts": ts, "rule": r, "problem": pn, "reason": why}, sort_keys=True) for r, pn, why in recs]))
if amap != old_amap: ap = store.data(PROBLEMS); change[ap] = (_text(ap), json.dumps({str(k): v for k, v in sorted(amap.items())}, sort_keys=True)) if amap != old_amap:
ap = store.data(PROBLEMS)
change[ap] = (_text(ap), json.dumps({str(k): v for k, v in sorted(amap.items())}, sort_keys=True))
[_check_writable(p) for p in change if change[p][1] is not None and p != dp] [_check_writable(p) for p in change if change[p][1] is not None and p != dp]
now, home = {USER: got[USER], PROJECT: got[PROJECT], CORE: core}, {USER: up, PROJECT: pp, CORE: cp} now, home = {USER: got[USER], PROJECT: got[PROJECT], CORE: core}, {USER: up, PROJECT: pp, CORE: cp}
flat = [l for sec in now.values() for l in sec] flat = [l for sec in now.values() for l in sec]
@@ -530,7 +542,8 @@ def organize_record(answer_file, cwd):
back = [_back(l, home[lv], home[f], f) for lv in now for l in dict.fromkeys(now[lv]) for f in (USER, PROJECT) if f != lv and l in had[f] and l not in now[f] + had.get(lv, []) and l not in over + merged] back = [_back(l, home[lv], home[f], f) for lv in now for l in dict.fromkeys(now[lv]) for f in (USER, PROJECT) if f != lv and l in had[f] and l not in now[f] + had.get(lv, []) and l not in over + merged]
warn = _apply("rules organized" if proj else "rules organized (user only)", change, warn = _apply("rules organized" if proj else "rules organized (user only)", change,
{"reverse": back} if back else {"path": pp, "before": before, "after": change[pp][1]} if proj else {}) {"reverse": back} if back else {"path": pp, "before": before, "after": change[pp][1]} if proj else {})
except OSError as e: return "could not record the organized rules (%s)" % e except OSError as e:
return "could not record the organized rules (%s)" % e
if warn: return "could not record the organized rules (%s); the files are restored, so run `sisi rules organize --record %s` again once the store is free" % (warn, answer_file) if warn: return "could not record the organized rules (%s); the files are restored, so run `sisi rules organize --record %s` again once the store is free" % (warn, answer_file)
proj and change[pp][1] is not None and note(pp) proj and change[pp][1] is not None and note(pp)
with contextlib.suppress(OSError), store.lock("organize"): with contextlib.suppress(OSError), store.lock("organize"):