Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions pm-stack/aggregator/src/decisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,106 @@ export async function listDecisionResolutions(file: string): Promise<DecisionRes
}
return out;
}

// --- Decisions-page draft persistence (#8777) ---------------------------
//
// The 2026-11-11 "front end crashed" incident: pm-aggregator was killed
// mid-session and Drew's typed-but-unsubmitted work existed only in the
// page's form fields — gone the moment the tab died. The page now keeps
// a per-set draft in localStorage: every in-progress answer, comment,
// defer pick, info note and bulk selection is saved as he types and
// restored on load. Drafts for questions that have been recorded (their
// blocks leave the DOM on submit) prune themselves via the
// MutationObserver re-save. Storage is best-effort: any localStorage
// failure (private mode, quota) leaves the page exactly as functional
// as before, just without drafts.

/** localStorage key prefix for one set's decisions-page draft. */
export const DECISIONS_DRAFT_KEY_PREFIX = "decisions-drafts:";

export function decisionsDraftKey(setId: string): string {
return DECISIONS_DRAFT_KEY_PREFIX + setId;
}

/**
* The draft-persistence script for the decisions page (JS source, no
* <script> tags — server.ts interpolates it as the page script's last
* block, after every handler it complements exists). It works purely
* off the page's DOM contract: `.q` blocks keyed by data-qid carrying
* radio inputs named per question, `textarea.ft`, `select.defer`,
* `textarea.infonote`; item cards `section.item[data-ledger]` with
* `input.bulk` checkboxes; the bulk bar's #bulkLean and #bulkInfoNote.
*/
export function decisionsDraftScript(setId: string): string {
const key = JSON.stringify(decisionsDraftKey(setId));
return `
// --- Draft persistence (#8777): in-progress work survives a crash ---
var DRAFT_KEY = ${key};
function draftSave() {
try {
var d = { q: {}, bulk: [], bulkLean: '', bulkNote: '' };
document.querySelectorAll('.q').forEach(function (block) {
var entry = {};
var picked = block.querySelector('input[type=radio]:checked');
if (picked) entry.opt = picked.value;
var ft = block.querySelector('textarea.ft').value;
if (ft) entry.ft = ft;
var lean = block.querySelector('select.defer').value;
if (lean) entry.lean = lean;
var note = block.querySelector('textarea.infonote').value;
if (note) entry.note = note;
if (Object.keys(entry).length) d.q[block.getAttribute('data-qid')] = entry;
});
document.querySelectorAll('section.item').forEach(function (card) {
var box = card.querySelector('input.bulk');
if (box && box.checked) d.bulk.push(card.getAttribute('data-ledger'));
});
var bl = document.getElementById('bulkLean');
if (bl) d.bulkLean = bl.value;
var bn = document.getElementById('bulkInfoNote');
if (bn) d.bulkNote = bn.value;
if (!Object.keys(d.q).length && !d.bulk.length && !d.bulkNote && !d.bulkLean) {
localStorage.removeItem(DRAFT_KEY);
} else {
localStorage.setItem(DRAFT_KEY, JSON.stringify(d));
}
} catch (e) { /* storage unavailable: drafts are best-effort */ }
}
function draftRestore() {
var d;
try {
var raw = localStorage.getItem(DRAFT_KEY);
if (!raw) return;
d = JSON.parse(raw);
} catch (e) { return; }
if (!d || typeof d !== 'object') return;
var restored = 0;
document.querySelectorAll('.q').forEach(function (block) {
var entry = (d.q || {})[block.getAttribute('data-qid')];
if (!entry) return;
if (entry.opt) {
block.querySelectorAll('input[type=radio]').forEach(function (r) {
if (r.value === entry.opt) { r.checked = true; restored++; }
});
}
if (entry.ft) { block.querySelector('textarea.ft').value = entry.ft; restored++; }
if (entry.lean) { block.querySelector('select.defer').value = entry.lean; restored++; }
if (entry.note) { block.querySelector('textarea.infonote').value = entry.note; restored++; }
});
(d.bulk || []).forEach(function (ledger) {
document.querySelectorAll('section.item').forEach(function (card) {
var box = card.querySelector('input.bulk');
if (box && card.getAttribute('data-ledger') === ledger) box.checked = true;
});
});
if (d.bulkLean) { var bl2 = document.getElementById('bulkLean'); if (bl2) bl2.value = d.bulkLean; }
if (d.bulkNote) { var bn2 = document.getElementById('bulkInfoNote'); if (bn2) { bn2.value = d.bulkNote; restored++; } }
if (restored) status('Restored your in-progress work from before the interruption — nothing you typed was lost.');
}
document.addEventListener('input', draftSave);
document.addEventListener('change', draftSave);
new MutationObserver(draftSave).observe(document.querySelector('main'), { childList: true, subtree: true });
draftRestore();
draftSave();
`;
}
2 changes: 2 additions & 0 deletions pm-stack/aggregator/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
type DecisionItem,
type DecisionSet,
type DecisionSubmission,
decisionsDraftScript,
} from "./decisions.ts";
import { listStackHealthDown, readStackHealthStates, setStackHealth, validateStackHealth } from "./stack-health.ts";
import {
Expand Down Expand Up @@ -980,6 +981,7 @@ document.querySelectorAll('[data-reqinfo]').forEach(function (b) {
b.addEventListener('click', function () { requestInfoOne(b.closest('.q')); });
});
document.getElementById('reqinfoChecked').addEventListener('click', requestInfoChecked);
${decisionsDraftScript(set.setId)}
</script>
</main></body></html>`;
}
Expand Down
46 changes: 46 additions & 0 deletions pm-stack/aggregator/test/decisions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { esc } from "../src/render.ts";
import {
decisionSetOpenState,
decisionSetProgress,
Expand All @@ -15,6 +16,8 @@ import {
recordResolutions,
requestInfoOpenQuestions,
validateDecisionSet,
decisionsDraftKey,
decisionsDraftScript,
type DecisionSetInput,
} from "../src/decisions.ts";

Expand Down Expand Up @@ -382,3 +385,46 @@ describe("bulk default flavor (#8766 follow-up)", () => {
expect(byQid.get("item-b#1")).toMatchObject({ kind: "deferred", lean: "skeptical" });
});
});

// --- Crash hardening (#8777) -------------------------------------------
// The 2026-11-11 incident: the aggregator was killed while Drew was
// mid-set on the decisions page. Two properties keep that class of
// failure from ever costing typed work or breaking the page: question
// text reaches the page only through esc(), and in-progress form state
// is continuously drafted to localStorage and restored on load.
describe("decisions page crash hardening (#8777)", () => {
test("esc neutralizes script-breaking and attribute-breaking text", () => {
const hostile = `</script><script>alert(1)</script> "quoted" & <b onmouseover="x">`;
const out = esc(hostile);
expect(out).not.toContain("</script>");
expect(out).not.toContain("<script");
expect(out).not.toContain(`"`);
expect(out).toContain("&lt;/script&gt;");
expect(out).toContain("&quot;");
});

test("draft key is per-set", () => {
expect(decisionsDraftKey("set-a")).toBe("decisions-drafts:set-a");
expect(decisionsDraftKey("set-b")).toBe("decisions-drafts:set-b");
});

test("draft script parses as JS and embeds only its own set's key", () => {
const js = decisionsDraftScript("set-a");
expect(js).toContain(`"decisions-drafts:set-a"`);
expect(js).not.toContain("decisions-drafts:set-b");
expect(() => new Function(js)).not.toThrow();
});

test("draft script wires save-on-input, restore-on-load and prune-on-remove", () => {
const js = decisionsDraftScript("set-a");
expect(js).toContain("localStorage.setItem");
expect(js).toContain("localStorage.getItem");
expect(js).toContain("localStorage.removeItem");
expect(js).toContain("addEventListener('input', draftSave)");
expect(js).toContain("addEventListener('change', draftSave)");
// Recorded questions leave the DOM on submit; the observer re-save
// is what prunes their drafts so a stale draft can never resurrect.
expect(js).toContain("MutationObserver(draftSave)");
expect(js).toContain("draftRestore()");
});
});
Loading