Add the Review Loop: unattended review-and-fix rounds in the desktop app and in loop phases

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-28 21:09:02 -08:00
1 parent d6a28b5dc8
commit 57567d7a0b
71 files changed
+11331 -443

No files matched your search

+163 -5
View File
@@ -598,7 +598,7 @@ export function applyPatch(state, patch) {
if (v === null) {
// The loop's report fields are set, never deleted: a null here would
// strip the app's criteria or an already-posted report.
if (["loopExpect", "loopOutcome", "loopEnd"].includes(k)) throw new Error(rule("", k, "cannot be deleted by a patch"));
if (["loopExpect", "loopOutcome", "loopEnd", "roundOutcome"].includes(k)) throw new Error(rule("", k, "cannot be deleted by a patch"));
delete next[k];
continue;
}
@@ -664,9 +664,19 @@ export function applyPatch(state, patch) {
next.chat = chat;
break;
}
case "roundOutcome":
// A Review Loop round's one report. validate() checks its shape and
// the report doc; here only who may send it, and how often.
if (state.loop?.driving !== true || !state.loop.round) {
throw new Error(rule("", "roundOutcome", "is only accepted in a Review Loop round session"));
}
if (state.roundOutcome !== undefined) throw new Error(rule("", "roundOutcome", "was already posted for this session"));
next.roundOutcome = v;
break;
case "loopOutcome":
// The skill's one report of a phase built under the loop. The shape is
// checked by validate(); here only who may send it, and how often.
if (state.loop?.round) throw new Error(rule("", "loopOutcome", "is not accepted in a round session: a round reports roundOutcome, not loopOutcome"));
if (!state.loop?.driving) {
throw new Error(rule("", "loopOutcome", "is only accepted in a loop session; a resume without --loop unlocked it, so run open --resume <sid> --workflow <yours> --loop, then post this again"));
}
@@ -1248,6 +1258,7 @@ export function validate(state) {
if (state.loopExpect !== undefined) problems.push(...loopExpectProblems(state.loopExpect));
if (state.loopOutcome !== undefined) problems.push(...loopOutcomeProblems(state.loopOutcome, state.loopExpect));
if (state.loopEnd !== undefined) problems.push(...loopEndProblems(state.loopEnd, state));
if (state.roundOutcome !== undefined) problems.push(...roundOutcomeProblems(state.roundOutcome, state));
// The dashboard agent's answer to Suggest for me on the loop setup screen.
if (state.loopSuggest !== undefined) problems.push(...loopSuggestProblems(state.loopSuggest));
@@ -1489,6 +1500,7 @@ export function isOpen(item) {
/* ------------------------------------------------------ the loop's reports */
const LOOP_LEVELS = ["critical", "warning", "suggestion"];
export const ROUND_ROLES = ["both", "reviewer", "fixer"];
const LOOP_REASONS = ["tests", "findings", "blocked", "conflict", "other"];
const LOOP_CAN = ["resume", "ask", "fix", "finalize", "dashboard"];
const TASK_ID = /^\d+\.\d+$/;
@@ -1707,9 +1719,153 @@ export function loopSuggestProblems(v) {
return p;
}
/**
* Problems with a `roundOutcome` (what a Review Loop round reports), as
* sentences naming the field; an empty array when it is good. Rules are the
* overview's Data Model. `state` is the merged session, which holds the
* round's stamp and the report doc the outcome names.
*/
export function roundOutcomeProblems(o, state) {
const p = [];
const bad = (field, rule) => p.push(`roundOutcome.${field} ${rule}`);
if (!isPlainObject(o)) return ["roundOutcome must be an object"];
const prose = (field, s) => {
if (typeof s === "string") p.push(...proseProblems(s, `roundOutcome.${field}`));
};
const stamp = state?.loop || {};
if (!isPosInt(o.round)) bad("round", "must be a positive integer");
else if (o.round !== stamp.round) bad("round", `must be ${stamp.round}, this session's round`);
if (!ROUND_ROLES.includes(o.role)) bad("role", `must be one of ${ROUND_ROLES.join(", ")}`);
else if (o.role !== stamp.role) bad("role", `must be "${stamp.role}", this session's role`);
const role = ROUND_ROLES.includes(o.role) ? o.role : stamp.role;
if (o.result !== "passed" && o.result !== "failed") bad("result", 'must be "passed" or "failed"');
if (o.result === "passed") {
if (o.reason !== null) bad("reason", "must be null when the round passed");
} else if (o.result === "failed" && !LOOP_REASONS.includes(o.reason)) {
bad("reason", `must be one of ${LOOP_REASONS.join(", ")} when the round failed`);
}
if (!oneLine(o.summary, 200)) bad("summary", "must be one line of 1 to 200 characters");
else prose("summary", o.summary);
if (!oneLine(o.subject, 100)) bad("subject", "must be one line of 1 to 100 characters");
else prose("subject", o.subject);
if (role === "both" || role === "reviewer") {
// A report is a doc the agent already posted: docs carry no kind field,
// so the outcome names the doc by its id.
if (typeof o.report !== "string" || !(state?.docs || []).some((d) => d && d.id === o.report)) {
bad("report", "must name a report doc already posted in this session");
}
} else if (o.report !== undefined && typeof o.report !== "string") bad("report", "must be a doc id when present");
if (!isPlainObject(o.review)) bad("review", "must be an object { critical, warning, suggestion, fixed, unfixed, suggestions }");
else {
const r = o.review;
for (const level of LOOP_LEVELS) if (!isNonNegInt(r[level])) bad(`review.${level}`, "must be a non-negative integer");
for (const list of ["fixed", "unfixed", "suggestions"]) {
if (!Array.isArray(r[list])) bad(`review.${list}`, "must be an array");
else if (r[list].length > 50) bad(`review.${list}`, "has at most 50 entries");
else {
r[list].forEach((f, i) => {
const at = `review.${list}[${i}]`;
if (!isPlainObject(f)) return bad(at, "must be an object { title, file, line? }");
if (!oneLine(f.title, 200)) bad(`${at}.title`, "must be one line of 1 to 200 characters");
else prose(`${at}.title`, f.title);
if (!relativePath(f.file)) bad(`${at}.file`, "must be a relative path inside the project");
if (f.line !== undefined && !isPosInt(f.line)) bad(`${at}.line`, "must be a positive integer when present");
});
}
}
}
if (role === "both" || role === "fixer") {
if (!isPlainObject(o.tests)) bad("tests", "must be an object { ran, passed, summary }");
else {
if (typeof o.tests.ran !== "boolean") bad("tests.ran", "must be true or false");
if (typeof o.tests.passed !== "boolean") bad("tests.passed", "must be true or false");
if (typeof o.tests.summary !== "string" || /[\r\n]/.test(o.tests.summary)) bad("tests.summary", "must be a one-line string");
else prose("tests.summary", o.tests.summary);
}
} else if (o.tests !== undefined && !isPlainObject(o.tests)) bad("tests", "must be an object when present");
return p;
}
const REVIEW_END_REASONS = ["clean", "cap", "person", "failed"];
/**
* The Review Loop's `loopEnd` (`kind: "review"`): why the run ended, the report
* doc, what Continue and the partial diff offer, the Suggestions pick, and
* after sign-off (`signedOff`) the note on what it did. `report` and
* `partial.diff` name docs on the page, as `evidence` does for the other kinds.
*/
function reviewLoopEndProblems(e, state) {
const p = [];
const bad = (field, rule) => p.push(`loopEnd.${field} ${rule}`);
const prose = (field, s) => {
if (typeof s === "string") p.push(...proseProblems(s, `loopEnd.${field}`));
};
const onPage = (id) => typeof id === "string" && (state?.docs || []).some((d) => d && d.id === id);
if (!oneLine(e.line, 200)) bad("line", "must be one line of 1 to 200 characters");
else prose("line", e.line);
if (!REVIEW_END_REASONS.includes(e.reason)) bad("reason", `must be one of ${REVIEW_END_REASONS.join(", ")}`);
if (!onPage(e.report)) bad("report", "must be the id of a doc on the page");
if (typeof e.canContinue !== "boolean") bad("canContinue", "must be true or false");
if (typeof e.canSquash !== "boolean") bad("canSquash", "must be true or false");
if (e.signedOff !== undefined && typeof e.signedOff !== "boolean") bad("signedOff", "must be true or false when present");
if (e.note !== undefined) {
if (!oneLine(e.note, 200)) bad("note", "must be one line of 1 to 200 characters");
else prose("note", e.note);
}
if (!Array.isArray(e.can) || !e.can.every((c) => LOOP_CAN.includes(c))) {
bad("can", `must be an array with entries from ${LOOP_CAN.join(", ")}`);
}
if (e.partial !== undefined) {
if (!isPlainObject(e.partial)) bad("partial", "must be an object { files, added, removed, diff }");
else {
for (const k of ["files", "added", "removed"]) if (!isNonNegInt(e.partial[k])) bad(`partial.${k}`, "must be a non-negative integer");
if (!onPage(e.partial.diff)) bad("partial.diff", "must be the id of a doc on the page");
}
}
if (e.suggestions !== undefined) {
if (!Array.isArray(e.suggestions)) bad("suggestions", "must be an array");
else if (e.suggestions.length > 50) bad("suggestions", "has at most 50 entries");
else {
const seen = new Set();
e.suggestions.forEach((s, i) => {
const at = `suggestions[${i}]`;
if (!isPlainObject(s)) return bad(at, "must be an object { id, title, file, line? }");
if (typeof s.id !== "string" || !/^s[0-9]{1,3}$/.test(s.id)) bad(`${at}.id`, "must look like s1");
else if (seen.has(s.id)) bad(`${at}.id`, "must be unique");
else seen.add(s.id);
if (!oneLine(s.title, 200)) bad(`${at}.title`, "must be one line of 1 to 200 characters");
else prose(`${at}.title`, s.title);
if (s.file !== "" && !relativePath(s.file)) bad(`${at}.file`, "must be a relative path inside the project or empty");
else prose(`${at}.file`, s.file);
if (s.line !== undefined && !isPosInt(s.line)) bad(`${at}.line`, "must be a positive integer when present");
});
}
}
if (e.open !== undefined) {
if (!Array.isArray(e.open)) bad("open", "must be an array");
else if (e.open.length > 50) bad("open", "has at most 50 entries");
else {
e.open.forEach((f, i) => {
const at = `open[${i}]`;
if (!isPlainObject(f)) return bad(at, "must be an object { title, file, line? }");
if (f.level !== undefined && f.level !== "critical" && f.level !== "warning") bad(`${at}.level`, 'must be "critical" or "warning" when present');
if (!oneLine(f.title, 200)) bad(`${at}.title`, "must be one line of 1 to 200 characters");
else prose(`${at}.title`, f.title);
if (!relativePath(f.file)) bad(`${at}.file`, "must be a relative path inside the project");
if (f.line !== undefined && !isPosInt(f.line)) bad(`${at}.line`, "must be a positive integer when present");
});
}
}
return p;
}
/**
* Problems with a `loopEnd` (the app's word on how the loop ended), given the
* merged state so `evidence` can be checked against the docs on the page.
* merged state so `evidence` (or, for a Review Loop's `kind: "review"`, `report`
* and `partial.diff`) can be checked against the docs on the page.
*/
export function loopEndProblems(e, state) {
const p = [];
@@ -1718,7 +1874,8 @@ export function loopEndProblems(e, state) {
const prose = (field, s) => {
if (typeof s === "string") p.push(...proseProblems(s, `loopEnd.${field}`));
};
if (e.kind !== "stopped" && e.kind !== "finished") bad("kind", 'must be "stopped" or "finished"');
if (e.kind === "review") return reviewLoopEndProblems(e, state);
if (e.kind !== "stopped" && e.kind !== "finished") bad("kind", 'must be "stopped" or "finished" (or "review" for a Review Loop)');
if (!oneLine(e.line, 200)) bad("line", "must be one line of 1 to 200 characters");
else prose("line", e.line);
if (e.evidence !== undefined) {
@@ -1772,7 +1929,8 @@ export function loopEndProblems(e, state) {
// Loop mode adds four optional keys that a blank state does not hold (absent,
// not null): `loop: { driving: true, strength? }` (set by `open --loop`), `loopExpect`
// (the app's judging criteria), `loopOutcome` (the skill's report) and
// `loopEnd` (the app's ending).
// `loopEnd` (the app's ending). A Review Loop round adds `roundOutcome` and
// stamps `loop: { driving, round, role, scopeFile }`.
export function blankState({ sid, workflow, title, project, specDir }) {
return {
schema: SCHEMA,
@@ -1809,7 +1967,7 @@ export function switchWorkflow(state, { workflow, title, specDir }) {
// A blank state holds no `loop`, `loopExpect`, `loopOutcome` or `loopEnd`,
// so a hop never carries them over; `open --loop` stamps `loop` again after
// the switch, and the app restamps `loopExpect` on its next look.
for (const key of ["loop", "loopExpect", "loopOutcome", "loopEnd"]) delete next[key];
for (const key of ["loop", "loopExpect", "loopOutcome", "loopEnd", "roundOutcome"]) delete next[key];
if (state.chat) next.chat = state.chat;
return next;
}