Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// The job runtime's public types: one record for every hand-off between bots, one lifecycle, one wake lane.
import type { ApprovalMode } from "../../shared/approval-mode.ts";
import type { HostFault } from "../host-fault.ts";
/** Where the work ultimately started. Immutable down the whole tree. */
export type OriginKind = "person" | "routine" | "outside";
/** The concrete source behind an outside origin. */
export type OriginSource = "chat" | "routine" | "webhook" | "relay" | "external_runtime" | "guest" | "peer" | "api";
export interface Origin {
kind: OriginKind;
source: OriginSource;
/** The thread, routine or webhook the work came from, when known. */
ref?: string;
/** The person's own request, harness-quoted (at most 400 characters), when kind is "person". Never model text. */
quote?: string;
/** Who the work is for when a bot acts for a named person. */
onBehalfOf?: string;
}
export type RequesterKind = "person" | "bot_turn" | "routine_run" | "external_runtime";
export interface Requester {
kind: RequesterKind;
/** The bot whose turn or run asked (absent for a person or an external runtime). */
botId?: string;
/** The conversation the result returns to (absent for an external runtime). */
threadId?: string;
/** The routine run or the external runtime's own reference. */
runId?: string;
/** The requester is a Chief of Staff conversation: its level may flow down. */
chief?: boolean;
}
export type DestinationKind = "work_thread" | "standing_thread" | "room_thread";
export interface Assignee {
botId: string;
/** `threadId` is empty only on a legacy-import row that named no thread: the kind is then
* "standing_thread" and the host resolves or refuses it at dispatch. */
destination: { kind: DestinationKind; threadId: string; groupId?: string };
}
/** work: do it with your own tools. consult: answer from what you know, act
* on nothing. notify: one-way, nothing comes back. */
export type Mode = "work" | "consult" | "notify";
/** resume: the requester is woken with the result. poll: the result waits
* in the inbox for status(). none: no inbox entry (notify). */
export type Reply = "resume" | "poll" | "none";
export type Status =
| "queued" // accepted, not yet admitted
| "running" // a turn of the assignee is executing it
| "needs_input" // parked on the person (a card or a NEEDS INPUT line)
| "waiting" // its turn ended after handing out work; waits on children
| "resume" // every child settled, or one it waits on parked a new question; owes its own follow-up turn
| "completed"
| "failed"
| "cancelled";
export const TERMINAL: readonly Status[] = ["completed", "failed", "cancelled"];
/** A stop with no other cause is the person's own. */
export const STOPPED_BY_USER = "Stopped by user";
/** The cause a stop leaves on the work its stopped assignment had handed on. */
export const STOPPED_WITH_REQUEST = "Stopped with the request it was for";
/** The causes a stopped requester leaves on the work it had queued: it never started, or was due to start again. Whose stop
* it was is the requester's. */
export const STOPPED_BEFORE_START = "Stopped before it started";
export const STOPPED_BEFORE_RESUME = "Stopped before it resumed";
/** A coordinated turn's own abort. Within a stop under way it is that stop; with none it is a cut the system made, never
* the person's Stop. */
export const TURN_CUT = "The coordinated turn was cut";
/** Why it ended the way it did, typed so a requester can branch on it. */
export type Outcome =
| "done"
| "needs_input" // ended with a question the person has not answered
| "cap_continued" // a step cap was hit and the work continued in place
| "stalled"
| "interrupted" // a restart, reload or sleep cut it and it was not resumed again
| "budget"
| "provider_error"
| "denied" // a permission the level did not cover was refused
| "expired" // a wait on the person ran out: not run, not refused
| "stopped" // the person stopped it
| "refused" // validation refused it at dispatch
| "duplicate";
export type Level = ApprovalMode;
export type LevelSource = "own" | "chief" | "engine_cap";
export interface Assignment {
id: string;
rootId: string;
parentId?: string;
/** Edges from the root: a root is 0. */
depth: number;
origin: Origin;
requester: Requester;
assignee: Assignee;
mode: Mode;
reply: Reply;
/** The request, harness-stored (at most 12 000 characters). */
brief: string;
/** The requester's own completion criterion, if it gave one. */
doneWhen?: string;
/** A root that is one step of a room goal run (the run's id): it neither parks nor continues in
* place and is not run again after a restart. Never changed after begin. */
goalRunId?: string;
status: Status;
outcome?: Outcome;
outcomeDetail?: string;
/** A failed turn's cause outside the work itself, read from the system's own failure words (never the bot's reply),
* when sending the same work again would meet it again. */
fault?: HostFault;
level: Level;
levelSource: LevelSource;
/** Turns started for it, including resumes and reruns. */
executions: number;
/** Turns of it a restart cut and ran again in place. */
restarts: number;
/** Set until the turn after a restart settles: "rerun" its cut turn runs again; "cut" it was cut after
* handing out work; "teammates" only work it waits on was cut. */
restart?: "rerun" | "cut" | "teammates";
createdAt: number;
startedAt?: number;
/** Last durable progress: a dispatch, a settlement, a child's settlement — and, while a turn
* runs, activity the host reports through progress(), written on a coarse cadence. */
lastActivityAt: number;
finishedAt?: number;
/** What the assignee last said or how it ended (at most 12 000 characters). */
result: string;
/** What the harness observed of the turn (commands, files, tool counts),
* attached to the report as evidence. Never model text. */
evidence?: TurnEvidence;
/** The report reached the requester's conversation. */
reportedAt?: number;
/** The same brief to the same assignee under one parent is one request;
* a repeat lands on it unless rework is set. */
rework: boolean;
/** While parked: the exact question for the person. */
/** Its reply only acknowledged: its requester takes no turn for it (status-reply.ts). */
statusOnly?: boolean;
question?: string;
/** While parked: when the person was reminded of it, so a restart never reminds them twice. */
remindedAt?: number;
/** While parked under Full access: the question is its requester's to answer, never the person's.
* Still unanswered when the requester's turn that carries it ends, the assignee goes on with `default`, or with its
* own pick when it named none. */
decide?: { default?: string };
/** The answer, harness-quoted, carried into the next turn. */
answer?: string;
/** Who gave `answer` when the person did not: a bot that passed one on or decided it, or nobody (the default). */
answeredBy?: Answerer;
/** On a root only: the budget window since its last durable progress. */
/** `total`: every turn the root's tree has started, which no progress resets. `failed`: handed-off work that failed
* since any last completed. The rest count since the root's last progress. */
budgets?: { since: number; requests: number; executions: number; wakes: number; total?: number; failed?: number };
/** While parked on the person's cards (a root): the ids of the cards the
* turn left open; the answer arrives when the person acts on them. */
cards?: string[];
/** The next execution continues the last one in place: after a step cap,
* or a wait the host could not hold (a computer seat). Cleared at dispatch. */
continuation?: { note: string; handoffPath?: string; count: number };
/** While a node continues in place, for the loop guard: `stale` counts consecutive continuations
* with no successful tool call; `errors` holds the last three tool error texts. */
guard?: { stale: number; errors: string[] };
/** While a turn runs: the awaited children whose open questions its input carries. A restart that cuts the turn
* reruns it with them once more; no other turn carries them again. */
carrying?: string[];
/** A peer-review card allowed this before it was sent. */
approved?: boolean;
/** A room brief shown once per call: every recipient of that call shares it. */
batchKey?: string;
/** The call that handed this out, when one call reached several teammates. */
fanOut?: string;
version: 1;
}
export type Answerer = { kind: "bot"; botId: string } | { kind: "default" };
export interface TurnEvidence {
commands?: number;
filesChanged?: string[];
toolCalls?: number;
summary?: string;
/** Tool calls of this execution that returned ok. */
okToolCalls?: number;
/** The tool error texts of this execution, oldest first, at most 3, each clipped to 200 chars. */
errors?: string[];
/** Outward actions this execution completed — sends, posts, publishes, commits, pushes, payments —
* as short labels ("GMAIL_SEND_EMAIL", "git push", "COMPOSIO_PROXY_EXECUTE"). */
outward?: string[];
}
export type EventKind =
| "enqueued" | "refused" | "admitted" | "started" | "progress" | "parked" | "answered"
| "waiting" | "resume_due" | "settled" | "reported" | "woken" | "cancelled"
| "restart_reconciled" | "budget" | "stalled" | "continued" | "held" | "reminded";
export interface AssignmentEvent {
id: number;
assignmentId: string;
at: number;
kind: EventKind;
detail?: string;
}
export type InboxKind = "settled" | "needs_input" | "stalled";
/** The one wake lane: a requester learns about its work only through this. */
export interface InboxItem {
id: string;
ownerBotId?: string;
ownerThreadId?: string;
/** External runtimes poll by run id instead of a thread. */
ownerRunId?: string;
assignmentId: string;
kind: InboxKind;
createdAt: number;
deliveredAt?: number;
}
export interface EnqueueRequest {
/** The requesting conversation's own running assignment; absent when a new root opens. */
parentId?: string;
origin: Origin;
requester: Requester;
assignee: Assignee;
mode?: Mode;
reply?: Reply;
brief: string;
doneWhen?: string;
rework?: boolean;
/** The requester's own level and, for a Chief lineage, the Chief
* conversation's level, as the harness knows them. */
levels: { own: Level; chief?: Level };
/** The assignee's engine, for the level cap. */
driverKind?: string;
approved?: boolean;
batchKey?: string;
/** One call that hands the same work to several teammates: it counts once against the request budget. */
fanOut?: string;
/** A new thread of the requester's own bot (tree.ts ownThread): the requesting turn is not an ancestor it refuses. */
ownThread?: boolean;
}
export type EnqueueResult =
| { ok: true; assignment: Assignment; duplicate: boolean }
| { ok: false; reason: string; outcome: "refused" | "budget" | "duplicate" };
export interface RunInput {
/** first; resume (returns settled children's results and awaited children's open questions); rerun (after a restart cut it); answered; continued. */
turn: "first" | "resume" | "rerun" | "answered" | "continued";
/** Not the node's first turn (any `turn` but "first"). */
resumed: boolean;
/** What the restart did, for the first line of the turn ("" when nothing). */
restartNote: string;
/** The harness-written envelope that opens the turn. */
envelope: string;
/** The person's answer to the question this assignment was parked on. */
answer?: string;
/** The settled children whose results this (resume) turn returns. */
settled?: Assignment[];
/** The awaited children parked on a question this turn passes on: a question not yet delivered, or, in the rerun
* of a turn a restart cut, the questions that turn carried. */
asked?: Assignment[];
/** For a continued turn: why it continues and, after a step cap, where
* the steps so far are listed. */
continuation?: { note: string; handoffPath?: string };
level: Level;
mode: Mode;
}
export type RunResult =
/** statusOnly: nothing in `text` for the requester to do (status-reply.ts). */
| { kind: "completed"; text: string; evidence?: TurnEvidence; statusOnly?: boolean }
/** failure: the system's own words for why the turn failed and the engine's stop reason, apart from anything the bot said. */
| { kind: "failed"; outcome: Exclude<Outcome, "done" | "needs_input">; text: string; evidence?: TurnEvidence; failure?: { text: string; stopReason?: string | null } }
| { kind: "needs_input"; question: string; cards?: string[]; decide?: { default?: string }; evidence?: TurnEvidence }
/** The turn stopped short (a step cap, a seat it could not get) and the
* same node continues in place with its next execution. */
| { kind: "continue"; text: string; note: string; handoffPath?: string; evidence?: TurnEvidence };
/** How a host-run root turn ended (Runtime.end). */
export interface EndResult {
ok: boolean;
text: string;
evidence?: TurnEvidence;
/** The turn left the person something to act on: it parks until they do. */
needsInput?: { question: string; cards?: string[] };
/** The turn stopped short and continues in place. */
continue?: { note: string; handoffPath?: string };
/** The host's stall watchdog stopped the turn. */
stalled?: boolean;
}
export interface Owner {
botId?: string;
threadId?: string;
runId?: string;
}
/** Display names the envelope needs. The host owns names; the runtime
* owns ids. */
export interface EnvelopeNames {
requester: string;
assignee: string;
/** The person the work is for, when the origin is a person. */
person?: string;
/** The requester conversation's title, when there is one. */
thread?: string;
/** The tool the assignee asks the person a question with set answers, when its engine has one: the exact name its
* model calls. */
askTool?: string;
/** The bot that gave the answer the turn carries, when a bot did (`answeredBy`). */
answerer?: string;
}
/** What the runtime needs from the server. No method may throw into the runtime: a throw is logged and read as "not now". */
export interface TurnRunner {
/** Recheck addresses and routes right before every dispatch. A string
* refuses the assignment with that reason. */
validate(a: Assignment, parent?: Assignment): string | undefined;
/** Whether the assignee can take a turn now. A string says why not, once,
* where the work was sent; false waits silently. */
canStart(a: Assignment): true | false | string;
/** Run one turn of the assignee. Resolves when the engine settles. The
* signal aborts the turn (a cancel or a stop). */
run(a: Assignment, input: RunInput, signal: AbortSignal): Promise<RunResult>;
/** The assignment is parked on the person; show what it needs. `restored`
* is the restart's re-park: show it again only if it never got shown. */
park(a: Assignment, question: string, restored?: boolean): void;
/** Work parked on the person has waited inputRemindMs: remind them, once. `closesAt`: when it expires. */
remind?(a: Assignment, closesAt: number): void;
/** A settled child, into its requester's conversation (a chip, a line), or
* a root that ended alone, into its own. Called once per settlement. */
report(a: Assignment, parent?: Assignment): void;
/** Rows changed: refresh what shows them. */
changed(assignments: readonly Assignment[]): void;
/** Display names for the envelope of a turn (ids are the fallback). */
names(a: Assignment, parent?: Assignment): EnvelopeNames;
/** What the cut turn of this node already did outward, as short labels. Read at open() only: a
* turn that acted outward is not run again. Absent or empty: the turn reruns once. */
outward?(a: Assignment): string[];
/** The assignee lives in a place that cannot be reached now (a computer that is off or asleep). Queued work for it is
* held by no window: neither the queue bound nor the tree's stall window runs while this is true. */
away?(a: Assignment): boolean;
}
export interface Clock {
/** Wall-clock milliseconds, for timestamps. */
now(): number;
/** Monotonic milliseconds that do not advance while the host sleeps, for
* stall windows. */
monotonic(): number;
}
export interface Limits {
/** Edges from the root a child may sit at. */
depth: number;
/** Per root, counted since the root's last progress. */
requests: number;
executions: number;
wakes: number;
/** Turns one root's tree may start in all; progress does not renew it. */
totalExecutions: number;
/** Handed-off work that may fail in a row, with none completing between, before the root's tree is stopped. */
failedInARow: number;
/** A queued assignment that never started within this is expired. */
queueMs: number;
/** No durable progress on a root for this long fails the tree. */
stallMs: number;
/** A turn with no activity for this long is stalled (the host says what
* counts as activity by touching progress()). */
turnStallMs: number;
/** How long handed-off work parked on the person waits before it expires: not run, not refused. On the wall clock,
* restarts included. Infinite unless a host sets it. A root waits on no one above it and has no such limit. */
inputWaitMs: number;
/** How long that work waits before the person is reminded of it, once. The rest of inputWaitMs always follows the
* reminder, however late a restart let it go out. Infinite: no reminder. */
inputRemindMs: number;
}
export const DEFAULT_LIMITS: Limits = {
depth: 4,
requests: 24,
executions: 48,
wakes: 48,
totalExecutions: 300,
failedInARow: 5,
queueMs: 24 * 60 * 60_000,
stallMs: 4 * 60 * 60_000,
turnStallMs: 20 * 60_000,
inputWaitMs: Number.POSITIVE_INFINITY,
inputRemindMs: Number.POSITIVE_INFINITY,
};
export interface LiveEdge {
sourceBotId: string;
targetBotId: string;
state: "queued" | "running" | "needs_input" | "waiting";
threadId: string;
groupId?: string;
}
export interface StatusView {
assignment: Assignment;
children: Assignment[];
inbox: InboxItem[];
}
export const terminal = (status: Status): boolean => TERMINAL.includes(status);