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 Predicate from "effect/Predicate";
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { HostProcessPlatform } from "@akeru/shared/hostProcess";
import {
CommandId,
ExternalLauncherCommandNotFoundError,
ORCHESTRATION_WS_METHODS,
ProjectId,
ProviderInstanceId,
RoutineId,
RoutineRunId,
WS_METHODS,
EditorId,
} from "@akeru/contracts";
import { assert, it } from "@effect/vitest";
import { assertFailure, assertTrue } from "@effect/vitest/utils";
import * as Effect from "effect/Effect";
import * as Cause from "effect/Cause";
import * as Exit from "effect/Exit";
import * as PlatformError from "effect/PlatformError";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as TestClock from "effect/testing/TestClock";
import { buildAppUnderTest } from "./serverTestApp.ts";
import { getWsServerUrl, withWsRpcClient } from "./serverTestClients.ts";
import { TEST_EPOCH } from "./serverTestFixtures.ts";
it.layer(NodeServices.layer)("server router seam", (it) => {
it.effect("routes websocket rpc projects.searchEntries", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const workspaceDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ws-project-search-" });
yield* fs.writeFileString(
path.join(workspaceDir, "needle-file.ts"),
"export const needle = 1;",
);
yield* buildAppUnderTest();
const wsUrl = yield* getWsServerUrl("/ws");
const response = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.projectsSearchEntries]({
cwd: workspaceDir,
query: "needle",
limit: 10,
}),
),
);
assert.isAtLeast(response.entries.length, 1);
assert.isTrue(response.entries.some((entry) => entry.path === "needle-file.ts"));
assert.equal(response.truncated, false);
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
);
it.effect("maps workspace close failures to ProjectReadFileError over RPC", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "akeru-ws-close-" });
const target = path.join(cwd, "read.txt");
yield* fs.writeFileString(target, "content");
const closeError = PlatformError.systemError({
_tag: "Unknown",
module: "FileSystem",
method: "open",
cause: new Error("close failed"),
});
yield* buildAppUnderTest().pipe(
Effect.provideService(FileSystem.FileSystem, {
...fs,
open: (filePath, options) =>
Effect.gen(function* () {
const handle = yield* fs.open(filePath, options);
if (filePath === target) {
yield* Effect.addFinalizer(() => Effect.die(closeError));
}
return handle;
}),
}),
);
const wsUrl = yield* getWsServerUrl("/ws");
const exit = yield* Effect.exit(
Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.projectsReadFile]({
cwd,
relativePath: "read.txt",
}),
),
),
);
assert.isTrue(Exit.isFailure(exit));
if (Exit.isFailure(exit)) {
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
assert.isTrue(Predicate.isTagged(error, "ProjectReadFileError"));
if (Predicate.isTagged(error, "ProjectReadFileError")) {
assert.equal(error.cwd, cwd);
assert.equal(error.relativePath, "read.txt");
assert.equal(error.operation, "close");
assert.equal(error.operationPath, target);
assert.equal(error.resolvedPath, target);
}
}
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
);
it.effect("routes websocket rpc projects.listEntries and projects.readFile", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const workspaceDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ws-project-files-" });
yield* fs.makeDirectory(path.join(workspaceDir, "src"), { recursive: true });
yield* fs.writeFileString(
path.join(workspaceDir, "src", "index.ts"),
"export const answer = 42;\n",
);
yield* buildAppUnderTest();
const wsUrl = yield* getWsServerUrl("/ws");
const response = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
Effect.all({
listing: client[WS_METHODS.projectsListEntries]({ cwd: workspaceDir }),
file: client[WS_METHODS.projectsReadFile]({
cwd: workspaceDir,
relativePath: "src/index.ts",
}),
}),
),
);
assert.isTrue(response.listing.entries.some((entry) => entry.path === "src/index.ts"));
assert.deepEqual(response.file, {
relativePath: "src/index.ts",
contents: "export const answer = 42;\n",
byteLength: 26,
truncated: false,
});
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
);
it.effect("routes websocket rpc projects.searchEntries excludes gitignored files", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const workspaceDir = yield* fs.makeTempDirectoryScoped({
prefix: "t3-ws-project-search-gitignored-",
});
yield* fs.writeFileString(path.join(workspaceDir, ".gitignore"), ".venv/\n");
yield* fs.makeDirectory(path.join(workspaceDir, ".venv", "lib"), { recursive: true });
yield* fs.writeFileString(
path.join(workspaceDir, ".venv", "lib", "ignored-search-target.ts"),
"export const ignored = true;",
);
yield* fs.makeDirectory(path.join(workspaceDir, "src"), { recursive: true });
yield* fs.writeFileString(
path.join(workspaceDir, "src", "tracked.ts"),
"export const ok = 1;",
);
yield* buildAppUnderTest({
layers: {
vcsDriver: {
isInsideWorkTree: () => Effect.succeed(true),
listWorkspaceFiles: () =>
Effect.succeed({
paths: ["src/tracked.ts"],
truncated: false,
freshness: {
source: "live-local",
observedAt: TEST_EPOCH,
expiresAt: Option.none(),
},
}),
filterIgnoredPaths: (_cwd, relativePaths) =>
Effect.succeed(
relativePaths.filter((relativePath) => !relativePath.startsWith(".venv/")),
),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const response = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.projectsSearchEntries]({
cwd: workspaceDir,
query: "ignored-search-target",
limit: 10,
}),
),
);
assert.equal(response.entries.length, 0);
assert.equal(response.truncated, false);
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
);
it.effect("preserves structured workspace rpc failures", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const workspaceDir = yield* fs.makeTempDirectoryScoped({
prefix: "t3-ws-workspace-errors-",
});
const outsideDir = yield* fs.makeTempDirectoryScoped({
prefix: "t3-ws-workspace-errors-outside-",
});
const outsideFile = path.join(outsideDir, "outside.txt");
yield* fs.writeFileString(outsideFile, "outside\n");
yield* fs.symlink(outsideFile, path.join(workspaceDir, "linked-outside.txt"));
const resolvedOutsideFile = yield* fs.realPath(outsideFile);
yield* buildAppUnderTest();
const invalidWorkspace = path.join(workspaceDir, "missing-workspace");
const sensitiveQuery = "authorization: Bearer secret-token";
const wsUrl = yield* getWsServerUrl("/ws");
const results = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
Effect.all({
search: client[WS_METHODS.projectsSearchEntries]({
cwd: invalidWorkspace,
query: sensitiveQuery,
limit: 10,
}).pipe(Effect.result),
list: client[WS_METHODS.projectsListEntries]({ cwd: invalidWorkspace }).pipe(
Effect.result,
),
read: client[WS_METHODS.projectsReadFile]({
cwd: workspaceDir,
relativePath: "linked-outside.txt",
}).pipe(Effect.result),
}),
),
);
if (
!Predicate.isTagged(results.search, "Failure") ||
!Predicate.isTagged(results.search.failure, "ProjectSearchEntriesError")
) {
assert.fail("Expected a ProjectSearchEntriesError");
}
const searchError = results.search.failure;
assert.equal(
searchError.message,
`Failed to search workspace entries in '${invalidWorkspace}'.`,
);
assert.equal(searchError.cwd, invalidWorkspace);
assert.equal(searchError.queryLength, sensitiveQuery.length);
assert.notProperty(searchError, "query");
assert.notInclude(searchError.message, "Bearer");
assert.notInclude(searchError.message, "secret-token");
assert.equal(searchError.limit, 10);
assert.equal(searchError.failure, "workspace_root_not_found");
assert.equal(searchError.normalizedCwd, invalidWorkspace);
assert.isDefined(searchError.cause);
if (
!Predicate.isTagged(results.list, "Failure") ||
!Predicate.isTagged(results.list.failure, "ProjectListEntriesError")
) {
assert.fail("Expected a ProjectListEntriesError");
}
const listError = results.list.failure;
assert.equal(listError.message, `Failed to list workspace entries in '${invalidWorkspace}'.`);
assert.equal(listError.cwd, invalidWorkspace);
assert.equal(listError.failure, "workspace_root_not_found");
assert.equal(listError.normalizedCwd, invalidWorkspace);
assert.isDefined(listError.cause);
if (
!Predicate.isTagged(results.read, "Failure") ||
!Predicate.isTagged(results.read.failure, "ProjectReadFileError")
) {
assert.fail("Expected a ProjectReadFileError");
}
const readError = results.read.failure;
assert.equal(
readError.message,
`Failed to read workspace file 'linked-outside.txt' in '${workspaceDir}'.`,
);
assert.equal(readError.cwd, workspaceDir);
assert.equal(readError.relativePath, "linked-outside.txt");
assert.equal(readError.failure, "resolved_path_outside_root");
assert.equal(readError.resolvedPath, resolvedOutsideFile);
assert.isDefined(readError.cause);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("reports workspace root stat failures without relabeling them as missing", () =>
Effect.gen(function* () {
if ((yield* HostProcessPlatform) === "win32") return;
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const blockedRoot = yield* fs.makeTempDirectoryScoped({
prefix: "t3-ws-workspace-stat-error-",
});
const workspaceRoot = path.join(blockedRoot, "workspace");
yield* fs.makeDirectory(workspaceRoot);
yield* fs.chmod(blockedRoot, 0o000);
const result = yield* Effect.gen(function* () {
yield* buildAppUnderTest();
const wsUrl = yield* getWsServerUrl("/ws");
return yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.projectsListEntries]({ cwd: workspaceRoot }).pipe(Effect.result),
),
);
}).pipe(Effect.ensuring(fs.chmod(blockedRoot, 0o700).pipe(Effect.ignore)));
if (
!Predicate.isTagged(result, "Failure") ||
!Predicate.isTagged(result.failure, "ProjectListEntriesError")
) {
assert.fail("Expected a ProjectListEntriesError");
}
const error = result.failure;
assert.equal(error.failure, "workspace_root_stat_failed");
assert.equal(error.normalizedCwd, workspaceRoot);
assert.equal(error.detail, "validate-existing");
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket rpc projects.writeFile", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const workspaceDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ws-project-write-" });
yield* buildAppUnderTest();
const wsUrl = yield* getWsServerUrl("/ws");
const response = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.projectsWriteFile]({
cwd: workspaceDir,
relativePath: "nested/created.txt",
contents: "written-by-rpc",
}),
),
);
assert.equal(response.relativePath, "nested/created.txt");
const persisted = yield* fs.readFileString(path.join(workspaceDir, "nested", "created.txt"));
assert.equal(persisted, "written-by-rpc");
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("creates a missing workspace root during websocket project.create dispatch", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ws-project-create-" });
const missingWorkspaceRoot = path.join(parentDir, "nested", "new-project");
yield* buildAppUnderTest();
const wsUrl = yield* getWsServerUrl("/ws");
const response = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[ORCHESTRATION_WS_METHODS.dispatchCommand]({
type: "project.create",
commandId: CommandId.make("cmd-project-create-missing-root"),
projectId: ProjectId.make("project-create-missing-root"),
title: "New Project",
workspaceRoot: missingWorkspaceRoot,
createWorkspaceRootIfMissing: true,
defaultModelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
},
createdAt: "2026-01-01T00:00:00.000Z",
}),
),
);
const stat = yield* fs.stat(missingWorkspaceRoot);
assert.isAtLeast(response.sequence, 0);
assert.equal(stat.type, "Directory");
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("reports a manual routine run that does not start", () =>
Effect.gen(function* () {
let dispatchCount = 0;
yield* buildAppUnderTest({
layers: {
orchestrationEngine: {
dispatch: () =>
Effect.sync(() => {
dispatchCount += 1;
return { sequence: 7 };
}),
},
routineRuntime: {
canRunNow: () => Effect.succeed(false),
runNow: () => Effect.die("busy routine must not run"),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const result = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[ORCHESTRATION_WS_METHODS.dispatchCommand]({
type: "routine.run",
commandId: CommandId.make("cmd-routine-run-collision"),
routineId: RoutineId.make("routine-1"),
runId: RoutineRunId.make("run-1"),
trigger: "manual",
createdAt: "2026-01-01T00:00:00.000Z",
}).pipe(Effect.result),
),
);
assert.equal(result._tag, "Failure");
if (Predicate.isTagged(result, "Failure")) {
assert.include(result.failure.message, "did not start");
}
assert.equal(dispatchCount, 0);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket rpc projects.writeFile errors", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const workspaceDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ws-project-write-" });
yield* buildAppUnderTest();
const wsUrl = yield* getWsServerUrl("/ws");
const result = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.projectsWriteFile]({
cwd: workspaceDir,
relativePath: "../escape.txt",
contents: "nope",
}),
).pipe(Effect.result),
);
if (
!Predicate.isTagged(result, "Failure") ||
!Predicate.isTagged(result.failure, "ProjectWriteFileError")
) {
assert.fail("Expected a ProjectWriteFileError");
}
const writeError = result.failure;
assert.equal(
writeError.message,
`Failed to write workspace file '../escape.txt' in '${workspaceDir}'.`,
);
assert.equal(writeError.cwd, workspaceDir);
assert.equal(writeError.relativePath, "../escape.txt");
assert.equal(writeError.failure, "workspace_path_outside_root");
assert.isDefined(writeError.cause);
assert.notProperty(writeError, "contents");
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket rpc shell.openInEditor", () =>
Effect.gen(function* () {
let openedInput: { cwd: string; editor: EditorId } | null = null;
yield* buildAppUnderTest({
layers: {
externalLauncher: {
launchEditor: (input) =>
Effect.sync(() => {
openedInput = input;
}),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.shellOpenInEditor]({
cwd: "/tmp/project",
editor: "cursor",
}),
),
);
assert.deepEqual(openedInput, { cwd: "/tmp/project", editor: "cursor" });
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket rpc shell.openInEditor errors", () =>
Effect.gen(function* () {
const externalLauncherError = new ExternalLauncherCommandNotFoundError({
editor: "cursor",
command: "cursor",
});
yield* buildAppUnderTest({
layers: {
externalLauncher: {
launchEditor: () => Effect.fail(externalLauncherError),
},
},
});
const wsUrl = yield* getWsServerUrl("/ws");
const result = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
client[WS_METHODS.shellOpenInEditor]({
cwd: "/tmp/project",
editor: "cursor",
}),
).pipe(Effect.result),
);
assertFailure(result, externalLauncherError);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("routes websocket rpc shell.revealAttachment to the stored file", () =>
Effect.gen(function* () {
let revealed:
| Parameters<
import("./process/externalLauncher.ts").ExternalLauncher["Service"]["launchEditor"]
>[0]
| null = null;
const config = yield* buildAppUnderTest({
layers: {
externalLauncher: {
launchEditor: (input) =>
Effect.sync(() => {
revealed = input;
}),
},
},
});
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const attachmentId = "thread-reveal-00000000-0000-4000-8000-000000000001";
const attachmentPath = path.join(config.attachmentsDir, `${attachmentId}.png`);
yield* fileSystem.makeDirectory(config.attachmentsDir, { recursive: true });
yield* fileSystem.writeFile(attachmentPath, new Uint8Array([1, 2, 3]));
const wsUrl = yield* getWsServerUrl("/ws");
const missing = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) =>
Effect.gen(function* () {
yield* client[WS_METHODS.shellRevealAttachment]({ attachmentId });
return yield* client[WS_METHODS.shellRevealAttachment]({
attachmentId: "thread-reveal-00000000-0000-4000-8000-000000000002",
}).pipe(Effect.result);
}),
),
);
assert.deepEqual(revealed, { cwd: attachmentPath, editor: "file-manager", reveal: true });
assertTrue(Predicate.isTagged(missing, "Failure"));
assertTrue(Predicate.isTagged(missing.failure, "AttachmentNotFoundError"));
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
});