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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14903-the-fence-reads-a-person-s-words-as-perm → main 4 commits fork 2 threads · 2 unresolved
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) | ||
| 170 | 170 | const NEGATED_CLAUSE = /(^|[^a-z0-9'])(not|no|never|don't|dont|won't|wont|without|stop)($|[^a-z0-9'])/; |
| 171 | 171 | |
| 172 | 172 | /** |
| 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). | |
| 175 | 176 | */ |
| 176 | 177 | const says = (words: string, w: string): boolean => { |
| 177 | 178 | const want = plain(w); |
| 178 | 179 | 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"); | |
| 180 | 181 | for (const m of words.matchAll(re)) { |
| 181 | 182 | if (!NEGATED.test(words.slice(0, m.index + m[1]!.length))) return true; |
| 182 | 183 | } |
| @@ -199,15 +200,28 @@ const leaves = (v: unknown): unknown[] => | ||
| 199 | 200 | Array.isArray(v) ? v.flatMap(leaves) : v !== null && typeof v === "object" ? Object.values(v).flatMap(leaves) : [v]; |
| 200 | 201 | |
| 201 | 202 | /** |
| 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. | |
| 208 | 205 | */ |
| 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 { | |
| 210 | 223 | const said = plain(words); |
| 224 | if (said.includes("?") || WONDERS.test(said)) return false; | |
| 211 | 225 | const switches = Object.keys(args).filter((k) => k !== "dryRun" && typeof args[k] === "boolean"); |
| 212 | 226 | const verb = |
| 213 | 227 | tool.split("_").every((w) => says(said, w)) || |
| @@ -216,7 +230,7 @@ export function named(words: string, tool: string, args: Record<string, unknown> | ||
| 216 | 230 | for (const [k, v] of Object.entries(args)) { |
| 217 | 231 | for (const leaf of leaves(v)) { |
| 218 | 232 | if (typeof leaf === "string") { |
| 219 | const value = k === "repo" ? (leaf.split("/").pop() ?? "") : leaf; | |
| 233 | const value = k === "repo" ? repoAsSaid(leaf, self) : leaf; | |
| 220 | 234 | if (value !== "" && !saysValue(said, value)) return false; |
| 221 | 235 | } else if (typeof leaf === "number") { |
| 222 | 236 | if (!Number.isInteger(leaf) || !saysNumber(said, leaf)) return false; |
| @@ -255,11 +269,22 @@ const NOT_ASSENT: ReadonlySet<string> = new Set(["thanks", "thank", "thx", "you" | ||
| 255 | 269 | |
| 256 | 270 | /** |
| 257 | 271 | * 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. | |
| 260 | 277 | */ |
| 261 | 278 | 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, " ") | |
| 284 | .replace(/!\[[^\]\n]*\]\[[^\]\n]*\]/g, " ") | |
| 285 | .replace(/\[([^\]\n]*)\]\([^)\n]*\)/g, "$1") | |
| 286 | .replace(/\[([^\]\n]*)\]\[[^\]\n]*\]/g, "$1") | |
| 287 | .trim(); | |
| 263 | 288 | const last = shown.split(/\n\s*\n/).at(-1) ?? ""; |
| 264 | 289 | return last.includes("?") ? last : undefined; |
| 265 | 290 | } |
| @@ -306,10 +331,7 @@ export function askedFor(words: string, proposal: string | undefined, work: Work | ||
| 306 | 331 | if (work.composed.has(k)) { |
| 307 | 332 | if (typeof leaf === "string" && leaf.toLowerCase().includes("[[data ")) return false; |
| 308 | 333 | } 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; | |
| 313 | 335 | if (value !== "" && !saysValue(scope, value)) return false; |
| 314 | 336 | } else if (typeof leaf === "number") { |
| 315 | 337 | if (!Number.isInteger(leaf) || !saysNumber(scope, leaf)) return false; |
test/conversation-fence.test.ts +39 −22
| @@ -491,42 +491,59 @@ describe("the fence's parts", () => { | ||
| 491 | 491 | |
| 492 | 492 | test("named: the verb, the repository, every number and every string must be in the person's words", () => { |
| 493 | 493 | 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); | |
| 501 | 501 | }); |
| 502 | 502 | |
| 503 | 503 | test("named: a value is said as a whole word, not inside another (#8321)", () => { |
| 504 | 504 | 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); | |
| 508 | 508 | }); |
| 509 | 509 | |
| 510 | 510 | test("named: a negation is not an ask", () => { |
| 511 | 511 | 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); | |
| 517 | 517 | }); |
| 518 | 518 | |
| 519 | 519 | test("named: a field's name is not the verb, and every string value must be said", () => { |
| 520 | 520 | // `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); | |
| 522 | 522 | 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); | |
| 525 | 525 | 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); | |
| 528 | 528 | 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); | |
| 531 | 548 | }); |
| 532 | 549 | }); |
test/conversation-holds.test.ts +18
| @@ -272,6 +272,24 @@ describe("the parts", () => { | ||
| 272 | 272 | expect(proposalIn("Go? <!-- never closed")).toBe("Go?"); |
| 273 | 273 | }); |
| 274 | 274 | |
| 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 ?")).toBe("Want me to look at ?"); | |
outdated
This asserts the alt text ( |
||
| 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 = "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 | ||
| 275 | 293 | test("askedFor: a question or a long paste asks nothing; a block read whole is not the agent's text", () => { |
| 276 | 294 | const tell = UNDOABLE["tell"]!; |
| 277 | 295 | const args = { repo: "alice/one", text: "x" }; |
new above
offline
Both link rules keep
$2for images too. The alt text of an image is not shown when the image renders, so an agent can putmerge 12 in alice/onein the alt, askShall 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.