mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-30 23:59:20 -08:00
Quiet the Tidy up banner once a tidy-up finds nothing
AI Assisted
This commit is contained in:
1 parent
634e4df10e
commit
ee530d36c0
9 files changed
+148
-8
No files matched your search
@@ -126,6 +126,49 @@ export function writePause(cwd, paused, now) {
|
||||
return { paused: true, since };
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------- the tidy-up quiet */
|
||||
|
||||
/**
|
||||
* A short fingerprint of the project's saved lessons (every `.md` file's name
|
||||
* and text under `.plan2code/lessons/`, in name order). It changes when a
|
||||
* lesson is added, merged, reworded or retired, and only then. Reads only; a
|
||||
* missing folder or an unreadable file counts as empty.
|
||||
*/
|
||||
export function lessonsFingerprint(cwd = process.cwd()) {
|
||||
const dir = path.join(worktreeOf(cwd), ".plan2code", "lessons");
|
||||
const hash = crypto.createHash("sha1");
|
||||
let names = [];
|
||||
try {
|
||||
names = fs.readdirSync(dir).filter((n) => n.endsWith(".md")).sort();
|
||||
} catch {}
|
||||
for (const name of names) {
|
||||
let text = "";
|
||||
try {
|
||||
text = fs.readFileSync(path.join(dir, name), "utf8");
|
||||
} catch {}
|
||||
hash.update(`${name}\0${text}\0`);
|
||||
}
|
||||
return hash.digest("hex");
|
||||
}
|
||||
|
||||
/** The lessons a tidy-up already looked over (or the person waved off), or null. */
|
||||
export function readTidyQuiet(cwd) {
|
||||
const data = readJson(path.join(storeDir(cwd), "tidy-quiet.json"));
|
||||
if (!data || typeof data !== "object" || data.format !== FORMAT || typeof data.fingerprint !== "string") return null;
|
||||
return data.fingerprint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remember that the lessons as they are now need no tidy-up nudge: a tidy-up
|
||||
* proposed nothing, or the person pressed Not now. `now` pins the clock.
|
||||
*/
|
||||
export function writeTidyQuiet(cwd, now) {
|
||||
const fingerprint = lessonsFingerprint(cwd);
|
||||
ensureDir(storeDir(cwd));
|
||||
writeJsonAtomic(path.join(storeDir(cwd), "tidy-quiet.json"), { format: FORMAT, fingerprint, at: toDate(now).toISOString() });
|
||||
return fingerprint;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ follow counts */
|
||||
|
||||
/** One line per "Following a lesson" moment, on this machine only. */
|
||||
@@ -330,6 +373,8 @@ export function learningScan(cwd = process.cwd(), { now, read = learningState }
|
||||
pausedSince: s.pausedSince || null,
|
||||
waiting: Number.isInteger(waiting.count) ? waiting.count : 0,
|
||||
oldestDays: Number.isInteger(waiting.oldestDays) ? waiting.oldestDays : 0,
|
||||
nearCap: s.state === "on" && lessonsNearCap(cwd),
|
||||
// Crowded, and not already looked over: a tidy-up that proposed nothing (or
|
||||
// a Not now) quiets the nudge until the saved lessons change.
|
||||
nearCap: s.state === "on" && lessonsNearCap(cwd) && readTidyQuiet(cwd) !== lessonsFingerprint(cwd),
|
||||
};
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
} from "../selectors.js";
|
||||
import { whenTitle, whenWords } from "../labels.js";
|
||||
import { hooks, page, redraw, saveNow } from "../state.js";
|
||||
import { postJson } from "../net.js";
|
||||
import { el, approvalTip, mdInto, releasesLink, copyButton, lightDismiss, icon, SVG_NS } from "../render.js";
|
||||
import { needsRoleNudge, ROLE_NOT_SET } from "../starters.js";
|
||||
import { looks, saveLooks } from "../looks.js";
|
||||
@@ -1483,6 +1484,15 @@ function learningBanner(sc) {
|
||||
if (why) b.title = why;
|
||||
b.addEventListener("click", () => sendLearning(what));
|
||||
r.appendChild(b);
|
||||
if (what === "tidy") {
|
||||
// Not now needs no agent: the server remembers it, on this machine only,
|
||||
// and the nudge stays hidden until the saved lessons change.
|
||||
const later = el("button", "btn sm", "Not now");
|
||||
later.type = "button";
|
||||
later.title = "Hide this until your lessons change.";
|
||||
later.addEventListener("click", () => quietTidyNudge());
|
||||
r.appendChild(later);
|
||||
}
|
||||
banner.appendChild(r);
|
||||
};
|
||||
if (text) row(text, "Review", "review", true);
|
||||
@@ -1490,6 +1500,17 @@ function learningBanner(sc) {
|
||||
return banner;
|
||||
}
|
||||
|
||||
// Hide the tidy-up nudge until the saved lessons change (POST /learning/tidy-quiet;
|
||||
// the server pushes the patched scan, so the banner redraws without it). Used by
|
||||
// Not now and by a Tidy up that came back with nothing to propose. A failure is
|
||||
// quiet: the nudge simply stays, as it did before.
|
||||
async function quietTidyNudge() {
|
||||
try {
|
||||
await postJson("/learning/tidy-quiet", {});
|
||||
} catch {}
|
||||
redraw();
|
||||
}
|
||||
|
||||
// The agent finishing with nothing to show ends the wait early: a Review whose
|
||||
// card never came (the banner was stale) says so at once instead of spinning
|
||||
// to the limit, and a saved choice stops when the agent is back to waiting.
|
||||
@@ -1514,6 +1535,9 @@ function watchLearningBusy(busy) {
|
||||
clearInterval(timer);
|
||||
page.learningBusy = null;
|
||||
if (busy.what === "review" || busy.what === "tidy") {
|
||||
// A tidy-up with nothing to propose is an answer: remember it, so the
|
||||
// nudge does not come straight back for the same lessons.
|
||||
if (busy.what === "tidy") quietTidyNudge();
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ import { cleanSetting, offered } from "./public/subagents.js";
|
||||
import { DEFAULT_ACCENT, DEFAULT_CARD_WIDTH, DEFAULT_TEXT_SIZE, accentId, cardWidth, textSize } from "./public/palette.js";
|
||||
import { SERVABLE, TYPES, asset } from "./static.mjs";
|
||||
import { handoffsDir, isHandoffId } from "./handoffs.mjs";
|
||||
import { followCounts, learningScan, learningState, writePause } from "./learning.mjs";
|
||||
import { followCounts, learningScan, learningState, writePause, writeTidyQuiet } from "./learning.mjs";
|
||||
import { linkToOpen, openFile, openLink } from "./open-link.mjs";
|
||||
import {
|
||||
NotepadError,
|
||||
@@ -100,6 +100,7 @@ const ROUTES = [
|
||||
{ path: "/looks", method: "POST", origin: true, run: looksPost },
|
||||
{ path: "/subagents", method: "POST", origin: true, run: subagentsPost },
|
||||
{ path: "/learning/pause", method: "POST", origin: true, run: learningPause },
|
||||
{ path: "/learning/tidy-quiet", method: "POST", origin: true, run: learningTidyQuiet },
|
||||
{ path: "/learning/state", method: "GET", run: learningStateGet },
|
||||
{ path: "/learning/lessons", method: "GET", run: learningLessons },
|
||||
{ path: "/pr-host", method: "POST", origin: true, run: prHost },
|
||||
@@ -660,17 +661,40 @@ function subagentsPost(req, res, ctx) {
|
||||
// store), never inside the project. It needs no agent: the dashboard's
|
||||
// scan.learning is patched in place and pushed, so the row repaints at once.
|
||||
function learningPause(req, res, ctx) {
|
||||
return learningWrite(req, res, ctx, {
|
||||
valid: (body) => typeof body.paused === "boolean",
|
||||
states: ["on", "paused"],
|
||||
write: (worktree, body) => writePause(worktree, body.paused),
|
||||
event: (body) => ({ type: "learning-pause", paused: body.paused }),
|
||||
});
|
||||
}
|
||||
|
||||
// Learning's tidy-up quiet (learning.mjs): a Tidy up that proposed nothing, or
|
||||
// the banner's Not now, hides the tidy-up nudge until the saved lessons change.
|
||||
// Machine-local like the pause, and only while Learning is on.
|
||||
function learningTidyQuiet(req, res, ctx) {
|
||||
return learningWrite(req, res, ctx, {
|
||||
valid: () => true,
|
||||
states: ["on"],
|
||||
write: (worktree) => writeTidyQuiet(worktree),
|
||||
event: () => ({ type: "learning-tidy-quiet" }),
|
||||
});
|
||||
}
|
||||
|
||||
// The shared body of the Learning routes that write on this machine: check the
|
||||
// body and the state, write, patch the dashboard's scan in place and push it.
|
||||
function learningWrite(req, res, ctx, { valid, states, write, event }) {
|
||||
return readBody(req, res, (err, body) => {
|
||||
if (err) return json(res, err.tooBig ? 413 : 400, { error: err.tooBig ? "too large" : "body must be JSON" });
|
||||
if (!body || typeof body !== "object" || typeof body.paused !== "boolean") return json(res, 400, { error: "bad-request" });
|
||||
if (!body || typeof body !== "object" || !valid(body)) return json(res, 400, { error: "bad-request" });
|
||||
const worktree = ctx.worktree() || ctx.project;
|
||||
let learning;
|
||||
let written = null;
|
||||
let agentWrote = false;
|
||||
try {
|
||||
const current = learningState(worktree, undefined, { prune: false });
|
||||
if (current.state !== "on" && current.state !== "paused") return json(res, 409, { error: "not-on" });
|
||||
writePause(worktree, body.paused);
|
||||
if (!states.includes(current.state)) return json(res, 409, { error: "not-on" });
|
||||
write(worktree, body);
|
||||
learning = learningScan(worktree);
|
||||
written = updateState(ctx.dir, (fresh) => {
|
||||
if (!fresh || !fresh.scan || typeof fresh.scan !== "object") return null;
|
||||
@@ -700,7 +724,7 @@ function learningPause(req, res, ctx) {
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
noteEvent(ctx.dir, { type: "learning-pause", paused: body.paused });
|
||||
noteEvent(ctx.dir, event(body));
|
||||
ctx.pushState();
|
||||
return json(res, 200, { ok: true, learning });
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user