Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/shared/src/proposedPlanText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,22 @@ it("takes the plan title from headings outside HTML comments", () => {
}
expect(proposedPlanTitle("<!--\n# Hidden title\n# Release plan")).toBeNull();
expect(stripDisplayedPlanMarkdown("<!--\n# Hidden title\n# Release plan")).toBe("");
expect(stripDisplayedPlanMarkdown("<!--\nm\n--> note\n# Release plan\n\nBody")).toBe(
"<!--\nm\n--> note\n# Release plan\n\nBody",
);
});

it("keeps text on a comment's closing line while removing the plan title", () => {
for (const [preamble, titleBlock, displayedPreamble = preamble] of [
["<!-- notes --> note", "# Release plan\n"],
["<!--\n# Hidden title\n--> note", "\n#\n# Release plan\n\n## Summary\n"],
[
"<!-- first --> note\n#\n<!-- second --> more",
"# Release plan\n",
"<!-- first --> note\n<!-- second --> more",
],
]) {
const markdown = `${preamble}\n${titleBlock}\nBody`;
expect(proposedPlanTitle(markdown)).toBe("Release plan");
expect(stripDisplayedPlanMarkdown(markdown)).toBe(`${displayedPreamble}\n\nBody`);
}
});

it("skips empty plan headings without reading into the next line", () => {
Expand Down
23 changes: 19 additions & 4 deletions packages/shared/src/proposedPlanText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,25 @@ export function stripDisplayedPlanMarkdown(planMarkdown: string): string {
.split(/\r?\n/);
let start = 0;
let commentStart = -1;
const preambleBlocks: Array<{ start: number; end: number }> = [];
for (const line of lines) {
if (commentStart < 0 && /^ {0,3}<!--/.test(line)) commentStart = start;
if (commentStart < 0 && line.trim().length > 0 && headingText(line) !== "") break;
const end = commentStart >= 0 ? line.indexOf("-->") : -1;
if (end >= 0 && line.slice(end + 3).trim()) return lines.slice(commentStart).join("\n");
if (end >= 0) commentStart = -1;
if (end >= 0) {
if (preambleBlocks.length > 0 || line.slice(end + 3).trim()) {
preambleBlocks.push({ start: commentStart, end: start + 1 });
}
commentStart = -1;
}
start += 1;
}
const firstPreambleBlock = preambleBlocks[0];
if (headingText(lines[start] ?? "")) {
start += 1;
} else if (firstPreambleBlock) {
return lines.slice(firstPreambleBlock.start).join("\n");
}
if (headingText(lines[start] ?? "")) start += 1;
while (lines[start]?.trim().length === 0) {
start += 1;
}
Expand All @@ -56,5 +66,10 @@ export function stripDisplayedPlanMarkdown(planMarkdown: string): string {
start += 1;
}
}
return lines.slice(start).join("\n");
const body = lines.slice(start).join("\n");
if (!firstPreambleBlock) return body;
const preamble = preambleBlocks
.map(({ start, end }) => lines.slice(start, end).join("\n"))
.join("\n");
return body ? `${preamble}\n\n${body}` : preamble;
}
115 changes: 115 additions & 0 deletions scripts/scenarios/proposed-plan-title-once-after-comment.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";

import { T3_HOME } from "../e2e.config.ts";
import { makeState, rpcClient, runPromise, test, WEB } from "./t3.ts";

const PLAN = "<!-- generated notes\n--> note\n# Release plan\n\nRun a smoke check.";
const SettingsJson = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown));
const decodeSettings = Schema.decodeEffect(SettingsJson);
const encodeSettings = Schema.encodeEffect(SettingsJson);
const ACP = `
import { createInterface } from "node:readline";

const sessionId = "proposed-plan-scenario";
const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n");
const update = (update) => send({ method: "session/update", params: { sessionId, update } });
for await (const line of createInterface({ input: process.stdin })) {
const { id, method } = JSON.parse(line);
if (id === undefined) continue;
if (method === "initialize") {
send({ id, result: { protocolVersion: 2, info: { name: "plan-agent", version: "1.0.0" } } });
} else if (method === "session/new") {
send({ id, result: { sessionId } });
} else if (method === "session/prompt") {
update({ sessionUpdate: "plan_update", plan: { type: "markdown", planId: "release", content: ${JSON.stringify(PLAN)} } });
update({ sessionUpdate: "state_update", state: "idle", stopReason: "end_turn" });
send({ id, result: {} });
} else {
send({ id, error: { code: -32601, message: "Method not found" } });
}
}
`;

test(
"A proposed plan shows its title once after a leading HTML comment",
WEB,
async ({ t3, app, screen }) => {
const project = await t3.project("demo", { "plan-agent.mjs": ACP });
const restoreSettings = await runPromise(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const settingsPath = path.join(T3_HOME, "userdata/settings.json");
const original = yield* fs.readFileString(settingsPath);
const settings = yield* decodeSettings(original);
const planAgent = {
driver: "acpRegistry",
displayName: "Plan Agent",
enabled: true,
config: {
source: "local",
commandPath: process.execPath,
commandArgs: [path.join(project, "plan-agent.mjs")],
},
};
const providerInstances = {
...(settings.providerInstances as object),
"plan-agent": planAgent,
};
yield* fs.writeFileString(
settingsPath,
yield* encodeSettings({ ...settings, providerInstances }),
);
return fs.writeFileString(settingsPath, original);
}),
);

try {
await runPromise(
Effect.gen(function* () {
const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
yield* client.subscribeServerConfig({}).pipe(
Stream.filter((event) =>
(event.type === "snapshot"
? event.config.providers
: event.type === "providerStatuses"
? event.payload.providers
: []
).some((provider) => provider.instanceId === "plan-agent"),
),
Stream.runHead,
);
}).pipe(Effect.scoped),
);
const { cli } = makeState(T3_HOME);
const started = await runPromise(
cli([
"thread",
"start",
project,
"Propose the release plan.",
"--title",
"Release",
"--provider",
"plan-agent",
"--model",
"default",
"--wait",
"--json",
]),
);
const { threadId } = JSON.parse(started.stdout) as { threadId: string };
await t3.open({ threadId });
await screen.getByText("Run a smoke check.", { exact: true }).waitFor();
await app.screenshot("proposed-plan-heading");
await expect(screen.getByRole("heading", "Release plan", { exact: true })).toHaveCount(1);
} finally {
await runPromise(restoreSettings);
}
},
);
Loading