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
8 changes: 5 additions & 3 deletions docs/execution-boundaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ Backends map their native lifecycle into `queued`, `starting`, `running`,
`recovering`, and `cancelled` rows are presented as `succeeded`, `starting`, and
`stopped` respectively, avoiding a risky rewrite of durable history.

The current `ExeHerdrBackend` is one implementation. exe.dev supplies command
`ExeHerdrBackend` and `AmpBackend` implement this boundary. exe.dev supplies command
transport and VM wake-up, Herdr supplies workspace and agent supervision, and
the configured harness supplies Codex, Claude, Pi, or another Herdr-supported
agent. A future Amp implementation should implement `ExecutionBackend` without
adding Amp-specific state or commands to the orchestrator.
agent. Amp cloud runs persist an opaque thread handle and expose the canonical
Amp thread URL. Amp does not declare output retrieval, so Factorize does not
ingest thread contents. Amp credentials are dashboard-only; REST and MCP expose
only non-secret target metadata and capabilities.

Launch and prompt delivery are deliberately separate operations. An existing
harness proves only that launch reconciliation succeeded. It never proves that
Expand Down
48 changes: 48 additions & 0 deletions packages/app/src/amp-backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { BackendCommandResult, ExecutionBackend, ExecutionObservation, ExecutionState, LaunchReceipt, LaunchRequest, RunHandle } from "./execution";

export interface AmpConnection { accessToken: string; project: string; apiBaseUrl?: string; }

export function mapAmpState(value: unknown): ExecutionState {
switch (String(value ?? "").toLowerCase()) {
case "queued": case "pending": return "queued";
case "creating": case "provisioning": case "starting": return "starting";
case "running": case "working": case "active": return "running";
case "waiting": case "waiting_for_input": case "blocked": return "blocked";
case "stopping": case "cancelling": case "canceling": return "stopping";
case "stopped": case "cancelled": case "canceled": case "archived": return "stopped";
case "complete": case "completed": case "done": case "succeeded": case "success": return "succeeded";
case "failed": case "error": return "failed";
default: return "running";
}
}

export class AmpBackend implements ExecutionBackend {
readonly kind = "amp";
readonly capabilities = ["stop"] as const;
private base: string;
constructor(private connection: AmpConnection, private request: typeof fetch = fetch) { this.base = (connection.apiBaseUrl || "https://ampcode.com/api/cloud/v1").replace(/\/$/, ""); }
private async call(path: string, init: RequestInit = {}) {
const requestBody = typeof init.body === "string" ? init.body : "";
const response = await this.request(`${this.base}${path}`, { ...init, headers: { Authorization: `Bearer ${this.connection.accessToken}`, "Content-Type": "application/json", ...init.headers } });
const body = await response.text(); let data: any = {}; try { data = body ? JSON.parse(body) : {}; } catch { /* retain raw body */ }
const command: BackendCommandResult = { ok: response.ok, status: response.status, exitCode: null, body, requestBody };
return { data, command };
}
async test() { return (await this.call("/projects?limit=1")).command.ok; }
async launch(request: LaunchRequest): Promise<LaunchReceipt> {
const result = await this.call("/runs", { method: "POST", body: JSON.stringify({ prompt: request.prompt, executor: "orb", project: this.connection.project, title: `Factorize ${request.runId}`, externalId: request.runId }) });
if (!result.command.ok) throw new Error(`Amp launch failed (${result.command.status}): ${result.command.body.slice(0, 500)}`);
const threadId = String(result.data.threadId ?? result.data.threadID ?? result.data.id ?? "");
if (!threadId) throw new Error("Amp launch response did not include a thread ID.");
return { handle: { backendKind: this.kind, id: threadId }, destinationUrl: String(result.data.url ?? `https://ampcode.com/threads/${encodeURIComponent(threadId)}`), capabilities: this.capabilities, observation: { state: mapAmpState(result.data.status ?? "starting") }, command: result.command };
}
async inspect(handle: RunHandle): Promise<ExecutionObservation> {
const result = await this.call(`/runs/${encodeURIComponent(handle.id)}`);
if (!result.command.ok) return { state: result.command.status === 404 ? "failed" : "running", detail: result.command.body, command: result.command };
return { state: mapAmpState(result.data.status ?? result.data.state), detail: result.data.detail, command: result.command };
}
async stop(handle: RunHandle): Promise<ExecutionObservation> {
const result = await this.call(`/runs/${encodeURIComponent(handle.id)}/stop`, { method: "POST", body: "{}" });
return { state: result.command.ok ? mapAmpState(result.data.status ?? "stopped") : "running", detail: result.data.detail ?? result.command.body, command: result.command };
}
}
2 changes: 1 addition & 1 deletion packages/app/src/execution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type ExecutionState = "queued" | "starting" | "running" | "blocked" | "stopping" | "stopped" | "succeeded" | "failed";
export type ExecutionCapability = "output" | "prompt-delivery" | "recovery";
export type ExecutionCapability = "output" | "prompt-delivery" | "recovery" | "stop";
export type PromptDeliveryState = "pending" | "submitting" | "accepted" | "ambiguous" | "failed";

export interface BackendCommandResult {
Expand Down
9 changes: 9 additions & 0 deletions packages/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ app.post("/api/jobs/:id/disable", (c) => jobApi(c, `/jobs/${encodeURIComponent(c
app.post("/api/jobs/:id/invocations", async (c) => jobApi(c, `/jobs/${encodeURIComponent(c.req.param("id"))}/invocations`, { method: "POST", headers: { "Content-Type": "application/json" }, body: await c.req.text() }));
app.get("/api/job-runs", (c) => jobApi(c, `/runs?${new URL(c.req.url).searchParams.toString()}`));
app.get("/api/job-runs/:id", (c) => jobApi(c, `/runs/${encodeURIComponent(c.req.param("id"))}`));
app.post("/api/job-runs/:id/stop", (c) => jobApi(c, `/runs/${encodeURIComponent(c.req.param("id"))}/stop`, { method: "POST" }));
app.get("/api/execution-targets", (c) => jobApi(c, "/execution-targets"));
app.post("/api/schedules/preview", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
Expand Down Expand Up @@ -208,6 +209,14 @@ app.post("/api/connections/exe/test", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch("https://tenant/connections/exe/test", { method: "POST", headers: { "Content-Type": "application/json" }, body: await c.req.text() });
});
app.put("/api/connections/amp", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch("https://tenant/connections/amp", { method: "PUT", headers: { "Content-Type": "application/json" }, body: await c.req.text() });
});
app.post("/api/connections/amp/test", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch("https://tenant/connections/amp/test", { method: "POST", headers: { "Content-Type": "application/json" }, body: await c.req.text() });
});
app.post("/api/pipes", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
const input = await c.req.json() as PipeInput & { source?: any };
Expand Down
Loading