Skip to content
Français

Check work and retry

Use test output or a review to accept an agent’s work or request another attempt.

Use a loop when your code can decide whether a result is acceptable. For the agent-and-tests example, prepare the configuration and dependencies first. A retry after an execution error and another attempt after a failed check are different decisions.

Download all files. Extract into a dedicated directory, run npm install, then adapt outpost.config.ts using Installation. The commands below identify the scripts to run.

Define an attempt and a check with defineLoopTask(). The check accepts the result or returns feedback for the next attempt. Set maxRounds so a result that never passes ends with a bounded failure.

import { defineLoopTask, defineWorkflow } from "@elie-laloum/outpost/workflows";

const fix = defineLoopTask({
  key: "fix",
  maxRounds: 3,
  attempt: (context, feedback) => ({
    round: context.round,
    feedback: feedback ?? "",
  }),
  check: (_, candidate) =>
    candidate.round === 2
      ? { done: true }
      : { done: false, feedback: "Cover the missing edge case." },
});

const result = await defineWorkflow("verified", [fix]).start();
result.unwrap();
console.log(result.value(fix));
// Example output: { round: 2, feedback: 'Cover the missing edge case.' }
Drag to move · Ctrl + scroll to zoom
100 %
  • AttemptProduce or correct a candidate. (Agent)
    • → Check : candidate ready
  • CheckRun your validation. (Your check)
    • → Attempt : needs work
    • → Result : accepted
    • → Failure : rounds exhausted
  • ResultReturn the accepted candidate. (Workflow)
  • FailureStop with LoopTaskExhausted. (Workflow)

Round 1 is rejected; round 2 receives the feedback and is accepted. after, condition and cache work as on other tasks.

The agent works in attempt, the tests run in check, both in one warm sandbox.

Keep the sandbox, attempt and check separate so each step is easy to follow.

import { createSandbox } from "@elie-laloum/outpost/core";
import { repository, sandboxProvider, coder } from "./outpost.config.ts";

export function openLoopSandbox() {
  return createSandbox({
    repository,
    sandboxProvider,
    agent: coder,
    branch: { mode: "named", name: "outpost/fix-tests" },
  });
}
import type { Sandbox } from "@elie-laloum/outpost/core";
import { defineAgentTask } from "@elie-laloum/outpost/workflows";

export function codingTask(sandbox: Sandbox, feedback?: string) {
  return defineAgentTask({
    key: "coder",
    sandbox,
    request: () => ({
      brief: { text: `Fix the failing tests and commit.\n${feedback ?? ""}` },
    }),
  });
}
import type { Sandbox } from "@elie-laloum/outpost/core";
import type { LoopTaskContext } from "@elie-laloum/outpost/workflows";
import { codingTask } from "./coding-task.ts";

export function createAttempt(sandbox: Sandbox) {
  return async (context: LoopTaskContext, feedback: string | undefined) => {
    const result = await codingTask(sandbox, feedback).perform(context);
    return { summary: result.text, commits: result.commits.length };
  };
}
import type { Sandbox } from "@elie-laloum/outpost/core";
import type {
  LoopTaskContext,
  LoopCheckResult,
} from "@elie-laloum/outpost/workflows";

export function createCheck(sandbox: Sandbox) {
  return async (context: LoopTaskContext): Promise<LoopCheckResult> => {
    const tests = await sandbox.command({
      executable: "npm",
      arguments: ["test"],
      signal: context.signal,
    });
    return tests.status === 0
      ? { done: true }
      : { done: false, feedback: `${tests.stdout}\n${tests.stderr}` };
  };
}
import type { Sandbox } from "@elie-laloum/outpost/core";
import { defineLoopTask } from "@elie-laloum/outpost/workflows";
import { createAttempt } from "./loop-attempt.ts";
import { createCheck } from "./loop-check.ts";

export function defineFix(sandbox: Sandbox) {
  return defineLoopTask({
    key: "fix-tests",
    maxRounds: 4,
    attempt: createAttempt(sandbox),
    check: createCheck(sandbox),
  });
}

Run run-loop.ts; it keeps the sandbox open until the workflow finishes.

import { openLoopSandbox } from "./loop-sandbox.ts";
import { defineFix } from "./fix-loop.ts";
import { defineWorkflow } from "@elie-laloum/outpost/workflows";

await using sandbox = await openLoopSandbox();
export const fix = defineFix(sandbox);
export const result = await defineWorkflow("fix-tests", [fix]).start();
result.unwrap();
console.log(result.value(fix).summary);
// Example output: Fixed the parser and verified the tests.

coding.perform(context) ties the agent’s tokens and cancellation to the loop and its budget. coder is a helper: only fix goes in the workflow. attempt returns JSON because a checkpoint saves every candidate, even rejected ones.

A failing npm test returns a nonzero status, and its output becomes the feedback. You decide where it goes: the next brief, as here, or a continued conversation. attempt receives the rejected round’s result as its third argument, for example to continue its conversation.

check can dispatch a reviewer and turn its typed response into a verdict. Both agents count against the same budget.

import {
  createAgent,
  createClaudeHarness,
  defineJsonResponse,
} from "@elie-laloum/outpost/core";
import { z } from "zod";

export const reviewer = createAgent({
  harness: createClaudeHarness({ authentication: "account" }),
});
export const review = defineJsonResponse({
  tag: "review",
  schema: z.object({ approved: z.boolean(), feedback: z.string() }),
});
export const brief = {
  text: 'Review the last commit. End with <review>{"approved": false, "feedback": "What to change"}</review>.',
};
import type { Sandbox } from "@elie-laloum/outpost/core";
import { defineAgentTask } from "@elie-laloum/outpost/workflows";
import { reviewer, review, brief } from "./review-settings.ts";

export declare const sandbox: Sandbox;
export function reviewingTask() {
  return defineAgentTask({
    key: "reviewer",
    sandbox,
    request: () => ({ agent: reviewer, response: review, brief }),
  });
}
import type {
  LoopTaskContext,
  LoopCheckResult,
} from "@elie-laloum/outpost/workflows";
import { reviewingTask } from "./reviewing-task.ts";

export async function reviewChanges(
  context: LoopTaskContext,
): Promise<LoopCheckResult> {
  const { value } = await reviewingTask().perform(context);
  return value.approved
    ? { done: true }
    : { done: false, feedback: value.feedback };
}

Pass it as check: reviewChanges. The request’s agent replaces the sandbox’s agent for this dispatch only.

With a checkpoint, each candidate is saved before check. On resume, a saved candidate goes straight to check, and rejected rounds are not rerun.

An interrupted round replays only with resume: "retry-incomplete", since it may already have changed files. Changing maxRounds makes the checkpoint incompatible.

Each task record lists its rounds, and each phase emits a loop workflow event. The complete event of a rejected round carries its feedback, masked by the workflow’s redact patterns, then limited to its first and last 1,000 characters. context.idempotencyKey differs per round and phase, to deduplicate effects as in Job queues and workers.

API: defineLoopTask · LoopTaskOptions · LoopTaskContext · LoopCheckResult · LoopTaskExhausted · defineAgentTask.

A task copied with { ...task }, including one decorated with defineTask({ ...task, condition }), retains its loop definition and round limit. Pass that resulting task to the workflow and use the same object with result.value(task) and dependency references.

Repeat the brief until the agent declares it done

Section titled “Repeat the brief until the agent declares it done”

passes sends the brief again when a pass ends without a completion marker. Ask for the marker in the brief: Outpost does not add it.

import { dispatch } from "@elie-laloum/outpost/core";
import { coder, repository, sandboxProvider } from "./outpost.config.ts";

const result = await dispatch({
  repository,
  sandboxProvider,
  agent: coder,
  branch: { mode: "named", name: "outpost/flaky-tests" },
  brief: {
    text: "Fix the flaky tests and commit. When every test passes, end your answer with READY_FOR_REVIEW.",
  },
  passes: 3,
  until: "READY_FOR_REVIEW",
});
console.log(result.completed, result.completion);
// Example output: true READY_FOR_REVIEW

Each pass starts a new conversation and sees the commits retained or integrated by the preceding pass. Outpost stops at the first pass whose answer contains a marker. until also accepts a list; until: [] disables matching and runs every pass.

API reference: DispatchResult.

If the agent writes its marker but keeps running, Outpost stops it settleMs after its last output. The result is kept and warn receives a message.

With top-level dispatch(), each pass allocates a fresh sandbox and runs its preparation hooks. A named branch keeps its commits across passes; integrate creates and integrates a separate branch for each pass. With sandbox.dispatch(), passes reuse the open sandbox and workspace. Choose that form when preparation must run only once.

Limit or eventWhat happens
maxRoundsCaps the rounds, resumes included.
Workflow attemptsEach round, and each replay of an interrupted round, counts against budget.attempts.
timeoutMsBounds each round. It stops callbacks that honor context.signal.
An exceptionFails the task at once. A loop task has no retry option.
The last check rejectsThe task fails with LoopTaskExhausted. Its message ends with the first and last 1,000 characters of the last feedback, masked by redact; its feedback property keeps the full text. Dependents do not run.

Fix a failing CI build handles each outcome in a complete script. YAML recipes declare the same loops with agent and command steps; see the correspondence with TypeScript.