Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 17 additions & 5 deletions test/e2e/live/launch-agent-turn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ const interceptPath = process.env.OPENSHELL_NEMOCLAW_LAUNCH_INTERCEPT_PATH;
const monitorStarterScript = process.env.OPENSHELL_NEMOCLAW_LAUNCH_PTY_MONITOR_STARTER_SCRIPT;
const runtimeEnvScript = process.env.OPENSHELL_NEMOCLAW_LAUNCH_RUNTIME_ENV_SCRIPT;
const keyPath = process.env.OPENSHELL_NEMOCLAW_LAUNCH_PTY_MONITOR_KEY_PATH;
const firstInput = process.env.OPENSHELL_NEMOCLAW_LAUNCH_FIRST_INPUT;
const keyWriterScript = ${JSON.stringify(OPENCLAW_PTY_MONITOR_KEY_WRITER_SCRIPT)};

function fail(reason) {
Expand Down Expand Up @@ -607,6 +608,10 @@ const launchLike = sameSandbox && hasExpectedTail;

if (!launchLike) runRealOpenShell(argv);

if (!/^[\x20-\x7e]{1,512}$/.test(firstInput || "")) {
fail("openshell_shim_first_input_invalid");
}

let optionIndex = 4;
if (argv[optionIndex] === "-g") {
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(argv[optionIndex + 1] || "")) {
Expand Down Expand Up @@ -659,6 +664,15 @@ const keyWriterArgv = [
if (invokeRealOpenShell(keyWriterArgv, privateKeyBase64) !== 0) {
fail("openshell_shim_private_key_write_failed");
}
// OpenClaw submits --message only after its Gateway subscription and history
// load complete. Use a positional parameter so the generated input never
// enters shell source.
const launchRemoteArgv = [
...remoteArgv.slice(0, -1),
'exec openclaw tui --message "$1"',
"nemoclaw-launch-first-turn",
firstInput,
];
const replacement = [
...argv.slice(0, separator + 1),
"node",
Expand All @@ -668,7 +682,7 @@ const replacement = [
monitorRoot,
publicKeyBase64,
privateKeyPath,
...remoteArgv,
...launchRemoteArgv,
];
runRealOpenShell(replacement);
`;
Expand Down Expand Up @@ -1494,6 +1508,7 @@ OPENSHELL_NEMOCLAW_LAUNCH_REAL_COMMAND="$NEMOCLAW_OPENSHELL_COMMAND" \
OPENSHELL_NEMOCLAW_LAUNCH_SANDBOX="$NEMOCLAW_LAUNCH_SANDBOX" \
OPENSHELL_NEMOCLAW_LAUNCH_RUN_ID="$NEMOCLAW_LAUNCH_RUN_ID" \
OPENSHELL_NEMOCLAW_LAUNCH_INTERCEPT_PATH="$intercept_path" \
OPENSHELL_NEMOCLAW_LAUNCH_FIRST_INPUT="$NEMOCLAW_LAUNCH_FIRST_INPUT" \
OPENSHELL_NEMOCLAW_LAUNCH_PTY_MONITOR_STARTER_SCRIPT="$NEMOCLAW_LAUNCH_PTY_MONITOR_STARTER_SCRIPT" \
OPENSHELL_NEMOCLAW_LAUNCH_PTY_MONITOR_KEY_PATH="$pty_monitor_key_path" \
OPENSHELL_NEMOCLAW_LAUNCH_RUNTIME_ENV_SCRIPT="$NEMOCLAW_LAUNCH_RUNTIME_ENV_SCRIPT" \
Expand All @@ -1520,11 +1535,8 @@ if [[ "$capture_ready" != 1 ]]; then
fail_launch_session "launch did not create a PTY diagnostic capture"
fi

wait_for_pty_input_mode
if ! printf '%s\r' "$NEMOCLAW_LAUNCH_FIRST_INPUT" >&3; then
fail_launch_session "launch exited before the first PTY input was submitted"
fi
wait_for_turn_count 1
wait_for_pty_input_mode
if ! printf '%s\r' "$NEMOCLAW_LAUNCH_SECOND_INPUT" >&3; then
fail_launch_session "launch exited before the second PTY input was submitted"
fi
Expand Down
75 changes: 51 additions & 24 deletions test/e2e/support/launch-agent-turn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
| "cleanup-failure"
| "delayed-input-attachment"
| "delayed-recording"
| "delayed-tui-ready"
| "input-mode-timeout"
| "invalid-order"
| "late-extra"
Expand Down Expand Up @@ -438,6 +439,8 @@
fs.writeFileSync(process.env.NEMOCLAW_FIXTURE_TTY_MARKER, "");
const sessionFile = process.env.NEMOCLAW_FIXTURE_SESSION_FILE;
const terminalCopy = process.env.NEMOCLAW_FIXTURE_TERMINAL_COPY;
const firstInput = process.argv[process.argv.indexOf("--message") + 1];
if (!firstInput) process.exit(75);
Comment thread
ericksoa marked this conversation as resolved.
Resolved
const append = (role, content) => fs.appendFileSync(
sessionFile,
JSON.stringify({ message: { content: [{ text: content, type: "text" }], role }, type: "message" }) + "\n",
Expand All @@ -446,61 +449,61 @@
process.stdin.setRawMode(true);
await new Promise((resolve) => setTimeout(resolve, 750));
process.stdin.setRawMode(false);
append("user", firstInput);
append("assistant", "first response");
const recordUnexpectedInput = () =>
fs.writeFileSync(process.env.NEMOCLAW_FIXTURE_EARLY_INPUT_MARKER, "");
process.stdin.on("data", recordUnexpectedInput);
fs.writeFileSync(process.env.NEMOCLAW_FIXTURE_CANONICAL_RESTORED_MARKER, "");
await new Promise((resolve) => setTimeout(resolve, 10_000));
process.stdin.off("data", recordUnexpectedInput);
}
if (mode === "delayed-input-attachment" || mode === "input-mode-timeout") {
let inputBeforeAttachment = false;
const recordEarlyInput = () => { inputBeforeAttachment = true; };
if (mode === "input-mode-timeout") {
append("user", firstInput);
append("assistant", "first response");
await new Promise((resolve) => setTimeout(resolve, 10_000));
}
if (mode === "delayed-input-attachment" || mode === "delayed-tui-ready") {
const recordEarlyInput = () =>
fs.writeFileSync(process.env.NEMOCLAW_FIXTURE_EARLY_INPUT_MARKER, "");
if (mode === "delayed-tui-ready") process.stdin.setRawMode(true);
process.stdin.on("data", recordEarlyInput);
await new Promise((resolve) => setTimeout(resolve, mode === "input-mode-timeout" ? 10_000 : 1_500));
await new Promise((resolve) => setTimeout(resolve, 1_500));
process.stdin.off("data", recordEarlyInput);
if (inputBeforeAttachment) process.exit(67);
if (fs.existsSync(process.env.NEMOCLAW_FIXTURE_EARLY_INPUT_MARKER)) process.exit(67);
}
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
const ask = () => new Promise((resolve) => rl.question("", resolve));
if (terminalCopy === "ansi") process.stdout.write("\u001b[2Kgateway connected | idle\r");
if (terminalCopy === "reordered") process.stdout.write("idle | gateway connected\n");

const first = await ask();
process.kill(Number(monitorPid), "SIGTERM");
await new Promise((resolve) => setTimeout(resolve, 50));
const monitorStat = fs.readFileSync("/proc/" + monitorPid + "/stat", "utf8");
const monitorState = monitorStat ? monitorStat.slice(monitorStat.lastIndexOf(") ") + 2)[0] : null;
if (!monitorState || monitorState === "Z") process.exit(71);
const delayedInputs = [];
if (mode === "delayed-recording") {
const recordDelayedInput = (line) => delayedInputs.push(line);
rl.on("line", recordDelayedInput);
const publicationDeadline = Date.now() + 2_000;
while (!fs.existsSync(process.env.NEMOCLAW_FIXTURE_PENDING_QUALIFICATION_MARKER)) {
if (Date.now() >= publicationDeadline) process.exit(68);
await new Promise((resolve) => setTimeout(resolve, 10));
}
rl.off("line", recordDelayedInput);
}
if (mode === "recording-timeout") await new Promise((resolve) => setTimeout(resolve, 10_000));
if (mode === "invalid-order") {
append("assistant", "response before input");
append("user", first);
append("user", firstInput);
} else {
append("user", first);
append("user", firstInput);
append("assistant", "first response");
}
for (const duplicate of delayedInputs) {
append("user", duplicate);
append("assistant", "duplicate response");
}

process.kill(Number(monitorPid), "SIGTERM");
await new Promise((resolve) => setTimeout(resolve, 50));
const monitorStat = fs.readFileSync("/proc/" + monitorPid + "/stat", "utf8");
const monitorState = monitorStat ? monitorStat.slice(monitorStat.lastIndexOf(") ") + 2)[0] : null;
if (!monitorState || monitorState === "Z") process.exit(71);

const second = await ask();
append("user", second);
append("assistant", "second response");
const exitCommand = await ask();
if (mode === "late-extra") append("user", first);
if (mode === "late-extra") append("user", firstInput);
rl.close();
if (exitCommand !== "/exit") process.exit(65);
process.exit(mode.includes("nonzero") ? 23 : 0);
Expand Down Expand Up @@ -757,6 +760,7 @@
NEMOCLAW_OPENSHELL_BIN: shim,
NEMOCLAW_OPENSHELL_COMMAND: realOpenShell,
OPENSHELL_NEMOCLAW_LAUNCH_INTERCEPT_PATH: interceptPath,
OPENSHELL_NEMOCLAW_LAUNCH_FIRST_INPUT: "fixture input",
OPENSHELL_NEMOCLAW_LAUNCH_PTY_MONITOR_KEY_PATH: keyPath,
OPENSHELL_NEMOCLAW_LAUNCH_PTY_MONITOR_STARTER_SCRIPT: OPENCLAW_PTY_MONITOR_STARTER_SCRIPT,
OPENSHELL_NEMOCLAW_LAUNCH_REAL_COMMAND: realOpenShell,
Expand Down Expand Up @@ -799,6 +803,10 @@
const passThrough = runShim(passThroughArgv, hostEnv);
const ttyPassThrough = runShim(ttyPassThroughArgv);
const malformed = runShim(malformedArgv);
const invalidFirstInput = runShim(exactArgv, {
...env,
OPENSHELL_NEMOCLAW_LAUNCH_FIRST_INPUT: "",
});
const intercepted = runShim(exactArgv);
const duplicate = runShim(exactArgv);
const records = readFileSync(callsPath, "utf8")
Expand All @@ -813,6 +821,7 @@
exactArgv,
interceptMode: statSync(interceptPath).mode & 0o777,
intercepted,
invalidFirstInput,
malformed,
passThrough,
passThroughArgv,
Expand Down Expand Up @@ -926,6 +935,10 @@
expect(fixture.ttyPassThrough.status, fixture.ttyPassThrough.stderr).toBe(0);
expect(fixture.malformed.status).toBe(73);
expect(fixture.malformed.stderr).toContain('"reason":"openshell_launch_invocation_invalid"');
expect(fixture.invalidFirstInput.status).toBe(73);
expect(fixture.invalidFirstInput.stderr).toContain(
'"reason":"openshell_shim_first_input_invalid"',
);
expect(fixture.intercepted.status, fixture.intercepted.stderr).toBe(0);
expect(fixture.duplicate.status).toBe(73);
expect(fixture.duplicate.stderr).toContain('"reason":"openshell_launch_intercept_duplicate"');
Expand Down Expand Up @@ -956,7 +969,10 @@
fixture.monitorRoot,
fixture.publicKey,
`${fixture.monitorRoot}/pty-monitor-private-key`,
...expectedRemote,
...expectedRemote.slice(0, -1),
'exec openclaw tui --message "$1"',
"nemoclaw-launch-first-turn",
"fixture input",
]);
expect(fixture.calls.flat()).not.toContain(fixture.privateKey);
},
Expand Down Expand Up @@ -997,7 +1013,7 @@
);

it.runIf(process.platform === "linux").each(["absent", "ansi", "reordered"] as const)(
"keeps the monitor alive through SIGTERM, sends two PTY inputs and /exit, strips launch authority, and ignores terminal copy evidence [%s] (#9160)",
"keeps the monitor alive through SIGTERM, records an auto-message and PTY turn, sends /exit, strips launch authority, and ignores terminal copy evidence [%s] (#9160, #9384)",
(terminalCopy) => {
const {
baselineRemoved,
Expand Down Expand Up @@ -1053,6 +1069,17 @@
},
);

it.runIf(process.platform === "linux")(
"waits for OpenClaw startup to accept its auto-message before submitting one PTY input (#9384)",
() => {
const fixture = runLaunchSessionFixture("delayed-tui-ready", "absent");

expect(fixture.earlyInputObserved, fixture.result.stderr).toBe(false);
expect(fixture.baselineRemoved, fixture.result.stderr).toBe(true);
expect(fixture.result.status, fixture.result.stderr).toBe(0);
},
);

it.runIf(process.platform === "linux" && process.getuid?.() !== 0)(
"uses the inherited PTY descriptor when the sandbox user cannot reopen the device path (#9384)",
() => {
Expand Down Expand Up @@ -1170,7 +1197,7 @@
expect(orphanedMonitorProcessIds).toEqual([]);
expect(result.signal).toBeNull();
expect(result.status).toBe(1);
expect(result.stderr).toContain(

Check failure on line 1200 in test/e2e/support/launch-agent-turn.test.ts

View workflow job for this annotation

GitHub Actions / cli-test-shards (1)

[e2e-support] test/e2e/support/launch-agent-turn.test.ts > fails when the PTY remains in canonical input mode until the session deadline (#9160)

AssertionError: expected 'launch did not record the required st…' to contain 'launch did not observe noncanonical P…' - Expected + Received - launch did not observe noncanonical PTY input mode before the session deadline or before the PTY child process exited + launch did not record the required structured session turns + bounded terminal diagnostic (last 4096 bytes): + ❯ test/e2e/support/launch-agent-turn.test.ts:1200:27
"launch did not observe noncanonical PTY input mode before the session deadline or before the PTY child process exited",
);
expect(result.stderr).toContain('"reason":"pty_input_canonical"');
Expand Down Expand Up @@ -1216,7 +1243,7 @@
expect(ptyMonitorRemoved).toBe(true);
expect(result.signal).toBeNull();
expect(result.status).toBe(1);
expect(result.stderr).toContain('"reason":"pty_socket_missing"');

Check failure on line 1246 in test/e2e/support/launch-agent-turn.test.ts

View workflow job for this annotation

GitHub Actions / cli-test-shards (1)

[e2e-support] test/e2e/support/launch-agent-turn.test.ts > fails when the PTY monitor socket remains missing until the session deadline (#9160)

AssertionError: expected 'launch did not record the required st…' to contain '"reason":"pty_socket_missing"' - Expected + Received - "reason":"pty_socket_missing" + launch did not record the required structured session turns + bounded terminal diagnostic (last 4096 bytes): + ❯ test/e2e/support/launch-agent-turn.test.ts:1246:27
},
);

Expand Down
Loading