Grade every waiting lesson with a separate check before the lessons card asks for approval

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-27 23:19:42 -08:00
1 parent b06a4cb4ab
commit 1345aa0a95
36 files changed
+2812 -139

No files matched your search

+70
View File
@@ -41,6 +41,7 @@ import {
lessonDecision,
lessonMissing,
lessonEditStart,
lessonEditField,
lessonReworded,
startsTicked,
} from "../answers.js";
@@ -1825,6 +1826,8 @@ function buildLessons(card, item, staged) {
};
if (item.opening) card.appendChild(el("p", "lessons-opening", item.opening));
const how = lessonsCheckedLine(item.checked);
if (how) card.appendChild(el("p", "lessons-checked", how));
const list = el("ul", "lessons-rows");
for (const row of item.rows || []) list.appendChild(lessonRow(item, row, draft, commit));
card.appendChild(list);
@@ -1850,6 +1853,61 @@ function buildLessons(card, item, staged) {
commentField(card, item, "Anything to add?");
}
const plural = (n, one) => `${n} ${one}${n === 1 ? "" : "s"}`;
// The card's one how-checked line, or "" when the card carries none.
function lessonsCheckedLine(checked) {
if (!checked || typeof checked !== "object") return "";
if (typeof checked.failed === "string") return `The check could not run: ${checked.failed}`;
const by = checked.how === "helper" ? "a fresh helper that could only read" : checked.how === "same-agent" ? "the same agent that noted them" : "";
if (!by) return "";
return `Checked ${plural(checked.count, "lesson")} in ${plural(checked.seconds, "second")} by ${by}`;
}
// What the check found on one row: each Fail then each Doubt on a line of its
// own, then a quiet pass line. Nothing at all for a row the check never saw.
function lessonCheckLines(li, item, row, draft, commit) {
const check = row.check;
if (!check || typeof check !== "object") return;
const fails = Array.isArray(check.fails) ? check.fails : [];
const doubts = Array.isArray(check.doubts) ? check.doubts : [];
const box = el("div", "lesson-checks");
for (const [word, list] of [["Fail", fails], ["Doubt", doubts]]) {
for (const e of list) {
const line = el("p", "lesson-check " + word.toLowerCase());
line.appendChild(el("span", "lesson-check-word", word));
line.appendChild(document.createTextNode(` ${e.test}: ${e.reason}`));
box.appendChild(line);
}
}
const found = fails.length + doubts.length;
// With all four tests flagged there is nothing left that passed.
if (!found || check.passed !== 0) box.appendChild(el("p", "lesson-check-pass", found && Number.isInteger(check.passed) ? `Passed the other ${check.passed}` : "Passed the check"));
if (check.suggestion) {
const sug = el("p", "lesson-suggestion");
sug.appendChild(el("span", null, `Suggested wording: ${check.suggestion}`));
const use = el("button", "btn sm", "Use this wording");
use.type = "button";
use.id = `lesson-use-${item.id}-${row.id}`;
use.addEventListener("click", () => {
// Picks Reword and opens it on the suggestion; what is sent is a
// normal Reword of whatever the person leaves in the box.
const word = LESSON_EDIT_WORD[row.type];
draft.picks[row.id] = (row.choices || []).includes(word) ? word : "Reword";
draft.leaveAll = false;
// Only the words change: a Why or Area already typed stays.
const start = lessonEditStart(row, check.suggestion);
const key = lessonEditField(row).key;
draft.edits[row.id] = { ...(draft.edits[row.id] || start), [key]: start[key] };
commit(true);
document.getElementById(use.id)?.focus();
});
sug.appendChild(use);
box.appendChild(sug);
}
li.appendChild(box);
}
// One waiting item: what it is, why, where it came from, and its own choices.
function lessonRow(item, row, draft, commit) {
const pick = draft.picks[row.id] || "";
@@ -1868,6 +1926,16 @@ function lessonRow(item, row, draft, commit) {
pair.appendChild(el("p", "lessons-fix-label", "After"));
pair.appendChild(el("blockquote", "lessons-after", row.after || row.text || ""));
li.appendChild(pair);
} else if (row.type === "tidy") {
li.appendChild(el("p", "lessons-text", row.text));
// A merge or reword replaces lessons already saved: show them, so the change reads whole.
const was = (row.targets || []).filter((t) => t && t.text);
if ((row.change === "merge" || row.change === "reword") && was.length) {
li.appendChild(el("p", "lessons-heading", row.change === "merge" ? "It replaces these lessons:" : "It replaces this wording:"));
const ul = el("ul", "lessons-become");
for (const t of was) ul.appendChild(el("li", null, t.text));
li.appendChild(ul);
}
} else if (row.type === "log-line") {
li.appendChild(el("p", "lessons-text", row.text));
const become = (row.lessons || []).filter(Boolean);
@@ -1885,6 +1953,8 @@ function lessonRow(item, row, draft, commit) {
// The doubt's and the fix's origin repeat what is drawn above them.
if (row.origin && !["doubt", "gone", "agents-fix"].includes(row.type)) li.appendChild(el("p", "lessons-origin", row.origin));
if (row.note) li.appendChild(el("p", "lessons-note", row.note));
if (row.tag) li.appendChild(el("p", "lessons-tag", row.tag));
lessonCheckLines(li, item, row, draft, commit);
for (const f of Array.isArray(row.flags) ? row.flags : []) {
const what = FLAG_WORDS[f && f.pattern] || "something personal";
+25 -15
View File
@@ -12,6 +12,7 @@ import {
loopDriving,
learningBusyNow,
LEARNING_BUSY_MS,
learningBusyLimit,
} from "../selectors.js";
import { whenTitle, whenWords } from "../labels.js";
import { hooks, page, redraw, saveNow } from "../state.js";
@@ -49,6 +50,7 @@ import {
learningAction,
learningReply,
learningBannerText,
learningNearCapText,
noteSwitchShows,
noteTabEmpty,
defaultNoteTicks,
@@ -1440,16 +1442,17 @@ function learningBanner(sc) {
const busy = learningBusyNow(state);
// Review pressed, or the choices just saved: the banner says what the agent
// is doing, in the same place, so the wait is never silent.
const working = busy && (busy.what === "review" || busy.what === "decide") ? busy : null;
const working = busy && (busy.what === "review" || busy.what === "decide" || busy.what === "tidy") ? busy : null;
const text = learningBannerText(sc && sc.learning);
if (!text && !working) return null;
const nudge = learningNearCapText(sc && sc.learning);
if (!text && !nudge && !working) return null;
const banner = el("div", "callout is-info dash-nudge learn-banner");
banner.setAttribute("role", "status");
if (working) {
banner.classList.add("is-busy");
banner.setAttribute("aria-busy", "true");
const line = el("span", "dash-nudge-text learn-busy");
line.append(el("span", "spinner tiny"), document.createTextNode(working.what === "review" ? "Opening lessons…" : "Saving your lessons…"));
line.append(el("span", "spinner tiny"), document.createTextNode(working.what === "review" ? "Opening lessons…" : working.what === "tidy" ? "Looking over your lessons…" : "Saving your lessons…"));
banner.appendChild(line);
if (working.what === "review") {
const review = el("button", "btn sm primary", "Review");
@@ -1459,14 +1462,21 @@ function learningBanner(sc) {
}
return banner;
}
banner.appendChild(el("span", "dash-nudge-text", text));
const review = el("button", "btn sm primary", "Review");
review.type = "button";
const why = hooks.learningBlocked();
review.disabled = Boolean(why);
if (why) review.title = why;
review.addEventListener("click", () => sendLearning("review"));
banner.appendChild(review);
// Each line is its own row with its own button: lessons waiting, and lessons near their limit.
const row = (words, label, what, primary) => {
const r = el("div", "learn-banner-row");
r.appendChild(el("span", "dash-nudge-text", words));
const b = el("button", "btn sm" + (primary ? " primary" : ""), label);
b.type = "button";
b.disabled = Boolean(why);
if (why) b.title = why;
b.addEventListener("click", () => sendLearning(what));
r.appendChild(b);
banner.appendChild(r);
};
if (text) row(text, "Review", "review", true);
if (nudge) row(nudge, "Tidy up", "tidy", !text);
return banner;
}
@@ -1478,7 +1488,7 @@ function learningBanner(sc) {
function watchLearningBusy(busy) {
const timer = setInterval(() => {
if (page.learningBusy !== busy) return clearInterval(timer);
if (Date.now() - busy.at > LEARNING_BUSY_MS) {
if (Date.now() - busy.at > learningBusyLimit(busy)) {
clearInterval(timer);
page.learningBusy = null;
redraw();
@@ -1493,8 +1503,8 @@ function watchLearningBusy(busy) {
if (!busy.seen && Date.now() - busy.at < 3000) return;
clearInterval(timer);
page.learningBusy = null;
if (busy.what === "review") {
const none = hooks.banner("No lessons are waiting any more.", "info");
if (busy.what === "review" || busy.what === "tidy") {
const none = hooks.banner(busy.what === "tidy" ? "Nothing to tidy up right now." : "No lessons are waiting any more.", "info");
setTimeout(() => hooks.clearBanner(none), 4000);
}
redraw();
@@ -1523,13 +1533,13 @@ export async function sendLearning(what) {
// the agent answers.
const sc = scan();
const from = sc && sc.learning && sc.learning.state;
if ((what === "switch-on" || what === "switch-off" || what === "review") && from) {
if ((what === "switch-on" || what === "switch-off" || what === "review" || what === "tidy") && from) {
const busy = { what, from, at: Date.now(), seen: false };
page.learningBusy = busy;
redraw();
// One redraw at the limit, so an action the agent never answers stops spinning.
setTimeout(redraw, LEARNING_BUSY_MS + 500);
if (what === "review") watchLearningBusy(busy);
if (what === "review" || what === "tidy") watchLearningBusy(busy);
}
try {
const res = await fetch("/submit", {
+46 -4
View File
@@ -155,6 +155,7 @@ function moreMenu() {
};
list.append(
item("Switch off for this project", () => hooks.sendLearning("switch-off")),
item("Tidy up lessons", () => hooks.sendLearning("tidy")),
item("Move the failure log into lessons", () => hooks.sendLearning("move-log")),
item("Remove all lessons…", () => openRemoveConfirm(), " is-danger")
);
@@ -504,6 +505,49 @@ function openRemoveConfirm() {
dlg.showModal();
}
// The same slug lessons.mjs gives a lesson's heading (slugOf), so a lesson's id
// is `<file name without .md>/<slug>` here as there.
function slugOf(text) {
return String(text || "")
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "")
.slice(0, 60)
.replace(/-+$/, "");
}
/** "Followed 12 times, last on Nov 20", "Followed once, last on Nov 20" or "Not followed yet". */
function followLine(entry) {
if (!entry || !(entry.count > 0)) return "Not followed yet";
const last = shortDate(entry.last);
const times = entry.count === 1 ? "once" : `${entry.count} times`;
return `Followed ${times}${last ? ", last on " + last : ""}`;
}
// One file's text through the page's Markdown, a quiet follow line under each
// lesson's heading (a "### " line).
function appendLessonText(part, file, follows) {
const area = String(file.name || "").replace(/\.md$/i, "");
const chunks = String(file.text || "").split(/\r?\n(?=### )/);
const add = (text) => {
if (!text.trim()) return;
const div = el("div", "md");
mdInto(div, text);
part.appendChild(div);
};
for (const chunk of chunks) {
const m = /^### (.+?)\s*(?:\r?\n|$)/.exec(chunk);
if (!m) {
add(chunk);
continue;
}
add(m[0]);
const line = el("p", "learn-follow", followLine(follows[`${area}/${slugOf(m[1])}`]));
part.appendChild(line);
add(chunk.slice(m[0].length));
}
}
// Open the lessons: every lesson file, read-only, through the page's Markdown.
async function openLessons() {
let dlg = document.getElementById("learn-lessons-modal");
@@ -525,7 +569,7 @@ async function openLessons() {
try {
const res = await fetch("/learning/lessons", { cache: "no-store" });
if (res.status !== 200) throw new Error(String(res.status));
const { files } = await res.json();
const { files, follows } = await res.json();
body.replaceChildren();
if (!files || !files.length) {
body.appendChild(el("p", "md-p", "No lessons are saved in this project yet."));
@@ -534,9 +578,7 @@ async function openLessons() {
for (const f of files) {
const part = el("section", "learn-lessons-file");
part.appendChild(el("p", "learn-lessons-name", f.name));
const text = el("div", "md");
mdInto(text, f.text || "");
part.appendChild(text);
appendLessonText(part, f, follows || {});
body.appendChild(part);
}
} catch {