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
10 changes: 6 additions & 4 deletions apps/server/src/commandRuns/CommandRuns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* block in a reply. This service records the request, starts each run in a
* free terminal tab or a new one, follows the tab's run updates into the
* thread, and hands a run's output to the agent it was for: by itself when a
* request's Run finishes, or when the user asks.
* request's command finishes (run, or put in the terminal and entered), or
* when the user asks.
*
* The terminal does the running; orchestration keeps the record and decides
* every send, so Stop, reverts, skips and rooms are respected in one place.
Expand Down Expand Up @@ -191,12 +192,12 @@ const make = Effect.gen(function* () {
return {
outcome: "shown" as const,
detail: oneClick
? "Shown to the user as a card with Run, Put in terminal and Skip. Nothing runs until they choose. If they click Run, the output comes back to you as a message when the command finishes. If you can't go on without it, say in a sentence what you're waiting for and end your turn."
? "Shown to the user as a card with Run, Put in terminal and Skip. Nothing runs until they choose. When the command they run finishes, the output comes back to you as a message. If you can't go on without it, say in a sentence what you're waiting for and end your turn."
: `Shown to the user as a card. It can't run with one click (${assessment.cautions
.map((caution) => caution.label.replace(/\.$/, "").toLowerCase())
.join(
"; ",
)}), so the user puts it in the terminal, checks it and presses Enter, then sends you the output when they're done. If you can't go on without it, say in a sentence what you're waiting for and end your turn.`,
)}), so the user puts it in the terminal, checks it and presses Enter. When it finishes, what the terminal showed comes back to you as a message (they may have changed the command first). If you can't go on without it, say in a sentence what you're waiting for and end your turn.`,
};
});

Expand Down Expand Up @@ -446,7 +447,8 @@ const make = Effect.gen(function* () {
turnId: origin.turnId,
participantId: origin.participantId,
cwd: input.cwd,
autoSend: input.source.kind === "request" && input.mode === "run",
// A card's command answers the agent that asked, however it ran.
autoSend: input.source.kind === "request",
},
createdAt: yield* nowIso,
})
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/environment/Layers/ServerEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const makeServerEnvironment = Effect.fn("makeServerEnvironment")(function
browserLoopbackTunnel: true,
browserScreenshotTypes: true,
commandRuns: true,
followsInsertedCommands: true,
computerLinks: true,
scheduledTasks: true,
eventTasks: true,
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/mcp/commandTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const AskUserToRunTool = Tool.make(ASK_USER_TO_RUN_TOOL_NAME, {
description: [
"Ask the user to run a shell command in their Threadlines terminal, when you can't run it yourself: it needs their password or a sign-in, your sandbox or permissions blocked it, or it is a long-running process like a dev server they should own.",
"It shows a card in the chat with Run, Put in terminal and Skip, and returns right away; nothing runs until the user chooses.",
"When they click Run, the output comes back to you as a message once the command finishes. Risky commands (deleting files, discarding changes, sudo, publishing and the like) can't run with one click: the user puts them in the terminal, presses Enter themselves and sends you the output.",
"When the command they run finishes, the output comes back to you as a message. Risky commands (deleting files, discarding changes, sudo, publishing and the like) can't run with one click: the user puts them in the terminal and presses Enter themselves, and the output comes back the same way.",
"Use it instead of asking the user to copy a command into a terminal. Don't use it for commands you can run yourself.",
].join(" "),
parameters: AskUserToRunParameters,
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/orchestration/threadMoveDecisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function threadMoveRefusal(
}
if (
(thread.commandRuns ?? []).some((run) =>
["starting", "running", "waiting", "submitted"].includes(run.status),
["starting", "running", "waiting"].includes(run.status),
) ||
(thread.workflowRuns ?? []).some((run) => run.status === "running") ||
thread.voiceActive === true
Expand Down
164 changes: 156 additions & 8 deletions apps/server/src/terminal/Layers/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2266,17 +2266,31 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
}),
);

it.effect("puts a command at the prompt once the shell is ready and follows it to Enter", () =>
it.effect("puts a command at the prompt once the shell is ready and follows it to its end", () =>
Effect.gen(function* () {
// In front of the terminal at its own prompt, or behind a program it started.
const shellInFront = yield* Ref.make(true);
let shellPid = 0;
const { manager, ptyAdapter } = yield* createManager(50, {
platform: "darwin",
shellResolver: () => "/bin/zsh",
// Polling never gets a look in: the prompt alone ends it.
subprocessChecker: () => Effect.succeed(false),
subprocessPollIntervalMs: 60_000,
freshProcessRowsReader: () =>
Ref.get(shellInFront).pipe(
Effect.map((front) => [
{ ...processRow(shellPid, 1, "/bin/zsh"), status: front ? "Ss+" : "Ss" },
]),
),
});
const updates = yield* collectRunUpdates(manager);
const types = Ref.get(updates).pipe(Effect.map((list) => list.map((update) => update.type)));
yield* manager.startCommandRun(
commandRunInput({ mode: "insert", command: "git add .\ngit commit" }),
);
const pty = ptyAdapter.processes[0]!;
shellPid = pty.pid;
expect(ptyAdapter.spawnInputs[0]?.args).toEqual(["-o", "nopromptsp"]);
expect(pty.writes).toEqual([]);

Expand All @@ -2286,14 +2300,148 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (

pty.emitData("git add .\r\ngit commit");
yield* manager.write({ threadId: "thread-1", terminalId: "terminal-run", data: "\r" });
yield* waitFor(
Ref.get(updates).pipe(Effect.map((list) => list.some((u) => u.type === "submitted"))),
yield* waitFor(types.pipe(Effect.map((list) => list.includes("running"))));
// A program it started that reads lines (the commit message editor)
// turns the line editor on too: while it runs in front, the command goes on.
yield* Ref.set(shellInFront, false);
pty.emitData("\u001b[?2004l\r\nmessage: \u001b[?2004h");
yield* Effect.sleep("150 millis");
expect(yield* types).toEqual(["waiting", "running"]);

// Back at the shell's own prompt: done, with what the tab showed.
yield* Ref.set(shellInFront, true);
pty.emitData("\u001b[?2004l\r\n[main 1a2b3c] fix\r\nprompt % \u001b[?2004h");
yield* waitFor(types.pipe(Effect.map((list) => list.includes("finished"))));
expect((yield* Ref.get(updates)).at(-1)).toEqual({
type: "finished",
runId: "run-1",
at: expect.any(String),
startedAt: expect.any(String),
exitCode: null,
output: "git add .\ngit commit\nmessage:\n[main 1a2b3c] fix\nprompt %",
});
}),
);

it.effect("keeps following an entered insert until its shell's prompt is back", () =>
Effect.gen(function* () {
let shellPid = 0;
const nextCommandRunning = yield* Ref.make(false);
const { manager, ptyAdapter } = yield* createManager(50, {
platform: "darwin",
shellResolver: () => "/bin/zsh",
subprocessChecker: () => Ref.get(nextCommandRunning),
subprocessPollIntervalMs: 20,
// By the time anyone looks, the line typed ahead runs in front.
freshProcessRowsReader: () =>
Effect.sync(() => [{ ...processRow(shellPid, 1, "/bin/zsh"), status: "Ss" }]),
});
const updates = yield* collectRunUpdates(manager);
const types = Ref.get(updates).pipe(Effect.map((list) => list.map((update) => update.type)));
yield* manager.startCommandRun(commandRunInput({ mode: "insert", command: "read answer" }));
const pty = ptyAdapter.processes[0]!;
shellPid = pty.pid;
pty.emitData("prompt % \u001b[?2004h");
yield* waitFor(Effect.sync(() => pty.writes.length === 1));
yield* manager.write({ threadId: "thread-1", terminalId: "terminal-run", data: "\r" });
yield* waitFor(types.pipe(Effect.map((list) => list.includes("running"))));
// A builtin waiting for input runs no process: idle polls don't end it.
pty.emitData("read answer\u001b[?2004l\r\n");
yield* Effect.sleep("250 millis");
expect(yield* types).toEqual(["waiting", "running"]);

// Answered, and the next command typed before the prompt is back: the
// command ends at that prompt, before the next one takes the terminal.
yield* manager.write({ threadId: "thread-1", terminalId: "terminal-run", data: "yes\r" });
yield* manager.write({
threadId: "thread-1",
terminalId: "terminal-run",
data: "npm run dev\r",
});
yield* Ref.set(nextCommandRunning, true);
pty.emitData("yes\r\nprompt % \u001b[?2004hnpm run dev\u001b[?2004l\r\nready on :3000\r\n");
yield* waitFor(types.pipe(Effect.map((list) => list.includes("finished"))));
expect((yield* Ref.get(updates)).at(-1)).toMatchObject({
type: "finished",
output: "read answer\nyes\nprompt %",
});
}),
);

it.effect("never takes an answer typed to the command for a line typed ahead", () =>
Effect.gen(function* () {
const shellInFront = yield* Ref.make(false);
let shellPid = 0;
const { manager, ptyAdapter } = yield* createManager(50, {
platform: "darwin",
shellResolver: () => "/bin/zsh",
subprocessChecker: () => Ref.get(shellInFront).pipe(Effect.map((front) => !front)),
subprocessPollIntervalMs: 20,
freshProcessRowsReader: () =>
Ref.get(shellInFront).pipe(
Effect.map((front) => [
{ ...processRow(shellPid, 1, "/bin/zsh"), status: front ? "Ss+" : "Ss" },
]),
),
});
const updates = yield* collectRunUpdates(manager);
const types = Ref.get(updates).pipe(Effect.map((list) => list.map((update) => update.type)));
yield* manager.startCommandRun(commandRunInput({ mode: "insert", command: "ssh build-box" }));
const pty = ptyAdapter.processes[0]!;
shellPid = pty.pid;
pty.emitData("prompt % \u001b[?2004h");
yield* waitFor(Effect.sync(() => pty.writes.length === 1));
yield* manager.write({ threadId: "thread-1", terminalId: "terminal-run", data: "\r" });
yield* waitFor(types.pipe(Effect.map((list) => list.includes("running"))));
// The password is a line entered before any prompt; the remote shell's
// prompt then stays up for the user, who works there.
pty.emitData("ssh build-box\u001b[?2004l\r\npassword: ");
yield* manager.write({ threadId: "thread-1", terminalId: "terminal-run", data: "secret\r" });
pty.emitData("\r\nremote $ \u001b[?2004h");
yield* Effect.sleep("100 millis");
yield* manager.write({ threadId: "thread-1", terminalId: "terminal-run", data: "ls\r" });
pty.emitData("ls\u001b[?2004l\r\nfile\r\nremote $ \u001b[?2004h");
yield* Effect.sleep("150 millis");
expect(yield* types).toEqual(["waiting", "running"]);

yield* Ref.set(shellInFront, true);
pty.emitData("logout\r\nprompt % \u001b[?2004h");
yield* waitFor(types.pipe(Effect.map((list) => list.includes("finished"))));
expect((yield* Ref.get(updates)).at(-1)).toMatchObject({
type: "finished",
output: expect.stringContaining("remote $ ls\nfile"),
});
}),
);

it.effect("ends a PowerShell command entered from an insert at its prompt's marker", () =>
Effect.gen(function* () {
const { manager, ptyAdapter } = yield* createManager(50, {
platform: "win32",
shellResolver: () => "pwsh.exe",
subprocessChecker: () => Effect.succeed(false),
subprocessPollIntervalMs: 20,
});
const updates = yield* collectRunUpdates(manager);
const types = Ref.get(updates).pipe(Effect.map((list) => list.map((update) => update.type)));
yield* manager.startCommandRun(
commandRunInput({ mode: "insert", dialect: "powershell", command: "gh auth login" }),
);
expect((yield* Ref.get(updates)).map((update) => update.type)).toEqual([
"waiting",
"submitted",
]);
expect(yield* manager.readCommandRunOutput("run-1")).toBe("git add .\ngit commit");
const pty = ptyAdapter.processes[0]!;
pty.emitData("\u001b]633;D\u0007PS C:\\repo> ");
yield* waitFor(Effect.sync(() => pty.writes.length === 1));
yield* manager.write({ threadId: "thread-1", terminalId: "terminal-run", data: "\r" });
yield* waitFor(types.pipe(Effect.map((list) => list.includes("running"))));
// A cmdlet has no process to find: polling never ends it, the prompt does.
yield* Effect.sleep("150 millis");
expect(yield* types).toEqual(["waiting", "running"]);
pty.emitData("gh auth login\r\nLogged in\r\n\u001b]633;D\u0007PS C:\\repo> ");
yield* waitFor(types.pipe(Effect.map((list) => list.includes("finished"))));
expect((yield* Ref.get(updates)).at(-1)).toMatchObject({
type: "finished",
exitCode: null,
output: expect.stringContaining("Logged in"),
});
}),
);

Expand Down
Loading
Loading