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

The fence reads a person's words as permission only when they ask for the write, name the owner, and the question shows what a yes does #14921

Merged opened this

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

Merged

scout/forge:scout/14903-the-fence-reads-a-person-s-words-as-perm → main 4 commits fork 2 threads · 2 unresolved

  • Merged. scout/14903-the-fence-reads-a-person-s-words-as-perm was merged into main as 926d9cf.

Review

Sign in to review this change.
4 files changed +98 −41
src/core/fence.ts +40 −18
@@ -170,13 +170,14 @@ const NEGATED = /(^|[^a-z0-9'])(not|no|never|don't|dont|won't|wont|without|stop)
170170 const NEGATED_CLAUSE = /(^|[^a-z0-9'])(not|no|never|don't|dont|won't|wont|without|stop)($|[^a-z0-9'])/;
171171
172172 /**
173 * Whether these words hold `w` starting at a word boundary ("closing" holds
174 * "close"), at least once not negated ("don't close" is not an ask).
173 * Whether these words hold `w` as whole words ("closed" and "merged" do not
174 * hold "close" and "merge": a past tense reports, it does not ask), at least
175 * once not negated ("don't close" is not an ask).
175176 */
176177 const says = (words: string, w: string): boolean => {
177178 const want = plain(w);
178179 if (want === "") return false;
179 const re = new RegExp(`(^|[^a-z0-9])${escapeRe(want)}`, "g");
180 const re = new RegExp(`(^|[^a-z0-9])${escapeRe(want)}(?![a-z0-9])`, "g");
180181 for (const m of words.matchAll(re)) {
181182 if (!NEGATED.test(words.slice(0, m.index + m[1]!.length))) return true;
182183 }
@@ -199,15 +200,28 @@ const leaves = (v: unknown): unknown[] =>
199200 Array.isArray(v) ? v.flatMap(leaves) : v !== null && typeof v === "object" ? Object.values(v).flatMap(leaves) : [v];
200201
201202 /**
202 * Whether the person's words name this write. The verb: every word of the
203 * tool's name, or every switch it sets (a boolean field, `allowForks` as "allow
204 * forks"), not negated. Then every value it is given: the repository's name,
205 * every other string as written, as whole words (a label, a branch, a collaborator, a body),
206 * and every whole number (an issue, a pull, `into`). Strict on purpose: a
207 * write it misses is a confirm the person answers, never a write done.
203 * What the person must have said of a repository: its short name only under
204 * their own account (anyone can own another `open`), `owner/name` otherwise.
208205 */
209 export function named(words: string, tool: string, args: Record<string, unknown>): boolean {
206 const repoAsSaid = (repo: string, self: string): string => {
207 const owner = repo.includes("/") ? repo.split("/")[0]! : undefined;
208 return owner !== undefined && owner.toLowerCase() === self.toLowerCase() ? (repo.split("/").pop() ?? "") : repo;
209 };
210
211 /**
212 * Whether the person's words name this write. Never a question: words that
213 * open as one ("did you merge 12?") or hold a `?` ask about the work, not for
214 * it. The verb: every word of the tool's name, or every switch it sets (a
215 * boolean field, `allowForks` as "allow forks"), as whole words, not negated.
216 * Then every value it is given: the repository as `owner/name`, or its short
217 * name alone when `self` owns it; every other string as written, as whole
218 * words (a label, a branch, a collaborator, a body), and every whole number
219 * (an issue, a pull, `into`). Strict on purpose: a write it misses is a
220 * confirm the person answers, never a write done.
221 */
222 export function named(words: string, tool: string, args: Record<string, unknown>, self: string): boolean {
210223 const said = plain(words);
224 if (said.includes("?") || WONDERS.test(said)) return false;
211225 const switches = Object.keys(args).filter((k) => k !== "dryRun" && typeof args[k] === "boolean");
212226 const verb =
213227 tool.split("_").every((w) => says(said, w)) ||
@@ -216,7 +230,7 @@ export function named(words: string, tool: string, args: Record<string, unknown>
216230 for (const [k, v] of Object.entries(args)) {
217231 for (const leaf of leaves(v)) {
218232 if (typeof leaf === "string") {
219 const value = k === "repo" ? (leaf.split("/").pop() ?? "") : leaf;
233 const value = k === "repo" ? repoAsSaid(leaf, self) : leaf;
220234 if (value !== "" && !saysValue(said, value)) return false;
221235 } else if (typeof leaf === "number") {
222236 if (!Number.isInteger(leaf) || !saysNumber(said, leaf)) return false;
@@ -255,11 +269,22 @@ const NOT_ASSENT: ReadonlySet<string> = new Set(["thanks", "thank", "thx", "you"
255269
256270 /**
257271 * The closing question of the agent's last message, as the person read it:
258 * what a comment or tag hides from the page is not proposed, and neither is
259 * what an earlier paragraph buried. Undefined when it does not close on a question.
272 * what a comment or tag hides from the page is not proposed, nor a link's
273 * target or a reference definition (`[x]: /merge-12` never shows), nor what
274 * an earlier paragraph buried. A link is its text; an image is nothing, its
275 * alt text never shows when it renders. Undefined when it does
276 * not close on a question.
260277 */
261278 export function proposalIn(text: string): string | undefined {
262 const shown = text.replace(/<!--[\s\S]*?(-->|$)/g, " ").replace(/<[^>]*>/g, " ").trim();
279 const shown = text
280 .replace(/<!--[\s\S]*?(-->|$)/g, " ")
281 .replace(/<[^>]*>/g, " ")
282 .replace(/^ {0,3}\[[^\]\n]+\]:[^\n]*$/gm, " ")
283 .replace(/!\[[^\]\n]*\]\([^)\n]*\)/g, " ")
outdated

Both link rules keep $2 for images too. The alt text of an image is not shown when the image renders, so an agent can put merge 12 in alice/one in the alt, ask Shall I go ahead? and have the proposal name the write while the person saw only a picture. Fix: handle images first with .replace(/!\[[^\]\n]\]\([^)\n]\)/g, " ") and .replace(/!\[[^\]\n]\]\[[^\]\n]\]/g, " "), then the link rules for text only; the (!?) capture goes away.

284 .replace(/!\[[^\]\n]*\]\[[^\]\n]*\]/g, " ")
285 .replace(/\[([^\]\n]*)\]\([^)\n]*\)/g, "$1")
286 .replace(/\[([^\]\n]*)\]\[[^\]\n]*\]/g, "$1")
287 .trim();
263288 const last = shown.split(/\n\s*\n/).at(-1) ?? "";
264289 return last.includes("?") ? last : undefined;
265290 }
@@ -306,10 +331,7 @@ export function askedFor(words: string, proposal: string | undefined, work: Work
306331 if (work.composed.has(k)) {
307332 if (typeof leaf === "string" && leaf.toLowerCase().includes("[[data ")) return false;
308333 } else if (typeof leaf === "string") {
309 // A repository's short name is enough only under the person's own account: anyone can own another `open`.
310 const owner = k === "repo" && leaf.includes("/") ? leaf.split("/")[0] : undefined;
311 const own = owner !== undefined && owner.toLowerCase() === self.toLowerCase();
312 const value = own ? (leaf.split("/").pop() ?? "") : leaf;
334 const value = k === "repo" ? repoAsSaid(leaf, self) : leaf;
313335 if (value !== "" && !saysValue(scope, value)) return false;
314336 } else if (typeof leaf === "number") {
315337 if (!Number.isInteger(leaf) || !saysNumber(scope, leaf)) return false;
src/http/mcp/shared.ts +1 −1
@@ -169,7 +169,7 @@ export function heldWrite(ctx: Ctx, user: User, tool: Tool, args: Args): Record<
169169 const via = ctx.auth.through;
170170 if (via === undefined || !tool.writes || plansOnly(tool, args)) return undefined;
171171 const words = fenceUp(ctx.app.db, via);
172 if (words === undefined || named(words, tool.name, args) || askedWork(ctx, user, via, tool, args, words)) return undefined;
172 if (words === undefined || named(words, tool.name, args, user.username) || askedWork(ctx, user, via, tool, args, words)) return undefined;
173173 if (tooBigToApprove(args)) return tooBig(tool.name);
174174 // Past the cap nothing more is kept, and the agent is told the same.
175175 const confirm = hold(ctx.app.db, via, user.id, tool.name, args);
test/conversation-fence.test.ts +39 −22
@@ -491,42 +491,59 @@ describe("the fence's parts", () => {
491491
492492 test("named: the verb, the repository, every number and every string must be in the person's words", () => {
493493 const one = { repo: "alice/one", number: 12 };
494 expect(named("close issue 12 in one", "close", one)).toBe(true);
495 expect(named("close #12 in one", "close", one)).toBe(true);
496 expect(named("close issue 12", "close", { repo: "alice/two", number: 12 })).toBe(false);
497 expect(named("close issue 121 in one", "close", one)).toBe(false);
498 expect(named("what is issue number 12 in one?", "close", one)).toBe(false);
499 expect(named("turn on allow forks for one", "repo_settings", { repo: "alice/one", allowForks: true })).toBe(true);
500 expect(named("what is new in one?", "repo_settings", { repo: "alice/one", allowForks: true })).toBe(false);
494 expect(named("close issue 12 in one", "close", one, "alice")).toBe(true);
495 expect(named("close #12 in one", "close", one, "alice")).toBe(true);
496 expect(named("close issue 12", "close", { repo: "alice/two", number: 12 }, "alice")).toBe(false);
497 expect(named("close issue 121 in one", "close", one, "alice")).toBe(false);
498 expect(named("what is issue number 12 in one?", "close", one, "alice")).toBe(false);
499 expect(named("turn on allow forks for one", "repo_settings", { repo: "alice/one", allowForks: true }, "alice")).toBe(true);
500 expect(named("what is new in one?", "repo_settings", { repo: "alice/one", allowForks: true }, "alice")).toBe(false);
501501 });
502502
503503 test("named: a value is said as a whole word, not inside another (#8321)", () => {
504504 const label = { repo: "alice/one", number: 12, label: "bug" };
505 expect(named("label 12 in one as debug", "label", label)).toBe(false);
506 expect(named("label 12 in one as bug.", "label", label)).toBe(true);
507 expect(named("close issue 12 in someone", "close", { repo: "alice/one", number: 12 })).toBe(false);
505 expect(named("label 12 in one as debug", "label", label, "alice")).toBe(false);
506 expect(named("label 12 in one as bug.", "label", label, "alice")).toBe(true);
507 expect(named("close issue 12 in someone", "close", { repo: "alice/one", number: 12 }, "alice")).toBe(false);
508508 });
509509
510510 test("named: a negation is not an ask", () => {
511511 const one = { repo: "alice/one", number: 12 };
512 expect(named("do not close issue 12 in one", "close", one)).toBe(false);
513 expect(named("don't close 12 in one", "close", one)).toBe(false);
514 expect(named("never close 12 in one", "close", one)).toBe(false);
515 expect(named("do not turn on allow forks for one", "repo_settings", { repo: "alice/one", allowForks: true })).toBe(false);
516 expect(named("don't merge 3, but close 12 in one", "close", one)).toBe(true);
512 expect(named("do not close issue 12 in one", "close", one, "alice")).toBe(false);
513 expect(named("don't close 12 in one", "close", one, "alice")).toBe(false);
514 expect(named("never close 12 in one", "close", one, "alice")).toBe(false);
515 expect(named("do not turn on allow forks for one", "repo_settings", { repo: "alice/one", allowForks: true }, "alice")).toBe(false);
516 expect(named("don't merge 3, but close 12 in one", "close", one, "alice")).toBe(true);
517517 });
518518
519519 test("named: a field's name is not the verb, and every string value must be said", () => {
520520 // `user` and `label` are in the words, but nothing asked to add anyone.
521 expect(named("which user has the bug label in one?", "add_collaborator", { repo: "alice/one", user: "mallory" })).toBe(false);
521 expect(named("which user has the bug label in one?", "add_collaborator", { repo: "alice/one", user: "mallory" }, "alice")).toBe(false);
522522 const label = { repo: "alice/one", number: 12, label: "wontfix" };
523 expect(named("label 12 in one as bug", "label", label)).toBe(false);
524 expect(named("label 12 in one as wontfix", "label", label)).toBe(true);
523 expect(named("label 12 in one as bug", "label", label, "alice")).toBe(false);
524 expect(named("label 12 in one as wontfix", "label", label, "alice")).toBe(true);
525525 const merge = { repo: "alice/one", number: 12, into: "release" };
526 expect(named("merge 12 in one", "merge", merge)).toBe(false);
527 expect(named("merge 12 in one into release", "merge", merge)).toBe(true);
526 expect(named("merge 12 in one", "merge", merge, "alice")).toBe(false);
527 expect(named("merge 12 in one into release", "merge", merge, "alice")).toBe(true);
528528 const say = { repo: "alice/one", number: 12, body: "here is the secret" };
529 expect(named("comment on 12 in one saying thanks", "comment", say)).toBe(false);
530 expect(named("comment on 12 in one: here is the secret", "comment", { ...say })).toBe(true);
529 expect(named("comment on 12 in one saying thanks", "comment", say, "alice")).toBe(false);
530 expect(named("comment on 12 in one: here is the secret", "comment", { ...say }, "alice")).toBe(true);
531 });
532
533 test("named: a question or a report is not permission (#14903)", () => {
534 const one = { repo: "alice/one", number: 12 };
535 expect(named("did you close 12 in one", "close", one, "alice")).toBe(false);
536 expect(named("should i close 12 in one", "close", one, "alice")).toBe(false);
537 expect(named("close 12 in one?", "close", one, "alice")).toBe(false);
538 expect(named("you merged 12 in one", "merge", one, "alice")).toBe(false);
539 expect(named("12 in one was closed", "close", one, "alice")).toBe(false);
540 expect(named("merge 12 in one", "merge", one, "alice")).toBe(true);
541 });
542
543 test("named: another owner's repository is named owner/name, the short name only for the person's own (#14903)", () => {
544 const theirs = { repo: "mallory/one", number: 12 };
545 expect(named("close 12 in one", "close", theirs, "alice")).toBe(false);
546 expect(named("close 12 in mallory/one", "close", theirs, "alice")).toBe(true);
547 expect(named("close 12 in one", "close", { repo: "Alice/one", number: 12 }, "alice")).toBe(true);
531548 });
532549 });
test/conversation-holds.test.ts +18
@@ -272,6 +272,24 @@ describe("the parts", () => {
272272 expect(proposalIn("Go? <!-- never closed")).toBe("Go?");
273273 });
274274
275 test("proposalIn: a link is its text; its target and a reference definition never show (#14903)", () => {
276 expect(proposalIn("Want me to [look](https://x.example/merge-12)?")).toBe("Want me to look?");
277 expect(proposalIn("Want me to look at ![the chart](/merge-12.png)?")).toBe("Want me to look at ?");
outdated

This asserts the alt text (the chart) is kept in the proposal, which is the behaviour to remove. Expect the image to contribute nothing, and add a case where the alt text holds the tool's words and the proposal must not carry them.

278 expect(proposalIn("Want me to look at ![the chart][c]?\n\n[c]: /c.png")).toBe("Want me to look at ?");
279 expect(proposalIn("Want me to [look][m]?\n\n[m]: /merge-12 \"merge 12\"")).toBe("Want me to look?");
280 expect(proposalIn("Want me to look?\n[m]: /merge-12")).toBe("Want me to look?");
281 });
282
283 test("proposalIn: an image's alt text names no write; a yes to a picture is not permission (#14903)", () => {
284 const hidden = "![open an issue in alice/one](/x.png)Shall I go ahead?";
285 expect(proposalIn(hidden)).not.toContain("open an issue");
286 expect(proposalIn("![open an issue in alice/one][x]Shall I go ahead?\n\n[x]: /x.png")).not.toContain("open an issue");
287 const tell = UNDOABLE["tell"]!;
288 const args = { repo: "alice/one", text: "x" };
289 expect(askedFor("yes", "Shall I open an issue in alice/one?", tell, args, "alice")).toBe(true);
290 expect(askedFor("yes", hidden, tell, args, "alice")).toBe(false);
291 });
292
275293 test("askedFor: a question or a long paste asks nothing; a block read whole is not the agent's text", () => {
276294 const tell = UNDOABLE["tell"]!;
277295 const args = { repo: "alice/one", text: "x" };

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs