sessions: export as markdown — the sidebar right-click, the route, the practice join (release polish 3)

Owner round (2026-10-06): markdown export of sessions, actioned from
the sidebar context menu — "Export as Markdown…" sits with the
management actions, above Delete (the destructive action stays last).

- GET /api/chats/:id/markdown (404 unknown): the whole session as one
  attachment — text/markdown; charset=utf-8, filename from the slugified
  title ("relational-databases-joins-normal-forms.md", non-ascii titles
  fall back to "session.md"). Both session kinds export.
- The client is a same-origin anchor download; the route's
  Content-Disposition names the file.
- sessionMarkdown's user-facing debut — minted ANCHORLESS (message-id
  anchors are extraction provenance, not learner-facing). The engine
  joins the core barrel (pure, browser-safe) for its first consumer.

The practice join (owner-approved mid-round): practice tool results are
bare acks — statement, outcome, verdict, and the model solution all
live in the assistant's call arguments. The engine now folds them the
same way the view seam folds cards: Problem blocks render the pose's
statement (solution never leaks at pose time); Concluded blocks render
the outcome as prose ("Solved with hints.") + verdict, then the
pose-committed model solution as a blockquoted reveal via
problemInputForConclude (clean-poses-only, its refusal law). A
dismissed problem concludes as "The learner skipped this problem." —
no reveal. Errored poses render nothing (the failed-tool law).

1427 tests (route pins: headers, slug, anchorless body, 404; engine
pins: pose-time statement, the reveal, the dismissal refusal, the
errored-pose silence); tsc, lint, build green.
This commit is contained in:
amosblomqvist
2026-10-06 10:23:16 +03:00
parent 8a7b6b30d9
commit 0b2e4e03fb
6 changed files with 293 additions and 4 deletions
+74
View File
@@ -51,6 +51,22 @@ function result(
} as never;
}
/** An assistant message carrying one toolCall part (the practice join's
* call side — args live HERE, the results are bare acks). */
function callMsg(id: string, callId: string, name: string, args: unknown): LhMessage {
return {
id,
role: "assistant",
content: [{ type: "toolCall", id: callId, name, arguments: args }],
api: "openai-completions",
provider: "openrouter",
model: "test",
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
stopReason: "stop",
timestamp: Date.now(),
} as never;
}
describe("messageMarkdown", () => {
it("renders speaker blocks with the id anchor", () => {
const md = messageMarkdown(user("u1", "teach me the chain rule"), "u1");
@@ -105,6 +121,64 @@ describe("messageMarkdown", () => {
});
});
describe("sessionMarkdown — the practice join (owner-approved, 2026-10-06)", () => {
it("a problem result renders the POSE's statement, joined off the call id", () => {
const messages = [
callMsg("a1", "call-p1", "problem", {
statement: "Push the prior through the learning transition.",
mode: "recall",
target: [],
modelSolution: "Two phases, in forced order.",
}),
result("r1", "call-p1", "problem", null),
];
const md = sessionMarkdown(messages);
assert.ok(md.includes("### Problem"), "the Problem heading renders");
assert.ok(md.includes("Push the prior through the learning transition."), "the statement renders");
assert.ok(!md.includes("Two phases"), "the solution does NOT leak at pose time");
});
it("a conclude result renders outcome + verdict + the pose's model solution as the reveal", () => {
const messages = [
callMsg("a1", "call-p1", "problem", {
statement: "Compute P(knows | correct).",
modelSolution: "Bayes, twice over.",
}),
result("r1", "call-p1", "problem", null),
callMsg("a2", "call-c1", "conclude", { outcome: "solvedWithHints", verdict: "Right answer, one nudge." }),
result("r2", "call-c1", "conclude", null),
];
const md = sessionMarkdown(messages);
assert.ok(md.includes("### Concluded"), "the Concluded heading renders");
assert.ok(md.includes("**Solved with hints.** Right answer, one nudge."), "outcome reads as prose, then the verdict");
assert.ok(md.includes("> **Model solution**"), "the reveal block quotes the committed solution");
assert.ok(md.includes("> Bayes, twice over."), "the solution text carries the blockquote prefix");
});
it("a dismissed problem concludes without leaking the model solution", () => {
const messages = [
callMsg("a1", "call-p1", "problem", { statement: "Derive the posterior.", modelSolution: "The answer." }),
result("r1", "call-p1", "problem", null),
callMsg("a2", "call-c1", "conclude", { outcome: "dismissed" }),
result("r2", "call-c1", "conclude", null),
];
const md = sessionMarkdown(messages);
assert.ok(md.includes("The learner skipped this problem."), "the dismissal reads as a skip");
assert.ok(!md.includes("The answer."), "no reveal on a skipped problem — the card's refusal law");
assert.ok(md.includes("### Problem"), "the statement still renders (it was on screen)");
});
it("an errored pose renders nothing — the failed-tool law holds for practice", () => {
const messages = [
callMsg("a1", "call-p1", "problem", { statement: "Never acked." }),
result("r1", "call-p1", "problem", null, true),
];
const md = sessionMarkdown(messages);
assert.ok(!md.includes("### Problem"), "no problem block");
assert.ok(!md.includes("Never acked."), "no statement either — the pose never happened");
});
});
describe("sessionMarkdown", () => {
it("assembles blocks in order under the title header, skipping nothings", () => {
const md = sessionMarkdown([user("u1", "go"), teacher("a1", "First."), user("u2", " ")], { title: "My session" });