feat(sessions): keep every session, record review details, name and stamp the PDF

Sessions are now the unit of the product instead of an implicit capture run.

- Nothing is deleted by default: the fixed 10-session cap is gone. An age limit
  (Never / 7 / 14 / 30 / 90 / 180 / 365 days) is chosen in Settings > Sessions and
  applied on launch, after archiving, and by "Clean up now". Sent PDFs are never
  touched.
- Each session carries review details: lane, deployed version, commit, reviewer
  (pre-filled with the Mac user) and a free note. They persist in session.json and
  are inherited by the next session, so a review run is typed once.
- PDF file names are Redline-<yyyyMMdd-HHmm>-<LANE>-<SHORTID>.pdf, and every page
  carries a two-line reference footer (tool, build time, session / lane, deployed
  build, commit, reviewer, note). The PDF document info carries the same values in
  Author and Keywords.
- Verification: 159 tests, a headless panel self-test, and a LaunchServices-driven
  end-to-end run that types and clicks the real panel.
This commit is contained in:
Claude Fable 5
2026-10-31 11:32:54 +04:00
parent 542aad296b
commit 02fa791b78
26 changed files with 2780 additions and 279 deletions
+109 -1
View File
@@ -36,6 +36,12 @@ public final class AppModel {
public private(set) var pastSessions: [CaptureSession] = []
public private(set) var statusLine: String?
public private(set) var updateStatus: String?
/// The user's age limit for kept sessions (Settings > Sessions). `.never` until
/// the user chooses otherwise, so every session is saved by default.
public private(set) var retention: SessionRetention = .never
/// How many archived sessions the current age limit would delete right now.
/// Recomputed on launch, on a policy change and after every cleanup.
public private(set) var expiredSessionCount: Int = 0
public private(set) var isCapturing: Bool = false
public private(set) var isSending: Bool = false
/// True while "Start new session" is between its guard and its store call — the
@@ -76,6 +82,9 @@ public final class AppModel {
let ledger: ReturnLedger
let watcher: ReturnWatcher
let updateChecker: UpdateChecker
/// Where the session age limit is persisted. Production is `.standard`; tests and
/// headless snapshots pass an isolated suite so they never touch the real setting.
let retentionDefaults: UserDefaults
public init(
paths: AppSupportPaths,
@@ -85,8 +94,10 @@ public final class AppModel {
hotkeys: HotkeyCenter,
picker: RegionPickerController,
ledger: ReturnLedger,
watcher: ReturnWatcher
watcher: ReturnWatcher,
retentionDefaults: UserDefaults = .standard
) {
self.retentionDefaults = retentionDefaults
self.paths = paths
self.spool = spool
self.composer = composer
@@ -181,6 +192,82 @@ public final class AppModel {
/// `Redline-*.pdf` in the outbox exists on disk.
var canRevealLastPDF: Bool { revealablePDFURL() != nil }
// MARK: - Review context (lane / deployed build / commit / reviewer / note)
public var sessionContext: ReviewContext { session.context }
/// "DEV03-4F2A9C1D" — the readable handle for the open session.
public var sessionLabel: String {
PDFFileName.sessionLabel(lane: session.context.lane, sessionID: session.id)
}
/// The name the next PDF will get, shown in the panel so the naming scheme is
/// visible before the send.
public var nextPDFFileNameHint: String {
PDFFileName.make(builtAt: Date(), lane: session.context.lane, sessionID: session.id)
}
/// Persists one context field change. Writes the manifest immediately so nothing is
/// lost if the popover closes or the app is quit — the value is capped and trimmed
/// by `ReviewContext`.
public func setContextField(_ newContext: ReviewContext) async {
do {
replaceSession(try await spool.setContext(newContext))
} catch {
setStatus((error as? ShotdeckError)?.errorDescription ?? "Could not save the review details.")
}
}
// MARK: - Session retention
/// The sentence the panel footer and Settings show for the current policy.
public var retentionSummary: String { retention.policyDescription }
/// Changes the age limit and persists it. Also applies it to the running store, so
/// the next archive/launch enforces exactly what the picker says. Nothing is
/// deleted here — deleting is an explicit "Clean up now".
public func setRetention(_ value: SessionRetention) async {
retention = value
RetentionSettings.save(value, defaults: retentionDefaults)
await spool.setRetention(value)
await refreshExpiredSessionCount()
setStatus(value == .never ? "Sessions are kept until you delete them." : value.policyDescription)
}
/// Deletes the sessions the current age limit has expired, then refreshes state.
/// Reports the exact number; nothing is deleted under `.never`.
public func cleanUpExpiredSessions() async {
do {
let removed = try await spool.pruneExpiredSessions()
let message: String
if removed.isEmpty {
message = retention == .never
? "Nothing to remove — sessions are kept until you delete them."
: "Nothing to remove — no session is older than \(retention.displayName)."
} else {
message = "Removed \(removed.count) past session\(removed.count == 1 ? "" : "s"). Sent PDFs were not touched."
}
if let refreshProblem = await reloadFromSpool() {
setStatus("\(message) \(refreshProblem)")
} else {
setStatus(message)
}
await refreshExpiredSessionCount()
} catch {
setStatus((error as? ShotdeckError)?.errorDescription ?? "Could not remove expired sessions.")
}
}
/// Counts what the current policy would delete. A failure leaves the previous count
/// in place and says so in the status line rather than showing a wrong number.
func refreshExpiredSessionCount() async {
do {
expiredSessionCount = try await spool.expiredSessionIDs().count
} catch {
setStatus((error as? ShotdeckError)?.errorDescription ?? "Could not read the expired sessions.")
}
}
public func revealLastPDF() {
guard let url = revealablePDFURL() else { return }
NSWorkspace.shared.activateFileViewerSelecting([url])
@@ -221,6 +308,9 @@ public final class AppModel {
}
public func bootstrap() async {
retention = RetentionSettings.load(defaults: retentionDefaults)
await spool.setRetention(retention)
if let data = UserDefaults.standard.data(forKey: CaptureRegion.defaultsKey),
let decoded = try? JSONDecoder().decode(CaptureRegion.self, from: data),
decoded.isStillValid {
@@ -242,11 +332,23 @@ public final class AppModel {
// Empty ledger on first run is not an error.
}
// An age limit chosen in Settings is enforced on every launch. Under `.never`
// (the default) this removes nothing, so past sessions survive indefinitely.
do {
let removed = try await spool.pruneExpiredSessions()
if !removed.isEmpty {
setStatus("Removed \(removed.count) past session\(removed.count == 1 ? "" : "s") older than \(retention.displayName).")
}
} catch {
setStatus((error as? ShotdeckError)?.errorDescription ?? "Could not remove expired sessions.")
}
do {
setPastSessions(try await spool.archivedSessions())
} catch {
setStatus((error as? ShotdeckError)?.errorDescription ?? "Could not load past sessions.")
}
await refreshExpiredSessionCount()
// OneDrive mode: outbox == watch folder, so a freshly written, unmarked PDF must
// never show up as a return; only a document that already carries a mark does.
@@ -465,6 +567,12 @@ public final class AppModel {
problems.append(
"Could not load past sessions: \((error as? ShotdeckError)?.errorDescription ?? error.localizedDescription)")
}
do {
expiredSessionCount = try await spool.expiredSessionIDs().count
} catch {
problems.append(
"Could not read the expired sessions: \((error as? ShotdeckError)?.errorDescription ?? error.localizedDescription)")
}
return problems.isEmpty ? nil : problems.joined(separator: " ")
}