Harden the shared console install, launcher, Loop staging, spec state and prompts

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-08 13:48:28 -08:00
1 parent 120c259d9c
commit 3ee96834bf
130 files changed
+5988 -1434

No files matched your search

+89 -20
View File
@@ -8,7 +8,7 @@ import { startersFor } from "../starters.js";
import { looks } from "../looks.js";
import { thumbs, uploadUrl, docChip, attachTray, CHAT_IMAGES, attachFiles, incomingFiles, deleteUrl, sentAttachments, noteImages, attachRow, openPicview, refocusThumb } from "../uploads.js";
import { attachMentions } from "../mention-box.js";
import { makeMdField } from "../md-field.js";
import { destroyFields, makeMdField } from "../md-field.js";
import { pickerAccept, attachmentKind, noteAwaiting, threadWho, threadText } from "../answers.js";
import { postJson } from "../net.js";
import { reconnectOffer } from "../continue-button.js";
@@ -29,10 +29,19 @@ export const declineOpen = new Map();
const deciding = new Set();
// The Subagents dialog's instruction box, a Markdown field made once in
// wireAsk into its #subagents-instruction mount.
// The Subagents dialog's instruction box, a Markdown field made the first
// time the dialog needs it, into its #subagents-instruction mount: made at
// start-up, it would pull CodeMirror into every page load (md-field.js).
let instrField = null;
function instr() {
if (!instrField) {
instrField = makeMdField({ className: "subagents-instruction", label: "Instruction", maxLength: 2000 });
$("subagents-instruction").appendChild(instrField.el);
}
return instrField;
}
// The frame's chat block, with `offline` worked out now rather than when the
// frame was sent: the agent comes and goes between frames, and /ping keeps
// agentLastSeen current. The frame's own verdict stands when there is no clock.
@@ -66,7 +75,7 @@ export function askState() {
// frame for the moment after a 409 stale, before the new frame lands.
const askConversation = () => page.local.chat.conversation ?? askFrame().conversation;
export function renderAsk(main) {
export function renderAsk(main, held = null) {
const { frame, view: v, send: ss } = askState();
// Anything drawn here has been seen: no dot for it later.
const newest = newestReplyId(frame);
@@ -77,7 +86,6 @@ export function renderAsk(main) {
page.chatDot = false;
askPaintedOffline = ss.state === "offline";
const pane = el("section", "ask-pane");
const head = el("div", "ask-head");
head.appendChild(el("h2", null, "Quick question"));
head.appendChild(el("span", "ask-counter", v.counterText));
@@ -86,17 +94,42 @@ export function renderAsk(main) {
reset.disabled = ss.state === "offline" || chatResetting;
reset.addEventListener("click", newConversation);
head.appendChild(reset);
pane.appendChild(head);
const list = el("div", "ask-list");
list.setAttribute("role", "log");
list.setAttribute("aria-live", "polite");
if (!v.rows.length) list.appendChild(askStarters(ss));
// The list is a live region (role="log"): one taken up from the last render
// keeps every row that would draw the same, so a redraw announces nothing
// and a new message is one appended row, the only thing read out.
const kept = held && held.querySelector(":scope > .ask-list");
const list = kept || el("div", "ask-list");
if (!kept) {
list.setAttribute("role", "log");
list.setAttribute("aria-live", "polite");
}
const notes = new Map(v.rows.filter((r) => r.kind === "decision").map((r) => [r.re, r.text]));
for (const r of v.rows) list.appendChild(askRow(r, ss, notes));
pane.appendChild(list);
pane.appendChild(askComposer(ss));
main.appendChild(pane);
const wanted = v.rows.length
? v.rows.map((r) => [askRowKey(r, ss, notes), () => askRow(r, ss, notes)])
: [[askStartersKey(ss), () => askStarters(ss)]];
syncRows(list, wanted);
const composer = askComposer(ss);
if (kept) {
const oldHead = held.querySelector(":scope > .ask-head");
const oldComposer = held.querySelector(":scope > .ask-composer");
if (oldHead) oldHead.replaceWith(head);
else held.insertBefore(head, list);
if (oldComposer) {
destroyFields(oldComposer);
oldComposer.replaceWith(composer);
} else held.appendChild(composer);
if (held.parentNode !== main) main.appendChild(held);
} else {
if (held) {
destroyFields(held);
held.remove();
}
const pane = el("section", "ask-pane");
pane.append(head, list, composer);
main.appendChild(pane);
}
// Back where the person left the list, unless something new arrived (or it
// is the first look), which brings the newest row into view.
@@ -106,6 +139,44 @@ export function renderAsk(main) {
list.scrollTop = !saved || grew ? list.scrollHeight : saved.top;
}
// What a row draws from: the row itself and every outside fact askRow() reads,
// so two equal keys draw the same row and the one on the page can stay.
// Each outside fact counts only on the rows that read it, or a send in flight
// (chatSending) would redraw every row in the log.
function askRowKey(r, ss, notes) {
const offline = ss.state === "offline";
if (r.kind === "decision") return JSON.stringify([r]);
if (r.kind === "agent") {
const open = Boolean(r.proposal && !r.decided);
return JSON.stringify([r, notes.get(r.id) ?? null, open && [offline, deciding.has(r.id), declineOpen.has(r.id)]]);
}
return JSON.stringify([
r,
r.pending && r.failed ? [offline, chatSending] : null,
!r.pending && r.waiting && r.working ? [offline, agentAdrift()] : null,
(r.images || []).map((img) => thumbs.get(img.key) || ""),
]);
}
const askStartersKey = (ss) =>
JSON.stringify(["starters", ss.canType, page.S.workflow, looks.role, Boolean(page.local.chat.about)]);
// Brings the live list's children in line with `wanted` ([key, make] pairs,
// in order): a child whose key matches stays untouched, a changed one is
// redrawn in its place, new ones are appended and leftovers removed.
function syncRows(list, wanted) {
const have = [...list.children];
wanted.forEach(([key, make], i) => {
const cur = have[i];
if (cur && cur.dataset.rowKey === key) return;
const node = make();
node.dataset.rowKey = key;
if (cur) cur.replaceWith(node);
else list.appendChild(node);
});
for (const extra of have.slice(wanted.length)) extra.remove();
}
// An empty conversation offers a few questions for this step, the role's
// first. A click only fills the box: nothing is ever sent for the person.
function askStarters(ss) {
@@ -867,7 +938,7 @@ export function openSubagents() {
for (const radio of document.querySelectorAll('input[name="subagents-on"]')) {
radio.checked = radio.value === (on ? "on" : "off");
}
instrField.value = page.subagentsFrame.instruction ?? defaultInstruction(page.S.workflow);
instr().value = page.subagentsFrame.instruction ?? defaultInstruction(page.S.workflow);
$("subagents-max").value = String(page.subagentsFrame.max);
const strength = STRENGTHS.includes(page.subagentsFrame.strength) ? page.subagentsFrame.strength : DEFAULT_STRENGTH;
for (const radio of document.querySelectorAll('input[name="subagents-strength"]')) radio.checked = radio.value === strength;
@@ -889,7 +960,7 @@ export function openSubagents() {
export function syncSubagentsFields() {
const picked = document.querySelector('input[name="subagents-on"]:checked');
const off = !picked || picked.value !== "on";
instrField.disabled = off;
instr().disabled = off;
for (const id of ["subagents-max", "subagents-default"]) $(id).disabled = off;
for (const radio of document.querySelectorAll('input[name="subagents-strength"]')) radio.disabled = off;
}
@@ -914,7 +985,7 @@ export async function saveSubagents() {
const save = $("subagents-save");
const status = $("subagents-status");
const picked = document.querySelector('input[name="subagents-on"]:checked');
const text = instrField.value.trim();
const text = instr().value.trim();
// The shipped default is stored as null, so a later release's wording
// reaches everyone who never changed it.
const instruction = text === defaultInstruction(page.S.workflow) ? null : text;
@@ -942,14 +1013,12 @@ export const askOfflineFlipped = () => (askState().send.state === "offline") !==
// The Subagents dialog's buttons, and dropping files onto the notes panel.
export function wireAsk() {
instrField = makeMdField({ className: "subagents-instruction", label: "Instruction", maxLength: 2000 });
$("subagents-instruction").appendChild(instrField.el);
for (const radio of document.querySelectorAll('input[name="subagents-on"]')) radio.addEventListener("change", syncSubagentsFields);
for (const radio of document.querySelectorAll('input[name="subagents-strength"]')) radio.addEventListener("change", syncStrengthHelp);
lightDismiss($("subagents-modal"));
$("btn-subagents").addEventListener("click", openSubagents);
$("subagents-default").addEventListener("click", () => {
instrField.value = defaultInstruction(page.S.workflow);
instr().value = defaultInstruction(page.S.workflow);
});
$("subagents-save").addEventListener("click", saveSubagents);
$("subagents-cancel").addEventListener("click", () => $("subagents-modal").close());
+30 -4
View File
@@ -880,6 +880,23 @@ function buildChoice(card, item, staged) {
clearRow(card, item);
}
// A small eraser-style mark: a cross in a circle, drawn inline so it takes the
// button's text color in both themes. Hidden from readers; the aria-label names it.
function clearIcon() {
const ns = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(ns, "svg");
svg.setAttribute("viewBox", "0 0 24 24");
svg.setAttribute("width", "14");
svg.setAttribute("height", "14");
svg.setAttribute("aria-hidden", "true");
for (const d of ["M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18z", "M9 9l6 6M15 9l-6 6"]) {
const path = document.createElementNS(ns, "path");
path.setAttribute("d", d);
svg.appendChild(path);
}
return svg;
}
// Takes a one-answer question back to how it arrived: no pick, and nothing in
// "Something else" (its draft too, or the render would put the words straight
// back). A radio group cannot be unticked by clicking it again, so without
@@ -887,8 +904,10 @@ function buildChoice(card, item, staged) {
// call it, and they only run for a form that has not been sent.
function clearRow(card, item) {
const row = el("div", "btn-row options-clear");
const b = el("button", "btn small ghost", "Clear");
const b = el("button", "btn small ghost clear-btn");
b.type = "button";
b.appendChild(clearIcon());
b.appendChild(el("span", null, "Clear answer"));
// An id so focus survives the render the click sets off (see withFocus).
b.id = "clear-" + item.id;
b.setAttribute("aria-label", "Clear your answer to this question");
@@ -1307,9 +1326,16 @@ function buildReview(card, item, staged) {
verdictText(v)
);
b.type = "button";
b.addEventListener("click", () =>
stage(item.id, withNote(item, on ? null : { type: "answer", kind: "review", verdict: v.id }))
);
// A toggle: a screen reader hears which verdict is picked, not only sees
// it in the fill. Set on the row at once, ahead of the redraw the stage
// sets off, so the state never lags the click.
b.setAttribute("aria-pressed", String(Boolean(on)));
b.addEventListener("click", () => {
for (const other of row.querySelectorAll("button[aria-pressed]")) {
other.setAttribute("aria-pressed", String(other === b && !on));
}
stage(item.id, withNote(item, on ? null : { type: "answer", kind: "review", verdict: v.id }));
});
row.appendChild(b);
}
card.appendChild(row);
+35 -14
View File
@@ -46,6 +46,12 @@ function cleanupSummary(items, bytes) {
return parts.join(", ") + " · " + formatBytes(bytes);
}
// A completed spec git does not track as it stands is never deleted (the
// server refuses it), so it is listed apart as kept, out of the total and out
// of what Delete these sends.
const keptBack = (item) => item.where === "completed" && item.tracked === false;
const deletable = () => ((cleanupFound && cleanupFound.items) || []).filter((i) => !keptBack(i));
function resetCleanup() {
cleanupGen++;
cleanupFound = null;
@@ -59,19 +65,28 @@ function resetCleanup() {
function renderCleanup() {
const list = $("cleanup-list");
list.replaceChildren();
const items = (cleanupFound && cleanupFound.items) || [];
const all = (cleanupFound && cleanupFound.items) || [];
const items = deletable();
const kept = all.filter(keptBack);
$("cleanup-delete").hidden = !items.length;
if (!items.length) return;
const rows = (some) => some.map((item) => {
const row = el("div", "cleanup-row");
row.append(el("span", null, cleanupLabel(item)), el("span", "cleanup-size", formatBytes(item.bytes)));
return row;
});
list.append(el("p", "cleanup-summary", cleanupSummary(items, cleanupFound.bytes)), ...rows(items.slice(0, CLEANUP_SHOWN)));
if (items.length > CLEANUP_SHOWN) {
const more = el("details", "cleanup-more");
more.append(el("summary", null, (items.length - CLEANUP_SHOWN) + " more"), ...rows(items.slice(CLEANUP_SHOWN)));
list.appendChild(more);
if (items.length) {
const bytes = Math.max(0, cleanupFound.bytes - kept.reduce((n, i) => n + (i.bytes || 0), 0));
list.append(el("p", "cleanup-summary", cleanupSummary(items, bytes)), ...rows(items.slice(0, CLEANUP_SHOWN)));
if (items.length > CLEANUP_SHOWN) {
const more = el("details", "cleanup-more");
more.append(el("summary", null, (items.length - CLEANUP_SHOWN) + " more"), ...rows(items.slice(CLEANUP_SHOWN)));
list.appendChild(more);
}
}
for (const item of kept) {
const row = el("div", "cleanup-row cleanup-kept");
row.append(el("span", null, cleanupLabel(item)), el("span", "cleanup-size", "kept: git does not track it as it stands"));
list.appendChild(row);
}
}
@@ -93,17 +108,20 @@ async function scanCleanup() {
renderCleanup();
// Left in the status line, not cleared: it is the live region, so this is
// what a screen reader hears when the scan finishes.
const found = cleanupFound.items.length;
setCleanupStatus(found ? "Found " + found + (found === 1 ? " item." : " items.") : "Nothing to clean up.", false);
const found = deletable().length;
const keptCount = cleanupFound.items.length - found;
const keptNote = keptCount ? ` ${keptCount} old completed ${keptCount === 1 ? "spec is" : "specs are"} kept: git does not track ${keptCount === 1 ? "it" : "them"} as ${keptCount === 1 ? "it stands" : "they stand"}.` : "";
setCleanupStatus((found ? "Found " + found + (found === 1 ? " item." : " items.") : "Nothing to clean up.") + keptNote, false);
}
async function deleteCleanup() {
if (!cleanupFound || !cleanupFound.items.length) return;
const ids = deletable().map((i) => i.id);
if (!ids.length) return;
const gen = ++cleanupGen;
$("cleanup-delete").disabled = true;
$("cleanup-scan").disabled = true;
setCleanupStatus("Deleting…", false);
const { status, body } = await postJson("/cleanup", { delete: cleanupFound.items.map((i) => i.id) });
const { status, body } = await postJson("/cleanup", { delete: ids });
if (gen !== cleanupGen) return;
$("cleanup-delete").disabled = false;
$("cleanup-scan").disabled = false;
@@ -112,15 +130,18 @@ async function deleteCleanup() {
}
const removed = (body.removed || []).length;
const failed = (body.failed || []).length;
// A completed spec git does not track is refused, and the server says why.
const refused = (body.refused || []).length;
// The server re-checks every id and keeps anything that changed since the
// scan; say so, rather than let a count that came up short pass unremarked.
const asked = cleanupFound.items.length;
const kept = Math.max(0, asked - removed - failed);
const asked = ids.length;
const kept = Math.max(0, asked - removed - failed - refused);
cleanupFound = null;
renderCleanup();
const done = removed ? "Deleted " + removed + (removed === 1 ? " item" : " items") + " · " + formatBytes(body.bytes) + " freed." : "Nothing was deleted.";
const keptLine = kept ? ` ${kept} ${kept === 1 ? "was" : "were"} kept because ${kept === 1 ? "it" : "they"} changed since the scan.` : "";
setCleanupStatus((failed ? done + " " + failed + " could not be removed." : done) + keptLine, Boolean(failed));
const refusalLine = refused && body.refusal ? " " + body.refusal : "";
setCleanupStatus((failed ? done + " " + failed + " could not be removed." : done) + keptLine + refusalLine, Boolean(failed || refused));
}
// The Cleanup section's buttons, and starting over whenever Preferences closes.
+36 -12
View File
@@ -65,6 +65,7 @@ import { renderLoopSetup } from "./loop-setup.js";
import { renderLaunching, renderWaiting } from "./starting.js";
import { attachFiles, heldNotes, noteImages, pendingNotes, removeImage } from "../uploads.js";
import { closeMentions } from "../mention-box.js";
import { destroyFields } from "../md-field.js";
import { buildPager, fitGrowBox, markFor, markMore, renderCard, renderEndItem, renderNotice } from "./cards.js";
import { sendState, suggestState } from "../send-state.js";
import { renderAgentsDoc, renderBriefButton, renderDoc, renderOverview } from "./docs.js";
@@ -1608,16 +1609,31 @@ export function renderSidebar() {
}
}
// The Ask pane on screen when a render of the Ask tab began, held back from
// the clear like the pose pane, so renderAsk() can take it up again: its list
// is a live region, and a list rebuilt on every redraw would be announced
// over and over instead of only its new rows.
let heldAsk = null;
// Takes `node` off the page, ending the editor of every Markdown field in it.
function discard(node) {
destroyFields(node);
node.remove();
}
function renderMain() {
const main = $("main");
page.heldPose = main.querySelector(":scope > [data-pose]");
for (const child of [...main.children]) if (child !== page.heldPose) child.remove();
heldAsk = page.view === "ask" ? main.querySelector(":scope > .ask-pane") : null;
for (const child of [...main.children]) if (child !== page.heldPose && child !== heldAsk) discard(child);
try {
paintMain(main);
} finally {
// Nothing took it up: this screen is not the same pose.
page.heldPose?.remove();
if (page.heldPose) discard(page.heldPose);
page.heldPose = null;
if (heldAsk) discard(heldAsk);
heldAsk = null;
}
// Inside the render pass, before withFocus restores the card's scroll, so
// the restore is measured against the box's final height.
@@ -1636,7 +1652,11 @@ function paintMain(main) {
if (page.view.startsWith("doc:")) return renderDoc(main, page.view.slice(4));
if (isSpecFileView(page.view)) return renderOverview(main, page.view);
if (page.view === AGENTS_VIEW) return renderAgentsDoc(main);
if (page.view === "ask") return renderAsk(main);
if (page.view === "ask") {
const held = heldAsk;
heldAsk = null;
return renderAsk(main, held);
}
if (page.view === "subagents") return renderSubagents(main);
// The session's own page, reached from the PAUSED / SESSION ENDED item at
@@ -1771,20 +1791,23 @@ export function renderFooter() {
const summary = $("footer-summary");
const notes = pendingNotes();
const n = stagedCount();
summary.replaceChildren();
// Built off the page and swapped in only when it reads differently: the
// summary is a live region and the footer renders on every tick, so a
// rewrite with the same words would be announced again each time.
const next = document.createElement("div");
const sent = awaitingReply();
if (!n && !notes.length && sent.length) {
// "Answer a question above" is nonsense when everything answerable has
// already gone. Say what is actually outstanding instead: their own send.
summary.appendChild(el("strong", null, stranded() ? `${sent.length} saved, not picked up yet` : `${sent.length} with Plan2Code`));
summary.appendChild(document.createTextNode(" · " + sent.map((i) => i.title).join(" · ")));
next.appendChild(el("strong", null, stranded() ? `${sent.length} saved, not picked up yet` : `${sent.length} with Plan2Code`));
next.appendChild(document.createTextNode(" · " + sent.map((i) => i.title).join(" · ")));
} else if (finish()) {
// Not "nothing to send yet". There is no yet.
const fin = finish();
summary.appendChild(el("strong", null, finishPaused(fin) ? "Paused." : "All done."));
summary.appendChild(
next.appendChild(el("strong", null, finishPaused(fin) ? "Paused." : "All done."));
next.appendChild(
document.createTextNode(
n || notes.length
? " Anything still waiting here was never sent."
@@ -1796,7 +1819,7 @@ export function renderFooter() {
)
);
} else if (!n && !notes.length) {
summary.appendChild(
next.appendChild(
document.createTextNode(
myTurn().length
? "Nothing to send yet. Answer a question above, then press Send."
@@ -1805,7 +1828,7 @@ export function renderFooter() {
);
} else {
const strong = el("strong", null, `${n + notes.length} ready`);
summary.appendChild(strong);
next.appendChild(strong);
const bits = [];
for (const [id, v] of Object.entries(page.local.staged)) {
const item = byId(id);
@@ -1819,12 +1842,12 @@ export function renderFooter() {
const item = byId(note.id);
bits.push(`note on ${item ? item.title : "a question"}`);
}
summary.appendChild(document.createTextNode(" · " + bits.join(" · ")));
next.appendChild(document.createTextNode(" · " + bits.join(" · ")));
}
const held = finish() ? 0 : heldNotes().length;
if (held) {
summary.appendChild(
next.appendChild(
el(
"div",
"footer-held",
@@ -1834,6 +1857,7 @@ export function renderFooter() {
)
);
}
if (summary.innerHTML !== next.innerHTML) summary.replaceChildren(...next.childNodes);
const state = sendState();
const btn = $("btn-send");