Skip to content
Français

Add a CLI agent

Build an adapter that starts your agent CLI and reads its output as Outpost events.

Use this integration path for a CLI Outpost does not provide. You need its executable in the sandbox and sample output from the real CLI. Keep process execution in the sandbox provider; this adapter only builds requests and decodes events.

The example uses a local demo CLI, with no account or model call. Save demo-cli.ts in the target repository and the other files beside your configuration. copies makes this CLI available in the agent’s worktree. It reads the brief and emits two JSON events.

let prompt = "";
for await (const chunk of process.stdin) prompt += chunk;
console.log(
  JSON.stringify({
    type: "message",
    text: `Received ${prompt.length} characters`,
  }),
);
console.log(JSON.stringify({ type: "usage", input: 0, output: 0 }));

Implement an AgentAdapter to describe how your CLI starts and how its output becomes events: request() builds the command and events() reads its output lines. A CliHarness binds that adapter to the selected model, and createAgent() makes it usable in a dispatch.

import type { AgentEvent } from "@elie-laloum/outpost/integrations";

export const parse = (line: string) => {
  try {
    return JSON.parse(line);
  } catch {
    return undefined;
  }
};
export function usage(input: number, output: number): AgentEvent {
  return { kind: "usage", tokens: { input, cached: 0, output } };
}
import type { AgentEvent } from "@elie-laloum/outpost/integrations";
import { parse, usage } from "./protocol-values.ts";

export function events(line: string): AgentEvent[] {
  const event = parse(line);
  switch (event?.type) {
    case "session":
      return [{ kind: "conversation", id: event.id }];
    case "message":
      return [{ kind: "text", text: event.text }];
    case "tool":
      return [{ kind: "tool", name: event.name, input: event.input }];
    case "usage":
      return [usage(event.input, event.output)];
    case "error":
      return [{ kind: "failure", message: event.message }];
    default:
      return [];
  }
}
import type { AgentModel } from "@elie-laloum/outpost/core";
import type { AgentAdapter } from "@elie-laloum/outpost/integrations";

export function requestForModel(model?: AgentModel): AgentAdapter["request"] {
  return ({ text }) => ({
    executable: "node",
    arguments: ["demo-cli.ts", ...(model ? ["--model", model.name] : [])],
    stdin: text ?? "",
  });
}
import type { CliHarness } from "@elie-laloum/outpost/core";
import { requestForModel } from "./model-request.ts";
import { events } from "./events.ts";
import { createAgent } from "@elie-laloum/outpost/core";

export const myCliHarness: CliHarness = {
  kind: "cli",
  bind(model) {
    if (model?.reasoning || model?.maxOutputTokens)
      throw new Error("mycli accepts only a model name");
    return {
      name: "mycli",
      request: requestForModel(model),
      events,
    };
  },
};
export const myCli = createAgent({ harness: myCliHarness, model: "mycli-pro" });
import { dispatch } from "@elie-laloum/outpost/core";
import { repository, sandboxProvider } from "./outpost.config.ts";
import { myCli } from "./mycli-agent.ts";

export const result = await dispatch({
  repository,
  sandboxProvider,
  agent: myCli,
  copies: ["demo-cli.ts"],
  branch: { mode: "named", name: "outpost/mycli-review" },
  brief: { text: "Review the README for incorrect setup instructions." },
});
console.log(result.text);
// Example output: Received 153 characters

Outpost runs node demo-cli.ts --model mycli-pro in the sandbox with the brief on stdin, passes each stdout line to events() and returns the collected text in result.text. Run node mycli.ts: expect Received … characters. For a real integration, replace the demo command and decoder with the CLI installed in your agent image.

request() receives an AgentInput and returns a Command: executable, arguments, stdin, variables. Outpost adds the deadline and the cancellation signal.

API reference: AgentInput.

events() receives each stdout line and returns zero, one or several events. Stderr never reaches it: Outpost reports stderr lines as stderr events.

API reference: AgentEvent.

Other kinds, such as tool, tool-result, reasoning, file-change and warning, reach only observers. A nonzero exit status fails the turn. Without text or result events, result.text holds the end of stdout.

Each optional member enables one feature. Declare only what the CLI really does.

API reference: AgentAdapter.

usage tells Outpost where the counters come from. Incomplete usage is a lower bound that budgets treat separately.

API reference: Usage.

import type { AgentAdapter } from "@elie-laloum/outpost/integrations";

export const sessionUsage: Pick<
  AgentAdapter,
  "usage" | "usageCommand" | "usageResult"
> = {
  usage: "session",
  usageCommand: (conversation) => ({
    executable: "mycli",
    arguments: ["usage", "--json", conversation],
  }),
  usageResult: (text) => {
    const totals = JSON.parse(text);
    return {
      input: totals.input,
      cached: totals.cached,
      output: totals.output,
    };
  },
};

Spread sessionUsage into the adapter. The command has five seconds and is skipped when a cumulative usage event already gave the totals. On a continued conversation, Outpost measures a baseline first and keeps the difference; a fork has no baseline, so its usage is incomplete.

With storage, transcriptUsage(text) can instead read the totals from the captured transcript.

A steered dispatch reaches your CLI in one of two ways.

import type { AgentLiveInput } from "@elie-laloum/outpost/integrations";

export const liveInput: AgentLiveInput = {
  open: () => ({
    encode: (text) => `${JSON.stringify({ type: "user", text })}\n`,
    read: (line) => ({
      consumed: line.includes('"type":"user-ack"') ? 1 : 0,
      replies: [],
    }),
  }),
};
AdapterDeliveryWhat happens
With liveInputinjectedOutpost writes each instruction to the running process’s stdin.
resumable: true, no liveInputresumedOutpost stops the process once its conversation is known, then resumes it with the text.
Neither―The dispatch is rejected before it starts.

encode() turns one instruction into stdin text. read() sees every stdout line and returns the number of messages the CLI confirmed, plus protocol replies to write, such as answers to permission requests.

The prompt counts as the first message to confirm. Outpost keeps stdin open and closes it after a finished event once every message was consumed.

Injection also needs a SandboxLease with liveInput: true, which every built-in provider returns. On a custom provider without it, a resumable adapter falls back to resumed.

  • ProcessDeadlines, idle timeouts and cancellation stop the whole process group.
  • Sandbox and workspaceAllocation, the worktree, commits and cleanup work as for built-in agents.
  • Agent homeThe plans from credentials() and configuration() are installed in the private sandbox home.
  • AnswersTyped responses, repairs and completion markers apply to your decoded text.
  • UsageCounters add up across turns, retries and workflows, and feed budgets.
  • ObservationDecoded events, raw lines and stderr reach observe, reporters and journals.

Record real output lines of the CLI once, then replay them through events(). diagnoseAgentProtocol() checks the built-in agents the same way.

import type { AgentEvent } from "@elie-laloum/outpost/integrations";

export interface ProtocolFixture {
  readonly name: string;
  readonly lines: readonly string[];
  readonly expected: readonly AgentEvent[];
}
import type { CliHarness } from "@elie-laloum/outpost/core";
import type { ProtocolFixture } from "./protocol.types.ts";
import assert from "node:assert/strict";

export function checkProtocol(
  harness: CliHarness,
  fixtures: readonly ProtocolFixture[],
): void {
  const adapter = harness.bind();
  for (const fixture of fixtures)
    assert.deepEqual(
      fixture.lines.flatMap((line) => adapter.events(line)),
      fixture.expected,
      fixture.name,
    );
}

Add fixtures for failures, and check that quota() matches only terminal limit messages, not retry notices. Then run one real dispatch in a container: fixtures do not test installation, sign-in, exit status or capture.

  • Remote bootstrap installs only built-in CLIs. Install yours in the image of every provider you use.
  • outpost doctor, outpost image and diagnoseAgentProtocol() know only the built-in agents.
  • One stdout line holds at most 16 MiB; a longer line fails the turn.

API: createAgent · CliHarness · AgentAdapter · AgentInput · AgentEvent · AgentLiveInput · Usage · diagnoseAgentProtocol.