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 * as Schema from "effect/Schema";
import * as Predicate from "effect/Predicate";
import { ProviderDriverKind } from "@akeru/contracts";
import { CommandId, EventId, MessageId, ThreadId } from "@akeru/contracts";
import * as Effect from "effect/Effect";
import { afterEach, describe, expect, it } from "vite-plus/test";
import { ProviderAdapterRequestError } from "../../provider/Errors.ts";
import {
asApprovalRequestId,
createProviderCommandHarness,
} from "./test-support/ProviderCommandHarness.ts";
describe("ProviderCommandReactor", () => {
const testScope = createProviderCommandHarness();
const { createHarness } = testScope;
afterEach(testScope.dispose);
it("reacts to thread.approval.respond by forwarding provider approval response", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
await harness.runEffect(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-session-set-for-approval"),
threadId: ThreadId.make("thread-1"),
session: {
threadId: ThreadId.make("thread-1"),
status: "running",
providerName: "codex",
runtimeMode: "approval-required",
activeTurnId: null,
lastError: null,
updatedAt: now,
},
createdAt: now,
}),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.approval.respond",
commandId: CommandId.make("cmd-approval-respond"),
threadId: ThreadId.make("thread-1"),
requestId: asApprovalRequestId("approval-request-1"),
decision: "accept",
createdAt: now,
}),
);
await harness.waitFor(() => harness.respondToRequest.mock.calls.length === 1);
expect(harness.respondToRequest.mock.calls[0]?.[0]).toEqual({
threadId: "thread-1",
requestId: "approval-request-1",
decision: "accept",
});
});
it("reacts to thread.user-input.respond by forwarding structured user input answers", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
harness.respondToUserInput.mockImplementation(() => Effect.never);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-session-set-for-user-input"),
threadId: ThreadId.make("thread-1"),
session: {
threadId: ThreadId.make("thread-1"),
status: "running",
providerName: "codex",
runtimeMode: "approval-required",
activeTurnId: null,
lastError: null,
updatedAt: now,
},
createdAt: now,
}),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.user-input.respond",
commandId: CommandId.make("cmd-user-input-respond"),
threadId: ThreadId.make("thread-1"),
requestId: asApprovalRequestId("user-input-request-1"),
answers: {
sandbox_mode: "workspace-write",
},
createdAt: now,
}),
);
await harness.waitFor(() => harness.respondToUserInput.mock.calls.length === 1);
const readModel = await harness.readModel();
const thread = readModel.threads.find((entry) => entry.id === ThreadId.make("thread-1"));
expect(thread?.messages.filter((message) => message.role === "user")).toEqual([
expect.objectContaining({
id: MessageId.make("user-input:thread-1:user-input-request-1"),
text: "workspace-write",
}),
]);
expect(harness.respondToUserInput.mock.calls[0]?.[0]).toEqual({
threadId: "thread-1",
requestId: "user-input-request-1",
answers: {
sandbox_mode: "workspace-write",
},
});
});
it("normalizes stale Codex approval callbacks without faking approval resolution", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
harness.respondToRequest.mockImplementation(() =>
Effect.fail(
new ProviderAdapterRequestError({
provider: ProviderDriverKind.make("codex"),
method: "item/requestApproval/decision",
detail: "Unknown pending Codex approval request: approval-request-1",
}),
),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-session-set-for-approval-error"),
threadId: ThreadId.make("thread-1"),
session: {
threadId: ThreadId.make("thread-1"),
status: "running",
providerName: "codex",
runtimeMode: "approval-required",
activeTurnId: null,
lastError: null,
updatedAt: now,
},
createdAt: now,
}),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.activity.append",
commandId: CommandId.make("cmd-approval-requested"),
threadId: ThreadId.make("thread-1"),
activity: {
id: EventId.make("activity-approval-requested"),
tone: "approval",
kind: "approval.requested",
summary: "Command approval requested",
payload: {
requestId: "approval-request-1",
requestKind: "command",
},
turnId: null,
createdAt: now,
},
createdAt: now,
}),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.approval.respond",
commandId: CommandId.make("cmd-approval-respond-stale"),
threadId: ThreadId.make("thread-1"),
requestId: asApprovalRequestId("approval-request-1"),
decision: "acceptForSession",
createdAt: now,
}),
);
await harness.drain();
const readModel = await harness.readModel();
const thread = readModel.threads.find((entry) => entry.id === ThreadId.make("thread-1"));
expect(thread).toBeDefined();
const failureActivity = thread?.activities.find(
(activity) => activity.kind === "provider.approval.respond.failed",
);
expect(failureActivity).toBeDefined();
expect(failureActivity?.payload).toMatchObject({
requestId: "approval-request-1",
detail: expect.stringContaining("Stale pending approval request: approval-request-1"),
});
const resolvedActivity = thread?.activities.find(
(activity) =>
activity.kind === "approval.resolved" &&
(activity.payload === null || Predicate.isObjectOrArray(activity.payload)) &&
activity.payload !== null &&
(activity.payload as Record<string, Schema.Json>).requestId === "approval-request-1",
);
expect(resolvedActivity).toBeUndefined();
expect(thread?.messages).toContainEqual(
expect.objectContaining({
role: "assistant",
text: "I could not continue that approval because the bot session restarted. Send it again.",
}),
);
expect(thread?.session).toMatchObject({
status: "error",
activeTurnId: null,
});
});
it("surfaces non-resumable provider user-input callbacks as stale failures", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
harness.respondToUserInput.mockImplementation(() =>
Effect.fail(
new ProviderAdapterRequestError({
provider: ProviderDriverKind.make("claudeAgent"),
method: "item/tool/respondToUserInput",
detail: "Unknown pending Codex user input request: user-input-request-1",
}),
),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-session-set-for-user-input-error"),
threadId: ThreadId.make("thread-1"),
session: {
threadId: ThreadId.make("thread-1"),
status: "running",
providerName: "claudeAgent",
runtimeMode: "approval-required",
activeTurnId: null,
lastError: null,
updatedAt: now,
},
createdAt: now,
}),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.activity.append",
commandId: CommandId.make("cmd-user-input-requested"),
threadId: ThreadId.make("thread-1"),
activity: {
id: EventId.make("activity-user-input-requested"),
tone: "info",
kind: "user-input.requested",
summary: "User input requested",
payload: {
requestId: "user-input-request-1",
questions: [
{
id: "sandbox_mode",
header: "Sandbox",
question: "Which mode should be used?",
options: [
{
label: "workspace-write",
description: "Allow workspace writes only",
},
],
},
],
},
turnId: null,
createdAt: now,
},
createdAt: now,
}),
);
await harness.runEffect(
harness.engine.dispatch({
type: "thread.user-input.respond",
commandId: CommandId.make("cmd-user-input-respond-stale"),
threadId: ThreadId.make("thread-1"),
requestId: asApprovalRequestId("user-input-request-1"),
answers: {
sandbox_mode: "workspace-write",
},
createdAt: now,
}),
);
await harness.drain();
const readModel = await harness.readModel();
const thread = readModel.threads.find((entry) => entry.id === ThreadId.make("thread-1"));
expect(thread).toBeDefined();
const failureActivity = thread?.activities.find(
(activity) => activity.kind === "provider.user-input.respond.failed",
);
expect(failureActivity).toBeDefined();
expect(failureActivity?.payload).toMatchObject({
requestId: "user-input-request-1",
detail: expect.stringContaining("Stale pending user-input request: user-input-request-1"),
});
const resolvedActivity = thread?.activities.find(
(activity) =>
activity.kind === "user-input.resolved" &&
(activity.payload === null || Predicate.isObjectOrArray(activity.payload)) &&
activity.payload !== null &&
(activity.payload as Record<string, Schema.Json>).requestId === "user-input-request-1",
);
expect(resolvedActivity).toBeUndefined();
expect(thread?.messages).toContainEqual(
expect.objectContaining({
role: "assistant",
text: "I could not continue that request because the bot session restarted. Send it again.",
}),
);
expect(thread?.session).toMatchObject({
status: "error",
activeTurnId: null,
});
});
});