Skip to content

Cursor plugin specification and official plugins

TypeScript

fix(pstack): respect fenced code blocks in plan checks (#536)

Ehsan-hash87c810326a9a
diff --git a/pstack/skills/poteto-mode/scripts/check-plan.mjs b/pstack/skills/poteto-mode/scripts/check-plan.mjs
index d242f28e..c8626766 100755
--- a/pstack/skills/poteto-mode/scripts/check-plan.mjs
+++ b/pstack/skills/poteto-mode/scripts/check-plan.mjs
@@ -44,13 +44,33 @@ if (raw[0] === "---") {
}
const lines = [];
-let fence = false;
+let fence = null;
+const openingFence = (text) => {
+ const match = text.match(/^ {0,3}(`{3,}|~{3,})(.*)$/);
+ if (!match || (match[1][0] === "`" && match[2].includes("`"))) return null;
+ return { char: match[1][0], length: match[1].length };
+};
+const closingFence = (text, opening) => {
+ const match = text.match(/^ {0,3}(`+|~+)[ \t]*$/);
+ return Boolean(
+ match &&
+ match[1][0] === opening.char &&
+ match[1].length >= opening.length,
+ );
+};
for (let i = start; i < raw.length; i++) {
const text = raw[i];
const n = i + 1;
- if (/^```/.test(text)) fence = !fence;
- lines.push({ n, text, code: fence });
- if (fence) continue;
+ let code = Boolean(fence);
+ if (fence && closingFence(text, fence)) {
+ code = false;
+ fence = null;
+ } else if (!fence) {
+ fence = openingFence(text);
+ code = Boolean(fence);
+ }
+ lines.push({ n, text, code });
+ if (code) continue;
const prose = text
.replace(/`[^`]*`/g, "`")
.replace(/!\[[^\]]*\]\([^)]*\)/g, "")
diff --git a/pstack/skills/poteto-mode/scripts/check-plan.test.ts b/pstack/skills/poteto-mode/scripts/check-plan.test.ts
new file mode 100644
index 00000000..fd966c2b
--- /dev/null
+++ b/pstack/skills/poteto-mode/scripts/check-plan.test.ts
@@ -0,0 +1,96 @@
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { spawnSync } from "node:child_process";
+import { expect, test } from "bun:test";
+
+const checker = join(import.meta.dir, "check-plan.mjs");
+const rule =
+ "Tests alone are not sufficient verification. A PR is verified only when its unit, live, and perf boxes are all checked.";
+
+const validPlan = `# Demo
+
+## How to read this
+One box is one unit of work and names the evidence. Check a box only when its evidence exists. See playbooks/.
+${rule}
+
+## Program checklist
+### Arm the program
+### Spawn owners
+### PR mechanics
+### Verdict and merge
+### Boot recipe
+\`git show origin/main:\` /loop 1h status message
+
+## PR 1
+**Depends on.** None.
+**Files.**
+- [ ] Do the thing.
+**Build.**
+- [ ] Do the thing.
+**You see.**
+- [ ] Do the thing.
+**Verify, unit.** ${rule}
+- [ ] Check the thing.
+**Verify, live.** ${rule} Ten lanes on \`example-model\` at the PR head
+${Array.from({ length: 10 }, (_, i) => `- [ ] Lane ${i + 1}. Save \`lane-${i + 1}.png\`. Pass when evidence exists.`).join("\n")}
+**Verify, perf.** ${rule}
+- [ ] Metric. Recorded.
+- [ ] Probe. Recorded.
+- [ ] Baseline. Recorded.
+- [ ] Rule. Recorded.
+**Review gate.** None.
+**Merge.**
+- [ ] Merge when approved.
+
+## Close the program
+Close.
+
+## Appendix Prototype evidence
+Recorded.
+`;
+
+const runPlan = (example: string, expectedExit: number) => {
+ const directory = mkdtempSync(join(tmpdir(), "check-plan-test-"));
+ const file = join(directory, "plan.md");
+ writeFileSync(file, validPlan + example);
+ try {
+ const result = spawnSync("node", [checker, file], { encoding: "utf8" });
+ expect(result.status).toBe(expectedExit);
+ return result;
+ } finally {
+ rmSync(directory, { recursive: true, force: true });
+ }
+};
+
+test("accepts ordinary, indented, and longer closing fences", () => {
+ for (const indent of ["", " ", " ", " "])
+ runPlan(`${indent}\`\`\`markdown\n## Example only\n${indent}\`\`\`\n`, 0);
+ runPlan("```markdown\n## Example only\n````\n", 0);
+});
+
+test("accepts nested backticks and tilde info strings", () => {
+ runPlan("````markdown\n```markdown\n## Example only\n```\n````\n", 0);
+ runPlan("~~~markdown `info`\n## Example only\n ~~~\n", 0);
+});
+
+test("keeps mismatched, shorter, and suffixed closers inside the fence", () => {
+ runPlan("````markdown\n~~~\n## Example only\n````\n", 0);
+ runPlan("````markdown\n```\n## Example only\n````\n", 0);
+ runPlan("````markdown\n``` info\n## Example only\n````\n", 0);
+});
+
+test("rejects headings and prose outside a proper close", () => {
+ for (const suffix of [" ", "\t"]) {
+ const heading = runPlan(`\`\`\`markdown\n## Example only\n\`\`\`${suffix}\n## Example only\n`, 1);
+ expect(heading.stderr).toContain('"## Example only" after Close the program is not an appendix');
+ }
+
+ const prose = runPlan("```markdown\nThis prose has a mid-sentence colon: inside code\n```\nThis prose has a mid-sentence colon: bad.\n", 1);
+ expect(prose.stderr).toContain("mid-sentence colon");
+});
+
+test("rejects backtick info strings containing a backtick", () => {
+ const result = runPlan("```markdown `invalid`\n## Example only\n```\n", 1);
+ expect(result.stderr).toContain('"## Example only" after Close the program is not an appendix');
+});
diff --git a/pstack/skills/poteto-mode/scripts/package.json b/pstack/skills/poteto-mode/scripts/package.json
index 8a3dfe27..d8abe49a 100644
--- a/pstack/skills/poteto-mode/scripts/package.json
+++ b/pstack/skills/poteto-mode/scripts/package.json
@@ -3,7 +3,7 @@
"private": true,
"type": "module",
"scripts": {
- "test": "bun test orch watch-pr",
+ "test": "bun test orch watch-pr check-plan.test.ts",
"typecheck": "tsc --project watch-pr/tsconfig.json --noEmit --strict"
},
"dependencies": {