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 { projectionSnapshotLayer } from "./test-support/ProjectionSnapshotHarness.ts";
import { MessageId, ThreadId } from "@akeru/contracts";
import { assert } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
import {
asProjectId,
asTurnId,
asCheckpointRef,
asEventId,
asMessageId,
} from "./test-support/ProjectionSnapshotHarness.ts";
projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
it.effect("reads the channel origin of a turn start message", () =>
Effect.gen(function* () {
const snapshotQuery = yield* ProjectionSnapshotQuery;
const sql = yield* SqlClient.SqlClient;
yield* sql`DELETE FROM projection_thread_messages`;
yield* sql`
INSERT INTO projection_thread_messages (
message_id,
thread_id,
turn_id,
role,
text,
channel_origin_json,
is_streaming,
created_at,
updated_at
)
VALUES
(
'message-channel',
'thread-channel',
NULL,
'user',
'From Telegram',
'{"provider":"telegram","externalThreadId":"chat-1"}',
0,
'2026-04-06T00:00:01.000Z',
'2026-04-06T00:00:01.000Z'
),
(
'message-local',
'thread-channel',
NULL,
'user',
'From the app',
NULL,
0,
'2026-04-06T00:00:02.000Z',
'2026-04-06T00:00:02.000Z'
)
`;
const read = (messageId: string) =>
snapshotQuery
.getTurnStartMessage({
threadId: ThreadId.make("thread-channel"),
messageId: MessageId.make(messageId),
})
.pipe(Effect.map(Option.getOrThrow));
assert.deepEqual((yield* read("message-channel")).message.channelOrigin, {
provider: "telegram",
externalThreadId: "chat-1",
});
assert.equal((yield* read("message-local")).message.channelOrigin, undefined);
}),
);
it.effect("reads single-thread checkpoint context without hydrating unrelated threads", () =>
Effect.gen(function* () {
const snapshotQuery = yield* ProjectionSnapshotQuery;
const sql = yield* SqlClient.SqlClient;
yield* sql`DELETE FROM projection_projects`;
yield* sql`DELETE FROM projection_threads`;
yield* sql`DELETE FROM projection_turns`;
yield* sql`
INSERT INTO projection_projects (
project_id,
title,
workspace_root,
default_model_selection_json,
scripts_json,
created_at,
updated_at,
deleted_at
)
VALUES (
'project-context',
'Context Project',
'/tmp/context-workspace',
NULL,
'[]',
'2026-03-02T00:00:00.000Z',
'2026-03-02T00:00:01.000Z',
NULL
)
`;
yield* sql`
INSERT INTO projection_threads (
thread_id,
project_id,
title,
model_selection_json,
runtime_mode,
interaction_mode,
branch,
worktree_path,
latest_turn_id,
created_at,
updated_at,
archived_at,
deleted_at
)
VALUES (
'thread-context',
'project-context',
'Context Thread',
'{"provider":"codex","model":"gpt-5-codex"}',
'full-access',
'default',
'feature/perf',
'/tmp/context-worktree',
NULL,
'2026-03-02T00:00:02.000Z',
'2026-03-02T00:00:03.000Z',
NULL,
NULL
)
`;
yield* sql`
INSERT INTO projection_turns (
thread_id,
turn_id,
pending_message_id,
source_proposed_plan_thread_id,
source_proposed_plan_id,
assistant_message_id,
state,
requested_at,
started_at,
completed_at,
checkpoint_turn_count,
checkpoint_ref,
checkpoint_status,
checkpoint_files_json
)
VALUES
(
'thread-context',
'turn-1',
NULL,
NULL,
NULL,
NULL,
'completed',
'2026-03-02T00:00:04.000Z',
'2026-03-02T00:00:04.000Z',
'2026-03-02T00:00:04.000Z',
1,
'checkpoint-a',
'ready',
'[]'
),
(
'thread-context',
'turn-2',
NULL,
NULL,
NULL,
NULL,
'completed',
'2026-03-02T00:00:05.000Z',
'2026-03-02T00:00:05.000Z',
'2026-03-02T00:00:05.000Z',
2,
'checkpoint-b',
'ready',
'[]'
)
`;
const context = yield* snapshotQuery.getThreadCheckpointContext(
ThreadId.make("thread-context"),
);
assert.equal(context._tag, "Some");
if (Predicate.isTagged(context, "Some")) {
assert.deepEqual(context.value, {
threadId: ThreadId.make("thread-context"),
projectId: asProjectId("project-context"),
workspaceRoot: "/tmp/context-workspace",
worktreePath: "/tmp/context-worktree",
checkpoints: [
{
turnId: asTurnId("turn-1"),
checkpointTurnCount: 1,
checkpointRef: asCheckpointRef("checkpoint-a"),
status: "ready",
files: [],
assistantMessageId: null,
completedAt: "2026-03-02T00:00:04.000Z",
},
{
turnId: asTurnId("turn-2"),
checkpointTurnCount: 2,
checkpointRef: asCheckpointRef("checkpoint-b"),
status: "ready",
files: [],
assistantMessageId: null,
completedAt: "2026-03-02T00:00:05.000Z",
},
],
});
}
}),
);
it.effect("keeps thread detail activity ordering consistent with shell snapshot ordering", () =>
Effect.gen(function* () {
const snapshotQuery = yield* ProjectionSnapshotQuery;
const sql = yield* SqlClient.SqlClient;
yield* sql`DELETE FROM projection_projects`;
yield* sql`DELETE FROM projection_threads`;
yield* sql`DELETE FROM projection_thread_activities`;
yield* sql`DELETE FROM projection_state`;
yield* sql`
INSERT INTO projection_projects (
project_id,
title,
workspace_root,
default_model_selection_json,
scripts_json,
created_at,
updated_at,
deleted_at
)
VALUES (
'project-1',
'Project 1',
'/tmp/project-1',
'{"provider":"codex","model":"gpt-5-codex"}',
'[]',
'2026-04-01T00:00:00.000Z',
'2026-04-01T00:00:01.000Z',
NULL
)
`;
yield* sql`
INSERT INTO projection_threads (
thread_id,
project_id,
title,
model_selection_json,
runtime_mode,
interaction_mode,
branch,
worktree_path,
latest_turn_id,
latest_user_message_at,
pending_approval_count,
pending_user_input_count,
has_actionable_proposed_plan,
created_at,
updated_at,
deleted_at
)
VALUES (
'thread-1',
'project-1',
'Thread 1',
'{"provider":"codex","model":"gpt-5-codex"}',
'full-access',
'default',
NULL,
NULL,
NULL,
NULL,
0,
0,
0,
'2026-04-01T00:00:02.000Z',
'2026-04-01T00:00:03.000Z',
NULL
)
`;
yield* sql`
INSERT INTO projection_thread_activities (
activity_id,
thread_id,
turn_id,
tone,
kind,
summary,
payload_json,
sequence,
created_at
)
VALUES
(
'activity-unsequenced',
'thread-1',
NULL,
'info',
'runtime.note',
'unsequenced first',
'{"source":"unsequenced"}',
NULL,
'2026-04-01T00:00:06.000Z'
),
(
'activity-sequence-2',
'thread-1',
NULL,
'info',
'runtime.note',
'sequence two',
'{"source":"sequence-2"}',
2,
'2026-04-01T00:00:04.000Z'
),
(
'activity-sequence-1',
'thread-1',
NULL,
'info',
'runtime.note',
'sequence one',
'{"source":"sequence-1"}',
1,
'2026-04-01T00:00:05.000Z'
)
`;
const snapshot = yield* snapshotQuery.getSnapshot();
const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
assert.equal(threadDetail._tag, "Some");
if (Predicate.isTagged(threadDetail, "Some")) {
assert.deepEqual(threadDetail.value.activities, snapshot.threads[0]?.activities ?? []);
}
assert.deepEqual(snapshot.threads[0]?.activities ?? [], [
{
id: asEventId("activity-unsequenced"),
tone: "info",
kind: "runtime.note",
summary: "unsequenced first",
payload: { source: "unsequenced" },
turnId: null,
createdAt: "2026-04-01T00:00:06.000Z",
},
{
id: asEventId("activity-sequence-1"),
tone: "info",
kind: "runtime.note",
summary: "sequence one",
payload: { source: "sequence-1" },
turnId: null,
sequence: 1,
createdAt: "2026-04-01T00:00:05.000Z",
},
{
id: asEventId("activity-sequence-2"),
tone: "info",
kind: "runtime.note",
summary: "sequence two",
payload: { source: "sequence-2" },
turnId: null,
sequence: 2,
createdAt: "2026-04-01T00:00:04.000Z",
},
]);
}),
);
it.effect("uses projection_threads.latest_turn_id for targeted thread latest turn queries", () =>
Effect.gen(function* () {
const snapshotQuery = yield* ProjectionSnapshotQuery;
const sql = yield* SqlClient.SqlClient;
yield* sql`DELETE FROM projection_projects`;
yield* sql`DELETE FROM projection_threads`;
yield* sql`DELETE FROM projection_turns`;
yield* sql`
INSERT INTO projection_projects (
project_id,
title,
workspace_root,
default_model_selection_json,
scripts_json,
created_at,
updated_at,
deleted_at
)
VALUES (
'project-1',
'Project 1',
'/tmp/project-1',
'{"provider":"codex","model":"gpt-5-codex"}',
'[]',
'2026-04-02T00:00:00.000Z',
'2026-04-02T00:00:01.000Z',
NULL
)
`;
yield* sql`
INSERT INTO projection_threads (
thread_id,
project_id,
title,
model_selection_json,
runtime_mode,
interaction_mode,
branch,
worktree_path,
latest_turn_id,
latest_user_message_at,
pending_approval_count,
pending_user_input_count,
has_actionable_proposed_plan,
created_at,
updated_at,
archived_at,
deleted_at
)
VALUES (
'thread-1',
'project-1',
'Thread 1',
'{"provider":"codex","model":"gpt-5-codex"}',
'full-access',
'default',
NULL,
NULL,
'turn-running',
'2026-04-02T00:00:04.000Z',
0,
0,
0,
'2026-04-02T00:00:02.000Z',
'2026-04-02T00:00:03.000Z',
NULL,
NULL
)
`;
yield* sql`
INSERT INTO projection_turns (
thread_id,
turn_id,
pending_message_id,
source_proposed_plan_thread_id,
source_proposed_plan_id,
assistant_message_id,
state,
requested_at,
started_at,
completed_at,
checkpoint_turn_count,
checkpoint_ref,
checkpoint_status,
checkpoint_files_json
)
VALUES
(
'thread-1',
'turn-completed',
'message-user-1',
NULL,
NULL,
'message-assistant-1',
'completed',
'2026-04-02T00:00:05.000Z',
'2026-04-02T00:00:06.000Z',
'2026-04-02T00:00:20.000Z',
5,
'checkpoint-5',
'ready',
'[]'
),
(
'thread-1',
'turn-running',
'message-user-2',
NULL,
NULL,
NULL,
'running',
'2026-04-02T00:00:30.000Z',
'2026-04-02T00:00:30.000Z',
NULL,
NULL,
NULL,
NULL,
'[]'
)
`;
const threadShell = yield* snapshotQuery.getThreadShellById(ThreadId.make("thread-1"));
assert.equal(threadShell._tag, "Some");
if (Predicate.isTagged(threadShell, "Some")) {
assert.equal(threadShell.value.latestTurn?.turnId, asTurnId("turn-running"));
assert.equal(threadShell.value.latestTurn?.state, "running");
assert.equal(threadShell.value.latestTurn?.requestMessageId, asMessageId("message-user-2"));
assert.equal(threadShell.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
}
const threadDetail = yield* snapshotQuery.getThreadDetailById(ThreadId.make("thread-1"));
assert.equal(threadDetail._tag, "Some");
if (Predicate.isTagged(threadDetail, "Some")) {
assert.equal(threadDetail.value.latestTurn?.turnId, asTurnId("turn-running"));
assert.equal(threadDetail.value.latestTurn?.state, "running");
assert.equal(
threadDetail.value.latestTurn?.requestMessageId,
asMessageId("message-user-2"),
);
assert.equal(threadDetail.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");
}
}),
);
});