feat(mentor): dismiss & decay Learnings (#27)
CI / ci (push) Canceled after 0s

Dismissed patterns fade instead of haunting. Dismissing a Learning
stamps decay state into its own frontmatter (dismissed date,
dismiss_count, dismissed_at_pass — every other line preserved
verbatim, hand-editable, git-diffable) and hides it from every pass:
query_learnings serves only visible learnings. Resurfacing is an
output clock, not a calendar — a per-type counter under
<home>/learnings/pass-counts.json, ticked by every completed pass
(cancels and refusals never advance, so never tick), returns the
Learning after 10 subsequent passes of its content type; each
re-dismiss doubles the interval (10→20→40…), and a per-Learning
resurface_after frontmatter key overrides the base. Hand-edited decay
state reads leniently: garbage means visible, absurd values clamp, a
hand-edit can never trap the pane or the rail. The Learnings pane
rows dismiss (no confirm — low-stakes, self-healing), dim while
hidden with a resurfaces-in-N countdown, and foreign unclocked type
dirs neither offer Dismiss nor show a verdict — their clock can never
advance. Two review waves (two-axis implement review + three-reviewer
finish-audit: security clean) produced five fixes, each pinned or
noted in the session log.

Closes #27
This commit is contained in:
AlexKucera committed 2026-10-21 14:19:43 +02:00
1 parent 0e5c28ad95
commit 78c8844184
15 files changed
+911 -66

No files matched your search

+120 -31
View File
@@ -17,20 +17,27 @@ import SwiftUI
nonisolated enum LearningsIndex {
/// One Learning on disk. `pattern` falls back to the filename
/// (extension and leading date + hyphen stripped); `problem`/
/// `promoted` omit when the frontmatter key is absent.
/// `promoted` omit when the frontmatter key is absent. #27:
/// `resurfacesIn` is the decay verdict — nil = visible now,
/// positive = passes until resurface. `currentPass: nil` = an
/// unclocked (foreign) type dir: never a verdict, so the pane
/// cannot show a countdown that could never advance (#27 audit —
/// the store refuses to dismiss there for the same reason).
nonisolated struct Entry: Identifiable {
/// The file name (with extension) — unique within its group.
let id: String
let pattern: String
var problem: String?
var promoted: String?
let resurfacesIn: Int?
init(file: String, text: String) {
init(file: String, text: String, currentPass: Int? = nil) {
let frontmatter = Post.frontmatter(of: text)
id = file
pattern = frontmatter["pattern"] ?? Self.pattern(fromFilename: file)
problem = frontmatter["problem"].flatMap { $0.isEmpty ? nil : $0 }
promoted = frontmatter["promoted"].flatMap { $0.isEmpty ? nil : $0 }
resurfacesIn = currentPass.flatMap { LearningDecay.passesHidden(frontmatter, currentPass: $0) }
}
/// "20261021-fold-the-ladder.md" → "fold-the-ladder".
@@ -60,6 +67,7 @@ nonisolated enum LearningsIndex {
/// empty; hidden dirs/files, non-.md, loose files, and unreadable
/// files never surface. Same skip posture as `Post.scan`.
static func scan(home: URL) -> [Group] {
let counts = LearningDecay.passCounts(home: home) // #27: the pass clock feeds the decay verdict
let root = home.appendingPathComponent("learnings", isDirectory: true)
guard let typeDirs = try? FileManager.default.contentsOfDirectory(
at: root,
@@ -70,6 +78,11 @@ nonisolated enum LearningsIndex {
guard isPlainDirectory(dir), !dir.lastPathComponent.hasPrefix(".") else { continue }
guard let files = try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)
else { continue }
// #27: the pass clock feeds the decay verdict — clocked dirs
// carry their count, foreign dirs nil (no verdict; Entry's audit).
let currentPass = LearningDecay.isClocked(dir.lastPathComponent)
? counts[dir.lastPathComponent] ?? 0
: nil
let entries = files
.filter {
!$0.lastPathComponent.hasPrefix(".")
@@ -81,7 +94,7 @@ nonisolated enum LearningsIndex {
// local-Home threat only); isSymbolicLink + size cap if a
// hostile Home ever matters.
(try? String(contentsOf: file, encoding: .utf8))
.map { Entry(file: file.lastPathComponent, text: $0) }
.map { Entry(file: file.lastPathComponent, text: $0, currentPass: currentPass) }
}
guard !entries.isEmpty else { continue }
groups.append(Group(
@@ -103,22 +116,41 @@ nonisolated enum LearningsIndex {
}
}
/// #26: the Learnings pane — list-only, like the Archive pane.
/// #26: the Learnings pane — list-only, like the Archive pane. #27:
/// rows dismiss learnings (decay state stamps into the file's
/// frontmatter; the pane re-scans after).
struct LearningsPaneView: View {
let home: URL
@State private var groups: [LearningsIndex.Group] = []
@State private var lastError: String?
/// The home the LAST-enqueued scan is for — a dismiss Task captures the
/// OLD home, and Task.isCancelled cannot reach it, so the assignment
/// gates on this instead (#27 audit).
@State private var scannedHome: URL?
var body: some View {
VStack(spacing: 0) {
// No New button — learnings arrive by promotion, not creation.
PaneHeader(title: "Learnings")
if let lastError {
Text(lastError)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(.orange)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 14)
.padding(.bottom, 6)
}
if groups.isEmpty {
emptyState
} else {
ScrollView {
LazyVStack(alignment: .leading, spacing: 4) {
ForEach(groups, id: \.type) { group in
LearningSection(group: group)
LearningSection(group: group) { entry in
dismiss(entry, in: group.type)
}
}
}
.padding(.bottom, 12)
@@ -135,14 +167,44 @@ struct LearningsPaneView: View {
// goes stale until the pane is re-entered. Add a watcher if that
// ever annoys.
.task(id: home) {
let scanned = await Task.detached(priority: .userInitiated) {
LearningsIndex.scan(home: home)
}.value
// A home repoint cancels the awaiting task, but a detached
// scan is not cancellation-aware — a slow OLD scan must not
// overwrite the new Home's groups after it resumes.
guard !Task.isCancelled else { return }
groups = scanned
scannedHome = home
await rescan(home)
}
}
/// The detached scan + cancellation-gated assignment both entries into
/// the list share (.task and post-dismiss refresh).
private func rescan(_ target: URL) async {
let scanned = await Task.detached(priority: .userInitiated) {
LearningsIndex.scan(home: target)
}.value
// A home repoint cancels the awaiting .task, but a detached scan is
// not cancellation-aware — and a dismiss Task holds the OLD home
// unreached by that cancellation. Either way: a scan that is not for
// the Home we last entered must not overwrite its groups.
guard !Task.isCancelled, scannedHome == target else { return }
groups = scanned
lastError = nil // a successful rescan clears the strip
}
/// #27: dismissal is low-stakes and self-healing (it decays back) —
/// no confirm sheet. Failures land in the orange strip, never an alert.
private func dismiss(_ entry: LearningsIndex.Entry, in typeDir: String) {
Task {
var failure: String?
do {
try await Task.detached(priority: .userInitiated) {
try LearningDecay.dismiss(home: home, typeDir: typeDir, file: entry.id)
}.value
} catch {
failure = error.localizedDescription
}
await rescan(home)
// #27 review: a repointed Home must not inherit the old
// Home's failure — the same gate rescan enforces for groups.
if scannedHome == home {
lastError = failure
}
}
}
@@ -165,14 +227,19 @@ struct LearningsPaneView: View {
/// own body, not uneven outer-row content).
private struct LearningSection: View {
let group: LearningsIndex.Group
let onDismiss: (LearningsIndex.Entry) -> Void
var body: some View {
SectionHeadView(type: group.type, count: group.entries.count)
.padding(.top, 16)
.padding(.horizontal, 14)
.padding(.bottom, 6)
// Foreign (unclocked) dirs never offer Dismiss — their clock
// reads 0 forever, a dismiss there would hide permanently (the
// store refuses too; #27 audit).
let dismissable = LearningDecay.isClocked(group.type)
ForEach(group.entries) { entry in
LearningRow(entry: entry)
LearningRow(entry: entry, onDismiss: dismissable ? { onDismiss(entry) } : nil)
}
}
}
@@ -199,36 +266,58 @@ private struct SectionHeadView: View {
}
/// One Learning row: pattern as the title, problem as the secondary
/// line, promoted date + file name in mono-11.
/// line, promoted date + file name in mono-11. #27: visible rows offer
/// a mono-11 Dismiss (the Archive pane's Unarchive voice); dismissed
/// rows dim (closed-card voice) and count down their resurface.
private struct LearningRow: View {
let entry: LearningsIndex.Entry
/// nil = foreign dir, never decay-clocked (#27 audit).
let onDismiss: (() -> Void)?
var body: some View {
VStack(alignment: .leading, spacing: 5) {
Text(entry.pattern)
.font(.callout.weight(.semibold))
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
if let problem = entry.problem {
Text(problem)
.font(.callout)
HStack(alignment: .top) {
VStack(alignment: .leading, spacing: 5) {
Text(entry.pattern)
.font(.callout.weight(.semibold))
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
if let problem = entry.problem {
Text(problem)
.font(.callout)
.foregroundStyle(DesignTokens.muted)
.lineLimit(2)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
}
Text(metaLine)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
.lineLimit(2)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
}
Text(metaLine)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
.lineLimit(1)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
if entry.resurfacesIn == nil, let onDismiss {
Button("Dismiss", action: onDismiss)
.buttonStyle(.plain)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
.help("Hide this learning from passes until it resurfaces")
.accessibilityLabel("Dismiss \(entry.pattern)")
}
}
.padding(10)
.opacity(entry.resurfacesIn == nil ? 1 : 0.45)
}
private var metaLine: String {
if let remaining = entry.resurfacesIn {
return "dismissed · resurfaces in \(remaining) \(remaining == 1 ? "pass" : "passes") · " + metaTail
}
return metaTail
}
private var metaTail: String {
entry.promoted.map { "promoted · \($0) · \(entry.id)" } ?? entry.id
}
}