diff --git a/docs/pipeline.md b/docs/pipeline.md index cbdfb15b..3569d3af 100644 --- a/docs/pipeline.md +++ b/docs/pipeline.md @@ -38,7 +38,7 @@ Finds open issues and PRs that describe the same bug or request as another open - **Decision.** One per group, asked once the group is compared: "Keep #A and close #B, #C as duplicates?" with Use this answer, Keep #N instead (up to three alternatives) and Not duplicates. The summary lists, per item, what it does better, what is worth carrying over or what it adds. Every `#number` in the decision links to the item page and to GitHub. Only an operator's answer counts. Open per-pair decisions of the group are superseded by it (audited); answered ones keep their effect. - **Held out.** While the decision is open or answered Use this answer / Keep #N instead, the items it closes are left out of Decide, Review, Verify, Fix-up and Fix. "Not duplicates" releases them for good and stores every pair of the group as distinct. - **Comments.** The exact texts are written when the decision is asked, one set per answer (Use this answer and each Keep #N instead), and shown under that answer, so the answer approves exactly the words it posts. The page sends a hash of the shown texts with the answer; a changed set is refused (409) and the page reloads. Each member to close gets "Closing as a duplicate of #A." and, for a PR with parts to carry over, a second line "Parts to carry over to #A: ..." naming the first two points. For issue groups the kept issue gets one comment, "Details from the closed duplicates:", with at most six bullets built from the points each closed report adds, each crediting its source (`- #n: ...`; links, HTML, cross-repo references and mentions removed). The kept PR's fallback suggestion (used when its branch can't be pushed to) is part of the set. No model writes these, and none is written after an answer. -- **Act.** In a repo approved for comments, Act posts the stored texts for an answered group (Use this answer or Keep #N instead) and closes the members, within the step's limit and pilot. Each write is recorded in `dupe_group_posts` right after it happens, so a crash or retry never repeats a comment or a close. Before closing, Act reads the kept items (a closed, unmerged one stops the decision) and each member on GitHub: one already closed or merged, a PR whose head differs from the head stored when the decision was asked, or an issue whose text changed is skipped and recorded. Roles and answer are re-read before every write, so an undo or refresh stops the decision. A member whose writes fail three times is skipped. Undo is refused once Act wrote to GitHub. The decision is applied when every member to close has been closed. +- **Act.** In a repo approved for comments, Act posts the stored texts for an answered group (Use this answer or Keep #N instead) and closes the members, within the step's limit and pilot. Each write is recorded in `dupe_group_posts` right after it happens, so a crash or retry never repeats a comment or a close. Before closing, Act reads the kept items (a closed, unmerged one stops the decision) and each member on GitHub: one already closed or merged, a PR whose head differs from the head stored when the decision was asked, or an issue whose text changed is skipped and recorded; such a member (and one whose close failed three times) is released: its role becomes stay, it is no longer held or settled by the decision, and the next comparison may ask about it again. Roles and answer are re-read before every write, so an undo or refresh stops the decision. A member whose writes fail three times is skipped. Undo is refused once Act wrote to GitHub. The decision is applied when every member to close has been closed. - **Carry-over.** When PRs closed in a group name parts worth carrying over, Act queues a job for the kept PR (`dupe_carries`). Fix-up works it with its take-fix rules: a model (sandboxed) writes a diff porting the listed parts, shown with the closed PRs' diffs; the patch check (Opus) must approve; the push to the PR's branch needs push approval and maintainer edits. Without them, in a repo approved for comments, the parts are posted on the kept PR as one comment instead. Two failed tries hold the job. Take-fix tries and stages of the PR are not touched. - **Hand-off.** A close goes to Act. Confirmed duplicates raise the original's priority. diff --git a/src/act/dupe-groups.ts b/src/act/dupe-groups.ts index 59a895c7..22f97d1f 100644 --- a/src/act/dupe-groups.ts +++ b/src/act/dupe-groups.ts @@ -68,8 +68,14 @@ export async function actOnGroups(db: DatabaseSync, run: GroupRun): Promise { + const skip = (n: number, step: string, reason: string, release = false) => { record(dec.id, n, step, null); + if (release) { + // Not closed by this decision: it stays open, is no longer held or settled by it, and the next comparison may ask about it again. + db.prepare("UPDATE dupe_group_items SET role = 'stay', target = NULL, base_role = 'stay', base_target = NULL WHERE decision_id = ? AND number = ?").run(dec.id, n); + record(dec.id, n, "released", null); + audit(db, ACTOR, "duplicate-member-released", ref(n), { decision: dec.id, reason }); + } audit(db, ACTOR, "act-skip", ref(n), { decision: dec.id, reason }); res.skipped.push({ ref: ref(n), reason }); run.log(` ${ref(n)}: skipped: ${reason}`); @@ -115,10 +121,10 @@ export async function actOnGroups(db: DatabaseSync, run: GroupRun): Promise posted.get(dec.id, c.number, "close")) && (!details || posted.get(dec.id, details.number, "details"))) { diff --git a/src/dash/api/decisions.ts b/src/dash/api/decisions.ts index 020238a7..84682533 100644 --- a/src/dash/api/decisions.ts +++ b/src/dash/api/decisions.ts @@ -332,6 +332,8 @@ export async function adopt(db: DatabaseSync, req: Req): Promise { { id: string; login: string; option: string | null } | undefined; if (!o) throw new HttpError(404, "no such live opinion on this decision"); if (!o.option || !optionsOf(d).some((x) => x.key === o.option)) throw new HttpError(400, "that opinion picks no option"); + const stored = d.kind === "duplicate" && d.id.includes(":dupegroup:") ? textsFor(db, d.id, o.option) : []; + if (stored.length && body(req).textsHash !== textsHash(stored)) throw new HttpError(409, "the comments for this answer changed; reload the decision"); const r = db.prepare(`UPDATE main.decisions SET answer = ?, answer_note = NULL, exceptions = ?, answered_by = ?, answered_at = ?, adopted_opinion = ? WHERE id = ? AND answer IS NULL`).run(o.option, isBatch(d) ? "[]" : null, req.user.login, new Date().toISOString(), o.id, d.id); if (r.changes !== 1) throw new HttpError(409, "someone answered this a moment ago"); diff --git a/src/dupes/decide.ts b/src/dupes/decide.ts index 4e2d5ac1..1a929d4b 100644 --- a/src/dupes/decide.ts +++ b/src/dupes/decide.ts @@ -39,7 +39,8 @@ function answeredKeeps(db: DatabaseSync, repo: string, members: number[]): { set WHERE d.kind = 'duplicate' AND d.answer IS NOT NULL AND d.answer NOT IN ('superseded', 'distinct') AND gi.repo = ? AND gi.number IN (${ph}) ORDER BY d.asked_at, d.id`).all(repo, ...members) as { id: string }[]; const settled = new Set(), keep = new Map(); for (const { id } of ids) - for (const r of db.prepare("SELECT number, kind, role FROM dupe_group_items WHERE decision_id = ?").all(id) as { number: number; kind: string; role: string }[]) { + for (const r of db.prepare(`SELECT gi.number, gi.kind, gi.role FROM dupe_group_items gi WHERE gi.decision_id = ? + AND NOT EXISTS (SELECT 1 FROM dupe_group_posts p WHERE p.decision_id = gi.decision_id AND p.number = gi.number AND p.step = 'released')`).all(id) as { number: number; kind: string; role: string }[]) { settled.add(r.number); if (r.role === "keep") keep.set(r.kind, r.number); } diff --git a/test/api.test.ts b/test/api.test.ts index b1589827..72f85978 100644 --- a/test/api.test.ts +++ b/test/api.test.ts @@ -217,10 +217,17 @@ describe("api", () => { db.prepare("UPDATE dupe_group_texts SET text = 'Closing as a duplicate of #4.\nParts to carry over to #4: x.' WHERE decision_id = ?").run(id); await boss.post(dpath(id, "/answer"), { answer: "accept", textsHash: ot.hash }, 409); const fresh = (await boss.get(dpath(id))).optionTexts[0]!; + // Adopting an opinion approves the same texts, so it needs the same hash. + db.prepare("INSERT INTO decision_opinions (id, decision_id, login, option, note, at, permission) VALUES ('op1', ?, 'maint', 'accept', NULL, 'now', 'write')").run(id); + await boss.post(dpath(id, "/adopt"), { opinionId: "op1" }, 409); + await boss.post(dpath(id, "/adopt"), { opinionId: "op1", textsHash: ot.hash }, 409); + assert.equal((await boss.post(dpath(id, "/adopt"), { opinionId: "op1", textsHash: fresh.hash })).answer, "accept"); + await boss.post(dpath(id, "/undo")); assert.equal((await boss.post(dpath(id, "/answer"), { answer: "accept", textsHash: fresh.hash })).answer, "accept"); db.prepare("INSERT INTO dupe_group_posts (decision_id, number, step, url, at) VALUES (?, 5, 'comment', 'https://x/c', 'now')").run(id); await boss.post(dpath(id, "/undo"), undefined, 409); db.prepare("UPDATE main.decisions SET applied_at = 'now' WHERE id = ?").run(id); + db.prepare("DELETE FROM decision_opinions WHERE id = 'op1'").run(); }); test("adopt records the operator's answer and credits the opinion; opinions close once answered", async () => { @@ -232,7 +239,8 @@ describe("api", () => { assert.equal(d.answeredBy, "boss"); assert.equal(d.adoptedOpinion?.id, op.id); assert.equal(d.adoptedOpinion?.login, "maint"); - assert.equal(auditCount("adopt", "boss"), 1); + // The group decision test above adopted once as well. + assert.equal(auditCount("adopt", "boss"), 2); await maint.post(dpath(P4, "/opinion"), { option: "small" }, 409); await maint.post(dpath(P4, "/retract"), undefined, 409); }); diff --git a/test/dupe-group-act.test.ts b/test/dupe-group-act.test.ts index c16122d0..4231ed86 100644 --- a/test/dupe-group-act.test.ts +++ b/test/dupe-group-act.test.ts @@ -9,6 +9,7 @@ import { setCommentApproval } from "../src/act/approval.ts"; import { runAct, writeEffect, type ActDeps } from "../src/act/index.ts"; import { connect } from "../src/db.ts"; import { getDecision } from "../src/decisions.ts"; +import { duplicateHeld } from "../src/dupes/held.ts"; import { kindSetsOf, duplicateGroups } from "../src/dupes/plan.ts"; import { askGroupDecisions } from "../src/dupes/decide.ts"; import { cleanReason } from "../src/act/templates.ts"; @@ -260,6 +261,31 @@ describe("act on group decisions", () => { }); }); +describe("released members", () => { + test("a member skipped as stale is released and the next comparison plans a new decision for it", async () => { + const db = await world(); + answer(db, PR_GROUP, "accept"); + assert.equal(duplicateHeld(db, "app", 12), true); + await run(db, fakes([], { heads: new Map([[12, "newsha"]]) })); + assert.equal(duplicateHeld(db, "app", 12), false); + assert.equal(db.prepare("SELECT role FROM dupe_group_items WHERE decision_id = ? AND number = 12").get(PR_GROUP)!.role, "stay"); + assert.equal(db.prepare("SELECT COUNT(*) AS n FROM audit WHERE action = 'duplicate-member-released'").get()!.n, 1); + // The mirror sees the new head; the comparison runs again for the released member. + db.prepare("UPDATE items SET head_oid = 'newsha' WHERE repo = 'app' AND number = 12").run(); + const cd: CompareDeps = { + fetchDiff: async () => ({ ok: true, text: "diff --git a/src/a.rs b/src/a.rs\n--- a/src/a.rs\n+++ b/src/a.rs\n@@ -1 +1 @@\n-old\n+new\n" }), + baseCheckout: async () => "/checkout/base", + ask: (async (): Promise => ({ text: JSON.stringify({ keep: 11, reason: "again", others: [{ number: 12, duplicate: true, better: [], carry: [] }] }), usage: USAGE })) as CompareDeps["ask"], + now: () => new Date(T1), + }; + db.prepare("UPDATE items SET state = 'CLOSED' WHERE repo = 'app' AND number = 13").run(); + const r = await runDupeCompare(db, {}, quiet, cd); + assert.ok(r.asked.includes("app:dupegroup:12")); + assert.deepEqual(db.prepare("SELECT number, role FROM dupe_group_items WHERE decision_id = 'app:dupegroup:12' ORDER BY number").all().map((x: any) => `${x.number}:${x.role}`), ["11:keep", "12:close"]); + assert.equal(duplicateHeld(db, "app", 12), true); + }); +}); + describe("cleanReason", () => { test("strips HTML tags, protocol-relative links and cross-repo references", () => { assert.equal(cleanReason("see this //evil.example/x and other/repo#12 plus @bob"), "see this and plus bob."); diff --git a/web/src/api/client.ts b/web/src/api/client.ts index c3ed25d7..4456e967 100644 --- a/web/src/api/client.ts +++ b/web/src/api/client.ts @@ -77,7 +77,7 @@ export interface Api { opine(id: string, input: { option: string | null; note?: string }): Promise; retract(id: string): Promise; /** Operator only: answer with the opinion's option. */ - adopt(id: string, opinionId: string): Promise; + adopt(id: string, opinionId: string, textsHash?: string): Promise; claim(id: string): Promise; thread(id: string): Promise; comment(id: string, body: string): Promise; diff --git a/web/src/api/http.ts b/web/src/api/http.ts index 18f78331..0775ceb1 100644 --- a/web/src/api/http.ts +++ b/web/src/api/http.ts @@ -113,7 +113,7 @@ export function createHttpApi(): Api { undo: (id) => post(dec(id, "/undo")), opine: (id, input) => post(dec(id, "/opinion"), input), retract: (id) => post(dec(id, "/retract")), - adopt: (id, opinionId) => post(dec(id, "/adopt"), { opinionId }), + adopt: (id, opinionId, textsHash) => post(dec(id, "/adopt"), { opinionId, textsHash }), // A claim changes nothing anyone is looking at, so open views don't refetch. claim: (id) => call("POST", dec(id, "/claim")), thread: (id) => get(dec(id, "/thread")),