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 NodeAssert from "node:assert/strict";
import { it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { describe } from "vite-plus/test";
import { DEFAULT_MODEL } from "@akeru/contracts";
import * as CodexErrors from "effect-codex-app-server/errors";
import * as EffectCodexSchema from "effect-codex-app-server/schema";
import {
buildCodexDeveloperInstructions,
codexDefaultModeDeveloperInstructions,
} from "../CodexDeveloperInstructions.ts";
import { codexSessionAppServerArgs } from "./codexLaunchArgs.ts";
import {
buildTurnStartParams,
describeMcpElicitation,
hasConfiguredMcpServer,
isRecoverableThreadResumeError,
memoryConsolidationNotificationFilter,
toMcpElicitationResponse,
} from "./CodexSessionRuntime.ts";
import { request, makeThreadStartedNotification } from "./test-support/codexProtocol.ts";
describe("buildTurnStartParams", () => {
it("keeps invalid turn values only in the schema cause", () => {
const secret = "codex-turn-input-secret-sentinel";
const attachment = { type: "image", url: "https://example.test/image.png" } as const;
// Runtime corruption exercises the protocol decoder's diagnostics.
Object.defineProperty(attachment, "url", { value: { secret } });
const error = Effect.runSync(
buildTurnStartParams({
threadId: "provider-thread-1",
runtimeMode: "full-access",
attachments: [attachment],
}).pipe(Effect.flip),
);
const { cause, ...directDiagnostics } = error;
NodeAssert.equal(error.operation, "decode-request-payload");
NodeAssert.equal(error.method, "turn/start");
NodeAssert.ok((error.issueCount ?? 0) > 0);
NodeAssert.ok(error.issueKinds?.includes("Pointer"));
NodeAssert.ok((error.maximumPathDepth ?? 0) > 0);
NodeAssert.ok(Schema.isSchemaError(cause));
NodeAssert.doesNotMatch(error.message, new RegExp(secret));
NodeAssert.doesNotMatch(JSON.stringify(directDiagnostics), new RegExp(secret));
});
});
describe("buildTurnStartParams", () => {
it("runs a stored plan-mode turn in the default collaboration mode", () => {
const params = Effect.runSync(
buildTurnStartParams({
threadId: "provider-thread-1",
runtimeMode: "full-access",
prompt: "Make a plan",
model: "gpt-5.3-codex",
effort: "medium",
interactionMode: "plan",
}),
);
NodeAssert.deepStrictEqual(params, {
threadId: "provider-thread-1",
approvalPolicy: "never",
approvalsReviewer: "user",
sandboxPolicy: {
type: "dangerFullAccess",
},
input: [
{
type: "text",
text: "Make a plan",
},
],
model: "gpt-5.3-codex",
effort: "medium",
collaborationMode: {
mode: "default",
settings: {
model: "gpt-5.3-codex",
reasoning_effort: "medium",
developer_instructions: buildCodexDeveloperInstructions({
model: "gpt-5.3-codex",
reasoningEffort: "medium",
}),
},
},
});
});
});
describe("buildTurnStartParams", () => {
it("includes default collaboration mode and image attachments", () => {
const params = Effect.runSync(
buildTurnStartParams({
threadId: "provider-thread-1",
runtimeMode: "auto-accept-edits",
prompt: "Implement it",
model: "gpt-5.3-codex",
interactionMode: "default",
attachments: [
{
type: "image",
url: "data:image/png;base64,abc",
},
],
}),
);
NodeAssert.deepStrictEqual(params, {
threadId: "provider-thread-1",
approvalPolicy: "on-request",
approvalsReviewer: "user",
sandboxPolicy: {
type: "workspaceWrite",
},
input: [
{
type: "text",
text: "Implement it",
},
{
type: "image",
url: "data:image/png;base64,abc",
},
],
model: "gpt-5.3-codex",
collaborationMode: {
mode: "default",
settings: {
model: "gpt-5.3-codex",
reasoning_effort: "medium",
developer_instructions: buildCodexDeveloperInstructions({
model: "gpt-5.3-codex",
reasoningEffort: "medium",
}),
},
},
});
});
});
describe("buildTurnStartParams", () => {
it("reports the same fallback model and effort in settings and instructions", () => {
const params = Effect.runSync(
buildTurnStartParams({
threadId: "provider-thread-1",
runtimeMode: "full-access",
prompt: "Go",
interactionMode: "default",
}),
);
const settings = params.collaborationMode?.settings;
NodeAssert.equal(settings?.model, DEFAULT_MODEL);
NodeAssert.equal(settings?.reasoning_effort, "medium");
NodeAssert.ok(settings?.developer_instructions?.includes(`as ${DEFAULT_MODEL} with medium`));
});
});
describe("buildTurnStartParams", () => {
it.effect("routes approvals to the auto reviewer in auto mode", () =>
Effect.gen(function* () {
const params = yield* buildTurnStartParams({
threadId: "provider-thread-1",
runtimeMode: "auto",
prompt: "Ship it",
});
NodeAssert.deepStrictEqual(params, {
threadId: "provider-thread-1",
approvalPolicy: "on-request",
approvalsReviewer: "auto_review",
sandboxPolicy: {
type: "workspaceWrite",
},
input: [
{
type: "text",
text: "Ship it",
},
],
});
}),
);
});
describe("buildTurnStartParams", () => {
it("omits collaboration mode when interaction mode is absent", () => {
const params = Effect.runSync(
buildTurnStartParams({
threadId: "provider-thread-1",
runtimeMode: "approval-required",
prompt: "Review",
}),
);
NodeAssert.deepStrictEqual(params, {
threadId: "provider-thread-1",
approvalPolicy: "untrusted",
approvalsReviewer: "user",
sandboxPolicy: {
type: "readOnly",
},
input: [
{
type: "text",
text: "Review",
},
],
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("preserves the app name and advertised persistence choices", () => {
NodeAssert.deepStrictEqual(describeMcpElicitation(request), {
appName: "Safari",
options: [
{ decision: "cancel", label: "Cancel" },
{ decision: "decline", label: "Decline" },
{ decision: "acceptForSession", label: "Allow for this session" },
{ decision: "acceptAlways", label: "Always allow Safari" },
{ decision: "accept", label: "Approve" },
],
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("extracts the app name from a Computer Use request without metadata", () => {
const { _meta, ...requestWithoutMetadata } = request;
NodeAssert.equal(describeMcpElicitation(requestWithoutMetadata).appName, "Safari");
});
});
describe("Codex MCP elicitation approvals", () => {
it("returns the accepted form option to Codex", () => {
NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "accept"), {
action: "accept",
content: { approval: "once" },
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("returns session-scoped approval in the MCP response", () => {
NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "acceptForSession"), {
action: "accept",
_meta: { persist: "session" },
content: { approval: "session" },
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("returns persistent approval in the MCP response", () => {
NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "acceptAlways"), {
action: "accept",
_meta: { persist: "always" },
content: { approval: "always" },
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("returns rejection without form content", () => {
NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "decline"), {
action: "decline",
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("returns cancellation without form content", () => {
NodeAssert.deepStrictEqual(toMcpElicitationResponse(request, "cancel"), {
action: "cancel",
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("supports boolean permanent-approval fields", () => {
const booleanRequest = {
...request,
_meta: { app_name: "Safari" },
requestedSchema: {
type: "object",
properties: {
always: { type: "boolean", title: "Always allow Safari" },
},
},
} satisfies EffectCodexSchema.McpServerElicitationRequestParams;
NodeAssert.ok(
describeMcpElicitation(booleanRequest).options.some(
(option) => option.decision === "acceptAlways",
),
);
NodeAssert.deepStrictEqual(toMcpElicitationResponse(booleanRequest, "acceptAlways"), {
action: "accept",
_meta: { persist: "always" },
content: { always: true },
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("preserves valid nullable MCP form fields and persistence choices", () => {
const nullableRequest = {
...request,
_meta: {
app_name: null,
appName: "Safari",
connector_name: null,
persist: null,
target: null,
tool_params: null,
},
requestedSchema: {
type: "object",
properties: {
approval: {
type: "string",
title: null,
description: null,
default: null,
enum: ["once", "always"],
enumNames: null,
},
},
required: ["approval"],
},
} satisfies EffectCodexSchema.McpServerElicitationRequestParams;
NodeAssert.equal(describeMcpElicitation(nullableRequest).appName, "Safari");
NodeAssert.ok(
describeMcpElicitation(nullableRequest).options.some(
(option) => option.decision === "acceptAlways",
),
);
NodeAssert.deepStrictEqual(toMcpElicitationResponse(nullableRequest, "acceptAlways"), {
action: "accept",
_meta: { persist: "always" },
content: { approval: "always" },
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("declines required form fields that an approval prompt cannot collect", () => {
const inputRequest = {
...request,
requestedSchema: {
type: "object",
properties: {
email: { type: "string", format: "email" },
},
required: ["email"],
},
} satisfies EffectCodexSchema.McpServerElicitationRequestParams;
NodeAssert.deepStrictEqual(toMcpElicitationResponse(inputRequest, "accept"), {
action: "decline",
});
});
});
describe("Codex MCP elicitation approvals", () => {
it("omits persistence choices that cannot satisfy required form fields", () => {
const onceOnlyRequest = {
...request,
_meta: { app_name: "Safari", persist: ["session", "always"] },
requestedSchema: {
type: "object",
properties: {
approval: {
type: "string",
enum: ["once"],
},
},
required: ["approval"],
},
} satisfies EffectCodexSchema.McpServerElicitationRequestParams;
NodeAssert.deepStrictEqual(describeMcpElicitation(onceOnlyRequest).options, [
{ decision: "cancel", label: "Cancel" },
{ decision: "decline", label: "Decline" },
{ decision: "accept", label: "Approve" },
]);
});
});
describe("buildCodexDeveloperInstructions", () => {
it("appends runtime info after the mode instructions", () => {
const instructions = buildCodexDeveloperInstructions({
model: "gpt-5.3-codex",
reasoningEffort: "high",
});
NodeAssert.ok(instructions.startsWith(codexDefaultModeDeveloperInstructions(true)));
NodeAssert.match(instructions, /Akeru Bot/);
NodeAssert.match(instructions, /Codex harness/);
NodeAssert.match(instructions, /as gpt-5\.3-codex with high reasoning effort/);
});
});
describe("buildCodexDeveloperInstructions", () => {
it("varies with the model and effort of each turn", () => {
const first = buildCodexDeveloperInstructions({
model: "gpt-5.3-codex",
reasoningEffort: "medium",
});
const second = buildCodexDeveloperInstructions({
model: "gpt-5.4",
reasoningEffort: "high",
});
NodeAssert.notEqual(first, second);
});
});
describe("buildCodexDeveloperInstructions", () => {
it("flattens multiline metadata into single-line runtime info", () => {
const instructions = buildCodexDeveloperInstructions({
model: "gpt\n5.3\ncodex",
reasoningEffort: " high\neffort ",
});
NodeAssert.match(instructions, /as gpt 5\.3 codex with high effort reasoning effort/);
NodeAssert.doesNotMatch(instructions, /<runtime_info>[^<]*\n/);
});
});
describe("Akeru browser developer instructions", () => {
it("prefers the product-native preview tools", () => {
for (const instructions of [codexDefaultModeDeveloperInstructions(true)]) {
NodeAssert.match(instructions, /akeru/);
NodeAssert.match(instructions, /preview_status/);
NodeAssert.match(instructions, /preview_open/);
NodeAssert.match(instructions, /Do not switch to global browser skills/);
}
});
});
describe("Akeru browser developer instructions", () => {
it("omits the browser block entirely when the preview tools are not attached", () => {
for (const instructions of [codexDefaultModeDeveloperInstructions(false)]) {
NodeAssert.doesNotMatch(instructions, /preview_status/);
NodeAssert.doesNotMatch(instructions, /preview_open/);
NodeAssert.doesNotMatch(instructions, /Akeru Bot collaborative browser/);
// Steering away from other browser automation must go with the tools;
// keeping it would leave the model talked out of its only option.
NodeAssert.doesNotMatch(instructions, /Do not switch to global browser skills/);
// The rest of the collaboration mode is untouched.
NodeAssert.match(instructions, /<collaboration_mode>/);
NodeAssert.match(instructions, /<\/collaboration_mode>/);
}
});
});
describe("Akeru browser developer instructions", () => {
it("tracks the turn's MCP configuration rather than defaulting to on", () => {
const runtime = { model: "gpt-5.3-codex", reasoningEffort: "high" };
NodeAssert.match(buildCodexDeveloperInstructions(runtime, true), /preview_open/);
NodeAssert.doesNotMatch(buildCodexDeveloperInstructions(runtime, false), /preview_open/);
});
});
describe("hasConfiguredMcpServer", () => {
it("detects inline Codex MCP configuration arguments", () => {
NodeAssert.equal(hasConfiguredMcpServer(undefined), false);
NodeAssert.equal(hasConfiguredMcpServer(["--model", "gpt-5.4"]), false);
NodeAssert.equal(
hasConfiguredMcpServer(["-c", 'mcp_servers.akeru.url="http://127.0.0.1/mcp"']),
true,
);
});
});
describe("memoryConsolidationNotificationFilter", () => {
it("suppresses memory consolidation without hiding other Codex subagents", () => {
const shouldSuppress = memoryConsolidationNotificationFilter();
NodeAssert.equal(
shouldSuppress(
makeThreadStartedNotification("memory-thread", "unknown", "memory_consolidation"),
),
true,
);
NodeAssert.equal(
shouldSuppress({
method: "item/agentMessage/delta",
params: {
delta: "internal memory update",
itemId: "memory-message",
threadId: "memory-thread",
turnId: "memory-turn",
},
}),
true,
);
NodeAssert.equal(
shouldSuppress({
method: "serverRequest/resolved",
params: {
requestId: "memory-approval",
threadId: "memory-thread",
},
}),
false,
);
NodeAssert.equal(
shouldSuppress({
method: "warning",
params: {
message: "internal warning",
threadId: "memory-thread",
},
}),
true,
);
NodeAssert.equal(
shouldSuppress({
method: "item/agentMessage/delta",
params: {
delta: "normal reply",
itemId: "root-message",
threadId: "root-thread",
turnId: "root-turn",
},
}),
false,
);
NodeAssert.equal(
shouldSuppress(
makeThreadStartedNotification("legacy-memory-thread", {
subAgent: "memory_consolidation",
}),
),
true,
);
for (const source of [
{ subAgent: "review" as const },
{ subAgent: "compact" as const },
{
subAgent: {
thread_spawn: {
depth: 1,
parent_thread_id: "root-thread",
},
},
},
]) {
NodeAssert.equal(
shouldSuppress(makeThreadStartedNotification("visible-subagent", source)),
false,
);
}
});
});
describe("memoryConsolidationNotificationFilter", () => {
it("forgets memory consolidation threads after they close", () => {
const shouldSuppress = memoryConsolidationNotificationFilter();
shouldSuppress(
makeThreadStartedNotification("memory-thread", "unknown", "memory_consolidation"),
);
NodeAssert.equal(
shouldSuppress({
method: "thread/closed",
params: { threadId: "memory-thread" },
}),
true,
);
NodeAssert.equal(
shouldSuppress({
method: "item/agentMessage/delta",
params: {
delta: "later message",
itemId: "later-message",
threadId: "memory-thread",
turnId: "later-turn",
},
}),
false,
);
});
});
describe("codexSessionAppServerArgs", () => {
it("keeps the app-server subcommand when explicit args are provided", () => {
NodeAssert.deepStrictEqual(codexSessionAppServerArgs(["-c", "model=gpt-5"], undefined), [
"app-server",
"-c",
"model=gpt-5",
]);
});
});
describe("isRecoverableThreadResumeError", () => {
it("matches missing thread errors", () => {
NodeAssert.equal(
isRecoverableThreadResumeError(
new CodexErrors.CodexAppServerRequestError({
code: -32603,
errorMessage: "Thread does not exist",
}),
),
true,
);
});
});
describe("isRecoverableThreadResumeError", () => {
it("matches a missing rollout for a known thread id", () => {
NodeAssert.equal(
isRecoverableThreadResumeError(
new CodexErrors.CodexAppServerRequestError({
code: -32603,
errorMessage: "no rollout found for thread id 019fdf74-aaa9-7950-b252-7cc7a8650470",
}),
),
true,
);
});
});
describe("isRecoverableThreadResumeError", () => {
it("ignores unrelated missing-resource errors that do not mention threads", () => {
NodeAssert.equal(
isRecoverableThreadResumeError(
new CodexErrors.CodexAppServerRequestError({
code: -32603,
errorMessage: "Config file not found",
}),
),
false,
);
NodeAssert.equal(
isRecoverableThreadResumeError(
new CodexErrors.CodexAppServerRequestError({
code: -32603,
errorMessage: "Model does not exist",
}),
),
false,
);
});
});