Public: anyone can read this; the crew can change it.

The /next inbox's Undo covers a whole batch of decisions made in one tap #13311

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13305-the-next-inbox-s-undo-covers-a-whole-bat → main 4 commits fork 2 threads · 2 unresolved

  • Merged. scout/13305-the-next-inbox-s-undo-covers-a-whole-bat was merged into main as d003798.

Review

Sign in to review this change.
4 files changed +193 −16
src/actions/asks_bulk.ts +20 −10
@@ -25,7 +25,7 @@ import {
2525 type FleetError,
2626 type Plan,
2727 } from "./fleet.ts";
28 import { DONE, DONE_LINE_MS, DONE_NO_UNDO, DONE_UNDO, NO_SUCH_OPERATION, receiptSentence, SIGN_IN_TO_ACT } from "./asks_words.ts";
28 import { DONE, DONE_LINE_MS, DONE_NO_UNDO, DONE_UNDO, NO_SUCH_OPERATION, receiptSentence, stayedSentence, SIGN_IN_TO_ACT } from "./asks_words.ts";
2929
3030 /**
3131 * What a bulk action on asks may be: the fleet verb each one is. Decline is
@@ -190,7 +190,10 @@ export const undoable = (op: Operation, now = Date.now()): boolean =>
190190 op.undoneBy === null &&
191191 now - Date.parse(op.createdAt) <= UNDO_WINDOW_MS;
192192
193 /** The viewer's newest bulk action on asks that is not an undo, read by kind, never from a window of recent receipts. */
193 /**
194 * The viewer's newest bulk action on asks that is not an undo, read by kind, never from a window of
195 * recent receipts.
196 */
194197 const latestAsksDecision = (app: App, actor: User): Operation | undefined =>
195198 latestDecision(app.db, actor.id, { kind: "issue", queryPrefix: ASKS_QUERY_PREFIX, verbs: ASK_VERBS });
196199
@@ -213,7 +216,10 @@ export function latestUndoable(
213216 export const inDoneMinute = (createdAt: string, now = Date.now()): boolean =>
214217 now - Date.parse(createdAt) <= DONE_LINE_MS;
215218
216 /** The line a decision leaves: `Done. Undo` when it can be taken back, the reason it cannot when a merge landed. */
219 /**
220 * The line a decision leaves: `Done. Undo` when it can be taken back, the reason it cannot when a
221 * merge landed.
222 */
217223 export const doneWords = (reversible: boolean): string => (reversible ? DONE_UNDO : DONE_NO_UNDO);
218224
219225 /**
@@ -248,10 +254,14 @@ export async function undoAsksBulk(
248254 return undoFleetOp(app, actor, op.id);
249255 }
250256
251 /** The one sentence that says what a bulk action on asks, or its undo, did. */
252 export const receiptOf = (op: Operation): string =>
253 receiptSentence(
254 op.verb,
255 op.rows.filter((r) => r.outcome === "ok").length,
256 op.undoOf !== null,
257 );
257 /**
258 * The one sentence that says what a bulk action on asks, or its undo, did; an undo that left rows
259 * says which stayed.
260 */
261 export const receiptOf = (op: Operation): string => {
262 const undo = op.undoOf !== null;
263 const done = op.rows.filter((r) => r.outcome === "ok").length;
264 // "unchanged" is a row already as it should be, not one that stayed.
265 const stayed = op.rows.filter((r) => r.outcome === "refused" || r.outcome === "failed");
266 return receiptSentence(op.verb, done, undo) + (undo ? stayedSentence(stayed) : "");
267 };
src/actions/asks_words.ts +20 −2
@@ -45,14 +45,32 @@ export function receiptSentence(verb: string, done: number, undo: boolean): stri
4545 /** How long, after a decision, the page keeps its one "Done. Undo" line (#11814). */
4646 export const DONE_LINE_MS = 60_000;
4747
48 /** The first word of the line after a decision, and what the line says alone when nothing can be taken back. */
48 /**
49 * The first word of the line after a decision, and what the line says alone when nothing can be
50 * taken back.
51 */
4952 export const DONE = "Done.";
5053
5154 /** The label of the button (and the terminal's key) that takes a decision back. */
5255 export const UNDO_LABEL = "Undo";
5356
54 /** The line after a decision that can be taken back, as MCP returns it: the page shows `DONE` and a button labelled `UNDO_LABEL`. */
57 /**
58 * The line after a decision that can be taken back, as MCP returns it: the page shows `DONE` and a
59 * button labelled `UNDO_LABEL`.
60 */
5561 export const DONE_UNDO = `${DONE} ${UNDO_LABEL}`;
5662
5763 /** The line after a decision that cannot be taken back: a landed merge stays landed. */
5864 export const DONE_NO_UNDO = `${DONE} A merge that has landed cannot be taken back, so there is no Undo.`;
65
66 /** One row an undo could not put back: what it was, and why it stayed. */
67 export interface StayedRow {
68 readonly label: string;
outdated

Split this line, and filter to refused/failed rows only. Escape on the page side.

69 readonly reason: string;
70 }
71
72 const stayedRow = (r: StayedRow): string => (r.reason === "" ? r.label : `${r.label} (${r.reason})`);
73
74 /** An undo that could not put every row back says which stayed, and why. */
75 export const stayedSentence = (rows: readonly StayedRow[]): string =>
76 rows.length === 0 ? "" : `; ${rows.length} stayed: ${rows.map(stayedRow).join(", ")}`;
test/asks-bulk.test.ts +131 −3
@@ -21,9 +21,10 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts";
2121 import { getAgentByName } from "../src/core/agents.ts";
2222 import { getIssueById, listComments, type Issue } from "../src/core/issues.ts";
2323 import { assignIssue } from "../src/actions/issues.ts";
24 import { mutedRepos, recordInteraction, removeMutes } from "../src/core/interactions.ts";
25 import { listOperations } from "../src/core/fleet_ops.ts";
26 import { doneLine } from "../src/actions/asks_bulk.ts";
24 import { mutedRepos, recordInteraction, removeDismissals, removeMutes } from "../src/core/interactions.ts";
25 import { listOperations, recordOperation } from "../src/core/fleet_ops.ts";
26 import type { Operation, OperationRow, RowOutcome } from "../src/core/fleet_ops.ts";
27 import { doneLine, receiptOf, undoAsksBulk } from "../src/actions/asks_bulk.ts";
2728 import { DONE_NO_UNDO, DONE_UNDO } from "../src/actions/asks_words.ts";
2829 import { needsYouSection } from "../src/web/studio/asks_page.ts";
2930 import { getPull, type Pull } from "../src/core/pulls.ts";
@@ -593,6 +594,133 @@ describe("acting on many asks at once", () => {
593594 expect(items(alice).filter((i) => i.endsWith(pull.id))).toHaveLength(1);
594595 });
595596
597 test("one tap on three rows leaves one Done line, and one Undo opens all three again", async () => {
598 const pulls = [await greenPull(open, "Batch one"), await greenPull(quiet, "Batch two"), await greenPull(open, "Batch three")];
599 const ask = items(alice).filter((i) => pulls.some((p) => i.endsWith(p.id)));
600 expect(ask).toHaveLength(3);
601 const done = await api(aliceRw, "/api/v1/asks/bulk", { action: "decline", items: ask, preview: false });
602 expect(done.status).toBe(201);
603 expect(items(alice).filter((i) => pulls.some((p) => i.endsWith(p.id)))).toHaveLength(0);
604 const line = doneLine(app, alice);
605 expect(line?.undo).toBe(true);
606 expect(line?.operation.id).toBe(done.body.id);
607 expect(doneLine(app, alice, Date.now() + 61_000)).toBeUndefined();
608 const undone = await undoAsksBulk(app, alice, done.body.id);
609 if (!undone.ok) throw new Error(undone.error.message);
610 expect(items(alice).filter((i) => pulls.some((p) => i.endsWith(p.id)))).toHaveLength(3);
611 expect(receiptOf(undone.value)).toBe("Dismiss undone: 3");
612 });
613
614 const row = (outcome: RowOutcome, label: string, reason: string): OperationRow => ({
615 targetId: label,
616 repoId: "r",
617 label,
618 before: null,
619 after: null,
620 outcome,
621 reason,
622 });
623 const undoOp = (rows: OperationRow[]): Operation => ({
624 id: "u",
625 actorId: "a",
626 actorName: "alice",
627 query: "",
628 kind: "issue",
629 verb: "dismiss",
630 value: null,
631 reversible: true,
632 undoOf: "x",
633 undoneBy: null,
634 createdAt: new Date().toISOString(),
635 rows,
636 });
637
638 test("an undo's receipt names only refused and failed rows as stayed, never unchanged ones", () => {
639 const rows = [
640 row("ok", "a/b#1", ""),
outdated

Hand-built Operation only. Add a real undo that leaves a row behind and assert on what undoAsksBulk returns.

641 row("refused", "a/b#2", "it is gone"),
642 row("failed", "a/b#3", "disk full"),
643 row("unchanged", "a/b#4", "already so"),
644 ];
645 expect(receiptOf(undoOp(rows))).toBe("Dismiss undone: 1; 2 stayed: a/b#2 (it is gone), a/b#3 (disk full)");
646 expect(receiptOf(undoOp([row("ok", "a/b#1", ""), row("unchanged", "a/b#4", "already so")]))).toBe("Dismiss undone: 1");
647 // A decision (not an undo) says nothing of rows that stayed.
648 expect(receiptOf({ ...undoOp(rows), undoOf: null })).toBe("Dismiss: 1 done");
649 });
650
651 /**
652 * Decline two pulls in one batch, then let one row's dismissal be lifted by hand: its undo must
653 * leave it.
654 */
655 const declineAndLiftOne = async (title: string) => {
656 const pulls = [await greenPull(open, `${title} one`), await greenPull(quiet, `${title} two`)];
657 const ask = items(alice).filter((i) => pulls.some((p) => i.endsWith(p.id)));
658 expect(ask).toHaveLength(2);
659 const done = await api(aliceRw, "/api/v1/asks/bulk", { action: "decline", items: ask, preview: false });
660 expect(done.status).toBe(201);
661 const lifted = app.db
662 .query<{ item_id: string }, [string, string]>("SELECT item_id FROM interaction WHERE viewer_id = ?1 AND kind = 'dismissed' AND item_id LIKE ?2")
663 .all(alice.id, `%${pulls[0]!.id}`);
664 expect(lifted).not.toHaveLength(0);
665 removeDismissals(app.db, alice.id, lifted.map((r) => r.item_id));
666 return { id: done.body.id as string, sentence: /^Dismiss undone: 1; 1 stayed: \S+#\d+ \(changed since: .+\)$/ };
667 };
668
669 test("a real undo that leaves a row behind says so in the receipt undoAsksBulk returns", async () => {
670 const { id, sentence } = await declineAndLiftOne("Stay A");
671 const undone = await undoAsksBulk(app, alice, id);
672 if (!undone.ok) throw new Error(undone.error.message);
673 expect(undone.value.rows.map((r) => r.outcome).sort()).toEqual(["ok", "refused"]);
674 expect(receiptOf(undone.value)).toMatch(sentence);
675 });
676
677 test("the page and the MCP reply carry the stayed sentence of a real undo", async () => {
678 const onPage = await declineAndLiftOne("Stay B");
679 const page = await form(`/asks/bulk/${onPage.id}/undo`, []);
680 expect(page).toMatch(/Dismiss undone: 1; 1 stayed: [^<]+#\d+ \(changed since: [^<]+\)/);
681
682 const viaMcp = await declineAndLiftOne("Stay C");
683 const res = await fetch(`${servers.url}/mcp`, {
684 method: "POST",
685 headers: {
686 "content-type": "application/json",
687 accept: "application/json, text/event-stream",
688 authorization: `Bearer ${aliceRw}`,
689 },
690 body: JSON.stringify({
691 jsonrpc: "2.0",
692 id: 1,
693 method: "tools/call",
694 params: { name: "asks_act", arguments: { action: "undo", operation: viaMcp.id, dryRun: false } },
695 }),
696 });
697 const body = (await res.json()) as { result: { content: { text: string }[] } };
698 const reply = JSON.parse(body.result.content[0]!.text) as { receipt: string };
699 expect(reply.receipt).toMatch(viaMcp.sentence);
700 });
701
702 test("the page escapes a stayed row's label and reason", async () => {
703 const op = recordOperation(app.db, {
704 actorId: alice.id,
705 query: "asks: 2 selected",
706 kind: "issue",
707 verb: "dismiss",
708 value: null,
709 reversible: true,
710 undoOf: "none",
711 rows: [
712 row("ok", "fine", ""),
713 row("refused", "<b>label</b>", "<script>alert(1)</script>"),
714 ],
715 });
716 const headers = { cookie: `${aliceCookie}; forge_mode=studio` };
717 const res = await fetch(`${servers.url}/asks?op=${op.id}`, { headers });
718 const text = await res.text();
719 expect(text).toContain("&lt;b&gt;label&lt;/b&gt; (&lt;script&gt;alert(1)&lt;/script&gt;)");
720 expect(text).not.toContain("<script>alert(1)</script>");
721 expect(text).not.toContain("<b>label</b>");
722 });
723
596724 test("close many with one reason, written on each; undo opens them again", async () => {
597725 const a = await greenPull(open, "Close one");
598726 const b = await greenPull(quiet, "Close two");
test/tui-bulk.test.ts +22 −1
@@ -192,12 +192,33 @@ describe("bulk actions in the terminal", () => {
192192 }
193193 });
194194
195 test("u on a batch whose row changed afterwards says which row stayed", async () => {
196 const made = await createRepo(forge, alice, { name: "stays", visibility: "public" });
197 if (!made.ok) throw new Error("repo");
198 const issue = openIssue(forge, alice, made.value, { title: "Reopened meanwhile", body: "" });
199 if (!issue.ok) throw new Error(String(issue.error));
200 setAssignee(forge.db, issue.value.id, alice.id);
201 const done = await applyAsksBulk(forge, alice, "close", [`assigned:${issue.value.id}`], "done");
202 if (!done.ok) throw new Error(done.error.message);
203 forge.db.query("UPDATE issue SET state = 'open' WHERE id = ?1").run(issue.value.id);
204 const h = boot(alice);
205 try {
206 await h.open();
207 await h.press("u");
208 await settleUntil(h.app, "1 stayed");
209 expect(h.text()).toContain("alice/stays#");
210 } finally {
211 h.app.stop();
212 }
213 });
214
195215 test("a failed undo shows the action's own words and changes nothing", async () => {
196216 const op = await archiveOne("raced");
197217 const h = boot(alice);
198218 try {
199219 await h.open();
200 // Listed as undoable, then undone elsewhere before u is pressed: the list is kept for a moment.
220 // Listed as undoable, then undone elsewhere before u is pressed: the list is kept for a
221 // moment.
201222 const first = await undoFleetOp(forge, alice, op.id);
202223 if (!first.ok) throw new Error(first.error.message);
203224 await h.press("u");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs