peppa/packages/runner/src/screen-lines.ts

37 lines
1.8 KiB
TypeScript

// PEP-303: the watchdog's screen guards look for phrases the agent CLI prints (a limit banner, a
// model dialog, a trust question). FORGE agents edit FORGE's own code, where these phrases stand in
// comments, tests and mocks — and a diff, a grep hit or a `cat` of such a file lands on the agent's
// screen. These helpers tell the CLI's own lines from text quoted by the agent's tools.
/** A tool-output gutter glyph that may precede a quoted line: Claude «⎿», Codex «└ │ ├». */
const GUTTER = String.raw`(?:[⎿└│├]\s*)?`;
const QUOTED: RegExp[] = [
// A diff or `cat -n` with line numbers: « 447 + code», «447 -x», « 12 context», grep -n «12:text».
new RegExp(String.raw`^\s*${GUTTER}\d+(?:\s+[+-]|\s{2,}\S|:\S)`),
// A diff without numbers: «+code», «- code», «@@ -1,3 +1,4 @@».
new RegExp(String.raw`^\s*${GUTTER}(?:[+-]|@@)`),
// grep / compiler output: «path/to/file.ts:12:text», «file.md:3-…».
new RegExp(String.raw`^\s*${GUTTER}[^\s:]+:\d+[:-]`),
];
/** True when the line is text the agent's tools quoted (a diff, grep output, numbered source), not the CLI's own UI. */
export function isQuotedLine(line: string): boolean {
return QUOTED.some((re) => re.test(line));
}
/** A line of a boxed dialog without its frame: «│ Do you trust…? │» → «Do you trust…?». */
export function unbox(line: string): string {
return line.replace(/^\s*[│┃]\s?/, "").replace(/\s*[│┃]\s*$/, "").trimEnd();
}
/** The screen without quoted lines (see `isQuotedLine`). */
export function ownLines(screen: string): string {
return screen.split("\n").filter((l) => !isQuotedLine(l)).join("\n");
}
/** The last `n` rows of a pane, without the blank rows that pad it below the text. */
export function screenBottom(screen: string, n: number): string[] {
return screen.trimEnd().split("\n").slice(-n);
}