Skip to content

Commit 22c2af3

Browse files
committed
fix(mantis): bind actions to recorded window
1 parent 1d53a90 commit 22c2af3

3 files changed

Lines changed: 40 additions & 18 deletions

File tree

‎scripts/e2e/telegram-desktop-recorder-contract.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const recorderSessionBaseSchema = z.object({
2727
/** Telegram Desktop window as placed on the recorded desktop; the crop uses it. */
2828
window: z.object({
2929
height: z.number().int().positive(),
30+
id: z.string().regex(/^0x[0-9a-f]+$/iu),
3031
width: z.number().int().positive(),
3132
x: z.number().int().nonnegative(),
3233
y: z.number().int().nonnegative(),

‎scripts/e2e/telegram-desktop-recorder.ts‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,13 @@ export DISPLAY=:99
175175
win="$(wmctrl -lx | awk 'tolower($0) ~ /telegramdesktop/ {print $1; exit}')"
176176
test -n "$win"
177177
eval "$(xdotool getwindowgeometry --shell "$win")"
178-
printf '%s %s %s %s\n' "$X" "$Y" "$WIDTH" "$HEIGHT"`;
178+
printf '%s %s %s %s %s\n' "$win" "$X" "$Y" "$WIDTH" "$HEIGHT"`;
179179
}
180180

181-
function renderHideTelegramWindow(): string {
181+
function renderHideTelegramWindow(windowId: string): string {
182182
return `set -euo pipefail
183183
export DISPLAY=:99
184-
win="$(wmctrl -lx | awk 'tolower($0) ~ /telegramdesktop/ {print $1; exit}')"
185-
test -n "$win"
184+
win=${shellQuote(windowId)}
186185
xdotool windowminimize "$win"
187186
sleep 0.2`;
188187
}
@@ -237,19 +236,26 @@ exit 1`;
237236

238237
export function parseWindowGeometry(raw: string): {
239238
height: number;
239+
id: string;
240240
width: number;
241241
x: number;
242242
y: number;
243243
} {
244-
const parts = raw.trim().split(/\s+/u).map(Number);
245-
if (parts.length !== 4 || parts.some((value) => !Number.isFinite(value) || value < 0)) {
244+
const [id, ...rawGeometry] = raw.trim().split(/\s+/u);
245+
const parts = rawGeometry.map(Number);
246+
if (
247+
!id ||
248+
!/^0x[0-9a-f]+$/iu.test(id) ||
249+
parts.length !== 4 ||
250+
parts.some((value) => !Number.isFinite(value) || value < 0)
251+
) {
246252
throw new Error(`Telegram Desktop window geometry was not readable: ${raw.trim()}`);
247253
}
248254
const [x, y, width, height] = parts as [number, number, number, number];
249255
if (width < 200 || height < 200) {
250256
throw new Error(`Telegram Desktop window is too small to crop: ${width}x${height}`);
251257
}
252-
return { height, width, x, y };
258+
return { height, id, width, x, y };
253259
}
254260

255261
function driverCommand(userDriver: string[], args: string[]) {
@@ -612,7 +618,7 @@ async function startRecorderAttempt(
612618
// The lane clears prior history before recorder start. Keep the empty chat hidden until
613619
// the first session-owned send is ready, so setup frames reveal neither account UI nor chat.
614620
await operations.sshRun({
615-
command: renderHideTelegramWindow(),
621+
command: renderHideTelegramWindow(windowGeometry.id),
616622
cwd,
617623
inspect,
618624
run: operations.runCommand,
@@ -873,8 +879,17 @@ export async function runRecorderActions(
873879
results.push({ command: "sleep", stderr: "", stdout: "" });
874880
continue;
875881
}
876-
const telegramWindow =
877-
'win="$(wmctrl -lx | awk \'tolower($0) ~ /telegramdesktop/ {print $1; exit}\')"\ntest -n "$win"\n';
882+
const telegramWindow = `win=${shellQuote(session.window.id)}
883+
if ! wmctrl -lx | awk -v win="$win" 'tolower($1) == tolower(win) && tolower($0) ~ /telegramdesktop/ {found=1} END {exit !found}'; then
884+
echo "Recorded Telegram window $win no longer exists." >&2
885+
exit 1
886+
fi
887+
eval "$(xdotool getwindowgeometry --shell "$win")"
888+
if [ "$X" -ne ${session.window.x} ] || [ "$Y" -ne ${session.window.y} ] || [ "$WIDTH" -ne ${session.window.width} ] || [ "$HEIGHT" -ne ${session.window.height} ]; then
889+
echo "Recorded Telegram window $win moved or resized." >&2
890+
exit 1
891+
fi
892+
`;
878893
const actionCommand =
879894
action.command === "click"
880895
? `xdotool windowactivate --sync "$win" mousemove --window "$win" ${action.x} ${action.y} click ${action.button}`

‎test/scripts/telegram-desktop-recorder.test.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function testSession(): RecorderSession {
6060
},
6161
schemaVersion: 1,
6262
startedAt: "2026-08-15T12:00:00.000Z",
63-
window: { height: 1000, width: 650, x: 635, y: 40 },
63+
window: { height: 1000, id: "0x04600007", width: 650, x: 635, y: 40 },
6464
userDriver: ["python3", "driver.py", "--account", "qa shared"],
6565
};
6666
}
@@ -152,7 +152,7 @@ describe("Telegram Desktop recorder CLI", () => {
152152
});
153153
});
154154

155-
it("runs agent-authored actions inside the recorded desktop", async () => {
155+
it("targets the recorded Telegram window instead of the first matching window", async () => {
156156
const root = makeTempDir();
157157
const sessionPath = path.join(root, "recorder.json");
158158
const actionsPath = path.join(root, "click.json");
@@ -208,6 +208,11 @@ describe("Telegram Desktop recorder CLI", () => {
208208
timeoutMs: 90_000,
209209
}),
210210
);
211+
const actionCommand = sshRun.mock.calls[0]?.[0].command;
212+
expect(actionCommand).toContain("win='0x04600007'");
213+
expect(actionCommand).toContain("tolower($1) == tolower(win)");
214+
expect(actionCommand).toContain('[ "$WIDTH" -ne 650 ]');
215+
expect(actionCommand).not.toContain("{print $1; exit}");
211216
});
212217

213218
it("requires start inputs and a -100 private-group chat id", () => {
@@ -540,7 +545,7 @@ describe("Telegram Desktop recorder remote contract", () => {
540545
throw new Error("first desktop stayed on QR");
541546
}
542547
if (command.includes("getwindowgeometry")) {
543-
return { stderr: "", stdout: "635 40 650 1000" };
548+
return { stderr: "", stdout: "0x04600007 635 40 650 1000" };
544549
}
545550
return { stderr: "", stdout: "" };
546551
}),
@@ -579,7 +584,7 @@ describe("Telegram Desktop recorder remote contract", () => {
579584
return { stderr: "", stdout: "tg://login?token=open-target-chat" };
580585
}
581586
if (command.includes("getwindowgeometry")) {
582-
return { stderr: "", stdout: "635 40 650 1000" };
587+
return { stderr: "", stdout: "0x04600007 635 40 650 1000" };
583588
}
584589
return { stderr: "", stdout: "" };
585590
});
@@ -860,22 +865,23 @@ describe("Telegram Desktop recorder window geometry", () => {
860865
});
861866

862867
it("parses the measured window and rejects unusable geometry", () => {
863-
expect(parseWindowGeometry(" 636 45 648 995 \n")).toEqual({
868+
expect(parseWindowGeometry(" 0x04600007 636 45 648 995 \n")).toEqual({
864869
height: 995,
870+
id: "0x04600007",
865871
width: 648,
866872
x: 636,
867873
y: 45,
868874
});
869-
expect(() => parseWindowGeometry("636 45 648")).toThrow("was not readable");
870-
expect(() => parseWindowGeometry("636 45 10 10")).toThrow("too small to crop");
875+
expect(() => parseWindowGeometry("0x04600007 636 45 648")).toThrow("was not readable");
876+
expect(() => parseWindowGeometry("0x04600007 636 45 10 10")).toThrow("too small to crop");
871877
});
872878

873879
it("crops the recorded window instead of a fixed rectangle", async () => {
874880
const root = makeTempDir();
875881
const sessionPath = path.join(root, "recorder.json");
876882
writeRecorderSession(sessionPath, {
877883
...testSession(),
878-
window: { height: 995, width: 648, x: 636, y: 45 },
884+
window: { height: 995, id: "0x04600007", width: 648, x: 636, y: 45 },
879885
});
880886
const cropped = vi.fn(async () => ({ crop: "", fps: 24, outputWidth: 648 }));
881887
const sshRun = vi.fn<RecorderOperations["sshRun"]>(async () => ({ stderr: "", stdout: "" }));

0 commit comments

Comments
 (0)