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

On /asked, each thought says where it went, linked: the issue it became, kept as a note, or not placed yet, as MCP `thought` answers #7539

Parked opened this
Closed without merging

mason/forge:mason/7509-on-asked-each-thought-says-where-it-went → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +171 −19
src/actions/ask_state_words.ts +6
@@ -9,3 +9,9 @@ export const NOT_PLACED_YET = "the planner has not placed it yet";
99
1010 /** An ask whose issue has a run going and no answer yet. */
1111 export const CREW_WORKING = "The crew is working on it.";
12
13 /** A thought's line on /asked while the planner has not reached it (#7509). */
14 export const THOUGHT_NOT_PLACED = "not placed yet";
15
16 /** A thought's line on /asked when the planner placed it on no issue. */
17 export const THOUGHT_KEPT = "kept as a note";
src/core/placements.ts +9 −3
@@ -73,11 +73,17 @@ export function isVisionPull(db: Database, repo: string, number: number): boolea
7373 return rows.some((r) => toPlacement(r).placed.some((p) => p.action === "proposed" && p.repo === repo && p.pull === number));
7474 }
7575
76 /** The placements of these thoughts: one query per chunk, never one per thought (#7509). */
7677 export function placementsOf(db: Database, thoughtIds: readonly string[]): Map<string, Placement> {
7778 const out = new Map<string, Placement>();
78 for (const id of thoughtIds) {
79 const p = placementOf(db, id);
80 if (p) out.set(id, p);
79 const unique = [...new Set(thoughtIds)];
80 const CHUNK = 400;
81 for (let i = 0; i < unique.length; i += CHUNK) {
82 const batch = unique.slice(i, i + CHUNK);
83 const rows = db
84 .query<Row, string[]>(`SELECT * FROM placement WHERE thought_id IN (${batch.map(() => "?").join(",")})`)
85 .all(...batch);
86 for (const r of rows) out.set(r.thought_id, toPlacement(r));
8187 }
8288 return out;
8389 }
src/web/studio/asked_page.ts +39 −16
@@ -27,7 +27,7 @@ import {
2727 type StreamThought,
2828 type ThoughtState,
2929 } from "../../core/thought_stream.ts";
30 import { NOT_PLACED_YET } from "../../actions/ask_state_words.ts";
30 import { THOUGHT_KEPT, THOUGHT_NOT_PLACED } from "../../actions/ask_state_words.ts";
3131 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
3232 import type { Ctx } from "../../http/context.ts";
3333 import { repoBase, signInFirst } from "../chrome.ts";
@@ -53,18 +53,42 @@ const THOUGHT_WORD: Record<ThoughtState, { text: string; tone: Tone }> = {
5353 superseded: { text: "Superseded", tone: "default" },
5454 };
5555
56 /** Where a thought went: each issue it touched, linked when the viewer may read it. */
57 function placedLinks(t: StreamThought): Html {
58 const links = t.placed.map((x) =>
59 !x.readable
60 ? html`${x.action} in a repository you cannot read`
61 : x.title === null
62 ? html`${x.action} ${x.repo} #${x.number}`
63 : html`${x.action} <a href="/${x.repo}/${x.pull === x.number ? "pulls" : "issues"}/${x.number}">${x.repo} #${x.number}</a>`,
64 );
65 return links.length === 0
66 ? html`${t.reply ?? NOT_PLACED_YET}`
67 : html`${links.map((l, i) => html`${i > 0 ? " · " : ""}${l}`)}`;
56 /** A thought's first words, for naming it in another thought's line. */
57 export function firstWords(text: string): string {
58 const words = text.replace(/\s+/g, " ").trim();
59 if (words.length <= 40) return words;
60 const space = words.lastIndexOf(" ", 39);
61 return `${words.slice(0, space > 0 ? space : 39)}…`;
62 }
63
64 /** The actions that put the thought on an issue; any other is said before the arrow. */
65 const GOES_TO = new Set(["opened", "linked", "proposed"]);
66
67 /**
68 * Where a thought went, as one short line (#7509): `→ owner/name#N title`
69 * linked for each issue it touched, `kept as a note` when the planner placed
70 * nothing, `not placed yet` while it waits; then the earlier thought it
71 * repeats or takes back, by its first words.
72 */
73 export function wentTo(t: StreamThought): Html {
74 const where =
75 t.placedAt === null
76 ? html`${THOUGHT_NOT_PLACED}`
77 : t.placed.length === 0
78 ? html`<span title="${t.reply ?? ""}">${THOUGHT_KEPT}</span>`
79 : html`${t.placed.map((x, i) => {
80 const verb = GOES_TO.has(x.action) ? null : html`${x.action} `;
81 const name = `${x.repo}#${x.number}`;
82 const to = !x.readable
83 ? html`an issue in a repository you cannot read`
84 : x.title === null
85 ? html`${name}`
86 : html`<a href="/${x.repo}/${x.pull === x.number ? "pulls" : "issues"}/${x.number}">${name} ${x.title}</a>`;
87 return html`${i > 0 ? " · " : ""}${verb}→ ${to}`;
88 })}`;
89 return html`${where}${t.repeats ? html` · repeats “${firstWords(t.repeats.text)}”` : null}${t.supersedes.map(
90 (x) => html` · takes back “${firstWords(x.text)}”`,
91 )}`;
6892 }
6993
7094 export function thoughtRow(t: StreamThought, days: number): Html {
@@ -78,11 +102,10 @@ export function thoughtRow(t: StreamThought, days: number): Html {
78102 ? `Not placed ${days} days after it was sent`
79103 : `Nothing merged ${days} days after it was placed`
80104 : t.text,
81 meta: html`${time(t.receivedAt)} · ${t.source} · ${placedLinks(t)}${t.supersededBy
105 meta: html`${time(t.receivedAt)} · ${t.source}${t.supersededBy
82106 ? html` · superseded by “${t.supersededBy.text.slice(0, 80)}” ${time(t.supersededBy.receivedAt)}`
83 : null}${t.repeats ? html` · said again, first on ${time(t.repeats.receivedAt)}` : null}${t.supersedes.length > 0
84 ? html` · takes back “${t.supersedes[0]!.text.slice(0, 80)}” ${time(t.supersedes[0]!.receivedAt)}`
85107 : null}`,
108 metaLine: wentTo(t),
86109 trail: badge(word.text, { tone: word.tone }),
87110 });
88111 }
src/web/ui.ts +3
@@ -925,6 +925,8 @@ export interface RowOptions {
925925 href?: string;
926926 title: Html | string;
927927 meta?: Html;
928 /** A second `.row-meta` line under `meta`. */
929 metaLine?: Html;
928930 /** Draw `.row-meta` even when `meta` is empty, for a script to write into. */
929931 metaSlot?: boolean;
930932 lead?: Html;
@@ -962,6 +964,7 @@ export function row(o: RowOptions): Html {
962964 >${o.title}</span
963965 >
964966 ${o.meta || o.metaSlot ? html`<span class="row-meta">${o.meta}</span>` : null}
967 ${o.metaLine ? html`<span class="row-meta">${o.metaLine}</span>` : null}
965968 </span>
966969 ${o.trail && o.trail.text.trim() !== ""
967970 ? html`<span class="row-trail">${o.trail}</span>`
test/asked-placement.test.ts added +114
@@ -0,0 +1,114 @@
1 /** On /asked each thought says where it went (#7509): the issue it became, kept as a note, or not placed yet. */
2
3 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
4 import { mkdtemp, rm } from "node:fs/promises";
5 import { tmpdir } from "node:os";
6 import { join } from "node:path";
7 import { createApp, type App } from "../src/app.ts";
8 import { openIssue } from "../src/actions/issues.ts";
9 import { createRepo } from "../src/actions/repos.ts";
10 import { placementsOf, savePlacement, type Placed } from "../src/core/placements.ts";
11 import { linkThoughts } from "../src/core/thought_links.ts";
12 import { thoughtStream } from "../src/core/thought_stream.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { serve } from "../src/http/server.ts";
15 import { firstWords, thoughtRow } from "../src/web/studio/asked_page.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17 import { promote } from "./helpers/members.ts";
18
19 quietLogs();
20
21 describe("/asked: where each thought went", () => {
22 let dir = "";
23 let app: App;
24 let alice: User;
25 let bob: User;
26 let n = 0;
27
28 beforeAll(async () => {
29 dir = await mkdtemp(join(tmpdir(), "asked-placement-"));
30 app = createApp({ dataDir: dir, port: 0 });
31 const mk = async (username: string, first = false) => {
32 const u = await createUser(app.db, {
33 origin: "seed",
34 trusted: true, username, email: `${username}@forge.local`,
35 password: "a-good-passphrase", adminIfFirst: first,
36 });
37 if (!u.ok) throw new Error(u.error);
38 promote(app.db, u.value);
39 return u.value;
40 };
41 // The first account is the admin; neither viewer here is it.
42 await mk("carol", true);
43 alice = await mk("alice");
44 bob = await mk("bob");
45 });
46 afterAll(async () => {
47 app.db.close();
48 await rm(dir, { recursive: true, force: true });
49 });
50
51 const thought = (text: string, who = alice) => {
52 const id = `t${++n}`;
53 app.db
54 .query("INSERT INTO thought (id, author, text, source, received_at) VALUES (?1, ?2, ?3, 'composer', ?4)")
55 .run(id, who.id, text, new Date(Date.now() - (100 - n) * 1000).toISOString());
56 return id;
57 };
58 const place = (id: string, placed: Placed[]) =>
59 savePlacement(app.db, {
60 thoughtId: id, kind: "work", placed, reply: "noted for later", asked: false, decidedBy: "rule",
61 placedAt: new Date().toISOString(),
62 });
63 const signIn = async (url: string, username: string) => {
64 const res = await fetch(`${url}/signin`, {
65 method: "POST",
66 body: new URLSearchParams({ username, password: "a-good-passphrase" }),
67 redirect: "manual",
68 });
69 return (res.headers.get("set-cookie") ?? "").split(";")[0]!;
70 };
71
72 test("the issue it became, linked; a repeat says so by first words; unplaced says it waits", async () => {
73 const made = await createRepo(app, alice, { name: "garden" });
74 if (!made.ok) throw new Error(String(made.error));
75 const issue = openIssue(app, alice, made.value, { title: "Water the <tomatoes>" });
76 if (!issue.ok) throw new Error(String(issue.error));
77 const first = thought("water the tomatoes every morning before work please");
78 const again = thought("did you hear me about the tomatoes");
79 const waiting = thought("and the roses too");
80 place(first, [{ action: "opened", repo: "alice/garden", number: issue.value.number }]);
81 place(again, []);
82 linkThoughts(app.db, again, first, "repeats");
83
84 expect([...placementsOf(app.db, [first, again, waiting]).keys()].sort()).toEqual([first, again].sort());
85
86 const stream = thoughtStream(app.db, alice);
87 const line = (id: string) => String(thoughtRow(stream.find((t) => t.id === id)!, 3)).replace(/\s+/g, " ");
88 const href = `/alice/garden/issues/${issue.value.number}`;
89 expect(line(first)).toContain(`→ <a href="${href}">alice/garden#${issue.value.number} Water the &lt;tomatoes&gt;</a>`);
90 expect(line(again)).toContain("kept as a note");
91 expect(line(again)).toContain(`repeats “${firstWords("water the tomatoes every morning before work please")}”`);
92 expect(firstWords("water the tomatoes every morning before work please")).toBe("water the tomatoes every morning before…");
93 expect(line(waiting)).toContain("not placed yet");
94 expect(line(waiting)).not.toContain("repeats");
95
96 const servers = await serve(app);
97 try {
98 const page = async (who: string) =>
99 (await (await fetch(`${servers.url}/asked`, { headers: { cookie: await signIn(servers.url, who) } })).text())
100 .replace(/\s+/g, " ");
101 const mine = await page("alice");
102 expect(mine).toContain(`href="${href}"`);
103 expect(mine).toContain("repeats “water the tomatoes");
104 expect(mine).toContain("not placed yet");
105 // Only your own thoughts carry where they went.
106 thought("bob's own idea", bob);
107 const his = await page("bob");
108 expect(his).toContain("bob&#39;s own idea");
109 expect(his).not.toContain("tomatoes");
110 } finally {
111 servers.stop();
112 }
113 });
114 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs