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

A tell from an app's page carries the app and the page it was sent from #15124

Merged opened this

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

Merged

scout/forge:scout/15092-a-tell-from-an-app-s-page-carries-the-ap → main 5 commits fork 1 thread · 1 unresolved

  • Merged. scout/15092-a-tell-from-an-app-s-page-carries-the-ap was merged into main as 3234dde.

Review

Sign in to review this change.
8 files changed +107 −51
src/actions/tell_context.ts +30 −1
@@ -6,7 +6,7 @@
66 * the tell as a plain block the issue shows and the agent reads.
77 */
88
9 import { APP_SLUG, previewLine } from "./tell_from.ts";
9 import { APP_SLUG, parseFrom, previewLine } from "./tell_from.ts";
1010
1111 export interface TellContext {
1212 readonly page?: string;
@@ -85,6 +85,19 @@ export function scrubQuery(query: string): string {
8585 return kept.length === 0 ? "" : `${lead}${kept.join("&")}`;
8686 }
8787
88 /**
89 * The context of a tell said on a page, from the raw page and pull a door was given. What does not
90 * check is dropped, never refused: the tell still goes, and `dropped` says something was left off
91 * so the door can say so once. The web and the terminal both call this.
92 */
93 export function whereTold(page: string, pull: string): { where: TellContext | undefined; dropped: boolean } {
94 const n = pull === "" ? undefined : /^\d{1,9}$/.test(pull) ? Number(pull) : -1;
95 const where =
96 parseContext({ page, ...(n === undefined ? {} : { pull: n }) }) ?? parseContext({ page }) ?? undefined;
97 const dropped = (page !== "" && where?.page === undefined) || (pull !== "" && where?.pull === undefined);
98 return { where, dropped };
99 }
100
88101 /** The checked context, nothing for none, or null when a field is malformed. */
89102 export function parseContext(v: unknown): TellContext | undefined | null {
90103 if (v === undefined) return undefined;
@@ -144,6 +157,7 @@ export function contextNote(c: TellContext | undefined, opts: { preview?: boolea
144157 const preview = opts.preview === true ? previewLine(c.pull, c.page, app) : "";
145158 const lines = [
146159 preview !== "" ? `- ${preview}` : "",
160 preview === "" ? fromLine(c.page, opts.app) : "",
147161 preview !== "" || c.page === undefined ? "" : `- page: ${c.page}`,
148162 preview !== "" || c.pull === undefined ? "" : `- pull: #${c.pull}`,
149163 preview !== "" || app === undefined || !APP_SLUG.test(app)
@@ -157,3 +171,18 @@ export function contextNote(c: TellContext | undefined, opts: { preview?: boolea
157171 ].filter((l) => l !== "");
158172 return lines.length === 0 ? "" : `\n\n---\nWhere this was told from:\n${lines.join("\n")}`;
159173 }
174
175 /**
176 * "- from <app> <path>": the app and the path of the page a tell was said on; empty unless both
177 * check.
178 */
179 function fromLine(page: string | undefined, app: string | undefined): string {
180 if (page === undefined || app === undefined || !APP_SLUG.test(app)) return "";
181 const url = URL.parse(page);
182 if (url === null) return "";
183 const path = url.pathname.replace(
184 /[\\\s\x00-\x1f\x7f<>()\[\]`"'*_~|!#&]/g,
185 (ch) => `%${ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`,
186 );
187 return parseFrom(path) === path ? `- from ${app} ${path}` : "";
188 }
src/actions/tell_words.ts +2 −2
@@ -7,8 +7,8 @@
77 /** A tell that failed for a reason nobody can be told about. */
88 export const NOT_TOLD_WORDS = "That could not be told.";
99
10 /** A tell from a page whose context could not be read. */
11 export const NOT_TOLD_FROM_WORDS = "That page could not be told from.";
10 /** Said after a sent tell whose page or pull did not check, and so was left off. */
11 export const WITHOUT_WHERE_WORDS = "It was sent without where it was told from.";
1212
1313 /** A tell, reply or report sent with nothing in it. */
1414 export const SAY_SOMETHING_WORDS = "Say something first.";
src/tui/screens/tell.ts +18 −19
@@ -4,14 +4,15 @@
44 * The row is the screen's own and only for who `mayDirectCrew` there — the
55 * gate `tellRow` in `src/web/scope.ts` asks. `from` is null: a terminal
66 * screen has no page path of its own. A second row tells from a page: it asks
7 * for the page's address and the pull, checked and written into the tell by
8 * the same `parseContext` and `contextNote` the pill's tell and the MCP use.
7 * for the page's address and the pull, checked by `whereTold` (what does not
8 * check is dropped, as on the page) and written into the tell by `contextNote` the pill's tell and
9 * the MCP use.
910 */
1011
1112 import { SENT_WORDS, tell } from "../../actions/tell.ts";
1213 import { tellAnswer } from "../../actions/tell_answer.ts";
13 import { contextNote, parseContext } from "../../actions/tell_context.ts";
14 import { NOT_TOLD_FROM_WORDS, SAY_SOMETHING_WORDS } from "../../actions/tell_words.ts";
14 import { contextNote, parseContext, whereTold } from "../../actions/tell_context.ts";
15 import { SAY_SOMETHING_WORDS, WITHOUT_WHERE_WORDS } from "../../actions/tell_words.ts";
1516 import type { Repo } from "../../core/repos.ts";
1617 import { mayDirectCrew } from "../../crew/direct.ts";
1718 import type { Ctx, Screen } from "../app/app.ts";
@@ -55,7 +56,6 @@ export function tellCommands(f: Session, ctx: Ctx, parent: () => Screen, repo: R
5556 label: name,
5657 hint: "http(s) address of the page; enter continues",
5758 onSubmit: (page) => {
58 if (parseContext({ page }) === null) return "That is not an http(s) address.";
5959 return later(ctx, () =>
6060 promptScreen(ctx, parent(), {
6161 title: "Pull",
@@ -63,8 +63,6 @@ export function tellCommands(f: Session, ctx: Ctx, parent: () => Screen, repo: R
6363 hint: "the pull number on a preview, or leave empty",
6464 onSubmit: (pull) => {
6565 const number = pull === "" ? undefined : /^\d{1,9}$/.test(pull) ? Number(pull) : -1;
66 if (parseContext({ page, ...(number === undefined ? {} : { pull: number }) }) === null)
67 return "A pull is a number, like 7.";
6866 return later(ctx, () =>
6967 promptScreen(ctx, parent(), {
7068 title: "App",
@@ -72,7 +70,6 @@ export function tellCommands(f: Session, ctx: Ctx, parent: () => Screen, repo: R
7270 hint: "the app's name, or leave empty; enter continues",
7371 onSubmit: (appName) => {
7472 const app = appName === "" ? undefined : appName;
75 if (parseContext({ page, app }) === null) return "An app is a name like notes.";
7673 return later(ctx, () =>
7774 promptScreen(ctx, parent(), {
7875 title: "Tell the crew",
@@ -107,17 +104,19 @@ export async function tellFromPage(
107104 pull: number | undefined,
108105 app?: string,
109106 ): Promise<void> {
110 const where = parseContext({
111 page,
112 ...(pull === undefined ? {} : { pull }),
113 ...(app === undefined ? {} : { app }),
114 });
115 if (where === null || where === undefined) {
116 ctx.toast(NOT_TOLD_FROM_WORDS, "danger");
117 return;
118 }
119 const answer = tellAnswer(await tell(f.app, f.user, repo, `${said}${contextNote(where, { preview: true })}`));
120 ctx.toast(answer.text, answer.tone);
107 // A name that does not check is dropped like a bad page: the tell goes under the repository's
108 // name.
109 const named = app !== undefined && parseContext({ app }) !== null;
110 const { where, dropped } = whereTold(page, pull === undefined ? "" : String(pull));
111 const told = await tell(
112 f.app,
113 f.user,
114 repo,
115 `${said}${contextNote(where, { preview: true, app: named ? app : repo.name })}`,
116 );
117 const answer = tellAnswer(told);
118 const left = dropped || (app !== undefined && !named);
119 ctx.toast(left && told.ok ? `${answer.text} ${WITHOUT_WHERE_WORDS}` : answer.text, answer.tone);
121120 }
122121
123122 /** A client's "Ask for something" on the first screen: one line to the app's crew, answered in the web's words. */
src/web/studio/actions.ts +6 −10
@@ -17,9 +17,9 @@ import { addComment } from "../../actions/issues.ts";
1717 import { overTellLimit, parseFrom } from "../../actions/tell_from.ts";
1818 import { SENT_WORDS, tell, toldWords, TOO_LONG_WORDS } from "../../actions/tell.ts";
1919 import { TAKEN_BACK_WORDS, TOO_LATE_WORDS, tellUndoable, undoTell } from "../../actions/tell_undo.ts";
20 import { contextNote, parseContext } from "../../actions/tell_context.ts";
20 import { contextNote, whereTold } from "../../actions/tell_context.ts";
2121 import { refusalWords } from "../../actions/tell_answer.ts";
22 import { NOT_TOLD_FROM_WORDS, SAY_SOMETHING_WORDS } from "../../actions/tell_words.ts";
22 import { SAY_SOMETHING_WORDS, WITHOUT_WHERE_WORDS } from "../../actions/tell_words.ts";
2323 import { keepThought } from "../../actions/thought.ts";
2424 import { mergePull } from "../../actions/pulls.ts";
2525 import { revertPull } from "../../actions/revert.ts";
@@ -172,19 +172,15 @@ export async function tellSubmit(ctx: Ctx): Promise<Response> {
172172 * tell, answered with a receipt that links to the issue it opened.
173173 */
174174 async function tellFromPage(ctx: Ctx, repo: Repo, form: Form): Promise<Response> {
175 const pull = field(form, "pull");
176 const where = parseContext({
177 page: field(form, "page"),
178 ...(pull === "" ? {} : { pull: /^\d{1,9}$/.test(pull) ? Number(pull) : -1 }),
179 });
175 // A page that does not check, or a pull that does not, is dropped: the
176 // tell is still sent, only without where it was said.
177 const { where, dropped } = whereTold(field(form, "page"), field(form, "pull"));
180178 // A refusal sends the person back to the box with their words, the page
outdated

The web door now drops a bad page. src/tui/screens/tell.ts still refuses with NOT_TOLD_FROM_WORDS, so the two doors disagree. Share this fallback (for example in tell_context.ts) and use it in both.

181179 // and the pull kept, so they can retry.
182180 const keep = new URLSearchParams({ text: field(form, "text").slice(0, 1000) });
183181 if (where?.page !== undefined) keep.set("page", where.page);
184182 if (where?.pull !== undefined) keep.set("pull", String(where.pull));
185183 const back = `${repoBase(repo)}/tell?${keep}`;
186 if (where === null || where === undefined)
187 return flashTo(ctx, back, { tone: "danger", message: NOT_TOLD_FROM_WORDS });
188184 const text = field(form, "text").trim();
189185 if (text === "") return flashTo(ctx, back, { tone: "danger", message: SAY_SOMETHING_WORDS });
190186 if (overTellLimit(text)) return flashTo(ctx, back, { tone: "danger", message: TOO_LONG_WORDS });
@@ -200,7 +196,7 @@ async function tellFromPage(ctx: Ctx, repo: Repo, form: Form): Promise<Response>
200196 title: `Issue opened · ${repo.name}`,
201197 crumbs: crumbsFor(repo),
202198 body: html`${pageHead("Issue opened")}
203 <p>${toldWords(told.value)}</p>
199 <p>${toldWords(told.value)}${dropped ? ` ${WITHOUT_WHERE_WORDS}` : ""}</p>
204200 <p><a href="${issue}" data-receipt>See issue #${String(told.value.issue.number)}</a></p>
205201 ${
206202 tellUndoable(ctx.app, repo, told.value.issue)
test/apps-toolbar-context.test.ts +9
@@ -22,4 +22,13 @@ describe("a tell's context line names the app", () => {
2222 expect(n).not.toContain("app:");
2323 expect(n.split("\n").find((l) => l.startsWith("- from"))!).not.toMatch(/[()\[\]*<>]/);
2424 });
25 test("production's page says 'from <app> <path>', without query or host", () => {
26 const n = contextNote({ page: "https://n.apps.test/a/b?x=1" }, { preview: true, app: "notes" });
27 expect(n).toContain("- from notes /a/b\n");
28 });
29 test("a hostile path or app is encoded or dropped from the from line", () => {
30 const bad = contextNote({ page: "https://n.apps.test/x)[e](http://e.test)*<b>" }, { preview: true, app: "notes" });
31 expect(bad.split("\n").find((l) => l.startsWith("- from "))!).not.toMatch(/[()\[\]*<>]/);
32 expect(contextNote({ page: "https://n.apps.test/a" }, { preview: true, app: "a](x)" })).not.toContain("- from ");
33 });
2534 });
test/apps-toolbar-tell.test.ts +18 −8
@@ -185,16 +185,26 @@ describe("tell the crew from the pill", () => {
185185 const issue = listIssues(app.db, repoId, {}).find((i) => i.title === "Hostile path")!;
186186 const line = issue.body.split("\n").find((l) => l.startsWith("- from preview"))!;
187187 expect(line).not.toMatch(/[()\[\]*<>]/);
188 const before = told().length;
189 expect((await post({ text: "x", page: "https://a.test/", pull: "3; drop" })).status).toBe(303);
190 expect(told().length).toBe(before);
191188 });
192189
193 test("a bad page or pull opens nothing", async () => {
194 const before = told().length;
195 expect((await post({ text: "x", page: "javascript:alert(1)" })).status).toBe(303);
196 expect((await post({ text: "x", page: "https://a.test/", pull: "-1" })).status).toBe(303);
197 expect(told().length).toBe(before);
190 test("a bad page or pull is dropped, not an error: the tell is sent without it", async () => {
191 expect((await post({ text: "Dropped page", page: "javascript:alert(1)" })).status).toBe(200);
192 const a = listIssues(app.db, repoId, {}).find((i) => i.title === "Dropped page")!;
193 expect(a.body).not.toContain("Where this was told from");
194 const flash = await (await post({ text: "Dropped again", page: "javascript:alert(1)" })).text();
195 expect(flash).toContain("It was sent without where it was told from.");
196 expect((await post({ text: "Dropped pull", page: "https://notes.apps.test/k", pull: "3; drop" })).status).toBe(200);
197 const b = listIssues(app.db, repoId, {}).find((i) => i.title === "Dropped pull")!;
198 expect(b.body).toContain("- from notes /k");
199 expect(b.body).toBe(
200 "---\nWhere this was told from:\n- from notes /k\n- page: https://notes.apps.test/k\n- app: notes",
201 );
202 });
203
204 test("a tell from production stores 'from <app> <path>'", async () => {
205 await post({ text: "Carried", page: "https://notes.apps.test/deep/page?q=1" });
206 const i = listIssues(app.db, repoId, {}).find((x) => x.title === "Carried")!;
207 expect(i.body).toContain("- from notes /deep/page\n");
198208 });
199209
200210 test("a refused tell sends the person back with their words, the page and the pull kept", async () => {
test/tell-words.test.ts +3 −3
@@ -8,14 +8,14 @@ import { readFileSync } from "node:fs";
88 import { join } from "node:path";
99 import {
1010 NOT_SENT_WORDS,
11 NOT_TOLD_FROM_WORDS,
1211 NOT_TOLD_WORDS,
1312 SAY_SOMETHING_WORDS,
13 WITHOUT_WHERE_WORDS,
1414 } from "../src/actions/tell_words.ts";
1515
1616 const SENTENCES = {
1717 NOT_TOLD_WORDS: "That could not be told.",
18 NOT_TOLD_FROM_WORDS: "That page could not be told from.",
18 WITHOUT_WHERE_WORDS: "It was sent without where it was told from.",
1919 SAY_SOMETHING_WORDS: "Say something first.",
2020 NOT_SENT_WORDS: "That could not be sent.",
2121 };
@@ -33,7 +33,7 @@ const read = (path: string): string => readFileSync(join(import.meta.dir, "..",
3333
3434 describe("tell words", () => {
3535 test("the module says each sentence word for word", () => {
36 expect({ NOT_TOLD_WORDS, NOT_TOLD_FROM_WORDS, SAY_SOMETHING_WORDS, NOT_SENT_WORDS }).toEqual(SENTENCES);
36 expect({ NOT_TOLD_WORDS, WITHOUT_WHERE_WORDS, SAY_SOMETHING_WORDS, NOT_SENT_WORDS }).toEqual(SENTENCES);
3737 });
3838
3939 for (const door of DOORS) {
test/tui-tell.test.ts +21 −8
@@ -97,20 +97,33 @@ describe("tell the crew in the terminal", () => {
9797 expect(await palette(bob, [":", "from a page"])).not.toContain("Tell the crew from a page");
9898 });
9999
100 test("a tell from a page can name the app, and a bad name is refused", async () => {
100 test("a tell from a page can name the app, and a bad name is dropped", async () => {
101101 const page = [":", "from a page", "\r", ..."https://notes.apps.test/a".split(""), "\r", "\r"];
102102 await palette(alice, [...page, ..."notes".split(""), "\r", ..."name the app".split(""), "\r"]);
103103 const issue = listIssues(app.db, repo.id, {}).find((i) => i.title === "name the app")!;
104104 expect(issue.body).toContain("- app: notes");
105 const text = await palette(alice, [...page, ..."<b>".split(""), "\r"]);
106 expect(text).toContain("An app is a name like notes.");
105 const text = await palette(alice, [...page, ..."<b>".split(""), "\r", ..."bad app name".split(""), "\r"]);
106 expect(text).toContain("It was sent without");
107 const bad = listIssues(app.db, repo.id, {}).find((i) => i.title === "bad app name")!;
108 expect(bad.body).not.toContain("<b>");
107109 });
108110
109 test("a page that is not http(s) keeps the prompt open and tells nothing", async () => {
110 const before = listIssues(app.db, repo.id, {}).length;
111 const text = await palette(alice, [":", "from a page", "\r", ..."javascript:alert(1)".split(""), "\r"]);
112 expect(text).toContain("That is not an http(s) address.");
113 expect(listIssues(app.db, repo.id, {}).length).toBe(before);
111 test("a page that is not http(s) is dropped: the tell is sent, and says it went without it", async () => {
112 const text = await palette(alice, [":", "from a page", "\r", ..."javascript:alert(1)".split(""), "\r", "\r", "\r", ..."bad page tell".split(""), "\r"]);
113 // the toast is cut at the screen's width, so the start of the sentence is what shows
114 expect(text).toContain("It was sent without");
115 const issue = listIssues(app.db, repo.id, {}).find((i) => i.title === "bad page tell")!;
116 expect(issue.body).not.toContain("Where this was told from");
117 });
118
119 test("a bad pull is dropped, the good page kept", async () => {
120 const text = await palette(alice, [":", "from a page", "\r", ..."https://notes.apps.test/k".split(""), "\r", ..."x7".split(""), "\r", "\r", ..."bad pull tell".split(""), "\r"]);
121 // the toast is cut at the screen's width, so the start of the sentence is what shows
122 expect(text).toContain("It was sent without");
123 const issue = listIssues(app.db, repo.id, {}).find((i) => i.title === "bad pull tell")!;
124 expect(issue.body).toBe(
125 "---\nWhere this was told from:\n- from lobby /k\n- page: https://notes.apps.test/k\n- app: lobby",
126 );
114127 });
115128
116129 test("an empty line says so and tells nothing", async () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs