feat(sessions): new post from session & drills (S2.7-8) (#30)
CI / ci (push) Canceled after 0s

A freeform Session births a Post. "New Post" in the Sessions pane
opens an ask-before sheet prefilled with the session's last mentor
reply (editable, G9); ONE confirm materializes the piece under
projects/posts/ stamped origin: ai - immutable frontmatter, never
stats-eligible - and opens it in Posts; a failed write keeps the
sheet up with the reason inline and the draft intact. writeNew
gains an origin parameter (additive default .writer, every existing
caller compiles).

"New Brainstorm" is a session started with an opener prompt - no new
machinery (beginBrainstormSession over the normal new+prompt rails).
The freeform composer (input, liveness, Stop) moved from
MentorPaneView into the shared SessionTranscriptView: one
conversation surface, two panes - the Sessions pane stops being a
read-only transcript. Drill offers (S2.8) wire as one pass-persona
section (v1.4): the seeded micro-drill doctrine owns the motivation
moment, the two-minute bound, the single shelf, and the vanish -
session flavor, no machinery beyond S2.3.

Resume now speaks the real adapter wire. Live-probed against the
engine: 0.21.5 answers session/load with the id at
_meta.hermes.sessionProvenance.acpSessionId and NO top-level
sessionId - resumeSession accepts both shapes; every prior
real-engine resume misread as "engine does not know this session".
The fake ACP server answers load in the real shape (a
loadReplayLegacy knob pins the older contract), so the whole resume
suite guards the live wire.

New pins: draft-candidate extraction (last mentor row wins; thought/
card/research/raw lanes and refusal/connection rows never prefill),
ai-stamp write + immutability through overwrite, brainstorm roster +
labeled opener, legacy-shape resume. Finish-audit (three
reviewers, parallel) converged on the confirm() dismissal that
destroyed the edited draft on a failed write - fixed with
sheet-local error state (stale-refusal re-show dies with the
presentation) - and surfaced the missing brainstorm and legacy-shape
pins; the resume fix was flip-verified (removing the provenance
fallback fails the provenance pins). Full suite green (379 tests).

Closes #30
This commit is contained in:
AlexKucera committed 2026-10-22 17:04:12 +02:00
1 parent b02d36bb5d
commit ae234350ef
16 files changed
+521 -128

No files matched your search

+124 -48
View File
@@ -23,6 +23,10 @@ struct SessionsPaneView: View {
var binaryPath: String?
/// #29 (S8.3): the Home a kept research note lands in.
var home: URL?
/// #30 (S2.7): the staged New Post from Session ask — the editable
/// draft candidate; nil = no sheet. The sheet is pane-local: this pane
/// holds posts + home, so no WorkspaceView staging is needed.
@State private var sessionDraft: String?
var body: some View {
VStack(spacing: 0) {
@@ -42,6 +46,24 @@ struct SessionsPaneView: View {
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(DesignTokens.background)
// #30 (S2.7): the ask prefills the candidate; dismissal is the
// cancel (nothing materializes).
.sheet(isPresented: Binding(
get: { sessionDraft != nil },
set: {
if !$0 {
sessionDraft = nil
}
},
)) {
NewPostFromSessionSheet(
draft: Binding(
get: { sessionDraft ?? "" },
set: { sessionDraft = $0 },
),
create: { newPostFromSession() },
)
}
// Same lazy lifecycle as MentorPaneView: idempotent open — whichever
// surface the writer reaches first spawns the engine.
.task {
@@ -93,10 +115,8 @@ struct SessionsPaneView: View {
VStack(spacing: 0) {
actionsRow
Divider()
if model.isStreaming {
livenessRow
Divider()
}
// #30: transcript + freeform input + liveness ride the shared
// surface (the composer lives in SessionTranscriptView).
SessionTranscriptView(model: model, showsDebugRows: false, home: home)
}
}
@@ -118,6 +138,18 @@ struct SessionsPaneView: View {
.help(model.versionNote ?? version)
}
Spacer()
// #30 (S2.7): the draft candidate prefills the ask sheet (empty
// transcript → empty prefill — the sheet still works). Reads
// in-memory transcript rows — engine-free, never gated on
// canStartSession (a crashed engine must not strand the draft).
Button("New Post") {
sessionDraft = model.draftCandidateText() ?? ""
}
.buttonStyle(.bordered)
.controlSize(.small)
.font(.system(size: 11, design: DesignTokens.mono))
.help("Start a Post from this session's draft candidate")
.accessibilityLabel("New Post from Session")
Button("Fork") {
Task { _ = await model.forkActiveSession() }
}
@@ -141,28 +173,6 @@ struct SessionsPaneView: View {
.padding(.vertical, 8)
}
/// Liveness (the Mentor pane's #23 posture, no input bar here): silent
/// tool work must not read as dead, Stop is the pane's Cancel affordance.
private var livenessRow: some View {
HStack(spacing: 8) {
ProgressView()
.controlSize(.small)
Text("Mentor is working — Stop to interrupt.")
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
Spacer()
Button("Stop") {
model.stopStreaming()
}
.buttonStyle(.bordered)
.controlSize(.small)
.accessibilityLabel("Stop the mentor")
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
}
/// No second restart flow — the Mentor pane owns Restart (map decision).
private var crashedNote: some View {
VStack(spacing: 8) {
@@ -181,39 +191,48 @@ struct SessionsPaneView: View {
// MARK: New Session control
@ViewBuilder
/// #30 (S2.7): one Menu in both states — New Brainstorm joined the
/// roster of actions, so the plain-button branch (single action) is
/// gone. Brainstorm rides the same piece association when Posts has
/// an open piece.
private var newSessionControl: some View {
if let piece = Self.openPiece(in: posts) {
Menu {
Button("New Session") { createSession(associatedWith: nil) }
let piece = Self.openPiece(in: posts)
return Menu {
Button("New Session") { createSession(associatedWith: nil) }
if let piece {
Button("New Session on \(piece.title)") { createSession(associatedWith: piece.title) }
} label: {
Image(systemName: "plus")
}
.menuStyle(.button)
.buttonStyle(.plain)
.foregroundStyle(DesignTokens.muted)
.disabled(!model.canStartSession)
.help("New Session")
.accessibilityLabel("New Session")
} else {
Button {
createSession(associatedWith: nil)
} label: {
Image(systemName: "plus")
Button("New Brainstorm") {
Task { _ = await model.beginBrainstormSession(pieceTitle: piece?.title) }
}
.buttonStyle(.plain)
.foregroundStyle(DesignTokens.muted)
.disabled(!model.canStartSession)
.help("New Session")
.accessibilityLabel("New Session")
} label: {
Image(systemName: "plus")
}
.menuStyle(.button)
.buttonStyle(.plain)
.foregroundStyle(DesignTokens.muted)
.disabled(!model.canStartSession)
.help("New Session")
.accessibilityLabel("New Session")
}
private func createSession(associatedWith title: String?) {
Task { _ = await model.newSession(name: "New Session", pieceTitle: title) }
}
/// #30 (S2.7): the ask's confirm — materialize + open in Posts. Returns
/// the failure reason for the sheet's inline refusal (nil = written +
/// opened); a failed write keeps the sheet up with the draft intact
/// (ApplyAskSheet posture — the edited draft is never destroyed).
private func newPostFromSession() -> String? {
guard let posts, let home else { return "No library to write into." }
if posts.newPostFromSession(body: sessionDraft ?? "", home: home) == nil {
return posts.lastError ?? "The piece couldn't be written."
}
sessionDraft = nil
return nil
}
/// The open piece in Posts — the association target for the New Session
/// choice (nil = free-floating only). Static + posts-coupled: test-pinned.
static func openPiece(in posts: PostsModel?) -> Post? {
@@ -296,3 +315,60 @@ private struct SessionEntryRow: View {
return parts.joined(separator: " · ")
}
}
/// #30 (S2.7): the New Post from Session ask — the draft candidate
/// (the session's last mentor reply) prefills and stays editable.
/// Confirm materializes the piece stamped `origin: ai` — never
/// stats-eligible — and opens it in Posts; Cancel writes nothing.
/// The error line is SHEET-LOCAL state: it dies with the presentation,
/// so a re-opened ask never shows a stale refusal.
private struct NewPostFromSessionSheet: View {
@Binding var draft: String
/// The confirm's failure reason — a failed write keeps the sheet up,
/// draft intact; dismissal itself is the cancel.
@State private var createError: String?
let create: () -> String?
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text("New Post from Session")
.font(.system(size: 15, weight: .semibold, design: DesignTokens.serif))
Text(
"The draft starts from the session's last mentor reply. It lands in your library stamped origin: ai — never counted as your writing.",
)
.font(.callout)
.foregroundStyle(DesignTokens.muted)
TextEditor(text: $draft)
.font(.system(size: 13, design: DesignTokens.serif))
.frame(width: 380, height: 160)
.overlay(
RoundedRectangle(cornerRadius: DesignTokens.radius)
.strokeBorder(DesignTokens.border),
)
if let createError {
Text(createError)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(.orange)
}
HStack {
Spacer()
// Dismissal itself is the cancel (binding set(false) →
// sessionDraft = nil) — Esc and ⌘. land here too.
Button("Cancel", role: .cancel) {}
.keyboardShortcut(.cancelAction)
Button("Create Post", action: confirm)
.keyboardShortcut(.defaultAction)
.disabled(draft.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
}
}
.padding(20)
.frame(width: 420)
}
/// No dismiss() here — success closes the sheet via the pane's
/// `sessionDraft = nil` (the binding); failure keeps it up with the
/// inline reason and the edited draft intact.
private func confirm() {
createError = create()
}
}