fix(854-2): M01 키 대장 쓰기 원자적 · 파일 쓰기 중간 실패 때 대장 · 파일 저장 전으로 되돌림 — 묶음 ②

- master_keys.save_book = 임시 파일 → 교체(반쪽 대장 없음 · 빌더도 같은 길)
- Store_Save.save = 대장 → 파일 차례 그대로 · 파일 쓰기가 끊기면 쓴(쓰려던) 파일 · 기계 대 · 대장을 저장 전 글로(대장만 앞서가지 않음)
- 시험 test_854_2_m01_key_book — 첫 파일 · 둘째 파일(반쯤 쓴 뒤) 끊김 = 바이트 그대로 · 끊김 없음 = 대장 · 파일 같이 감

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 16:00:36 +09:00
co-authored by Claude Opus 5.5
parent 9c67cdac64
commit ba6f25b971
3 changed files with 155 additions and 5 deletions
+38 -4
View File
@@ -116,16 +116,50 @@ def save(batch: list[dict]) -> list[dict]:
new = [x for x in new + cache.labor_errors(fresh, gone) if x not in old]
if new:
raise store.StoreError(422, {"errors": new})
if book["다음"] != issued:
mk.save_book(book, book_path)
out = [{"file": name, "version": _write(name, before[name], after[name])} for name in names]
# 대장(원자적) → 파일 · 중간에 끊기면 쓴 것 모두 저장 전으로(대장만 · 파일만 앞서가지 않게)
raw = {n: (store.FOLDER / n).read_bytes() for n in names if not _machine(n)}
book_raw = book_path.read_bytes() if book["다음"] != issued else None
tried: list[str] = []
try:
if book_raw is not None:
mk.save_book(book, book_path)
out = []
for name in names:
tried.append(name)
out.append({"file": name, "version": _write(name, before[name], after[name])})
except BaseException:
_undo(tried, before, after, raw, book_path, book_raw)
raise
# 저장 뒤 — 쓴 파일만 기억에 갈아 끼우고 흔들린 자리를 알려 줘 그 줄만 다시 세게
cache.adopt(store.FOLDER, after, fresh, names, narrowed[2])
return out
def _machine(name: str) -> bool:
return name == mmf.NAME and mmf.here(store.FOLDER) # 기계 = 폴더(826-2)
def _put(path, raw: bytes) -> None:
tmp = path.with_name(f".{path.name}.tmp")
tmp.write_bytes(raw)
os.replace(tmp, path)
def _undo(names, before, after, raw, book_path, book_raw) -> None:
"""쓰기 중간 실패 — 쓴(쓰려던) 파일 · 대장을 저장 전 글로 되돌림."""
for name in names:
if _machine(name):
mmf.write(
store.FOLDER, before[name], after[name]
) # 바뀐 · 더한 · 지운 대를 저장 전으로
else:
_put(store.FOLDER / name, raw[name])
if book_raw is not None:
_put(book_path, book_raw)
def _write(name: str, before: dict, after: dict) -> str:
if name == mmf.NAME and mmf.here(store.FOLDER): # 기계 = 바뀐 대 파일만(826-2)
if _machine(name): # 기계 = 바뀐 대 파일만(826-2)
mmf.write(store.FOLDER, after, before)
return store.version(name)
path = store.FOLDER / name