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 { describe, expect, it } from "@effect/vitest";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as Latch from "effect/Latch";
import * as Layer from "effect/Layer";
import { Atom, AtomRegistry } from "effect/unstable/reactivity";
import {
createAtomCommandScheduler,
createRuntimeCommand,
scheduleAtomCommandEffect,
} from "./runtime.ts";
describe("runtime command runner", () => {
it("settles generated command scheduler defects from direct callers", async () => {
const defect = new Error("invalid command key");
const runtime = Atom.runtime(Layer.empty);
const command = createRuntimeCommand(runtime, {
label: "test.invalid-key",
concurrency: {
mode: "serial",
key: () => {
throw defect;
},
},
execute: () => Effect.void,
});
const registry = AtomRegistry.make();
const result = await command.run(registry, undefined);
expect(result._tag).toBe("Failure");
if (Predicate.isTagged(result, "Failure")) {
expect(Cause.hasDies(result.cause)).toBe(true);
expect(Cause.squash(result.cause)).toBe(defect);
}
registry.dispose();
});
it("correlates parallel invocation results", async () => {
const firstLatch = Latch.makeUnsafe();
const secondLatch = Latch.makeUnsafe();
const runtime = Atom.runtime(Layer.empty);
const command = createRuntimeCommand(runtime, {
label: "test.parallel",
execute: (id: "first" | "second") =>
(id === "first" ? firstLatch : secondLatch).await.pipe(Effect.as(id)),
});
const registry = AtomRegistry.make();
const first = command.run(registry, "first");
const second = command.run(registry, "second");
secondLatch.openUnsafe();
firstLatch.openUnsafe();
expect(await first).toMatchObject({ _tag: "Success", value: "first", waiting: false });
expect(await second).toMatchObject({ _tag: "Success", value: "second", waiting: false });
registry.dispose();
});
it("serializes commands that share a scheduler and lane", async () => {
const firstLatch = Latch.makeUnsafe();
const events: string[] = [];
const runtime = Atom.runtime(Layer.empty);
const scheduler = createAtomCommandScheduler();
const concurrency = { mode: "serial" as const, key: () => "shared" };
const firstCommand = createRuntimeCommand(runtime, {
label: "test.first",
scheduler,
concurrency,
execute: () =>
Effect.sync(() => events.push("first:start")).pipe(
Effect.andThen(firstLatch.await),
Effect.tap(() => Effect.sync(() => events.push("first:end"))),
),
});
const secondCommand = createRuntimeCommand(runtime, {
label: "test.second",
scheduler,
concurrency,
execute: () => Effect.sync(() => events.push("second:start")),
});
const registry = AtomRegistry.make();
const first = firstCommand.run(registry, undefined);
const second = secondCommand.run(registry, undefined);
await Promise.resolve();
expect(events).toEqual(["first:start"]);
firstLatch.openUnsafe();
await Promise.all([first, second]);
expect(events).toEqual(["first:start", "first:end", "second:start"]);
registry.dispose();
});
it.effect("releases a shared scheduler lane before the outer command finishes", () =>
Effect.gen(function* () {
const handoffStarted = Latch.makeUnsafe();
const handoffComplete = Latch.makeUnsafe();
const resumeComplete = Latch.makeUnsafe();
const runtime = Atom.runtime(Layer.empty);
const scheduler = createAtomCommandScheduler();
const concurrency = { mode: "serial" as const, key: () => "shared" };
const updateCommand = createRuntimeCommand(runtime, {
label: "test.update",
execute: (_input: void, registry) =>
scheduleAtomCommandEffect(
registry,
scheduler,
concurrency,
undefined,
Effect.sync(() => handoffStarted.openUnsafe()).pipe(
Effect.andThen(handoffComplete.await),
),
).pipe(Effect.andThen(resumeComplete.await)),
});
const configCommand = createRuntimeCommand(runtime, {
label: "test.config",
scheduler,
concurrency,
execute: () => Effect.succeed("configured"),
});
const registry = AtomRegistry.make();
const update = updateCommand.run(registry, undefined);
yield* handoffStarted.await;
const config = configCommand.run(registry, undefined);
handoffComplete.openUnsafe();
expect(yield* Effect.promise(() => config)).toMatchObject({
_tag: "Success",
value: "configured",
waiting: false,
});
resumeComplete.openUnsafe();
expect(yield* Effect.promise(() => update)).toMatchObject({
_tag: "Success",
waiting: false,
});
registry.dispose();
}),
);
it("deduplicates single-flight commands by key", async () => {
const latch = Latch.makeUnsafe();
let executions = 0;
const runtime = Atom.runtime(Layer.empty);
const command = createRuntimeCommand(runtime, {
label: "test.single-flight",
concurrency: { mode: "singleFlight", key: (key: string) => key },
execute: () =>
Effect.sync(() => executions++).pipe(Effect.andThen(latch.await), Effect.as("done")),
});
const registry = AtomRegistry.make();
const first = command.run(registry, "same");
const second = command.run(registry, "same");
latch.openUnsafe();
expect(await first).toMatchObject({ _tag: "Success", value: "done", waiting: false });
expect(await second).toMatchObject({ _tag: "Success", value: "done", waiting: false });
expect(executions).toBe(1);
registry.dispose();
});
it("coalesces pending latest-value commands without interrupting the active call", async () => {
const firstLatch = Latch.makeUnsafe();
const executed: number[] = [];
const runtime = Atom.runtime(Layer.empty);
const command = createRuntimeCommand(runtime, {
label: "test.latest",
concurrency: { mode: "latest", key: () => "shared" },
execute: (value: number) =>
Effect.sync(() => executed.push(value)).pipe(
Effect.andThen(value === 1 ? firstLatch.await : Effect.void),
Effect.as(value),
),
});
const registry = AtomRegistry.make();
const first = command.run(registry, 1);
await Promise.resolve();
const second = command.run(registry, 2);
const third = command.run(registry, 3);
firstLatch.openUnsafe();
expect(await first).toMatchObject({ _tag: "Success", value: 1, waiting: false });
expect(await second).toMatchObject({ _tag: "Success", value: 3, waiting: false });
expect(await third).toMatchObject({ _tag: "Success", value: 3, waiting: false });
expect(executed).toEqual([1, 3]);
registry.dispose();
});
});