Skip to content

Commit c21eb08

Browse files
lseongjooclaude
andcommitted
Keep unsaved editor text when parking fails and reopen manual drafts (#18)
Parking the editor's document now reports a failed write. Switching documents and starting a new one stop instead of replacing the editor, and an arrival is listed without taking the editor, with a notice. A successful park records the document as the one to reopen, so a manual draft comes back after a restart like a received one. Addresses Codex review on PR #23 (P1 discussion_r4694214953, P2 discussion_r4694214961). Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ac1fa84 commit c21eb08

3 files changed

Lines changed: 54 additions & 11 deletions

File tree

‎Sources/Core/DraftStore.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ final class MemoryDraftStore: DraftStoring {
7474
private var order: [String] = []
7575
var activeID: String?
7676
var failWrites = false
77+
/// Refuse writes for these ids only, to test one failing document.
78+
var failWritesFor: Set<String> = []
7779

7880
func summaries() -> [DraftSummary] { order.compactMap { id in documents[id].map { DraftSummary(id: id, document: $0) } } }
7981
func load(_ id: String) -> DraftDocument? { documents[id] }
8082
func save(_ id: String, _ document: DraftDocument) -> Bool {
81-
guard !failWrites else { return false }
83+
guard !failWrites, !failWritesFor.contains(id) else { return false }
8284
if documents[id] == nil { order.append(id) }
8385
documents[id] = document
8486
return true

‎Sources/Core/ExplanationDraftWorkspace.swift‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,35 @@ final class ExplanationDraftWorkspace: ObservableObject {
170170
arrivalNotice = "새 대본이 목록에 추가되었습니다. 보던 글은 그대로입니다."
171171
return .accepted
172172
}
173-
parkActiveDocument()
173+
guard parkActiveDocument() else {
174+
// The arrival is stored; the unsaved editor text must not be replaced.
175+
index.append(DraftSummary(id: draft.id, document: arrived, unseen: true))
176+
arrivalNotice = "새 대본은 목록에 추가했지만, 보던 글을 저장하지 못해 편집기에 그대로 두었습니다."
177+
return .accepted
178+
}
174179
index.append(DraftSummary(id: draft.id, document: arrived))
175180
activate(draft.id, arrived)
176181
arrivalNotice = kept ? "새 대본을 받았습니다. 보던 글은 목록에 그대로 있습니다." : "새 대본을 받았습니다."
177182
return .accepted
178183
}
179184

180-
/// Writes the document in the editor to the store and lists it.
181-
private func parkActiveDocument() {
185+
static let parkFailureNotice = "보던 글을 저장하지 못해 편집기에 그대로 두었습니다. 저장 공간을 확인한 뒤 다시 시도하세요."
186+
187+
/// Writes the document in the editor to the store, lists it and records it
188+
/// as the document to reopen. Returns false when the write failed: the
189+
/// editor is then the only complete copy, so callers must not replace it.
190+
@discardableResult
191+
private func parkActiveDocument() -> Bool {
182192
saveTask?.cancel(); saveTask = nil
183-
guard shouldParkActiveDocument else { return }
193+
guard shouldParkActiveDocument else { return true }
184194
let id = editorDocumentID, document = capturedDocument
185-
// A failed write keeps the text in the editor's place in the list; the
186-
// stored copy is then the older one.
187-
_ = store.save(id, document)
195+
guard store.save(id, document) else { return false }
188196
let row = DraftSummary(id: id, document: document)
189197
if let position = index.firstIndex(where: { $0.id == id }) { index[position] = row }
190198
else { index.append(row) }
199+
// Manual documents reopen after a restart just like received ones.
200+
store.activeID = id
201+
return true
191202
}
192203

193204
/// Makes what is on screen durable. Called shortly after edits and at quit.
@@ -203,7 +214,7 @@ final class ExplanationDraftWorkspace: ObservableObject {
203214

204215
func selectDocument(_ id: String) {
205216
guard id != editorDocumentID, index.contains(where: { $0.id == id }), let next = store.load(id) else { return }
206-
parkActiveDocument()
217+
guard parkActiveDocument() else { arrivalNotice = Self.parkFailureNotice; return }
207218
activate(id, next)
208219
}
209220

@@ -245,7 +256,7 @@ final class ExplanationDraftWorkspace: ObservableObject {
245256
@discardableResult
246257
func newDocument() -> Bool {
247258
guard shouldParkActiveDocument else { return true }
248-
parkActiveDocument()
259+
guard parkActiveDocument() else { arrivalNotice = Self.parkFailureNotice; return false }
249260
resetToBlank()
250261
return true
251262
}

‎Tests/AgentCatalogTests.swift‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,37 @@ enum CodeExplanation { static let noChange = "변경 없음" }
330330
failing.editDisplay("쓰던 글")
331331
memory.failWrites = true
332332
precondition(failing.receiveDocument(titled) == .busy && failing.display == "쓰던 글" && failing.unseenArrivals == 0)
333-
print("Draft history: title rules, title kept out of body and playback, key duplicate/conflict, fallback title, 154 documents, search-time arrival, restart restore, storage failure passed")
333+
334+
// Parking the editor fails: switching, a new document and an arrival keep the unsaved text.
335+
let parkStore = MemoryDraftStore()
336+
let parking = ExplanationDraftWorkspace(store: parkStore)
337+
precondition(parking.receiveDocument(ExplanationDraft(id: "p1", display: "받은 글", speech: nil)!) == .accepted)
338+
precondition(parking.newDocument())
339+
parking.editDisplay("저장 안 된 글")
340+
let unsavedID = parking.editorDocumentID
341+
parkStore.failWritesFor = [unsavedID]
342+
parking.selectDocument("p1")
343+
precondition(parking.display == "저장 안 된 글" && parking.editorDocumentID == unsavedID && parking.arrivalNotice != nil)
344+
precondition(!parking.newDocument() && parking.display == "저장 안 된 글")
345+
precondition(parking.receiveDocument(ExplanationDraft(id: "p2", display: "실패 중 도착", speech: nil)!) == .accepted)
346+
precondition(parking.display == "저장 안 된 글" && parking.editorDocumentID == unsavedID)
347+
precondition(parking.documentTabs.contains { $0.id == "p2" && $0.unseen } && parkStore.load("p2") != nil)
348+
// Once storage works again the same text is saved and the switch goes through.
349+
parkStore.failWritesFor = []
350+
parking.selectDocument("p1")
351+
precondition(parking.display == "받은 글" && parkStore.load(unsavedID)?.display == "저장 안 된 글")
352+
353+
// A manual document being edited reopens after a restart.
354+
let manualStore = MemoryDraftStore()
355+
let manual = ExplanationDraftWorkspace(store: manualStore)
356+
manual.editDisplay("직접 쓴 글")
357+
let manualDocID = manual.editorDocumentID
358+
manual.flush()
359+
precondition(manualStore.activeID == manualDocID)
360+
let reopened = ExplanationDraftWorkspace(store: manualStore)
361+
precondition(reopened.display == "직접 쓴 글" && reopened.editorDocumentID == manualDocID)
362+
precondition(reopened.newDocument() && manualStore.activeID == nil)
363+
print("Draft history: title rules, title kept out of body and playback, key duplicate/conflict, fallback title, 154 documents, search-time arrival, restart restore, storage failure, failed park keeps editor, manual draft reopens passed")
334364
}
335365

336366
/// Approval record rules, with real P-256 signatures and a controllable clock.

0 commit comments

Comments
 (0)