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 { CommandId, CorrelationId, EventId, MessageId, ThreadId, TurnId } from "@akeru/contracts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import { OrchestrationEventStoreLive } from "../../persistence/Layers/OrchestrationEventStore.ts";
import { sqlitePersistenceLayer } from "../../persistence/Layers/Sqlite.ts";
import { OrchestrationEventStore } from "../../persistence/Services/OrchestrationEventStore.ts";
import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts";
import { ServerConfig } from "../../config.ts";
import { makeProjectionPipelinePrefixedTestLayer } from "./test-support/ProjectionPipelineHarness.ts";
it.layer(makeProjectionPipelinePrefixedTestLayer("t3-pending-turn-terminal-test-"))(
"OrchestrationProjectionPipeline pending turn cleanup",
(it) => {
it.effect("clears pending turn starts when startup reaches a terminal session state", () =>
Effect.gen(function* () {
const projectionPipeline = yield* OrchestrationProjectionPipeline;
const eventStore = yield* OrchestrationEventStore;
const sql = yield* SqlClient.SqlClient;
for (const [index, status] of (["error", "interrupted", "stopped"] as const).entries()) {
const threadId = ThreadId.make(`thread-terminal-${status}`);
const requestedAt = `2026-02-26T14:00:0${index}.000Z`;
yield* eventStore.append({
type: "thread.turn-start-requested",
eventId: EventId.make(`evt-terminal-pending-${status}`),
aggregateKind: "thread",
aggregateId: threadId,
occurredAt: requestedAt,
commandId: CommandId.make(`cmd-terminal-pending-${status}`),
causationEventId: null,
correlationId: CorrelationId.make(`cmd-terminal-pending-${status}`),
metadata: {},
payload: {
threadId,
messageId: MessageId.make(`message-terminal-${status}`),
runtimeMode: "approval-required",
createdAt: requestedAt,
},
});
yield* eventStore.append({
type: "thread.session-set",
eventId: EventId.make(`evt-terminal-session-${status}`),
aggregateKind: "thread",
aggregateId: threadId,
occurredAt: requestedAt,
commandId: CommandId.make(`cmd-terminal-session-${status}`),
causationEventId: null,
correlationId: CorrelationId.make(`cmd-terminal-session-${status}`),
metadata: {},
payload: {
threadId,
session: {
threadId,
status,
providerName: "codex",
runtimeMode: "approval-required",
activeTurnId: null,
lastError: status === "error" ? "startup failed" : null,
updatedAt: requestedAt,
},
},
});
}
yield* projectionPipeline.bootstrap;
const pendingRows = yield* sql<{ readonly threadId: string }>`
SELECT thread_id AS "threadId"
FROM projection_turns
WHERE turn_id IS NULL
AND state = 'pending'
`;
assert.deepEqual(pendingRows, []);
}),
);
},
);
it.effect("restores pending turn-start metadata across projection pipeline restart", () =>
Effect.gen(function* () {
const { dbPath } = yield* ServerConfig;
const persistenceLayer = sqlitePersistenceLayer(dbPath);
const firstProjectionLayer = OrchestrationProjectionPipelineLive.pipe(
Layer.provideMerge(OrchestrationEventStoreLive),
Layer.provideMerge(persistenceLayer),
);
const secondProjectionLayer = OrchestrationProjectionPipelineLive.pipe(
Layer.provideMerge(OrchestrationEventStoreLive),
Layer.provideMerge(persistenceLayer),
);
const threadId = ThreadId.make("thread-restart");
const turnId = TurnId.make("turn-restart");
const messageId = MessageId.make("message-restart");
const sourcePlanThreadId = ThreadId.make("thread-plan-source");
const sourcePlanId = "plan-source";
const turnStartedAt = "2026-02-26T14:00:00.000Z";
const sessionSetAt = "2026-02-26T14:00:05.000Z";
yield* Effect.gen(function* () {
const eventStore = yield* OrchestrationEventStore;
const projectionPipeline = yield* OrchestrationProjectionPipeline;
yield* eventStore.append({
type: "thread.turn-start-requested",
eventId: EventId.make("evt-restart-1"),
aggregateKind: "thread",
aggregateId: threadId,
occurredAt: turnStartedAt,
commandId: CommandId.make("cmd-restart-1"),
causationEventId: null,
correlationId: CorrelationId.make("cmd-restart-1"),
metadata: {},
payload: {
threadId,
messageId,
sourceProposedPlan: {
threadId: sourcePlanThreadId,
planId: sourcePlanId,
},
runtimeMode: "approval-required",
createdAt: turnStartedAt,
},
});
yield* projectionPipeline.bootstrap;
}).pipe(Effect.provide(firstProjectionLayer));
const turnRows = yield* Effect.gen(function* () {
const eventStore = yield* OrchestrationEventStore;
const projectionPipeline = yield* OrchestrationProjectionPipeline;
const sql = yield* SqlClient.SqlClient;
yield* eventStore.append({
type: "thread.session-set",
eventId: EventId.make("evt-restart-2"),
aggregateKind: "thread",
aggregateId: threadId,
occurredAt: sessionSetAt,
commandId: CommandId.make("cmd-restart-2"),
causationEventId: null,
correlationId: CorrelationId.make("cmd-restart-2"),
metadata: {},
payload: {
threadId,
session: {
threadId,
status: "running",
providerName: "codex",
runtimeMode: "approval-required",
activeTurnId: turnId,
lastError: null,
updatedAt: sessionSetAt,
},
},
});
yield* projectionPipeline.bootstrap;
const pendingRows = yield* sql<{ readonly threadId: string }>`
SELECT thread_id AS "threadId"
FROM projection_turns
WHERE thread_id = ${threadId}
AND turn_id IS NULL
AND state = 'pending'
`;
assert.deepEqual(pendingRows, []);
return yield* sql<{
readonly turnId: string;
readonly userMessageId: string | null;
readonly sourceProposedPlanThreadId: string | null;
readonly sourceProposedPlanId: string | null;
readonly startedAt: string;
}>`
SELECT
turn_id AS "turnId",
pending_message_id AS "userMessageId",
source_proposed_plan_thread_id AS "sourceProposedPlanThreadId",
source_proposed_plan_id AS "sourceProposedPlanId",
started_at AS "startedAt"
FROM projection_turns
WHERE turn_id = ${turnId}
`;
}).pipe(Effect.provide(secondProjectionLayer));
assert.deepEqual(turnRows, [
{
turnId: "turn-restart",
userMessageId: "message-restart",
sourceProposedPlanThreadId: "thread-plan-source",
sourceProposedPlanId: "plan-source",
startedAt: turnStartedAt,
},
]);
}).pipe(
Effect.provide(
Layer.provideMerge(
ServerConfig.layerTest(process.cwd(), {
prefix: "t3-projection-pipeline-restart-",
}),
NodeServices.layer,
),
),
),
);