Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import {
createStorageFailureSystem,
createProjectionFailureSystem,
createNonTransactionalSystem,
} from "./test-support/OrchestrationTransactionSystems.ts";
import {
CommandId,
DEFAULT_PROVIDER_INTERACTION_MODE,
ThreadId,
type OrchestrationEvent,
ProviderInstanceId,
} from "@akeru/contracts";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { describe, expect, it } from "vite-plus/test";
import {
now,
asProjectId,
asMessageId,
createOrchestrationSystem,
} from "./test-support/OrchestrationSystem.ts";
describe("OrchestrationEngine", () => {
it("keeps processing queued commands after a storage failure", async () => {
const { runtime, engine } = await createStorageFailureSystem();
const createdAt = now();
await runtime.runPromise(
engine.dispatch({
type: "project.create",
commandId: CommandId.make("cmd-project-flaky-create"),
projectId: asProjectId("project-flaky"),
title: "Flaky Project",
workspaceRoot: "/tmp/project-flaky",
defaultModelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
createdAt,
}),
);
await expect(
runtime.runPromise(
engine.dispatch({
type: "thread.create",
commandId: CommandId.make("cmd-flaky-1"),
threadId: ThreadId.make("thread-flaky-fail"),
projectId: asProjectId("project-flaky"),
title: "flaky-fail",
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
branch: null,
worktreePath: null,
createdAt,
}),
),
).rejects.toThrow("append failed");
const result = await runtime.runPromise(
engine.dispatch({
type: "thread.create",
commandId: CommandId.make("cmd-flaky-2"),
threadId: ThreadId.make("thread-flaky-ok"),
projectId: asProjectId("project-flaky"),
title: "flaky-ok",
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
branch: null,
worktreePath: null,
createdAt,
}),
);
expect(result.sequence).toBe(2);
const eventsAfterRetry = await runtime.runPromise(
Stream.runCollect(engine.readEvents(0)).pipe(
Effect.map((chunk): OrchestrationEvent[] => Array.from(chunk)),
),
);
expect(eventsAfterRetry.map((event) => event.type)).toEqual([
"project.created",
"thread.created",
]);
await runtime.dispose();
});
it("rolls back all events for a multi-event command when projection fails mid-dispatch", async () => {
const { runtime, engine } = await createProjectionFailureSystem();
const createdAt = now();
await runtime.runPromise(
engine.dispatch({
type: "project.create",
commandId: CommandId.make("cmd-project-atomic-create"),
projectId: asProjectId("project-atomic"),
title: "Atomic Project",
workspaceRoot: "/tmp/project-atomic",
defaultModelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
createdAt,
}),
);
await runtime.runPromise(
engine.dispatch({
type: "thread.create",
commandId: CommandId.make("cmd-thread-atomic-create"),
threadId: ThreadId.make("thread-atomic"),
projectId: asProjectId("project-atomic"),
title: "atomic",
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
branch: null,
worktreePath: null,
createdAt,
}),
);
const turnStartCommand = {
type: "thread.turn.start" as const,
commandId: CommandId.make("cmd-turn-start-atomic"),
threadId: ThreadId.make("thread-atomic"),
message: {
messageId: asMessageId("msg-atomic-1"),
role: "user" as const,
text: "hello",
attachments: [],
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required" as const,
createdAt,
};
await expect(runtime.runPromise(engine.dispatch(turnStartCommand))).rejects.toThrow(
"projection failed",
);
const eventsAfterFailure = await runtime.runPromise(
Stream.runCollect(engine.readEvents(0)).pipe(
Effect.map((chunk): OrchestrationEvent[] => Array.from(chunk)),
),
);
expect(eventsAfterFailure.map((event) => event.type)).toEqual([
"project.created",
"thread.created",
]);
const retryResult = await runtime.runPromise(engine.dispatch(turnStartCommand));
expect(retryResult.sequence).toBe(4);
const eventsAfterRetry = await runtime.runPromise(
Stream.runCollect(engine.readEvents(0)).pipe(
Effect.map((chunk): OrchestrationEvent[] => Array.from(chunk)),
),
);
expect(eventsAfterRetry.map((event) => event.type)).toEqual([
"project.created",
"thread.created",
"thread.message-sent",
"thread.turn-start-requested",
]);
expect(
eventsAfterRetry.filter((event) => event.commandId === turnStartCommand.commandId),
).toHaveLength(2);
await runtime.dispose();
});
it("reconciles command state when append persists but projection fails", async () => {
const { runtime, engine } = await createNonTransactionalSystem();
const createdAt = now();
await runtime.runPromise(
engine.dispatch({
type: "project.create",
commandId: CommandId.make("cmd-project-sync-create"),
projectId: asProjectId("project-sync"),
title: "Sync Project",
workspaceRoot: "/tmp/project-sync",
defaultModelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
createdAt,
}),
);
await runtime.runPromise(
engine.dispatch({
type: "thread.create",
commandId: CommandId.make("cmd-thread-sync-create"),
threadId: ThreadId.make("thread-sync"),
projectId: asProjectId("project-sync"),
title: "sync-before",
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
branch: null,
worktreePath: null,
createdAt,
}),
);
await expect(
runtime.runPromise(
engine.dispatch({
type: "thread.archive",
commandId: CommandId.make("cmd-thread-archive-sync-fail"),
threadId: ThreadId.make("thread-sync"),
}),
),
).rejects.toThrow("projection failed");
await expect(
runtime.runPromise(
engine.dispatch({
type: "thread.archive",
commandId: CommandId.make("cmd-thread-archive-sync-retry"),
threadId: ThreadId.make("thread-sync"),
}),
),
).rejects.toThrow("already archived");
await runtime.dispose();
});
it("replays the accepted receipt for a genuine retry of the same command", async () => {
const createdAt = now();
const system = await createOrchestrationSystem();
const { engine } = system;
await system.run(
engine.dispatch({
type: "project.create",
commandId: CommandId.make("cmd-retry-project-create"),
projectId: asProjectId("project-retry"),
title: "Retry Project",
workspaceRoot: "/tmp/project-retry",
defaultModelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
createdAt,
}),
);
await system.run(
engine.dispatch({
type: "thread.create",
commandId: CommandId.make("cmd-retry-thread-create"),
threadId: ThreadId.make("thread-retry"),
projectId: asProjectId("project-retry"),
title: "retry",
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
branch: null,
worktreePath: null,
createdAt,
}),
);
const turnStart = {
type: "thread.turn.start",
commandId: CommandId.make("cmd-retry-turn-start"),
threadId: ThreadId.make("thread-retry"),
message: {
messageId: asMessageId("msg-retry"),
role: "user",
text: "hello",
attachments: [],
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
createdAt,
} as const;
const first = await system.run(engine.dispatch(turnStart));
const second = await system.run(engine.dispatch(turnStart));
expect(second.sequence).toBe(first.sequence);
const readModel = await system.readModel();
const thread = readModel.threads.find((candidate) => candidate.id === "thread-retry");
expect(thread?.messages.filter((message) => message.role === "user")).toHaveLength(1);
await system.dispose();
});
it("rejects reusing an accepted command id for a different aggregate", async () => {
const createdAt = now();
const system = await createOrchestrationSystem();
const { engine } = system;
await system.run(
engine.dispatch({
type: "project.create",
commandId: CommandId.make("cmd-conflict-project-create"),
projectId: asProjectId("project-conflict"),
title: "Conflict Project",
workspaceRoot: "/tmp/project-conflict",
defaultModelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
createdAt,
}),
);
for (const threadId of ["thread-conflict-a", "thread-conflict-b"]) {
await system.run(
engine.dispatch({
type: "thread.create",
commandId: CommandId.make(`cmd-${threadId}-create`),
threadId: ThreadId.make(threadId),
projectId: asProjectId("project-conflict"),
title: threadId,
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
branch: null,
worktreePath: null,
createdAt,
}),
);
}
await system.run(
engine.dispatch({
type: "thread.turn.start",
commandId: CommandId.make("cmd-conflict-turn-start"),
threadId: ThreadId.make("thread-conflict-a"),
message: {
messageId: asMessageId("msg-conflict-a"),
role: "user",
text: "hello",
attachments: [],
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
createdAt,
}),
);
await expect(
system.run(
engine.dispatch({
type: "thread.turn.start",
commandId: CommandId.make("cmd-conflict-turn-start"),
threadId: ThreadId.make("thread-conflict-b"),
message: {
messageId: asMessageId("msg-conflict-b"),
role: "user",
text: "hello again",
attachments: [],
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
createdAt,
}),
),
).rejects.toThrow("already used for thread 'thread-conflict-a'");
const readModel = await system.readModel();
const targetThread = readModel.threads.find(
(candidate) => candidate.id === "thread-conflict-b",
);
expect(targetThread?.messages.filter((message) => message.role === "user")).toHaveLength(0);
await system.dispose();
});
});