diff --git a/apps/server/src/kin/KinEnabled.ts b/apps/server/src/kin/KinEnabled.ts new file mode 100644 index 0000000000000..ad7d312ee9853 --- /dev/null +++ b/apps/server/src/kin/KinEnabled.ts @@ -0,0 +1,34 @@ +import * as Cause from "effect/Cause"; +import * as Config from "effect/Config"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Ref from "effect/Ref"; + +export class KinEnabled extends Context.Service< + KinEnabled, + { + readonly enabled: boolean; + readonly reportFailure: (cause: Cause.Cause) => Effect.Effect; + } +>()("t3/kin/KinEnabled") {} + +export const layer = Layer.effect( + KinEnabled, + Effect.gen(function* () { + const enabled = yield* Config.Boolean("KIN_ENABLED").pipe( + Config.withDefault(false), + Effect.catch((error) => + Effect.logError("Kin disabled: invalid KIN_ENABLED.", error.message).pipe(Effect.as(false)), + ), + ); + const reported = yield* Ref.make(false); + return { + enabled, + reportFailure: Effect.fn("KinEnabled.reportFailure")(function* (cause) { + if (!(yield* Ref.getAndSet(reported, true))) + yield* Effect.logError("Kin disabled: initialization failed.", Cause.pretty(cause)); + }), + }; + }), +); diff --git a/apps/server/src/kin/chat/KinChatHub.test.ts b/apps/server/src/kin/chat/KinChatHub.test.ts index 3fadabe0166e1..7e5da46a31024 100644 --- a/apps/server/src/kin/chat/KinChatHub.test.ts +++ b/apps/server/src/kin/chat/KinChatHub.test.ts @@ -1,79 +1,78 @@ -import { assert, describe, it } from "@effect/vitest"; -import { KinChatId, KinMemberId, KinOrgId } from "@t3tools/contracts"; +import { assert, it } from "@effect/vitest"; +import { KinChatId, KinMemberId, KinOrgId, KinPersonId } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; -import * as PubSub from "effect/PubSub"; -import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; import * as SqlClient from "effect/sql/SqlClient"; import * as Sqlite from "../../persistence/Sqlite.ts"; import * as KinPersistence from "../persistence/KinPersistence.ts"; import * as Repository from "./KinChatRepository.ts"; import * as Hub from "./KinChatHub.ts"; -const layer = Layer.provideMerge( - Hub.layer, - Layer.provideMerge( - Repository.layer, - Layer.provideMerge(KinPersistence.layer, Sqlite.layerMemory), - ), +const layer = Hub.layer.pipe( + Layer.provideMerge(Repository.layer), + Layer.provideMerge(KinPersistence.layer), + Layer.provideMerge(Sqlite.layerMemory), ); -const orgId = Schema.decodeSync(KinOrgId)("org-a"); -const otherOrg = Schema.decodeSync(KinOrgId)("org-b"); -const event: Hub.KinChatHubEvent = { - type: "read.updated", - orgId, - chatId: Schema.decodeSync(KinChatId)("chat"), - memberId: Schema.decodeSync(KinMemberId)("member"), - seq: 1, -}; -describe("KinChatHub", () => { - it.effect( - "isolates org subscriptions and delivers only after the outer commit, discarding rollbacks", - () => - Effect.gen(function* () { - const hub = yield* Hub.KinChatHub; - const sql = yield* SqlClient.SqlClient; - const first = yield* hub.subscribe(orgId); - const second = yield* hub.subscribe(orgId); - const other = yield* hub.subscribe(otherOrg); - yield* sql`CREATE TABLE chat_hub_test (seq INTEGER)`; - yield* sql.withTransaction( - Effect.gen(function* () { - yield* sql`INSERT INTO chat_hub_test VALUES (1)`; - yield* sql.withTransaction(hub.publish(event)); - assert.deepStrictEqual(yield* PubSub.takeUpTo(first, 10), []); - yield* sql - .withTransaction( - hub - .publish({ ...event, seq: 2 }) - .pipe(Effect.andThen(Effect.fail("nested rollback"))), - ) - .pipe(Effect.flip); - }), - ); - assert.deepStrictEqual(yield* PubSub.take(first), event); - assert.deepStrictEqual(yield* PubSub.take(second), event); - assert.deepStrictEqual(yield* sql`SELECT seq FROM chat_hub_test ORDER BY seq`, [ - { seq: 1 }, - ]); - yield* sql - .withTransaction( - Effect.gen(function* () { - yield* sql`INSERT INTO chat_hub_test VALUES (3)`; - yield* hub.publish({ ...event, seq: 3 }); - return yield* Effect.fail("outer rollback"); - }), - ) - .pipe(Effect.flip); - assert.deepStrictEqual(yield* PubSub.takeUpTo(first, 10), []); - assert.deepStrictEqual(yield* PubSub.takeUpTo(second, 10), []); - assert.deepStrictEqual(yield* PubSub.takeUpTo(other, 10), []); - yield* hub.publish({ ...event, orgId: otherOrg }); - assert.deepStrictEqual(yield* PubSub.take(other), { ...event, orgId: otherOrg }); - assert.deepStrictEqual(yield* sql`SELECT seq FROM chat_hub_test ORDER BY seq`, [ - { seq: 1 }, - ]); - }).pipe(Effect.provide(layer)), - ); -}); +it.effect("delivers only committed viewer events and isolates organizations", () => + Effect.gen(function* () { + const hub = yield* Hub.KinChatHub; + const sql = yield* SqlClient.SqlClient; + const repo = yield* Repository.KinChatRepository; + const orgId = KinOrgId.make("org"); + const personId = KinPersonId.make("owner"); + const chatId = KinChatId.make("chat"); + const memberId = KinMemberId.make("member"); + const now = "1970-01-01T00:00:00.000Z"; + yield* repo.insertOrg({ orgId, name: "Kin", createdAt: now }); + yield* repo.insertPerson({ + orgId, + personId, + handle: "owner", + displayName: "Owner", + githubLogin: null, + createdAt: now, + }); + yield* repo.createChat({ + orgId, + chatId, + kind: "project_group", + title: "Test", + teamId: null, + directKey: null, + createdAt: now, + lastMessageAt: null, + archivedAt: null, + }); + yield* repo.addMember({ + orgId, + chatId, + memberId, + principal: { _tag: "person", personId }, + role: "owner", + joinedAt: now, + leftAt: null, + mutedUntil: null, + }); + const first = yield* hub.subscribeForViewer({ _tag: "person", orgId, personId }); + const second = yield* hub.subscribeForViewer({ _tag: "person", orgId, personId }); + const event: Hub.KinChatHubEvent = { type: "read.updated", orgId, chatId, memberId, seq: 1 }; + yield* sql + .withTransaction( + hub.publish({ ...event, seq: 0 }).pipe(Effect.andThen(Effect.fail("rollback"))), + ) + .pipe(Effect.flip); + yield* hub.publish({ ...event, orgId: KinOrgId.make("other") }); + yield* sql.withTransaction(hub.publish(event)); + yield* hub.publish({ ...event, seq: 2 }); + assert.deepStrictEqual(yield* Stream.runCollect(first.pipe(Stream.take(2))), [ + event, + { ...event, seq: 2 }, + ]); + assert.deepStrictEqual(yield* Stream.runCollect(second.pipe(Stream.take(2))), [ + event, + { ...event, seq: 2 }, + ]); + }).pipe(Effect.provide(layer)), +); diff --git a/apps/server/src/kin/chat/KinChatHub.ts b/apps/server/src/kin/chat/KinChatHub.ts index c5c8fde915709..d93c98d10f8bb 100644 --- a/apps/server/src/kin/chat/KinChatHub.ts +++ b/apps/server/src/kin/chat/KinChatHub.ts @@ -2,6 +2,7 @@ import type { KinChat, KinChatId, KinMemberId, + KinChatMember, KinMessage, KinOrgId, KinWaitingItem, @@ -23,6 +24,7 @@ type ChatEvent = | { readonly type: "message.posted"; readonly message: KinMessage } | { readonly type: "widget.updated"; readonly widget: KinWidget } | { readonly type: "waiting.updated"; readonly item: KinWaitingItem } + | { readonly type: "member.removed"; readonly member: KinChatMember } | { readonly type: "read.updated"; readonly chatId: KinChatId; @@ -42,9 +44,6 @@ export class KinChatHub extends Context.Service< never, Scope.Scope >; - readonly subscribe: ( - orgId: KinOrgId, - ) => Effect.Effect, never, Scope.Scope>; } >()("t3/kin/chat/KinChatHub") {} @@ -73,12 +72,17 @@ const make = Effect.gen(function* () { }), ); }), - subscribe, subscribeForViewer: Effect.fn("KinChatHub.subscribeForViewer")(function* (identity) { const subscription = yield* subscribe(identity.orgId); return Stream.fromEffectRepeat(PubSub.take(subscription)).pipe( Stream.filterEffect( Effect.fn("KinChatHub.canDeliver")(function* (event) { + if (event.type === "member.removed") + return identity._tag === "person" + ? event.member.principal._tag === "person" && + event.member.principal.personId === identity.personId + : event.member.principal._tag === "agent" && + event.member.principal.agentId === identity.agentId; const chatId = event.type === "chat.created" ? event.chat.chatId @@ -89,14 +93,19 @@ const make = Effect.gen(function* () { : event.type === "waiting.updated" ? event.item.chatId : event.chatId; - const member = (yield* repo.listMembers({ orgId: identity.orgId, chatId })).find( - (row) => - row.leftAt === null && - (identity._tag === "person" - ? row.principal._tag === "person" && row.principal.personId === identity.personId - : row.principal._tag === "agent" && row.principal.agentId === identity.agentId), - ); - if (member === undefined) return false; + if ( + event.type === "waiting.updated" && + (identity._tag !== "person" || event.item.personId !== identity.personId) + ) + return false; + if (event.type === "waiting.updated" && event.item.resolvedAt !== null) return true; + const memberOption = yield* repo.activeMember({ + orgId: identity.orgId, + chatId, + principal: identity, + }); + if (Option.isNone(memberOption)) return false; + const member = memberOption.value; const messageId = event.type === "widget.updated" ? event.widget.messageId diff --git a/apps/server/src/kin/chat/KinChatRepository.test.ts b/apps/server/src/kin/chat/KinChatRepository.test.ts index 2dbc36e374017..4141f6d614c14 100644 --- a/apps/server/src/kin/chat/KinChatRepository.test.ts +++ b/apps/server/src/kin/chat/KinChatRepository.test.ts @@ -178,7 +178,7 @@ describe("KinChatRepository", () => { ...agent, retiredAt: null, }); - assert.deepStrictEqual(yield* repo.listAgents({ orgId }), [agent]); + assert.deepStrictEqual(yield* repo.listAgents({ orgId }), [{ ...agent, retiredAt: null }]); assert.deepStrictEqual( yield* repo.createChat({ ...chat, @@ -192,6 +192,18 @@ describe("KinChatRepository", () => { assert.deepStrictEqual(yield* repo.listMembersByPerson({ orgId, personId }), [owner]); assert.deepStrictEqual(yield* repo.listMembersByAgent({ orgId, agentId }), [agentMember]); assert.deepStrictEqual(Option.getOrThrow(yield* repo.getMember(scope)), owner); + const indexSql = yield* SqlClient.SqlClient; + for (const [principal, column] of [ + [owner.principal, "person_id"], + [agentMember.principal, "agent_id"], + ] as const) { + assert.isTrue(Option.isSome(yield* repo.activeMember({ ...scope, principal }))); + const plan = yield* indexSql<{ + detail: string; + }>`EXPLAIN QUERY PLAN ${Repository.activeMemberStatement(indexSql, { ...scope, principal })}`; + assert.isTrue(plan.some((row) => row.detail.includes(`(chat_id=? AND ${column}=?)`))); + } + for (const retry of [owner, agentMember]) { assert.deepStrictEqual(yield* repo.addMember(retry), retry); } @@ -215,6 +227,11 @@ describe("KinChatRepository", () => { createdAt: timestamp, }); for (const retry of [ + { + ...owner, + memberId: decodeMemberId("second-owner"), + principal: { _tag: "person", personId: secondPerson }, + }, { ...owner, principal: { _tag: "person", personId: secondPerson } }, { ...owner, memberId: decodeMemberId("retry-member") }, { ...agentMember, memberId: decodeMemberId("retry-agent-member") }, @@ -291,8 +308,8 @@ describe("KinChatRepository", () => { ); assert.strictEqual(wrongChat._tag, "KinChatError"); if (wrongChat._tag === "KinChatError") assert.strictEqual(wrongChat.reason, "not_found"); - assert.strictEqual(yield* repo.leaveChat({ ...scope, leftAt: later }), true); - assert.strictEqual(yield* repo.leaveChat({ ...scope, leftAt: timestamp }), false); + assert.deepStrictEqual(yield* repo.leaveChat({ ...scope, leftAt: later }), []); + assert.deepStrictEqual(yield* repo.leaveChat({ ...scope, leftAt: timestamp }), []); assert.strictEqual( (yield* Effect.flip(repo.insertMessage(message("left-post"))))._tag, "KinChatError", diff --git a/apps/server/src/kin/chat/KinChatRepository.ts b/apps/server/src/kin/chat/KinChatRepository.ts index eace986b93f0f..6b6fc98147de7 100644 --- a/apps/server/src/kin/chat/KinChatRepository.ts +++ b/apps/server/src/kin/chat/KinChatRepository.ts @@ -98,7 +98,6 @@ const OrgRow = KinOrg; const orgColumns = `org_id AS "orgId", name AS "name", created_at AS "createdAt"`; const PersonRow = KinPerson; const personColumns = `person_id AS "personId", org_id AS "orgId", handle AS "handle", display_name AS "displayName", github_login AS "githubLogin"`; -const AgentRow = KinAgent; const AgentIdentityRow = Schema.Struct({ ...KinAgent.fields, retiredAt: Schema.NullOr(IsoDateTime), @@ -139,6 +138,14 @@ const agentThreadColumns = `org_id AS "orgId", thread_id AS "threadId", agent_id const ReadStateRow = KinReadState; const readStateColumns = `org_id AS "orgId", chat_id AS "chatId", member_id AS "memberId", last_read_seq AS "lastReadSeq", updated_at AS "updatedAt"`; +export const activeMemberStatement = ( + sql: SqlClient.SqlClient, + input: ChatScope & { readonly principal: KinChatMember["principal"] }, +) => + input.principal._tag === "person" + ? sql`SELECT ${sql.literal(memberColumns)} FROM kin_chat_members WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND person_id = ${input.principal.personId} AND left_at IS NULL` + : sql`SELECT ${sql.literal(memberColumns)} FROM kin_chat_members WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND agent_id = ${input.principal.agentId} AND left_at IS NULL`; + export class KinChatRepository extends Context.Service< KinChatRepository, { @@ -182,7 +189,12 @@ export class KinChatRepository extends Context.Service< >; readonly listAllOrgs: () => Effect.Effect, RepositoryError>; readonly listPeople: (input: Scope) => Effect.Effect, RepositoryError>; - readonly listAgents: (input: Scope) => Effect.Effect, RepositoryError>; + readonly listAgents: ( + input: Scope, + ) => Effect.Effect< + ReadonlyArray, + RepositoryError + >; readonly listChats: (input: Scope) => Effect.Effect, RepositoryError>; readonly listMembers: ( input: ChatScope, @@ -193,6 +205,26 @@ export class KinChatRepository extends Context.Service< readonly listMembersByAgent: ( input: Scope & { readonly agentId: KinAgentId }, ) => Effect.Effect, RepositoryError>; + readonly activeMember: ( + input: ChatScope & { readonly principal: KinChatMember["principal"] }, + ) => Effect.Effect, RepositoryError>; + readonly waitingForWidget: ( + input: Scope & { readonly widgetId: KinWidgetId }, + ) => Effect.Effect, RepositoryError>; + readonly approvalAtMessage: ( + input: ChatScope & { readonly messageId: KinMessageId }, + ) => Effect.Effect; + readonly resume: ( + input: ChatScope & { readonly memberId: KinMemberId; readonly afterSeq: number }, + ) => Effect.Effect< + { + readonly reset: boolean; + readonly lastSeq: number; + readonly messages: ReadonlyArray; + readonly widgets: ReadonlyArray; + }, + RepositoryError + >; readonly listOpenWaiting: ( input: Scope & { readonly personId: KinPersonId }, ) => Effect.Effect, RepositoryError>; @@ -206,6 +238,9 @@ export class KinChatRepository extends Context.Service< readonly addMember: (input: KinChatMember) => Effect.Effect; readonly leaveChat: ( input: MemberScope & { readonly leftAt: IsoDateTime }, + ) => Effect.Effect, RepositoryError>; + readonly addApprovalWaiting: ( + input: KinWaitingItem & { readonly reason: "approval"; readonly widgetId: KinWidgetId }, ) => Effect.Effect; readonly addWaiting: (input: KinWaitingItem) => Effect.Effect; readonly registerAgentThread: ( @@ -395,6 +430,98 @@ const make = Effect.gen(function* () { } }, ); + const addWaiting = Effect.fn("KinChatRepository.addWaiting")( + function* (input: KinWaitingItem, approval: boolean) { + if (input.resolvedAt !== null || input.resolution !== null) + return yield* new KinChatError({ + reason: "invalid_state", + message: "New waiting items must be open.", + }); + return yield* sql.withTransaction( + Effect.gen(function* () { + yield* requireRecord(chatOne(input)); + yield* requireRecord(personOne(input)); + yield* requireRecord( + SqlSchema.findOneOption({ + Request: Schema.Unknown, + Result: MemberRow, + execute: () => sql`SELECT ${sql.literal(memberColumns)} FROM kin_chat_members + WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND person_id = ${input.personId} AND left_at IS NULL`, + })(input), + ); + if (input.messageId !== null) { + const message = yield* requireRecord( + messageOne({ orgId: input.orgId, messageId: input.messageId }), + ); + if (message.chatId !== input.chatId) + return yield* new KinChatError({ + reason: "not_found", + message: "Kin record not found.", + }); + } + if (input.widgetId !== null) { + const widget = yield* requireRecord( + widgetOne({ orgId: input.orgId, widgetId: input.widgetId }), + ); + if (widget.chatId !== input.chatId) + return yield* new KinChatError({ + reason: "not_found", + message: "Kin record not found.", + }); + } + const approvalWidgets = + yield* sql`SELECT widget_id FROM kin_widgets WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND type = 'approval' AND (widget_id = ${input.widgetId} OR message_id = ${input.messageId} OR message_id IN (SELECT message_id FROM kin_widgets WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND widget_id = ${input.widgetId})) ORDER BY widget_id`; + if ( + approval + ? input.reason !== "approval" || + input.widgetId === null || + (yield* requireRecord(widgetOne({ orgId: input.orgId, widgetId: input.widgetId }))) + .type !== "approval" + : input.reason === "approval" || approvalWidgets.length > 0 + ) + return yield* new KinChatError({ + reason: "not_allowed", + message: "Chat operation not allowed.", + }); + const existing = yield* SqlSchema.findOneOption({ + Request: Schema.Unknown, + Result: WaitingRow, + execute: () => + sql`SELECT ${sql.literal(waitingColumns)} FROM kin_waiting WHERE waiting_id = ${input.waitingId}`, + })(input); + if (Option.isSome(existing)) { + if (existing.value.orgId !== input.orgId) + return yield* new KinChatError({ + reason: "not_found", + message: "Kin record not found.", + }); + if ( + !( + [ + "orgId", + "personId", + "chatId", + "messageId", + "widgetId", + "reason", + "createdAt", + ] as const + ).every((key) => existing.value[key] === input[key]) + ) + return yield* new KinChatError({ + reason: "not_allowed", + message: "Kin waiting identity conflict.", + }); + return true; + } + const inserted = + yield* sql`INSERT INTO kin_waiting (waiting_id, org_id, person_id, chat_id, message_id, widget_id, reason, created_at, resolved_at, resolution) VALUES (${input.waitingId}, ${input.orgId}, ${input.personId}, ${input.chatId}, ${input.messageId}, ${input.widgetId}, ${input.reason}, ${input.createdAt}, ${input.resolvedAt}, ${input.resolution}) ON CONFLICT (widget_id, person_id) WHERE resolved_at IS NULL AND widget_id IS NOT NULL DO NOTHING RETURNING waiting_id`; + return inserted.length > 0; + }), + ); + }, + Effect.mapError(toRepositoryError("KinChatRepository.addWaiting")), + ); return KinChatRepository.of({ getOrg: orgOne, getPerson: personOne, @@ -495,9 +622,9 @@ const make = Effect.gen(function* () { function* (input) { return yield* SqlSchema.findAll({ Request: Schema.Unknown, - Result: AgentRow, + Result: AgentIdentityRow, execute: () => - sql`SELECT ${sql.literal(agentColumns)} FROM kin_agents WHERE org_id = ${input.orgId} ORDER BY display_name, agent_id`, + sql`SELECT ${sql.literal(agentColumns)}, retired_at AS "retiredAt" FROM kin_agents WHERE org_id = ${input.orgId} ORDER BY display_name, agent_id`, })(input).pipe(NodeSqliteClient.readOnly); }, Effect.mapError(toRepositoryError("KinChatRepository.listAgents")), @@ -546,6 +673,63 @@ const make = Effect.gen(function* () { }, Effect.mapError(toRepositoryError("KinChatRepository.listMembersByAgent")), ), + activeMember: Effect.fn("KinChatRepository.activeMember")( + function (input) { + return SqlSchema.findOneOption({ + Request: Schema.Unknown, + Result: MemberRow, + execute: () => activeMemberStatement(sql, input), + })(input).pipe(NodeSqliteClient.readOnly); + }, + Effect.mapError(toRepositoryError("KinChatRepository.activeMember")), + ), + waitingForWidget: Effect.fn("KinChatRepository.waitingForWidget")( + function (input) { + return SqlSchema.findAll({ + Request: Schema.Unknown, + Result: WaitingRow, + execute: () => + sql`SELECT ${sql.literal(waitingColumns)} FROM kin_waiting WHERE org_id = ${input.orgId} AND widget_id = ${input.widgetId} AND resolved_at IS NULL ORDER BY created_at, waiting_id`, + })(input).pipe(NodeSqliteClient.readOnly); + }, + Effect.mapError(toRepositoryError("KinChatRepository.waitingForWidget")), + ), + approvalAtMessage: Effect.fn("KinChatRepository.approvalAtMessage")( + function* (input) { + const rows = + yield* sql`SELECT widget_id FROM kin_widgets WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND message_id = ${input.messageId} AND type = 'approval' ORDER BY widget_id LIMIT 1`; + return rows.length > 0; + }, + Effect.mapError(toRepositoryError("KinChatRepository.approvalAtMessage")), + ), + resume: Effect.fn("KinChatRepository.resume")( + function* (input) { + const member = yield* requireRecord(memberOne(input)); + if (member.chatId !== input.chatId || member.leftAt !== null) + return yield* new KinChatError({ reason: "not_found", message: "Kin record not found." }); + const chat = yield* requireRecord(chatOne(input)); + const messages = yield* SqlSchema.findAll({ + Request: Schema.Unknown, + Result: MessageRow, + execute: () => + sql`SELECT ${sql.literal(messageColumns)} FROM kin_messages WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND seq > ${input.afterSeq} AND seq <= ${chat.lastSeq} AND (${member.role === "owner" ? 1 : 0} = 1 OR status NOT IN ('suggested', 'dismissed') OR author_member_id = ${member.memberId}) ORDER BY seq LIMIT 201`, + })(input); + if (messages.length > 200) + return { reset: true, lastSeq: chat.lastSeq, messages: [], widgets: [] }; + const widgets = + messages.length === 0 + ? [] + : yield* SqlSchema.findAll({ + Request: Schema.Unknown, + Result: WidgetRow, + execute: () => + sql`SELECT ${sql.literal(widgetColumns)} FROM kin_widgets WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND (message_id IN ${sql.in(messages.map((m) => m.messageId))} OR widget_id IN ${sql.in(messages.flatMap((m) => (m.widgetId === null ? [] : [m.widgetId])))}) ORDER BY message_id, widget_id`, + })(input); + return { reset: false, messages, widgets, lastSeq: chat.lastSeq }; + }, + (effect) => sql.withTransaction(effect).pipe(NodeSqliteClient.readOnly), + Effect.mapError(toRepositoryError("KinChatRepository.resume")), + ), listOpenWaiting: Effect.fn("KinChatRepository.listOpenWaiting")( function* (input) { return yield* SqlSchema.findAll({ @@ -596,6 +780,11 @@ const make = Effect.gen(function* () { agentOne({ orgId: input.orgId, agentId: input.principal.agentId }), ); } + if (input.principal._tag === "agent" && input.role !== "member") + return yield* new KinChatError({ + reason: "not_allowed", + message: "Chat operation not allowed.", + }); // Global ids must detect hidden cross-org collisions without exposing their rows. const existing = yield* SqlSchema.findAll({ Request: Schema.Unknown, @@ -608,6 +797,20 @@ const make = Effect.gen(function* () { reason: "not_found", message: "Kin record not found.", }); + if (input.role === "owner") { + if (input.principal._tag !== "person") + return yield* new KinChatError({ + reason: "not_allowed", + message: "Chat operation not allowed.", + }); + const owners = + yield* sql`SELECT member_id FROM kin_chat_members WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND role = 'owner' AND member_id <> ${input.memberId} ORDER BY member_id LIMIT 1`; + if (owners.length > 0) + return yield* new KinChatError({ + reason: "not_allowed", + message: "Chat operation not allowed.", + }); + } for (const member of existing) { if ( member.chatId !== input.chatId || @@ -624,12 +827,23 @@ const make = Effect.gen(function* () { reason: "not_allowed", message: "Kin member identity conflict.", }); - if (member.leftAt === null) return member; + if (member.leftAt === null) { + if ( + member.role !== input.role || + member.joinedAt !== input.joinedAt || + member.mutedUntil !== input.mutedUntil + ) + return yield* new KinChatError({ + reason: "not_allowed", + message: "Kin member identity conflict.", + }); + return member; + } yield* sql`UPDATE kin_chat_members SET left_at = NULL, joined_at = ${input.joinedAt}, role = ${input.role} WHERE org_id = ${input.orgId} AND member_id = ${input.memberId}`; return yield* requireRecord(memberOne(input)); } - yield* sql`INSERT INTO kin_chat_members (org_id, chat_id, member_id, role, joined_at, left_at, muted_until, principal_kind, person_id, agent_id) VALUES (${input.orgId}, ${input.chatId}, ${input.memberId}, ${input.role}, ${input.joinedAt}, ${input.leftAt}, ${input.mutedUntil}, ${input.principal._tag}, ${input.principal._tag === "person" ? input.principal.personId : null}, ${input.principal._tag === "agent" ? input.principal.agentId : null})`; + yield* sql`INSERT INTO kin_chat_members (org_id, chat_id, member_id, role, joined_at, left_at, muted_until, principal_kind, person_id, agent_id) VALUES (${input.orgId}, ${input.chatId}, ${input.memberId}, ${input.role}, ${input.joinedAt}, ${input.leftAt}, ${input.mutedUntil}, ${input.principal._tag}, ${input.principal._tag === "person" ? input.principal.personId : null}, ${input.principal._tag === "agent" ? input.principal.agentId : null}) ON CONFLICT DO NOTHING RETURNING member_id`; return yield* requireRecord(memberOne(input)); }), ); @@ -642,94 +856,27 @@ const make = Effect.gen(function* () { Effect.gen(function* () { const changed = yield* sql`UPDATE kin_chat_members SET left_at = ${input.leftAt} WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND member_id = ${input.memberId} AND left_at IS NULL RETURNING member_id`; - if (changed.length === 0) return false; + if (changed.length === 0) return []; const member = yield* requireRecord(memberOne(input)); - if (member.principal._tag === "person") - yield* sql`UPDATE kin_waiting SET resolved_at = ${input.leftAt}, resolution = 'cancelled' - WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND person_id = ${member.principal.personId} AND resolved_at IS NULL`; - return true; - }), - ); - }, - Effect.mapError(toRepositoryError("KinChatRepository.leaveChat")), - ), - addWaiting: Effect.fn("KinChatRepository.addWaiting")( - function* (input) { - if (input.resolvedAt !== null || input.resolution !== null) - return yield* new KinChatError({ - reason: "invalid_state", - message: "New waiting items must be open.", - }); - return yield* sql.withTransaction( - Effect.gen(function* () { - yield* requireRecord(chatOne(input)); - yield* requireRecord(personOne(input)); - yield* requireRecord( - SqlSchema.findOneOption({ - Request: Schema.Unknown, - Result: MemberRow, - execute: () => sql`SELECT ${sql.literal(memberColumns)} FROM kin_chat_members - WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND person_id = ${input.personId} AND left_at IS NULL`, - })(input), - ); - if (input.messageId !== null) { - const message = yield* requireRecord( - messageOne({ orgId: input.orgId, messageId: input.messageId }), - ); - if (message.chatId !== input.chatId) - return yield* new KinChatError({ - reason: "not_found", - message: "Kin record not found.", - }); - } - if (input.widgetId !== null) { - const widget = yield* requireRecord( - widgetOne({ orgId: input.orgId, widgetId: input.widgetId }), - ); - if (widget.chatId !== input.chatId) - return yield* new KinChatError({ - reason: "not_found", - message: "Kin record not found.", - }); - } - const existing = yield* SqlSchema.findOneOption({ + if (member.principal._tag !== "person") return []; + const personId = member.principal.personId; + return yield* SqlSchema.findAll({ Request: Schema.Unknown, Result: WaitingRow, - execute: () => - sql`SELECT ${sql.literal(waitingColumns)} FROM kin_waiting WHERE waiting_id = ${input.waitingId}`, - })(input); - if (Option.isSome(existing)) { - if (existing.value.orgId !== input.orgId) - return yield* new KinChatError({ - reason: "not_found", - message: "Kin record not found.", - }); - if ( - !( - [ - "orgId", - "personId", - "chatId", - "messageId", - "widgetId", - "reason", - "createdAt", - ] as const - ).every((key) => existing.value[key] === input[key]) - ) - return yield* new KinChatError({ - reason: "not_allowed", - message: "Kin waiting identity conflict.", - }); - return true; - } - const inserted = - yield* sql`INSERT INTO kin_waiting (waiting_id, org_id, person_id, chat_id, message_id, widget_id, reason, created_at, resolved_at, resolution) VALUES (${input.waitingId}, ${input.orgId}, ${input.personId}, ${input.chatId}, ${input.messageId}, ${input.widgetId}, ${input.reason}, ${input.createdAt}, ${input.resolvedAt}, ${input.resolution}) ON CONFLICT (widget_id, person_id) WHERE resolved_at IS NULL AND widget_id IS NOT NULL DO NOTHING RETURNING waiting_id`; - return inserted.length > 0; + execute: + () => sql`UPDATE kin_waiting SET resolved_at = ${input.leftAt}, resolution = 'cancelled' + WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND person_id = ${personId} AND resolved_at IS NULL RETURNING ${sql.literal(waitingColumns)}`, + })(input).pipe( + Effect.map((rows) => rows.toSorted((a, b) => a.waitingId.localeCompare(b.waitingId))), + ); }), ); }, - Effect.mapError(toRepositoryError("KinChatRepository.addWaiting")), + Effect.mapError(toRepositoryError("KinChatRepository.leaveChat")), + ), + addWaiting: Effect.fn("KinChatRepository.addWaiting")((input) => addWaiting(input, false)), + addApprovalWaiting: Effect.fn("KinChatRepository.addApprovalWaiting")((input) => + addWaiting(input, true), ), registerAgentThread: Effect.fn("KinChatRepository.registerAgentThread")( function* (input) { diff --git a/apps/server/src/kin/chat/KinChatService.test.ts b/apps/server/src/kin/chat/KinChatService.test.ts index c642c870a7edf..e2b0f1a9767be 100644 --- a/apps/server/src/kin/chat/KinChatService.test.ts +++ b/apps/server/src/kin/chat/KinChatService.test.ts @@ -11,13 +11,15 @@ import { KinPersonId, KinTaskCardState, KinWaitingId, + KinWidgetId, ProviderInstanceId, ThreadId, } from "@t3tools/contracts"; +import * as Cause from "effect/Cause"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; -import * as PubSub from "effect/PubSub"; +import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; @@ -474,7 +476,9 @@ describe("KinChatService", () => { Effect.gen(function* () { const { repo, service, person, agent, chat } = yield* seed; const hub = yield* Hub.KinChatHub; - const events = yield* hub.subscribe(person.orgId); + const events = yield* Stream.toQueue(yield* hub.subscribeForViewer(person), { + capacity: "unbounded", + }); const sql = yield* SqlClient.SqlClient; const input = { chatId: chat.chatId, @@ -485,18 +489,18 @@ describe("KinChatService", () => { yield* sql .withTransaction(service.send(agent, input).pipe(Effect.andThen(Effect.fail("rollback")))) .pipe(Effect.flip); - assert.deepStrictEqual(yield* PubSub.takeUpTo(events, 20), []); + assert.isTrue(Option.isNone(yield* Queue.poll(events))); assert.deepStrictEqual(yield* repo.listOpenWaiting(person), []); const question = yield* service.send(agent, input); assert.strictEqual(question.seq, 1); - assert.strictEqual((yield* PubSub.take(events)).type, "message.posted"); - assert.strictEqual((yield* PubSub.take(events)).type, "waiting.updated"); + assert.strictEqual((yield* Queue.take(events)).type, "message.posted"); + assert.strictEqual((yield* Queue.take(events)).type, "waiting.updated"); assert.strictEqual((yield* repo.listOpenWaiting(person)).length, 1); assert.deepStrictEqual(yield* service.send(agent, input), question); - assert.deepStrictEqual(yield* PubSub.takeUpTo(events, 20), []); + assert.isTrue(Option.isNone(yield* Queue.poll(events))); yield* service.send(person, { chatId: chat.chatId, body: "Yes", clientMessageId: "reply" }); - assert.strictEqual((yield* PubSub.take(events)).type, "message.posted"); - const resolved = yield* PubSub.take(events); + assert.strictEqual((yield* Queue.take(events)).type, "message.posted"); + const resolved = yield* Queue.take(events); assert.strictEqual(resolved.type, "waiting.updated"); if (resolved.type === "waiting.updated") assert.strictEqual(resolved.item.resolution, "answered"); @@ -508,22 +512,29 @@ describe("KinChatService", () => { yield* service.markRead(person, { chatId: chat.chatId, seq: 0 }); assert.strictEqual(Option.getOrThrow(yield* repo.getReadState(member)).lastReadSeq, 2); assert.strictEqual(yield* repo.unreadCount(member), 0); - const read = yield* PubSub.take(events); + const read = yield* Queue.take(events); assert.include(read, { type: "read.updated", seq: 2 }); - assert.deepStrictEqual(yield* PubSub.takeUpTo(events, 20), []); + assert.isTrue(Option.isNone(yield* Queue.poll(events))); + const approvalWidget = yield* service.upsertWidget(agent, { + chatId: chat.chatId, + type: "task", + state, + }); + yield* sql`UPDATE kin_widgets SET type = 'approval' WHERE org_id = ${person.orgId} AND widget_id = ${approvalWidget.widgetId}`; const approval = { orgId: person.orgId, chatId: chat.chatId, waitingId: decodeKinWaitingId("server-approval"), personId: person.personId, - messageId: question.messageId, - widgetId: null, + messageId: approvalWidget.messageId, + widgetId: approvalWidget.widgetId, reason: "approval", createdAt: timestamp, resolvedAt: null, resolution: null, } as const; - yield* repo.addWaiting(approval); + assert.include(yield* Effect.flip(repo.addWaiting(approval)), { reason: "not_allowed" }); + yield* repo.addApprovalWaiting(approval); const humanQuestion = yield* service.send(person, { chatId: chat.chatId, body: "Another question", @@ -533,9 +544,12 @@ describe("KinChatService", () => { ...approval, reason: "question", messageId: humanQuestion.messageId, + widgetId: null, }); const ownQuestion = yield* service.addWaiting(agent, { ...approval, + messageId: question.messageId, + widgetId: null, reason: "question", }); for (const operation of [ @@ -548,12 +562,30 @@ describe("KinChatService", () => { }), ]) assert.include(yield* Effect.flip(operation), { reason: "not_allowed" }); - const approvalWidget = yield* service.upsertWidget(agent, { + const secondaryAnchor = yield* service.upsertWidget(agent, { chatId: chat.chatId, type: "task", state, }); - yield* sql`UPDATE kin_widgets SET type = 'approval' WHERE org_id = ${person.orgId} AND widget_id = ${approvalWidget.widgetId}`; + yield* repo.insertWidget({ + ...secondaryAnchor, + widgetId: KinWidgetId.make("secondary-approval"), + type: "approval", + }); + const secondaryWaiting = { + ...approval, + waitingId: KinWaitingId.make("secondary-waiting"), + reason: "question" as const, + messageId: secondaryAnchor.messageId, + widgetId: secondaryAnchor.widgetId, + }; + for (const attempt of [ + repo.addWaiting(secondaryWaiting).pipe(Effect.asVoid), + service.addWaiting(person, secondaryWaiting).pipe(Effect.asVoid), + repo.addWaiting({ ...secondaryWaiting, messageId: null }).pipe(Effect.asVoid), + service.addWaiting(person, { ...secondaryWaiting, messageId: null }).pipe(Effect.asVoid), + ]) + assert.include(yield* Effect.flip(attempt), { reason: "not_allowed" }); for (const identity of [person, agent]) for (const widgetId of [null, approvalWidget.widgetId]) assert.include( @@ -703,6 +735,121 @@ describe("KinChatService", () => { yield* hub.publish(marker); assert.deepStrictEqual(Option.getOrThrow(yield* Stream.runHead(agentViewer)), marker); assert.deepStrictEqual(Option.getOrThrow(yield* Stream.runHead(viewer)), suggestion); + const waitingEvent = events.find((event) => event.type === "waiting.updated"); + assert.isDefined(waitingEvent); + if (waitingEvent.type === "waiting.updated") + yield* hub.publish({ + ...waitingEvent, + item: { ...waitingEvent.item, personId: KinPersonId.make("someone-else") }, + }); + yield* hub.publish(marker); + assert.deepStrictEqual(Option.getOrThrow(yield* Stream.runHead(viewer)), marker); }).pipe(Effect.provide(layer)), ); + it.effect( + "requires an owner to remove members and publishes cancelled waiting only after commit", + () => + Effect.gen(function* () { + const { repo, service, person, chat } = yield* seed; + const hub = yield* Hub.KinChatHub; + const sql = yield* SqlClient.SqlClient; + const other = { ...person, personId: KinPersonId.make("other-person") }; + yield* repo.insertPerson({ + ...other, + handle: "other", + displayName: "Other", + githubLogin: null, + createdAt: timestamp, + }); + const member = yield* repo.addMember({ + orgId: person.orgId, + chatId: chat.chatId, + memberId: KinMemberId.make("other-member"), + principal: other, + role: "member", + joinedAt: timestamp, + leftAt: null, + mutedUntil: null, + }); + const waiting = yield* service.addWaiting(person, { + chatId: chat.chatId, + personId: other.personId, + messageId: null, + widgetId: null, + reason: "mention", + }); + const published: Array = []; + const monitored = yield* Service.KinChatService.pipe( + Effect.provide( + Layer.fresh(Service.layer).pipe( + Layer.provide( + Layer.succeed(Hub.KinChatHub, { + ...hub, + publish: (event) => + hub.publish(event).pipe( + Effect.andThen( + sql.afterCommit( + Effect.sync(() => { + published.push(event); + }), + ), + ), + ), + }), + ), + ), + ), + ); + assert.include(yield* Effect.flip(monitored.leaveChat(other, member)), { + reason: "not_allowed", + }); + yield* sql + .withTransaction( + monitored.leaveChat(person, member).pipe(Effect.andThen(Effect.fail("rollback"))), + ) + .pipe(Effect.flip); + assert.deepStrictEqual(published, []); + assert.deepStrictEqual(yield* repo.listOpenWaiting(other), [waiting]); + const chatEvents = yield* Stream.toQueue( + service.subscribe(other, { chatId: chat.chatId }), + { capacity: "unbounded" }, + ); + assert.strictEqual((yield* Queue.take(chatEvents))._tag, "backlog"); + const updates = yield* Stream.toQueue(service.subscribeChats([other], {}), { + capacity: "unbounded", + }); + assert.strictEqual((yield* Queue.take(updates))._tag, "snapshot"); + yield* monitored.leaveChat(person, member); + const cancelled = Option.getOrThrow(yield* repo.getWaiting(waiting)); + assert.strictEqual(cancelled.resolution, "cancelled"); + const markerChat = yield* repo.createChat({ + ...chat, + chatId: KinChatId.make("marker-chat"), + kind: "project_group", + directKey: null, + }); + yield* repo.addMember({ + ...member, + memberId: KinMemberId.make("marker-member"), + chatId: markerChat.chatId, + }); + yield* hub.publish({ type: "chat.created", orgId: other.orgId, chat: markerChat }); + assert.deepStrictEqual(yield* Queue.take(updates), { _tag: "waiting", item: cancelled }); + assert.strictEqual(published.length, 2); + assert.strictEqual((yield* Queue.take(updates))._tag, "chat"); + assert.deepStrictEqual(yield* Queue.take(chatEvents), { + _tag: "member", + member: Option.getOrThrow(yield* repo.getMember(member)), + }); + assert.isTrue(Cause.isDone(yield* Queue.take(chatEvents).pipe(Effect.flip))); + assert.deepStrictEqual(published[0], { + type: "waiting.updated", + orgId: person.orgId, + item: cancelled, + }); + assert.deepStrictEqual(yield* repo.listOpenWaiting(other), []); + yield* repo.addMember(member); + assert.deepStrictEqual(yield* repo.listOpenWaiting(other), []); + }).pipe(Effect.provide(layer)), + ); }); diff --git a/apps/server/src/kin/chat/KinChatService.ts b/apps/server/src/kin/chat/KinChatService.ts index 970b125421c54..4770d4b827169 100644 --- a/apps/server/src/kin/chat/KinChatService.ts +++ b/apps/server/src/kin/chat/KinChatService.ts @@ -10,6 +10,12 @@ import { KinAgentIntent, type KinAuthor, type KinChat, + type KinChatListEvent, + type KinChatStreamEvent, + type KinChatSubscribeInput, + type KinChatsSubscribeInput, + type KinOrgsListResult, + type KinChatSummary, type KinChatHistoryInput, type KinChatHistoryResult, type KinChatMember, @@ -17,6 +23,7 @@ import { type KinMessage, type KinMessagesMarkReadInput, type KinPersonId, + type KinOrgId, type KinWaitingItem, type KinWidget, } from "@t3tools/contracts"; @@ -27,6 +34,7 @@ import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as Stream from "effect/Stream"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; import * as SqlError from "effect/sql/SqlError"; @@ -38,6 +46,9 @@ import * as Repository from "./KinChatRepository.ts"; import type { AgentIdentity, PersonIdentity } from "./KinIdentity.ts"; type Identity = PersonIdentity | AgentIdentity; +type Viewers = ReadonlyArray; +type Caller = Identity | Viewers; +type PersonCaller = PersonIdentity | Viewers; type ServiceError = Repository.RepositoryError; type SendInput = KinMessagesSendInput & { readonly intent?: KinAgentIntent }; type WidgetUpsertInput = { @@ -57,20 +68,32 @@ type WaitingInput = Pick< export class KinChatService extends Context.Service< KinChatService, { - readonly createDirect: ( + readonly orgs: ( + identities: ReadonlyArray, + ) => Effect.Effect; + readonly subscribe: ( + identity: PersonCaller, + input: KinChatSubscribeInput, + ) => Stream.Stream; + readonly subscribeChats: ( + identities: ReadonlyArray, + input: KinChatsSubscribeInput, + ) => Stream.Stream; + readonly leaveChat: ( identity: PersonIdentity, + input: { readonly chatId: KinChatId; readonly memberId: KinMemberId }, + ) => Effect.Effect; + readonly createDirect: ( + identity: PersonCaller, input: KinChatsCreateDirectInput, ) => Effect.Effect; - readonly send: ( - identity: Identity, - input: SendInput, - ) => Effect.Effect; + readonly send: (identity: Caller, input: SendInput) => Effect.Effect; readonly history: ( - identity: Identity, + identity: Caller, input: KinChatHistoryInput, ) => Effect.Effect; readonly markRead: ( - identity: Identity, + identity: Caller, input: KinMessagesMarkReadInput, ) => Effect.Effect; readonly upsertWidget: ( @@ -78,7 +101,7 @@ export class KinChatService extends Context.Service< input: WidgetUpsertInput, ) => Effect.Effect; readonly act: ( - identity: PersonIdentity, + identity: PersonCaller, input: KinWidgetsActInput, ) => Effect.Effect; readonly addWaiting: ( @@ -129,6 +152,38 @@ const make = Effect.gen(function* () { ), ); const now = DateTime.now.pipe(Effect.map(DateTime.formatIso)); + const identityFor = Effect.fn("KinChatService.identityFor")(function* ( + caller: Caller, + target: { + readonly orgId?: KinOrgId; + readonly chatId?: KinChatId; + readonly widgetId?: KinWidgetId; + }, + ) { + if ("_tag" in caller) return caller; + for (const identity of caller) { + if (target.orgId !== undefined && target.orgId !== identity.orgId) continue; + if ( + target.chatId !== undefined && + Option.isNone(yield* repo.getChat({ ...identity, chatId: target.chatId })) + ) + continue; + if ( + target.widgetId !== undefined && + Option.isNone(yield* repo.getWidget({ ...identity, widgetId: target.widgetId })) + ) + continue; + return identity; + } + return yield* notFound; + }); + const personFor = Effect.fn("KinChatService.personFor")(function* ( + caller: PersonCaller, + target: Parameters[1], + ) { + const person = yield* identityFor(caller, target); + return person._tag === "person" ? person : yield* notFound; + }); const memberFor = Effect.fn("KinChatService.memberFor")(function* ( identity: Identity, chatId: KinChatId, @@ -275,8 +330,222 @@ const make = Effect.gen(function* () { } return message; }); + const summary = Effect.fn("KinChatService.summary")(function* ( + identity: PersonIdentity, + chatId: KinChatId, + ): Effect.fn.Return { + const member = yield* memberFor(identity, chatId); + const chat = yield* requireRecord(repo.getChat({ orgId: identity.orgId, chatId })); + const history = yield* repo.history({ + ...chat, + beforeSeq: chat.lastSeq + 1, + limit: 1, + viewerMemberId: member.memberId, + }); + return { + ...chat, + members: yield* repo.listMembers(chat), + lastMessage: history.messages[0] ?? null, + unread: yield* repo.unreadCount(member), + }; + }); + const snapshot = Effect.fn("KinChatService.snapshot")( + function* (identities: ReadonlyArray) { + const chats: Array = []; + const waiting: Array = []; + for (const identity of identities) { + for (const member of yield* repo.listMembersByPerson(identity)) { + if (member.leftAt !== null) continue; + chats.push(yield* summary(identity, member.chatId)); + } + for (const item of yield* repo.listOpenWaiting(identity)) { + if (Option.isSome(yield* repo.activeMember({ ...item, principal: identity }))) + waiting.push(item); + } + } + return { _tag: "snapshot" as const, chats, waiting }; + }, + (effect) => transaction(effect).pipe(NodeSqliteClient.readOnly), + ); return KinChatService.of({ + orgs: Effect.fn("KinChatService.orgs")(function* (identities) { + const orgs = yield* Effect.forEach( + identities, + Effect.fn("KinChatService.org")(function* (identity) { + const org = yield* requireRecord(repo.getOrg(identity)); + const agents = yield* repo.listAgents(identity); + return { + ...org, + agents: agents.map(({ agentId, displayName, retiredAt }) => ({ + agentId, + displayName, + retired: retiredAt !== null, + })), + }; + }), + ); + return { orgs, me: identities.map(({ orgId, personId }) => ({ orgId, personId })) }; + }), + leaveChat: Effect.fn("KinChatService.leaveChat")(function* (identity, input) { + const caller = yield* memberFor(identity, input.chatId); + if (caller.role !== "owner") return yield* notAllowed; + const member = yield* requireRecord( + repo.getMember({ orgId: identity.orgId, memberId: input.memberId }), + ); + if (member.chatId !== input.chatId) return yield* notFound; + if (member.role === "owner") return yield* notAllowed; + for (const item of yield* repo.leaveChat({ ...member, leftAt: yield* now })) + yield* hub.publish({ type: "waiting.updated", orgId: identity.orgId, item }); + const removed = yield* requireRecord(repo.getMember(member)); + yield* hub.publish({ type: "member.removed", orgId: identity.orgId, member: removed }); + }, transaction), + subscribe: (caller, input) => + Stream.unwrap( + Effect.gen(function* () { + const identity = yield* personFor(caller, input); + const live = yield* hub.subscribeForViewer(identity); + const member = yield* memberFor(identity, input.chatId); + const resumed = yield* repo.resume({ ...member, afterSeq: input.afterSeq ?? 0 }); + const backlog: KinChatStreamEvent = resumed.reset + ? { _tag: "reset", lastSeq: resumed.lastSeq } + : { + _tag: "backlog", + lastSeq: resumed.lastSeq, + messages: resumed.messages, + widgets: resumed.widgets, + }; + const lastHistorySeq = backlog.lastSeq; + const revisions = new Map( + backlog._tag === "backlog" ? backlog.widgets.map((w) => [w.widgetId, w.revision]) : [], + ); + return Stream.concat( + Stream.succeed(backlog), + live.pipe( + Stream.map((event) => { + switch (event.type) { + case "message.posted": { + if ( + event.message.chatId !== input.chatId || + event.message.seq <= lastHistorySeq + ) + return Option.none(); + return Option.some({ + _tag: "message", + message: event.message, + }); + } + case "widget.updated": { + if ( + event.widget.chatId !== input.chatId || + event.widget.revision <= (revisions.get(event.widget.widgetId) ?? 0) + ) + return Option.none(); + revisions.set(event.widget.widgetId, event.widget.revision); + return Option.some({ + _tag: "widget", + widget: event.widget, + }); + } + case "member.removed": + return event.member.chatId === input.chatId + ? Option.some({ _tag: "member", member: event.member }) + : Option.none(); + case "read.updated": + return event.chatId === input.chatId + ? Option.some({ + _tag: "read", + memberId: event.memberId, + seq: event.seq, + }) + : Option.none(); + default: + return Option.none(); + } + }), + Stream.filter(Option.isSome), + Stream.map((event) => event.value), + Stream.takeUntil((event) => event._tag === "member" && event.member.leftAt !== null), + ), + ); + }), + ), + subscribeChats: (identities, input) => + Stream.unwrap( + Effect.gen(function* () { + const viewers = + input.orgId === undefined + ? identities + : identities.filter((identity) => identity.orgId === input.orgId); + if (input.orgId !== undefined && viewers.length === 0) return yield* notFound; + const streams = yield* Effect.forEach(viewers, (viewer) => + hub + .subscribeForViewer(viewer) + .pipe( + Effect.map((stream) => stream.pipe(Stream.map((event) => ({ viewer, event })))), + ), + ); + const initial = yield* snapshot(viewers); + const summaries = new Map( + initial.chats.map((chat) => [`${chat.orgId}:${chat.chatId}`, JSON.stringify(chat)]), + ); + const waiting = new Map( + initial.waiting.map((item) => [item.waitingId, JSON.stringify(item)]), + ); + const live = Stream.mergeAll(streams, { concurrency: "unbounded" }).pipe( + Stream.mapEffect(({ viewer, event }) => + Effect.gen(function* (): Effect.fn.Return { + if (event.type === "waiting.updated") + return yield* requireRecord(repo.getWaiting(event.item)).pipe( + Effect.map((item) => ({ _tag: "waiting" as const, item })), + ); + const chatId = + event.type === "chat.created" + ? event.chat.chatId + : event.type === "message.posted" + ? event.message.chatId + : event.type === "widget.updated" + ? event.widget.chatId + : event.type === "member.removed" + ? event.member.chatId + : event.chatId; + return yield* summary(viewer, chatId).pipe( + Effect.map((chat) => ({ _tag: "chat" as const, chat })), + ); + }).pipe( + Effect.asSome, + Effect.catch((error) => + Effect.logDebug("Kin chat list event dropped", error).pipe( + Effect.as(Option.none()), + ), + ), + ), + ), + Stream.filter(Option.isSome), + Stream.map((event) => event.value), + ); + return Stream.concat( + Stream.succeed(initial), + live.pipe( + Stream.filter((event) => { + if (event._tag === "chat") { + const key = `${event.chat.orgId}:${event.chat.chatId}`; + const value = JSON.stringify(event.chat); + if (summaries.get(key) === value) return false; + summaries.set(key, value); + } else if (event._tag === "waiting") { + const value = JSON.stringify(event.item); + if (waiting.get(event.item.waitingId) === value) return false; + waiting.set(event.item.waitingId, value); + } + return true; + }), + ), + ); + }), + ), + createDirect: Effect.fn("KinChatService.createDirect")(function* (identity, input) { + identity = yield* personFor(identity, input); if (identity.orgId !== input.orgId) return yield* notFound; const person = yield* requireRecord(repo.getPerson(identity)); const agent = yield* requireRecord(repo.getAgent(input)); @@ -317,6 +586,7 @@ const make = Effect.gen(function* () { return chat; }, transaction), send: Effect.fn("KinChatService.send")(function* (identity, input) { + identity = yield* identityFor(identity, input); const request = yield* decodeSend(input).pipe(Effect.mapError(() => invalidState)); const member = yield* memberFor(identity, request.chatId); yield* canWrite(identity, member); @@ -341,6 +611,7 @@ const make = Effect.gen(function* () { }, transaction), history: Effect.fn("KinChatService.history")( function* (identity, input) { + identity = yield* identityFor(identity, input); const member = yield* memberFor(identity, input.chatId); return yield* repo.history({ ...input, @@ -351,6 +622,7 @@ const make = Effect.gen(function* () { (effect) => transaction(effect).pipe(NodeSqliteClient.readOnly), ), markRead: Effect.fn("KinChatService.markRead")(function* (identity, input) { + identity = yield* identityFor(identity, input); const member = yield* memberFor(identity, input.chatId); const previous = yield* repo.getReadState(member); yield* repo.markRead({ ...member, seq: input.seq, updatedAt: yield* now }); @@ -428,16 +700,9 @@ const make = Effect.gen(function* () { updatedAt: yield* now, }); } - for (const person of yield* repo.listMembers(member)) { - if (person.principal._tag !== "person") continue; - const personId = person.principal.personId; - for (const item of yield* repo.listOpenWaiting({ orgId: identity.orgId, personId })) { - if ( - item.widgetId === widget.widgetId && - (widget.closedAt !== null || !widget.awaiting.includes(personId)) - ) - yield* resolve(item, widget.closedAt !== null ? "closed" : "superseded"); - } + for (const item of yield* repo.waitingForWidget(widget)) { + if (widget.closedAt !== null || !widget.awaiting.includes(item.personId)) + yield* resolve(item, widget.closedAt !== null ? "closed" : "superseded"); } if (widget.closedAt === null) for (const personId of new Set(widget.awaiting)) @@ -452,6 +717,7 @@ const make = Effect.gen(function* () { return widget; }, transaction), act: Effect.fn("KinChatService.act")(function* (identity, input) { + identity = yield* personFor(identity, input); const request = yield* decodeAct(input).pipe(Effect.mapError(() => invalidState)); const widget = yield* requireRecord( repo.getWidget({ orgId: identity.orgId, widgetId: request.widgetId }), @@ -514,11 +780,13 @@ const make = Effect.gen(function* () { repo.getMessage({ orgId: identity.orgId, messageId: input.messageId }), ); if (message !== null && message.chatId !== input.chatId) return yield* notFound; + if (message !== null && (yield* repo.approvalAtMessage(message))) return yield* notAllowed; for (const widgetId of new Set([input.widgetId, message?.widgetId ?? null])) { if (widgetId === null) continue; const widget = yield* requireRecord(repo.getWidget({ orgId: identity.orgId, widgetId })); if (widget.chatId !== input.chatId) return yield* notFound; - if (widget.type === "approval") return yield* notAllowed; + if (widget.type === "approval" || (yield* repo.approvalAtMessage(widget))) + return yield* notAllowed; } return yield* add(identity, input); }, transaction), @@ -535,10 +803,12 @@ const make = Effect.gen(function* () { : yield* requireRecord( repo.getMessage({ orgId: identity.orgId, messageId: item.messageId }), ); + if (message !== null && (yield* repo.approvalAtMessage(message))) return yield* notAllowed; for (const widgetId of new Set([item.widgetId, message?.widgetId ?? null])) { if (widgetId === null) continue; const widget = yield* requireRecord(repo.getWidget({ orgId: identity.orgId, widgetId })); - if (widget.type === "approval") return yield* notAllowed; + if (widget.type === "approval" || (yield* repo.approvalAtMessage(widget))) + return yield* notAllowed; } if (identity._tag === "agent") { if (item.reason !== "question" || message === null) return yield* notAllowed; diff --git a/apps/server/src/kin/chat/KinIdentity.ts b/apps/server/src/kin/chat/KinIdentity.ts index df99409c662d2..4bd7f2aa94663 100644 --- a/apps/server/src/kin/chat/KinIdentity.ts +++ b/apps/server/src/kin/chat/KinIdentity.ts @@ -84,12 +84,10 @@ export class KinIdentity extends Context.Service< const repository = yield* KinChatRepository.KinChatRepository; const config = yield* KinIdentityConfig; const sql = yield* SqlClient.SqlClient; - const owner = yield* repository - .bootstrapIdentity({ - ...config, - createdAt: DateTime.formatIso(yield* DateTime.now), - }) - .pipe(Effect.tapError((error) => Effect.logError("Kin identity bootstrap failed", error))); + const owner = yield* repository.bootstrapIdentity({ + ...config, + createdAt: DateTime.formatIso(yield* DateTime.now), + }); const active = new Set( (yield* repository.listActiveAgentThreads()).map((row) => row.threadId), ); diff --git a/apps/server/src/kin/chat/layer.ts b/apps/server/src/kin/chat/layer.ts new file mode 100644 index 0000000000000..c81f17b40b04c --- /dev/null +++ b/apps/server/src/kin/chat/layer.ts @@ -0,0 +1,15 @@ +import * as Layer from "effect/Layer"; +import * as KinPersistence from "../persistence/KinPersistence.ts"; +import * as Repository from "./KinChatRepository.ts"; +import * as Hub from "./KinChatHub.ts"; +import * as Service from "./KinChatService.ts"; +import { KinIdentity, KinIdentityConfig } from "./KinIdentity.ts"; + +export const layer = Layer.mergeAll( + Service.layer, + KinIdentity.layer.pipe(Layer.provide(KinIdentityConfig.layer)), +).pipe( + Layer.provideMerge(Hub.layer), + Layer.provideMerge(Repository.layer), + Layer.provide(KinPersistence.layer), +); diff --git a/apps/server/src/kin/chat/rpcHandlers.test.ts b/apps/server/src/kin/chat/rpcHandlers.test.ts new file mode 100644 index 0000000000000..ed3944a4e798b --- /dev/null +++ b/apps/server/src/kin/chat/rpcHandlers.test.ts @@ -0,0 +1,515 @@ +import { assert, it } from "@effect/vitest"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import * as Hub from "./KinChatHub.ts"; +import * as Service from "./KinChatService.ts"; +import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; +import * as NodeSocket from "@effect/platform-node/NodeSocket"; +import { + AuthSessionId, + AuthSettingsWriteScope, + KinChatError, + KinMemberId, + KinPersonId, + KinTaskCardState, + KinWaitingId, + AuthStandardClientScopes, + KinAgentId, + KinChatId, + KinMessageId, + KinChatRpcs, + KinOrgId, + ThreadId, + RpcScopeAuthorization, + WsRpcGroup, + WS_METHODS, +} from "@t3tools/contracts"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Context from "effect/Context"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Queue from "effect/Queue"; +import * as Stream from "effect/Stream"; +import * as HttpRouter from "effect/http/HttpRouter"; +import * as HttpServer from "effect/http/HttpServer"; +import * as RpcTest from "effect/rpc/RpcTest"; +import * as Schema from "effect/Schema"; +import * as RpcClient from "effect/rpc/RpcClient"; +import * as RpcGroup from "effect/rpc/RpcGroup"; +import * as RpcServer from "effect/rpc/RpcServer"; +import * as RpcSerialization from "effect/rpc/RpcSerialization"; +import * as FileSystem from "effect/FileSystem"; +import * as SqlClient from "effect/sql/SqlClient"; +import * as Sqlite from "../../persistence/Sqlite.ts"; +import * as RpcAuthorization from "../../auth/RpcAuthorization.ts"; +import * as Kin from "../layer.ts"; +import * as Chat from "./layer.ts"; +import { KinIdentity } from "./KinIdentity.ts"; +import { KinChatService } from "./KinChatService.ts"; +import * as Repository from "./KinChatRepository.ts"; +import { makeKinChatHandlers } from "./rpcHandlers.ts"; + +const decodeTaskState = Schema.decodeEffect(KinTaskCardState); + +const session = { + sessionId: AuthSessionId.make("owner-session"), + subject: "browser", + method: "browser-session-cookie" as const, + scopes: AuthStandardClientScopes, +}; +const configured = ConfigProvider.layer(ConfigProvider.fromUnknown({ KIN_ENABLED: true })); +const root = Kin.layer.pipe(Layer.provideMerge(Chat.layer), Layer.provide(configured)); +const probe = WsRpcGroup.omit( + ...[...WsRpcGroup.requests.keys()].filter( + ( + tag, + ): tag is Exclude["_tag"], typeof WS_METHODS.serverProbe> => + tag !== WS_METHODS.serverProbe, + ), +); +const group = RpcGroup.make(...KinChatRpcs, ...probe.requests.values()).middleware( + RpcScopeAuthorization, +); +const seed = Effect.gen(function* () { + const context = yield* Effect.context< + KinIdentity | KinChatService | Repository.KinChatRepository | Hub.KinChatHub + >(); + const identity = Context.get(context, KinIdentity); + const chat = Context.get(context, KinChatService); + const repo = Context.get(context, Repository.KinChatRepository); + const person = (yield* identity.resolve(session))[0]; + assert.isDefined(person); + const agentId = KinAgentId.make("lead"); + yield* repo.insertAgent({ + ...person, + agentId, + ownerPersonId: person.personId, + displayName: "Lead", + role: "lead", + createdAt: "1970-01-01T00:00:00.000Z", + }); + const direct = yield* chat.createDirect(person, { ...person, agentId }); + return { identity, chat, repo, person, direct, context }; +}); + +it.effect("serves real WS sends, subscriptions and afterSeq resume with org isolation", () => + Effect.gen(function* () { + const { direct, repo, chat, person } = yield* seed; + const threadId = ThreadId.make("registered-through-runner"); + const runnerConsumer = yield* Effect.gen(function* () { + const root = yield* Kin.KinServer; + return Context.get(Option.getOrThrow(root.services), KinIdentity).registerAgentThread; + }); + const toolConsumer = yield* Effect.gen(function* () { + const root = yield* Kin.KinServer; + return Context.get(Option.getOrThrow(root.services), KinIdentity).isAgentThread; + }); + assert.isFalse(toolConsumer(threadId)); + yield* runnerConsumer({ ...person, agentId: KinAgentId.make("lead"), threadId }); + assert.isTrue(toolConsumer(threadId)); + const handlers = yield* makeKinChatHandlers(session); + const serverHandlers = group.toLayer({ ...handlers, "server.probe": () => Effect.succeed({}) }); + const route = Layer.effectDiscard( + Effect.gen(function* () { + const router = yield* HttpRouter.HttpRouter; + const effect = yield* RpcServer.toHttpEffectWebsocket(group); + yield* router.add("GET", "/ws", effect); + }), + ).pipe( + Layer.provide(serverHandlers), + Layer.provide(RpcSerialization.layerJson), + Layer.provide(RpcAuthorization.layer(AuthStandardClientScopes)), + ); + const server = yield* Layer.build( + HttpRouter.serve(route, { disableListenLog: true }).pipe( + Layer.provideMerge(NodeHttpServer.layerTest), + ), + ); + const address = Context.get(server, HttpServer.HttpServer).address; + if (address._tag === "UnixPathAddress") return assert.fail("Expected an internet socket"); + const transport = yield* Layer.build( + RpcClient.layerProtocolSocket({ retryTransientErrors: false }).pipe( + Layer.provide(NodeSocket.layerWebSocket(`ws://127.0.0.1:${address.port}/ws`)), + Layer.provide(RpcSerialization.layerJson), + ), + ); + const client = yield* RpcClient.make(group).pipe(Effect.provide(transport)); + assert.deepStrictEqual(yield* client["server.probe"]({}), {}); + const forged = { + chatId: direct.chatId, + clientMessageId: "first", + body: "Hello", + author: { _tag: "system" }, + }; + const first = yield* client["kin.messages.send"](forged); + assert.include(first.author, { _tag: "human", personId: person.personId }); + const events = yield* Stream.toQueue( + client["kin.chat.subscribe"]({ chatId: direct.chatId, afterSeq: 0 }), + { capacity: "unbounded" }, + ); + const backlog = yield* Queue.take(events); + assert.include(backlog, { _tag: "backlog", lastSeq: 1 }); + if (backlog._tag === "backlog") assert.deepStrictEqual(backlog.messages, [first]); + const next = yield* client["kin.messages.send"]({ + chatId: direct.chatId, + clientMessageId: "next", + body: "Live", + }); + assert.deepStrictEqual(yield* Queue.take(events), { _tag: "message", message: next }); + const resumed = Option.getOrThrow( + yield* Stream.runHead( + client["kin.chat.subscribe"]({ chatId: direct.chatId, afterSeq: first.seq }), + ), + ); + assert.include(resumed, { _tag: "backlog", lastSeq: 2 }); + if (resumed._tag === "backlog") assert.deepStrictEqual(resumed.messages, [next]); + const list = Option.getOrThrow(yield* Stream.runHead(client["kin.chats.subscribe"]({}))); + assert.strictEqual(list._tag, "snapshot"); + if (list._tag === "snapshot") assert.strictEqual(list.chats[0]?.lastSeq, 2); + const foreignOrg = KinOrgId.make("foreign"); + const foreignChat = KinChatId.make("foreign-chat"); + yield* repo.insertOrg({ orgId: foreignOrg, name: "Other", createdAt: direct.createdAt }); + yield* repo.createChat({ + ...direct, + orgId: foreignOrg, + chatId: foreignChat, + kind: "project_group", + directKey: null, + }); + const foreignPerson = { + _tag: "person" as const, + orgId: foreignOrg, + personId: KinPersonId.make("foreign-person"), + }; + yield* repo.insertPerson({ + ...foreignPerson, + handle: "foreign", + displayName: "Other", + githubLogin: null, + createdAt: direct.createdAt, + }); + yield* repo.addMember({ + ...foreignPerson, + chatId: foreignChat, + memberId: KinMemberId.make("foreign-member"), + principal: foreignPerson, + role: "owner", + joinedAt: direct.createdAt, + leftAt: null, + mutedUntil: null, + }); + const foreignAgentId = KinAgentId.make("foreign-agent"); + yield* repo.insertAgent({ + ...foreignPerson, + agentId: foreignAgentId, + displayName: "Other lead", + role: "lead", + ownerPersonId: foreignPerson.personId, + createdAt: direct.createdAt, + }); + const foreignMessage = yield* chat.send(foreignPerson, { + chatId: foreignChat, + clientMessageId: "foreign-message", + body: "Private", + }); + const foreignWidget = yield* chat.upsertWidget(foreignPerson, { + chatId: foreignChat, + type: "task", + state: yield* decodeTaskState({ + taskId: "task", + environmentId: "environment", + threadId, + remoteEnvironmentId: null, + title: "Private", + model: null, + status: "working", + pullRequests: [], + lastError: null, + updatedAt: direct.createdAt, + }), + }); + const orgs = yield* client["kin.orgs.list"]({}); + assert.deepStrictEqual( + orgs.orgs.map(({ orgId }) => orgId), + [person.orgId], + ); + assert.deepStrictEqual(orgs.orgs[0]?.agents, [ + { agentId: KinAgentId.make("lead"), displayName: "Lead", retired: false }, + ]); + const denials = [ + ["kin.chats.subscribe", Stream.runHead(client["kin.chats.subscribe"]({ orgId: foreignOrg }))], + [ + "kin.chats.createDirect", + client["kin.chats.createDirect"]({ orgId: foreignOrg, agentId: KinAgentId.make("lead") }), + ], + [ + "kin.chats.createDirect/agent", + client["kin.chats.createDirect"]({ orgId: person.orgId, agentId: foreignAgentId }), + ], + ["kin.chat.subscribe", Stream.runHead(client["kin.chat.subscribe"]({ chatId: foreignChat }))], + [ + "kin.chat.history", + client["kin.chat.history"]({ chatId: foreignChat, beforeSeq: 100, limit: 100 }), + ], + [ + "kin.messages.send/chat", + client["kin.messages.send"]({ chatId: foreignChat, body: "No", clientMessageId: "denied" }), + ], + [ + "kin.messages.send/message", + client["kin.messages.send"]({ + chatId: direct.chatId, + body: "No", + clientMessageId: "foreign-reply", + replyTo: foreignMessage.messageId, + }), + ], + ["kin.messages.markRead", client["kin.messages.markRead"]({ chatId: foreignChat, seq: 1 })], + [ + "kin.widgets.act", + client["kin.widgets.act"]({ + widgetId: foreignWidget.widgetId, + revision: 1, + actionId: "open", + clientActionId: "denied", + }), + ], + ] as const; + for (const [method, request] of denials) + assert.include( + yield* request.pipe(Effect.flip), + { reason: "not_found", message: "Kin record not found." }, + method, + ); + const sql = yield* SqlClient.SqlClient; + yield* sql`UPDATE kin_agents SET retired_at = ${direct.createdAt} WHERE org_id = ${person.orgId} AND agent_id = 'lead'`; + assert.deepStrictEqual((yield* client["kin.orgs.list"]({})).orgs[0]?.agents, [ + { agentId: KinAgentId.make("lead"), displayName: "Lead", retired: true }, + ]); + }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), +); + +it.effect( + "subscribes before the resume read, deduplicates a concurrent send, then emits live", + () => + Effect.gen(function* () { + const { chat, repo, person, direct, context } = yield* seed; + const subscribed = yield* Deferred.make(); + const historyStarted = yield* Deferred.make(); + const releaseHistory = yield* Deferred.make(); + const hub = Context.get(context, Hub.KinChatHub); + const interceptedRepo = { + ...repo, + resume: (input: Parameters[0]) => + Deferred.succeed(historyStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseHistory)), + Effect.andThen(repo.resume(input)), + ), + }; + const service = yield* KinChatService.pipe( + Effect.provide( + Layer.fresh(Service.layer).pipe( + Layer.provide(Layer.succeed(Repository.KinChatRepository, interceptedRepo)), + Layer.provide( + Layer.succeed(Hub.KinChatHub, { + ...hub, + subscribeForViewer: (viewer) => + hub + .subscribeForViewer(viewer) + .pipe(Effect.tap(() => Deferred.succeed(subscribed, undefined))), + }), + ), + ), + ), + ); + assert.notStrictEqual(service, chat); + const events = yield* Stream.toQueue( + service.subscribe(person, { chatId: direct.chatId, afterSeq: 0 }), + { capacity: "unbounded" }, + ); + yield* Deferred.await(historyStarted); + assert.isTrue(yield* Deferred.isDone(subscribed)); + const concurrent = yield* chat.send(person, { + chatId: direct.chatId, + body: "During read", + clientMessageId: "concurrent", + }); + yield* Deferred.succeed(releaseHistory, undefined); + const backlog = yield* Queue.take(events); + assert.strictEqual(backlog._tag, "backlog"); + if (backlog._tag === "backlog") assert.deepStrictEqual(backlog.messages, [concurrent]); + const live = yield* chat.send(person, { + chatId: direct.chatId, + body: "After read", + clientMessageId: "live", + }); + assert.deepStrictEqual(yield* Queue.take(events), { _tag: "message", message: live }); + const delayed = yield* repo.insertMessage({ + ...live, + messageId: KinMessageId.make("delayed"), + clientMessageId: { kind: "send", id: "delayed" }, + }); + const later = yield* repo.insertMessage({ + ...live, + messageId: KinMessageId.make("later"), + clientMessageId: { kind: "send", id: "later" }, + }); + yield* hub.publish({ type: "message.posted", orgId: person.orgId, message: later }); + yield* hub.publish({ type: "message.posted", orgId: person.orgId, message: delayed }); + assert.deepStrictEqual(yield* Queue.take(events), { _tag: "message", message: later }); + assert.deepStrictEqual(yield* Queue.take(events), { _tag: "message", message: delayed }); + }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), +); + +it.effect("keeps transport handlers out of persistence", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const source = yield* fs.readFileString(new URL("./rpcHandlers.ts", import.meta.url).pathname); + assert.notMatch(source, /KinChatRepository|sql\/SqlClient|(?:kin|\.\.)\/persistence/); + }).pipe(Effect.provide(NodeServices.layer)), +); + +it.effect("resets large backlogs and bounds a resume to the current snapshot", () => + Effect.gen(function* () { + const { chat, person, direct } = yield* seed; + for (let i = 0; i < 201; i++) + yield* chat.send(person, { + chatId: direct.chatId, + body: `Message ${i}`, + clientMessageId: `message-${i}`, + }); + assert.deepStrictEqual( + Option.getOrThrow( + yield* Stream.runHead(chat.subscribe(person, { chatId: direct.chatId, afterSeq: 0 })), + ), + { _tag: "reset", lastSeq: 201 }, + ); + const resume = Option.getOrThrow( + yield* Stream.runHead(chat.subscribe(person, { chatId: direct.chatId, afterSeq: 1 })), + ); + assert.strictEqual(resume._tag, "backlog"); + if (resume._tag === "backlog") { + assert.strictEqual(resume.messages.length, 200); + assert.strictEqual(resume.messages[0]?.seq, 2); + assert.strictEqual(resume.lastSeq, 201); + } + }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), +); + +it.effect("subscribes before the list snapshot and drops buffered state already in it", () => + Effect.gen(function* () { + const { chat, person, direct, context } = yield* seed; + const hub = Context.get(context, Hub.KinChatHub); + const service = yield* KinChatService.pipe( + Effect.provide( + Layer.fresh(Service.layer).pipe( + Layer.provide( + Layer.succeed( + Repository.KinChatRepository, + Context.get(context, Repository.KinChatRepository), + ), + ), + Layer.provide( + Layer.succeed(Hub.KinChatHub, { + ...hub, + subscribeForViewer: (viewer) => + hub.subscribeForViewer(viewer).pipe( + Effect.tap(() => + chat + .send(person, { + chatId: direct.chatId, + clientMessageId: "during-list", + body: "During snapshot", + }) + .pipe(Effect.orDie), + ), + ), + }), + ), + ), + ), + ); + const queue = yield* Stream.toQueue(service.subscribeChats([person], {}), { + capacity: "unbounded", + }); + const initial = yield* Queue.take(queue); + assert.strictEqual(initial._tag, "snapshot"); + if (initial._tag === "snapshot") assert.strictEqual(initial.chats[0]?.lastSeq, 1); + yield* chat.send(person, { + chatId: direct.chatId, + clientMessageId: "after-list", + body: "After snapshot", + }); + const live = yield* Queue.take(queue); + assert.strictEqual(live._tag, "chat"); + if (live._tag === "chat") assert.strictEqual(live.chat.lastSeq, 2); + }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), +); + +it.effect("denies settings-only sessions before Kin handlers execute", () => + Effect.gen(function* () { + const handlers = yield* makeKinChatHandlers({ ...session, scopes: [AuthSettingsWriteScope] }); + const client = yield* RpcTest.makeClient(group).pipe( + Effect.provide( + Layer.merge( + group.toLayer({ ...handlers, "server.probe": () => Effect.succeed({}) }), + RpcAuthorization.layer([AuthSettingsWriteScope]), + ), + ), + ); + assert.strictEqual( + (yield* client["kin.orgs.list"]({}).pipe(Effect.flip))._tag, + "EnvironmentAuthorizationError", + ); + }), +); + +it.effect("drops a failed waiting read without ending the chat list stream", () => + Effect.gen(function* () { + const { repo, person, direct, context, chat } = yield* seed; + const hub = Context.get(context, Hub.KinChatHub); + const service = yield* KinChatService.pipe( + Effect.provide( + Layer.fresh(Service.layer).pipe( + Layer.provide( + Layer.succeed(Repository.KinChatRepository, { + ...repo, + getWaiting: () => + Effect.fail( + new KinChatError({ reason: "not_found", message: "Kin record not found." }), + ), + }), + ), + ), + ), + ); + const events = yield* Stream.toQueue(service.subscribeChats([person], {}), { + capacity: "unbounded", + }); + assert.strictEqual((yield* Queue.take(events))._tag, "snapshot"); + yield* hub.publish({ + type: "waiting.updated", + orgId: person.orgId, + item: { + ...person, + chatId: direct.chatId, + waitingId: KinWaitingId.make("failed-read"), + messageId: null, + widgetId: null, + reason: "mention", + createdAt: direct.createdAt, + resolvedAt: null, + resolution: null, + }, + }); + yield* chat.send(person, { + chatId: direct.chatId, + body: "Still live", + clientMessageId: "marker", + }); + const next = yield* Queue.take(events); + assert.strictEqual(next._tag, "chat"); + if (next._tag === "chat") assert.strictEqual(next.chat.lastSeq, 1); + }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), +); diff --git a/apps/server/src/kin/chat/rpcHandlers.ts b/apps/server/src/kin/chat/rpcHandlers.ts new file mode 100644 index 0000000000000..c858b2131a226 --- /dev/null +++ b/apps/server/src/kin/chat/rpcHandlers.ts @@ -0,0 +1,85 @@ +import { KinChatError, KinChatRpcs } from "@t3tools/contracts"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Schema from "effect/Schema"; +import * as Option from "effect/Option"; +import * as Stream from "effect/Stream"; +import * as RpcGroup from "effect/rpc/RpcGroup"; +import type { AuthenticatedSession } from "../../auth/EnvironmentAuth.ts"; +import { KinServer } from "../layer.ts"; +import { KinIdentity } from "./KinIdentity.ts"; +import { KinChatService } from "./KinChatService.ts"; + +const group = RpcGroup.make(...KinChatRpcs); +const unavailable = new KinChatError({ reason: "unavailable", message: "Kin is unavailable." }); +const isKinChatError = Schema.is(KinChatError); +const mapError = (error: { readonly _tag: string }) => + isKinChatError(error) + ? error + : new KinChatError({ reason: "invalid_state", message: "Kin operation failed." }); + +export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( + session: AuthenticatedSession, +) { + const kin = yield* Effect.serviceOption(KinServer); + const services = Effect.gen(function* () { + if (Option.isNone(kin) || Option.isNone(kin.value.services)) return yield* unavailable; + return { + identity: Context.get(kin.value.services.value, KinIdentity), + chat: Context.get(kin.value.services.value, KinChatService), + }; + }); + const viewer = Effect.gen(function* () { + const { identity, chat } = yield* services; + return { chat, people: yield* identity.resolve(session) }; + }); + return group.of({ + "kin.orgs.list": () => + services.pipe( + Effect.flatMap(({ identity, chat }) => + identity.resolve(session).pipe(Effect.flatMap(chat.orgs)), + ), + Effect.mapError(mapError), + ), + "kin.chats.subscribe": (input) => + Stream.unwrap( + services.pipe( + Effect.flatMap(({ identity, chat }) => + identity + .resolve(session) + .pipe(Effect.map((people) => chat.subscribeChats(people, input))), + ), + ), + ).pipe(Stream.mapError(mapError)), + "kin.chat.subscribe": (input) => + Stream.unwrap( + viewer.pipe(Effect.map(({ chat, people }) => chat.subscribe(people, input))), + ).pipe(Stream.mapError(mapError)), + "kin.chats.createDirect": (input) => + viewer.pipe( + Effect.flatMap(({ chat, people }) => chat.createDirect(people, input)), + Effect.mapError(mapError), + ), + "kin.chat.history": (input) => + viewer.pipe( + Effect.flatMap(({ chat, people }) => chat.history(people, input)), + Effect.mapError(mapError), + ), + "kin.messages.send": (input) => + viewer.pipe( + Effect.flatMap(({ chat, people }) => chat.send(people, input)), + Effect.mapError(mapError), + ), + "kin.messages.markRead": (input) => + viewer.pipe( + Effect.flatMap(({ chat, people }) => chat.markRead(people, input)), + Effect.as({}), + Effect.mapError(mapError), + ), + "kin.widgets.act": (input) => + viewer.pipe( + Effect.flatMap(({ chat, people }) => chat.act(people, input)), + Effect.mapError(mapError), + ), + }); +}); diff --git a/apps/server/src/kin/layer.test.ts b/apps/server/src/kin/layer.test.ts new file mode 100644 index 0000000000000..87db879d28828 --- /dev/null +++ b/apps/server/src/kin/layer.test.ts @@ -0,0 +1,224 @@ +import * as ThreadLaunch from "../orchestration-v2/ThreadLaunchService.ts"; +import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts"; +import * as KinTriggerSink from "./triggers/KinTriggerSink.ts"; +import { expectTypeOf } from "vite-plus/test"; +import { layerWsRpc } from "../ws.ts"; +import { assert, it } from "@effect/vitest"; +import { + AuthSessionId, + CommandId, + ProjectId, + ProviderInstanceId, + ScheduledTaskId, + AuthStandardClientScopes, + KinChatRpcs, + RpcScopeAuthorization, + WsRpcGroup, + WS_METHODS, +} from "@t3tools/contracts"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as RpcGroup from "effect/rpc/RpcGroup"; +import * as RpcTest from "effect/rpc/RpcTest"; +import * as SqlClient from "effect/sql/SqlClient"; +import * as RpcAuthorization from "../auth/RpcAuthorization.ts"; +import * as Sqlite from "../persistence/Sqlite.ts"; +import * as Kin from "./layer.ts"; +import { runKinMigrations } from "./persistence/KinMigrations.ts"; +import * as Logger from "effect/Logger"; +import { makeKinChatHandlers } from "./chat/rpcHandlers.ts"; + +const session = { + sessionId: AuthSessionId.make("owner"), + subject: "browser", + method: "browser-session-cookie" as const, + scopes: AuthStandardClientScopes, +}; +const probe = WsRpcGroup.omit( + ...[...WsRpcGroup.requests.keys()].filter( + ( + tag, + ): tag is Exclude["_tag"], typeof WS_METHODS.serverProbe> => + tag !== WS_METHODS.serverProbe, + ), +); +const group = RpcGroup.make(...KinChatRpcs, ...probe.requests.values()).middleware( + RpcScopeAuthorization, +); + +it.effect.each(["identity", "defect", "unconfigured"] as const)( + "contains %s startup and leaves non-Kin RPCs working", + (mode) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + if (mode === "identity") { + yield* runKinMigrations(); + yield* sql`INSERT INTO kin_orgs VALUES ('unrecoverable', 'Other', '1970-01-01T00:00:00.000Z')`; + } + const broken = new Proxy(sql, { + apply() { + throw new Error("private SQL defect"); + }, + }); + const logs: Array = []; + const kin = yield* Kin.KinServer.pipe( + Effect.provide( + Kin.layer.pipe( + Layer.provide( + ConfigProvider.layer( + ConfigProvider.fromUnknown({ KIN_ENABLED: mode !== "unconfigured" }), + ), + ), + Layer.provide(Layer.succeed(SqlClient.SqlClient, mode === "identity" ? sql : broken)), + Layer.provide( + Logger.layer([ + Logger.make((entry) => { + logs.push(entry.message); + }), + ]), + ), + ), + ), + ); + assert.strictEqual(logs.length, mode === "unconfigured" ? 0 : 1); + if (mode === "identity") + assert.include(JSON.stringify(logs), "restore the reserved identity"); + if (mode === "defect") assert.include(JSON.stringify(logs), "private SQL defect"); + assert.isTrue(Option.isNone(kin.services)); + const handlers = yield* makeKinChatHandlers(session).pipe( + Effect.provideService(Kin.KinServer, kin), + ); + const client = yield* RpcTest.makeClient(group).pipe( + Effect.provide( + Layer.merge( + group.toLayer({ ...handlers, "server.probe": () => Effect.succeed({}) }), + RpcAuthorization.layer(AuthStandardClientScopes), + ), + ), + ); + assert.deepStrictEqual(yield* client["server.probe"]({}), {}); + assert.include(yield* client["kin.orgs.list"]({}).pipe(Effect.flip), { + reason: "unavailable", + message: "Kin is unavailable.", + }); + }).pipe(Effect.provide(Sqlite.layerMemory)), +); + +it("retains concrete WS handler layer requirements with Kin RPCs", () => { + expectTypeOf< + 0 extends 1 & Layer.Services> ? true : false + >().toEqualTypeOf(); + expectTypeOf< + Extract>, Kin.KinServer> + >().toEqualTypeOf(); +}); + +it.effect.each(["disabled", "invalid", "failed"] as const)( + "keeps the runtime contained when Kin is %s", + (mode) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const input: ThreadLaunch.ThreadLaunchInput = { + commandId: CommandId.make("scheduled-task:task:webhook:delivery"), + projectId: ProjectId.make("project"), + title: "Scheduled", + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "test" }, + runtimeMode: "approval-required", + interactionMode: "default", + workspaceStrategy: { type: "root" }, + initialMessage: { + scheduledTaskId: ScheduledTaskId.make("task"), + text: "Run", + attachments: [], + }, + createdBy: "system", + creationSource: "server", + }; + const rejected = new ThreadLaunch.ThreadLaunchError({ + operation: "dispatch-message", + commandId: input.commandId, + projectId: input.projectId, + cause: new Error("inner launcher"), + }); + const seen: ThreadLaunch.ThreadLaunchInput[] = []; + const launch = (request: ThreadLaunch.ThreadLaunchInput) => { + seen.push(request); + return Effect.fail(rejected); + }; + const sendToThread = () => Effect.die("management should not be called"); + const logs: Array = []; + const broken = new Proxy(sql, { + apply() { + throw new Error("Kin migration defect"); + }, + }); + const runtime = Layer.mergeAll( + Kin.layer, + KinTriggerSink.wrapTaskDispatch( + Layer.mock(ThreadLaunch.ThreadLaunchService)({ launch }), + Layer.mock(ThreadManagement.ThreadManagementService)({ sendToThread }), + ), + ).pipe( + Layer.provide( + ConfigProvider.layer( + ConfigProvider.fromUnknown( + mode === "failed" + ? { KIN_ENABLED: true } + : mode === "invalid" + ? { KIN_ENABLED: "maybe" } + : {}, + ), + ), + ), + Layer.provide(Layer.succeed(SqlClient.SqlClient, mode === "failed" ? broken : sql)), + Layer.provide( + Logger.layer([ + Logger.make((entry) => { + logs.push(entry.message); + }), + ]), + ), + ); + yield* Effect.gen(function* () { + assert.deepStrictEqual( + yield* sql`SELECT name FROM sqlite_master WHERE name GLOB 'kin_*' ORDER BY name`, + [], + ); + assert.isTrue(Option.isNone((yield* Kin.KinServer).services)); + const launcher = yield* ThreadLaunch.ThreadLaunchService; + const management = yield* ThreadManagement.ThreadManagementService; + if (mode !== "failed") { + assert.strictEqual(launcher.launch, launch); + assert.strictEqual(management.sendToThread, sendToThread); + assert.strictEqual(yield* launcher.launch(input).pipe(Effect.flip), rejected); + } else { + const failure = yield* launcher.launch(input).pipe(Effect.flip); + assert.notStrictEqual(failure, rejected); + assert.include(String(failure.cause), "webhook dispatch refused"); + const ordinary = { + ...input, + commandId: CommandId.make("ordinary-scheduled"), + initialMessage: { ...input.initialMessage, text: "Run", attachments: [] }, + }; + assert.strictEqual(yield* launcher.launch(ordinary).pipe(Effect.flip), rejected); + assert.strictEqual(seen[0], ordinary); + const lookalike = { + ...ordinary, + commandId: CommandId.make("scheduled-task:other:webhook:x"), + }; + assert.strictEqual(yield* launcher.launch(lookalike).pipe(Effect.flip), rejected); + } + }).pipe(Effect.provide(runtime)); + assert.strictEqual(seen.length, mode === "failed" ? 2 : 1); + if (mode !== "failed") assert.strictEqual(seen[0], input); + assert.strictEqual(logs.length, mode === "disabled" ? 0 : 1); + if (mode === "invalid") assert.include(JSON.stringify(logs), "invalid KIN_ENABLED"); + if (mode === "failed") assert.include(JSON.stringify(logs), "Kin migration defect"); + assert.deepStrictEqual( + yield* sql`SELECT name FROM sqlite_master WHERE name GLOB 'kin_*' ORDER BY name`, + [], + ); + }).pipe(Effect.provide(Sqlite.layerMemory)), +); diff --git a/apps/server/src/kin/layer.ts b/apps/server/src/kin/layer.ts new file mode 100644 index 0000000000000..6f529da783146 --- /dev/null +++ b/apps/server/src/kin/layer.ts @@ -0,0 +1,76 @@ +import * as Cause from "effect/Cause"; +import * as KinEnabled from "./KinEnabled.ts"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as HttpRouter from "effect/http/HttpRouter"; +import * as Option from "effect/Option"; +import * as KinChat from "./chat/layer.ts"; +import { KinIdentity } from "./chat/KinIdentity.ts"; +import { KinChatService } from "./chat/KinChatService.ts"; +import { KinChatHub } from "./chat/KinChatHub.ts"; + +type Services = KinIdentity | KinChatService; + +export class KinServer extends Context.Service< + KinServer, + { + readonly services: Option.Option>; + readonly subscribeForViewer: Option.Option; + } +>()("t3/kin/layer/KinServer") {} + +// LEAD-5 supplies its runner here, using this root's identity rather than building another. +const lead: Layer.Layer = Layer.empty; +const enabled = Layer.effect( + KinServer, + Effect.gen(function* () { + const services = yield* Effect.context(); + const hub = yield* KinChatHub; + return { + services: Option.some(services), + subscribeForViewer: Option.some(hub.subscribeForViewer), + }; + }), +).pipe(Layer.provide(lead.pipe(Layer.provideMerge(KinChat.layer)))); +const disabled = Layer.succeed(KinServer, { + services: Option.none(), + subscribeForViewer: Option.none(), +}); + +const runtime = Layer.unwrap( + Effect.gen(function* () { + const config = yield* KinEnabled.KinEnabled; + return config.enabled + ? enabled.pipe( + Layer.catchCause((cause) => + Layer.effect( + KinServer, + config + .reportFailure(cause) + .pipe(Effect.as({ services: Option.none(), subscribeForViewer: Option.none() })), + ), + ), + ) + : disabled; + }), +).pipe( + Layer.provide(KinEnabled.layer), + Layer.catchCause((cause) => + Layer.effect( + KinServer, + Effect.logError("Kin disabled: invalid configuration.", Cause.pretty(cause)).pipe( + Effect.as({ services: Option.none(), subscribeForViewer: Option.none() }), + ), + ), + ), +); + +const middleware = HttpRouter.middleware<{ provides: KinServer }>()( + Effect.gen(function* () { + const kin = yield* KinServer; + return (effect) => Effect.provideService(effect, KinServer, kin); + }), +).layer; + +export const layer = middleware.pipe(Layer.provideMerge(runtime)); diff --git a/apps/server/src/kin/lead/KinEventQueue.test.ts b/apps/server/src/kin/lead/KinEventQueue.test.ts index 0c4f25ca81355..8e8465668fb18 100644 --- a/apps/server/src/kin/lead/KinEventQueue.test.ts +++ b/apps/server/src/kin/lead/KinEventQueue.test.ts @@ -203,6 +203,7 @@ describe("KinEventQueue", () => { ["lead-left", "owner", false, true], ["retired", "owner", false, false], ] as const) { + yield* sql`UPDATE kin_chat_members SET left_at = ${role === "owner" && !personLeft ? "2026-11-07T00:00:00.000Z" : null} WHERE member_id = 'group-owner'`; yield* sql`UPDATE kin_chat_members SET role = ${role}, left_at = ${personLeft ? "2026-11-07T00:00:00.000Z" : null} WHERE member_id = 'member-lead-a'`; yield* sql`UPDATE kin_chat_members SET left_at = ${agentLeft ? "2026-11-07T00:00:00.000Z" : null} @@ -213,6 +214,8 @@ describe("KinEventQueue", () => { } yield* sql`UPDATE kin_agents SET retired_at = NULL WHERE agent_id = 'lead-a'`; yield* sql`UPDATE kin_chat_members SET left_at = NULL WHERE member_id = 'agent-member-lead-a'`; + yield* sql`UPDATE kin_chat_members SET role = 'member' WHERE member_id IN ('member-lead-a', 'member-lead-a2')`; + yield* sql`UPDATE kin_chat_members SET left_at = NULL WHERE member_id = 'group-owner'`; yield* sql`INSERT INTO kin_chat_members (member_id, org_id, chat_id, principal_kind, person_id, role, joined_at) VALUES ('owner-other-chat', 'a', 'chat-lead-a2', 'person', 'person-lead-a', 'owner', '2026-11-07T00:00:00.000Z')`; for (const [id, chatId, memberId, personId] of [ diff --git a/apps/server/src/kin/persistence/KinMigrations.test.ts b/apps/server/src/kin/persistence/KinMigrations.test.ts index 8427941cde643..5e2ac3b487004 100644 --- a/apps/server/src/kin/persistence/KinMigrations.test.ts +++ b/apps/server/src/kin/persistence/KinMigrations.test.ts @@ -82,6 +82,7 @@ describe("Kin migrations", () => { { migration_id: 2, name: "Lead" }, { migration_id: 3, name: "Triggers" }, { migration_id: 4, name: "MemoryReads" }, + { migration_id: 5, name: "OneChatOwner" }, ]); assert.deepStrictEqual( upstreamBefore.map((row) => [row.migration_id, row.name] as const), @@ -125,6 +126,7 @@ describe("Kin migrations", () => { { migration_id: 2, name: "Lead" }, { migration_id: 3, name: "Triggers" }, { migration_id: 4, name: "MemoryReads" }, + { migration_id: 5, name: "OneChatOwner" }, ]); assert.deepStrictEqual(yield* upstreamHistory, upstreamBefore); assert.deepStrictEqual(yield* sql`SELECT * FROM kin_orgs`, [ @@ -189,6 +191,7 @@ describe("Kin migrations", () => { [2, "Lead"], [3, "Triggers"], [4, "MemoryReads"], + [5, "OneChatOwner"], ]); const before = yield* kinSchema; assert.deepStrictEqual( @@ -200,6 +203,19 @@ describe("Kin migrations", () => { [], ); yield* sql`INSERT INTO kin_orgs (org_id, name, created_at) VALUES ('org', 'Kin', ${timestamp})`; + yield* sql`INSERT INTO kin_people (person_id, org_id, handle, display_name, created_at) + VALUES ('owner', 'org', 'owner', 'Owner', ${timestamp})`; + yield* sql`INSERT INTO kin_chats (chat_id, org_id, kind, last_seq, created_at) + VALUES ('existing-chat', 'org', 'project_group', 0, ${timestamp})`; + yield* sql`INSERT INTO kin_chat_members (member_id, org_id, chat_id, principal_kind, person_id, role, joined_at) + VALUES ('existing-owner', 'org', 'existing-chat', 'person', 'owner', 'owner', ${timestamp})`; + yield* sql`DROP INDEX kin_chat_members_one_owner`; + yield* sql`DELETE FROM kin_sql_migrations WHERE migration_id = 5`; + assert.deepStrictEqual(yield* runKinMigrations(), [[5, "OneChatOwner"]]); + assert.deepStrictEqual( + yield* sql`SELECT member_id FROM kin_chat_members WHERE org_id = 'org' ORDER BY member_id`, + [{ member_id: "existing-owner" }], + ); yield* runMigrations(); assert.deepStrictEqual( (yield* upstreamHistory).map((row) => [row.migration_id, row.name] as const), @@ -211,6 +227,7 @@ describe("Kin migrations", () => { { migration_id: 2, name: "Lead" }, { migration_id: 3, name: "Triggers" }, { migration_id: 4, name: "MemoryReads" }, + { migration_id: 5, name: "OneChatOwner" }, ]); assert.deepStrictEqual(yield* runKinMigrations(), []); assert.deepStrictEqual(yield* runMigrations(), []); @@ -614,6 +631,11 @@ describe("Kin migrations", () => { "UNIQUE constraint failed: kin_messages.chat_id, kin_messages.author_member_id, kin_messages.client_message_id", ); const rejected = [ + { + name: "second owner", + query: sql`UPDATE kin_chat_members SET role = 'owner' WHERE member_id = 'member-person'`, + error: "UNIQUE constraint failed: kin_chat_members.org_id, kin_chat_members.chat_id", + }, { name: "direct chat without direct_key", query: sql` @@ -665,7 +687,7 @@ describe("Kin migrations", () => { ) VALUES ( ${`duplicate-${member.id}`}, 'org', 'team_group', ${member.kind}, ${member.kind === "person" ? member.id : null}, ${member.kind === "agent" ? member.id : null}, - ${member.role}, ${timestamp} + ${member.role === "owner" ? "member" : member.role}, ${timestamp} ) `); assert.isTrue(Exit.isFailure(duplicateMember)); diff --git a/apps/server/src/kin/persistence/KinMigrations.ts b/apps/server/src/kin/persistence/KinMigrations.ts index 4d6272492bffc..23c3373697ad9 100644 --- a/apps/server/src/kin/persistence/KinMigrations.ts +++ b/apps/server/src/kin/persistence/KinMigrations.ts @@ -5,6 +5,7 @@ import ChatCore from "./migrations/K001_ChatCore.ts"; import Lead from "./migrations/K002_Lead.ts"; import Triggers from "./migrations/K003_Triggers.ts"; import MemoryReads from "./migrations/K004_MemoryReads.ts"; +import OneChatOwner from "./migrations/K005_OneChatOwner.ts"; const run = Migrator.make({}); const loader = Migrator.fromRecord({ @@ -12,6 +13,7 @@ const loader = Migrator.fromRecord({ "2_Lead": Lead, "3_Triggers": Triggers, "4_MemoryReads": MemoryReads, + "5_OneChatOwner": OneChatOwner, }); export const runKinMigrations = Effect.fn("KinMigrations.run")(function* () { diff --git a/apps/server/src/kin/persistence/migrations/K005_OneChatOwner.ts b/apps/server/src/kin/persistence/migrations/K005_OneChatOwner.ts new file mode 100644 index 0000000000000..5af8d90ece03a --- /dev/null +++ b/apps/server/src/kin/persistence/migrations/K005_OneChatOwner.ts @@ -0,0 +1,8 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql`CREATE UNIQUE INDEX kin_chat_members_one_owner + ON kin_chat_members (org_id, chat_id) WHERE role = 'owner' AND left_at IS NULL`; +}); diff --git a/apps/server/src/kin/triggers/KinTriggerSink.test.ts b/apps/server/src/kin/triggers/KinTriggerSink.test.ts index 4120cc9aac88c..3bfd4f82e3dfe 100644 --- a/apps/server/src/kin/triggers/KinTriggerSink.test.ts +++ b/apps/server/src/kin/triggers/KinTriggerSink.test.ts @@ -12,6 +12,7 @@ import { OrchestrationV2ThreadProjectionJson, ScheduledTaskUpsertInput, } from "@t3tools/contracts"; +import * as ConfigProvider from "effect/ConfigProvider"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -84,7 +85,11 @@ const projection = (orgId: KinOrgId) => updatedAt: timestamp, }); -const database = KinPersistence.layer.pipe(Layer.provideMerge(Sqlite.layerMemory)); +const configured = ConfigProvider.layer(ConfigProvider.fromUnknown({ KIN_ENABLED: true })); +const database = KinPersistence.layer.pipe( + Layer.provideMerge(Sqlite.layerMemory), + Layer.provideMerge(configured), +); const seed = Effect.gen(function* () { yield* TestClock.setTime(1794009600000); const sql = yield* SqlClient.SqlClient; diff --git a/apps/server/src/kin/triggers/KinTriggerSink.ts b/apps/server/src/kin/triggers/KinTriggerSink.ts index ebd0f07bd2fa4..2e91c2d0e7c25 100644 --- a/apps/server/src/kin/triggers/KinTriggerSink.ts +++ b/apps/server/src/kin/triggers/KinTriggerSink.ts @@ -1,3 +1,4 @@ +import * as KinEnabled from "../KinEnabled.ts"; import { CanonicalUtcTimestamp, KinTriggerFireId, @@ -128,7 +129,7 @@ export function wrapTaskDispatch( inner: Layer.Layer, threads: Layer.Layer, ) { - return Layer.mergeAll( + const wrapped = Layer.mergeAll( Layer.effect( ThreadLaunchService.ThreadLaunchService, Effect.gen(function* () { @@ -183,4 +184,73 @@ export function wrapTaskDispatch( Layer.provide(Layer.mergeAll(inner, threads)), Layer.provide(Layer.mergeAll(layer, KinTriggerStore.layer).pipe(Layer.provide(threads))), ); + const degraded = Layer.mergeAll( + Layer.effect( + ThreadLaunchService.ThreadLaunchService, + Effect.gen(function* () { + const service = yield* ThreadLaunchService.ThreadLaunchService; + return ThreadLaunchService.ThreadLaunchService.of({ + ...service, + launch: Effect.fn("KinTriggerSink.degradedLaunch")(function* (input) { + if ( + input.initialMessage?.scheduledTaskId !== undefined && + input.commandId.startsWith( + `scheduled-task:${input.initialMessage.scheduledTaskId}:webhook:`, + ) + ) + return yield* new ThreadLaunchService.ThreadLaunchError({ + operation: "dispatch-message", + commandId: input.commandId, + projectId: input.projectId, + cause: new Error("Kin is unavailable; webhook dispatch refused."), + }); + return yield* service.launch(input); + }), + }); + }), + ), + Layer.effect( + ThreadManagementService.ThreadManagementService, + Effect.gen(function* () { + const service = yield* ThreadManagementService.ThreadManagementService; + return ThreadManagementService.ThreadManagementService.of({ + ...service, + sendToThread: Effect.fn("KinTriggerSink.degradedSend")(function* (input) { + if ( + input.scheduledTaskId !== undefined && + input.commandId.startsWith(`scheduled-task:${input.scheduledTaskId}:webhook:`) + ) + return yield* new OrchestratorCommandRejectedError({ + commandId: input.commandId, + commandType: "kin.trigger.unavailable", + cause: new Error("Kin is unavailable; webhook dispatch refused."), + }); + return yield* service.sendToThread(input); + }), + }); + }), + ), + ).pipe(Layer.provide(Layer.mergeAll(inner, threads))); + return Layer.unwrap( + KinEnabled.KinEnabled.pipe( + Effect.map( + ( + config, + ): Layer.Layer< + Layer.Success, + Layer.Error, + Layer.Services | R | R2 + > => + config.enabled + ? wrapped.pipe( + Layer.catchCause((cause) => + degraded.pipe( + Layer.provideMerge(Layer.effectDiscard(config.reportFailure(cause))), + ), + ), + ) + : Layer.mergeAll(inner, threads), + ), + ), + ).pipe(Layer.provide(KinEnabled.layer)); } diff --git a/apps/server/src/observability/RpcInstrumentation.ts b/apps/server/src/observability/RpcInstrumentation.ts index d19f83a932d1f..6508a3cbc36fc 100644 --- a/apps/server/src/observability/RpcInstrumentation.ts +++ b/apps/server/src/observability/RpcInstrumentation.ts @@ -1,3 +1,4 @@ +import { KIN_RPC_AGGREGATES } from "@t3tools/contracts"; import { ORCHESTRATION_V2_WS_METHODS, WS_METHODS, type WsRpcGroup } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -11,6 +12,7 @@ type WsRpcMethod = RpcGroup.Rpcs["_tag"]; // Trace queries and dashboards depend on these labels, including deviations from method prefixes. const RPC_AGGREGATES = { + ...KIN_RPC_AGGREGATES, [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: "orchestrationV2", [ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: "orchestration", [ORCHESTRATION_V2_WS_METHODS.getNativeChildWork]: "orchestration", diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index fe2740a8c7a2d..2d469cf5f9f69 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -1,3 +1,4 @@ +import * as Kin from "./kin/layer.ts"; import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers"; import type { RelayManagedEndpointRuntimeConfig } from "@t3tools/contracts/relay"; import * as Random from "effect/Random"; @@ -801,6 +802,7 @@ const layerMakeRoutes = Layer.mergeAll( Layer.provide(RemoteDelegation.layer.pipe(Layer.provide(ProjectionStoreV2.layer))), // Links agents ask for: shown in a thread from /mcp, answered over WebSocket. Layer.provide(PeerLinkRequests.layer), + Layer.provide(Kin.layer), // Share the service so HTTP/WS clients single-flight reads and invalidate each other's cached patches. Layer.provide(layerPullRequestService), // The same activity instance RunExecutionService reports to, by layer memoization. diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 1fa81ddc7026e..f3d1edf2d858d 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -1,3 +1,4 @@ +import { makeKinChatHandlers } from "./kin/chat/rpcHandlers.ts"; import { OrchestrationDispatchCommandError } from "@t3tools/contracts"; import * as Context from "effect/Context"; import * as SecureRandom from "effect/crypto/SecureRandom"; @@ -2040,6 +2041,7 @@ export const layerWsRpc = ( }); const handlers = ServerWsRpcGroup.of({ + ...(yield* makeKinChatHandlers(currentSession)), [ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command) => Effect.annotateCurrentSpan({ "orchestration_v2.command_id": command.commandId, diff --git a/docs/kin/specs/chat.md b/docs/kin/specs/chat.md index 56fe99904d1f2..04d2c0cf8761b 100644 --- a/docs/kin/specs/chat.md +++ b/docs/kin/specs/chat.md @@ -12,33 +12,35 @@ Phase 1 ships direct chats only: the owner and one lead. The schema already has All paths are on `kin` as of 7 Nov 2026 (merged with upstream `main`). -| Piece | Where | What it means for chats | -| ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- | -| SQLite client and startup migrations | `apps/server/src/persistence/Sqlite.ts` (`layerSetup` runs `runMigrations()`, `layerConfig`, `layerMemory`) | Kin tables live in the same database file. `layerMemory` serves tests. | -| Migration list | `apps/server/src/persistence/Migrations.ts` (`migrationEntries`, 80 entries, `assertV2MigrationsCanRun`) | One numbered sequence; the migrator skips every id up to the highest recorded one. Kin can't add entries here (see Design corrections). | -| Separate migration track | `effect/sql/Migrator` `MigratorOptions.table` (`apps/server/node_modules/effect/src/sql/Migrator.ts`, default `"effect_sql_migrations"`) | Kin runs its own migrator with `table: "kin_sql_migrations"`. | -| Repository pattern | `apps/server/src/persistence/PullRequestFilesViewed.ts` (`Context.Service`, `SqlSchema.findAll`, `PersistenceSqlError`/`PersistenceDecodeError` from `Errors.ts`) | Kin repositories copy this shape. | -| RPC definitions | `packages/contracts/src/rpc.ts`: `WS_METHODS`, `Rpc.make(...)`, `WsRpcGroup = RpcGroup.make(...).middleware(RpcScopeAuthorization)` | Kin RPCs join `WsRpcGroup` before `.middleware`, so the same scope check covers them. | -| Stream RPCs | `rpc.ts` `WsScheduledTasksSubscribeRpc` (`stream: true`); handler in `ws.ts` returns a `Stream` | The chat list and chat streams follow it. | -| Scope check | `apps/server/src/auth/RpcAuthorization.ts`: `RPC_REQUIRED_SCOPES ... satisfies Record`, `layer(session.scopes, ...)` | Every Kin method needs an entry or typecheck fails. | -| Handlers and session | `apps/server/src/ws.ts`: `layerWsRpc(currentSession: EnvironmentAuth.AuthenticatedSession, ...)`, `ServerWsRpcGroup.of({...})`, split into `SplitWsRpcGroup` / `OtherWsRpcGroup` | Kin handlers are built here because only here is the caller's session known. `kin.*` falls in `OtherWsRpcGroup`. | -| Session identity | `apps/server/src/auth/EnvironmentAuth.ts` `AuthenticatedSession { sessionId, subject, method, scopes, proofKeyThumbprint?, expiresAt? }` | No person or role. `subject` is a credential label such as `"browser"` or `"one-time-token"`. | -| Scopes | `packages/contracts/src/auth.ts` `AuthEnvironmentScope`, `AuthStandardClientScopes` (includes terminal, filesystem write, settings) | Every paired device is the environment owner. | -| Agent caller identity | `apps/server/src/mcp/McpInvocationContext.ts` `McpInvocationScope.thread: McpThreadCaller { threadId, providerSessionId, providerInstanceId, subagent? }` | The server knows which thread is calling an MCP tool. This is the agent author. | -| MCP tool registration | `apps/server/src/mcp/McpHttpServer.ts` `toolkitRegistration(...)`, `layerTools = Layer.mergeAll(...)`; budget test `apps/server/src/provider/AgentContextBudget.test.ts` with `agentContextBudget.json` | Kin's agent tools register here. Every advertised tool counts against the committed budget. | -| Delegated tasks | `delegate_task` in `apps/server/src/mcp/toolkits/orchestrator/tools.ts`; result `OrchestratorMcpDelegateTaskResult { taskId: NodeId, childThreadId, remoteChild?, link?, status, ... }` in `packages/contracts/src/orchestratorMcp.ts` | A task card stores `taskId` and `childThreadId`. | -| Thread state | `apps/server/src/orchestration-v2/Orchestrator.ts` `OrchestratorV2Shape.getThreadShell(threadId)`, `streamDomainEvents`; `OrchestrationV2ThreadShell` (`status`, `activityRunStatus`, `pullRequests`, `pendingRuntimeRequest`, `lastError`) in `packages/contracts/src/orchestrationV2.ts` | The task card updater reads these. | -| Thread links | `packages/shared/src/threadLinks.ts` `formatThreadLink`, `hostedThreadLink`, `THREAD_LINK_PROTOCOL = "t3-thread"` | A task card opens its thread with these. | -| Guest sharing | `packages/contracts/src/guestShare.ts` (`ThreadGuestShare`, `GUEST_MESSAGE_MAX_LENGTH`, `GUEST_PENDING_MESSAGE_LIMIT`, `NAME_BREAKING_CHARACTER` not exported), `apps/server/src/guest/GuestShareService.ts` (invite and session tokens signed with `auth/PayloadSigner.ts`, gated by `guestSharingEnabled`) | Bound to one thread and one browser guest. Kin reuses the suggest-only model, the limits and the signer, not the record. | -| MCP App bridge | `apps/mobile/src/features/threads/McpAppWebView.tsx` `ThreadMcpApp({ environmentId, threadId, conversationThreadId, itemId, app: McpAppReference, ... })`; `packages/contracts/src/mcpApps.ts` `McpAppTarget = { threadId, itemId }`; `McpAppReference` in `packages/shared/src/mcpApp.ts` | Generated widgets are bound to the thread turn item that made them. | +| Piece | Where | What it means for chats | +| ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- | +| SQLite client and startup migrations | `apps/server/src/persistence/Sqlite.ts` (`layerSetup` runs `runMigrations()`, `layerConfig`, `layerMemory`) | Kin tables live in the same database file. `layerMemory` serves tests. | +| Migration list | `apps/server/src/persistence/Migrations.ts` (`migrationEntries`, 80 entries, `assertV2MigrationsCanRun`) | One numbered sequence; the migrator skips every id up to the highest recorded one. Kin can't add entries here (see Design corrections). | +| Separate migration track | `effect/sql/Migrator` `MigratorOptions.table` (`apps/server/node_modules/effect/src/sql/Migrator.ts`, default `"effect_sql_migrations"`) | Kin runs its own migrator with `table: "kin_sql_migrations"`. | +| Repository pattern | `apps/server/src/persistence/PullRequestFilesViewed.ts` (`Context.Service`, `SqlSchema.findAll`, `PersistenceSqlError`/`PersistenceDecodeError` from `Errors.ts`) | Kin repositories copy this shape. | +| RPC definitions | `packages/contracts/src/rpc.ts`: `WS_METHODS`, `Rpc.make(...)`, `WsRpcGroup = RpcGroup.make(...).middleware(RpcScopeAuthorization)` | Kin RPCs join `WsRpcGroup` before `.middleware`, so the same scope check covers them. | +| Stream RPCs | `rpc.ts` `WsScheduledTasksSubscribeRpc` (`stream: true`); handler in `ws.ts` returns a `Stream` | The chat list and chat streams follow it. | +| Scope check | `packages/contracts/src/clientRpcPermissions.ts` | import; `...KIN_RPC_SCOPES,` in `CLIENT_GUARDED_RPC_SCOPES` | +| `apps/server/src/observability/RpcInstrumentation.ts` | import; `...KIN_RPC_AGGREGATES,` in the exhaustive aggregate map | +| `apps/server/src/auth/RpcAuthorization.ts`: `RPC_REQUIRED_SCOPES ... satisfies Record`, `layer(session.scopes, ...)` | Every Kin method needs an entry or typecheck fails. | +| Handlers and session | `apps/server/src/ws.ts`: `layerWsRpc(currentSession: EnvironmentAuth.AuthenticatedSession, ...)`, `ServerWsRpcGroup.of({...})`, split into `SplitWsRpcGroup` / `OtherWsRpcGroup` | Kin handlers are built here because only here is the caller's session known. `kin.*` falls in `OtherWsRpcGroup`. | +| Session identity | `apps/server/src/auth/EnvironmentAuth.ts` `AuthenticatedSession { sessionId, subject, method, scopes, proofKeyThumbprint?, expiresAt? }` | No person or role. `subject` is a credential label such as `"browser"` or `"one-time-token"`. | +| Scopes | `packages/contracts/src/auth.ts` `AuthEnvironmentScope`, `AuthStandardClientScopes` (includes terminal, filesystem write, settings) | Every paired device is the environment owner. | +| Agent caller identity | `apps/server/src/mcp/McpInvocationContext.ts` `McpInvocationScope.thread: McpThreadCaller { threadId, providerSessionId, providerInstanceId, subagent? }` | The server knows which thread is calling an MCP tool. This is the agent author. | +| MCP tool registration | `apps/server/src/mcp/McpHttpServer.ts` `toolkitRegistration(...)`, `layerTools = Layer.mergeAll(...)`; budget test `apps/server/src/provider/AgentContextBudget.test.ts` with `agentContextBudget.json` | Kin's agent tools register here. Every advertised tool counts against the committed budget. | +| Delegated tasks | `delegate_task` in `apps/server/src/mcp/toolkits/orchestrator/tools.ts`; result `OrchestratorMcpDelegateTaskResult { taskId: NodeId, childThreadId, remoteChild?, link?, status, ... }` in `packages/contracts/src/orchestratorMcp.ts` | A task card stores `taskId` and `childThreadId`. | +| Thread state | `apps/server/src/orchestration-v2/Orchestrator.ts` `OrchestratorV2Shape.getThreadShell(threadId)`, `streamDomainEvents`; `OrchestrationV2ThreadShell` (`status`, `activityRunStatus`, `pullRequests`, `pendingRuntimeRequest`, `lastError`) in `packages/contracts/src/orchestrationV2.ts` | The task card updater reads these. | +| Thread links | `packages/shared/src/threadLinks.ts` `formatThreadLink`, `hostedThreadLink`, `THREAD_LINK_PROTOCOL = "t3-thread"` | A task card opens its thread with these. | +| Guest sharing | `packages/contracts/src/guestShare.ts` (`ThreadGuestShare`, `GUEST_MESSAGE_MAX_LENGTH`, `GUEST_PENDING_MESSAGE_LIMIT`, `NAME_BREAKING_CHARACTER` not exported), `apps/server/src/guest/GuestShareService.ts` (invite and session tokens signed with `auth/PayloadSigner.ts`, gated by `guestSharingEnabled`) | Bound to one thread and one browser guest. Kin reuses the suggest-only model, the limits and the signer, not the record. | +| MCP App bridge | `apps/mobile/src/features/threads/McpAppWebView.tsx` `ThreadMcpApp({ environmentId, threadId, conversationThreadId, itemId, app: McpAppReference, ... })`; `packages/contracts/src/mcpApps.ts` `McpAppTarget = { threadId, itemId }`; `McpAppReference` in `packages/shared/src/mcpApp.ts` | Generated widgets are bound to the thread turn item that made them. | ## Design ### Organizations and identity -Every row carries `orgId`. Phase 1 has one org, created on first start. Bootstrap creates reserved server ids `kin-default` and `kin-default-owner` in one repository transaction, only when no org exists. The org primary key and guarded insert make simultaneous starts idempotent. Restart preserves the stored identity, even if configuration changes. Existing data without that reserved org and owner fails startup rather than guessing ownership from a person's handle or row order. +Every row carries `orgId`. Phase 1 has one org, created on first start. Bootstrap creates reserved server ids `kin-default` and `kin-default-owner` in one repository transaction, only when no org exists. The org primary key and guarded insert make simultaneous starts idempotent. Restart preserves the stored identity, even if configuration changes. Existing data without that reserved org and owner disables Kin and logs the actionable initialization cause once, without stopping T3, rather than guessing ownership from a person's handle or row order. -`KinIdentityConfig.layer` reads `KIN_ORG_NAME`, `KIN_OWNER_HANDLE` and `KIN_OWNER_DISPLAY_NAME`, defaulting to `Kin`, `owner` and `Owner`. CHAT-6 composes that configuration and `KinIdentity.layer` with the repository; CHAT-4 does not change server startup. +`KinIdentityConfig.layer` reads `KIN_ORG_NAME`, `KIN_OWNER_HANDLE` and `KIN_OWNER_DISPLAY_NAME`, defaulting to `Kin`, `owner` and `Owner`. CHAT-6 composes that configuration and `KinIdentity.layer` with the repository in the shared `kin/layer.ts` root. `KIN_ENABLED=true` opts in; an unset or false value creates no Kin tables, does not bootstrap identity, and passes scheduled launches and management calls through without Kin sink queries or logging. The shared `KinEnabled` layer reads this flag once per server runtime. `KIN_MEMORY_REPOSITORIES` only configures memory runners; it does not enable Kin. CHAT-4 does not change server startup. - **`KinOrg`**: `orgId`, `name`, `createdAt`. - **`KinPerson`**: a human inside one org. `personId`, `orgId`, `handle` (slug used as `said_by` in kin-memory), `displayName`, `githubLogin` (nullable). The same human in two orgs is two persons. That keeps the walls in 10a: nothing joins across orgs except the session binding. @@ -246,8 +248,8 @@ Flow: - a widget upsert with `awaiting` (one row per person; removed people resolve as `superseded`), - an agent message with `intent: "question"` that mentions a person, or any agent question in a direct chat, - a guest suggestion (for the owner), - - the approvals spec calling the server-only repository `addWaiting` inside its transaction for its approval cards, then publishing `waiting.updated` after commit. -- Public `KinChatService.addWaiting` refuses `reason: "approval"` and any row anchored on an approval widget (including through its message) with `not_allowed`; only the approvals spec creates these rows through the server-only repository entry point, never a transport. + - the approvals spec calling the server-only repository `addApprovalWaiting` inside its transaction for its approval cards, then publishing `waiting.updated` after commit. +- Public `KinChatService.addWaiting` refuses `reason: "approval"` and any row anchored on an approval widget (including through its message) with `not_allowed`; only the approvals spec creates these rows through the server-only repository entry point, which requires an approval widget, never a transport. - Agents may resolve only `question` rows on their own messages, never approval rows or approval-widget anchors, even with an owner role. Persons may resolve their own rows; chat owners may resolve any row. Nobody resolves approval rows or approval-widget anchors through the public service; only the approvals spec's signed-grant flow does. Guests cannot resolve rows. Refusals use `not_allowed`. - When a person leaves a chat, their open waiting rows in that chat resolve as `cancelled` in the same transaction; rejoining applies the requested role and does not reopen cancelled rows. - New waiting rows must be unresolved; a caller-supplied resolution is refused. @@ -334,12 +336,16 @@ export const KinChatStreamEvent = Schema.Union([ ``` - **Resume:** `kin.chat.subscribe` subscribes to the hub first, then reads messages after `afterSeq` (up to 200; more sends `reset`), sends `backlog`, then live events with `seq > backlog.lastSeq`. Clients drop duplicates by `seq`. Reconnecting means subscribing again with the last seq held. +- **List resume:** `kin.chats.subscribe` has no `afterSeq`: message sequences are per chat, not an org-wide event cursor. It subscribes first, buffers while reading one fresh chat/waiting snapshot, then emits changed summaries and personal waiting rows, dropping state already in that snapshot. Reconnection replaces the list with a new snapshot. A durable list-event cursor needs a separate journal and is not CHAT-6. +- **Removal:** a removed member receives their own resolved waiting rows even after membership ends. Their chat stream emits a final typed `member` event with non-null `leftAt`, then ends. The chat-list stream stays open; an event whose summary or waiting read fails is dropped and logged at debug level, not allowed to terminate the subscription. +- **Agent discovery:** `kin.orgs.list` includes each org's `agents: [{ agentId, displayName, retired }]`, filtered by org and ordered by display name then agent id. Use a non-retired agent id with `createDirect`. - **Filtering per viewer:** suggestions go only to their author and the chat owner. Agent subscribers do not exist on `/ws`; agents read through `kin_chat_read`. +- **Availability:** disabled or failed Kin initialization returns `KinChatError.reason = "unavailable"`, distinct from a role refusal. - **Hub:** `KinChatHub` (**new**), a `PubSub` keyed by `orgId`. The lead spec subscribes to `message.posted` from it to fill its per-org event queue. ### Authorisation, in two layers -1. **Scope (coarse):** `KIN_CHAT_RPC_SCOPES` in `kinChat.ts` maps each method to a scope; `RpcAuthorization.ts` spreads it into `RPC_REQUIRED_SCOPES`. Phase 1 uses existing scopes (table above). Phase 2 adds `kin:chat` and moves every `kin.*` method to it (CHAT-11). +1. **Scope (coarse):** `KIN_CHAT_RPC_SCOPES` in `kinChat.ts` maps each method to a scope; `clientRpcPermissions.ts` spreads the combined `KIN_RPC_SCOPES` into `CLIENT_GUARDED_RPC_SCOPES`, which already feeds `RPC_REQUIRED_SCOPES`. Phase 1 uses existing scopes (table above). Phase 2 adds `kin:chat` and moves every `kin.*` method to it (CHAT-11). 2. **Membership (fine):** inside `KinChatService`, every call resolves the person, loads the chat by id, and checks an active member row in the same org. A client-sent `orgId` must match the chat's. Role checks: only `owner` adds or removes members, sets roles, resolves suggestions, pins. `guest` can only `send` (stored as `suggested`), `markRead` and subscribe. Until CHAT-12 supplies the suggestion flow, CHAT-5 refuses guest writes rather than posting them as normal messages. Muted agents can't post. ### Agent tools (MCP) @@ -445,7 +451,7 @@ export const JUDGE_THRESHOLDS = { act: 0.8, ask: 0.5 } as const; // below ask: a - **Agent thread ends mid-post:** the registry row has `ended_at`; `resolveAgent` refuses it with `not_allowed`. The repository rechecks that row inside the post transaction, so an identity resolved before the end cannot post afterwards. - **Task thread deleted or archived:** the card shows `cancelled` with "Thread no longer available" and keeps its last state. - **Widget state fails its schema** after an upgrade: the server sends it with `invalid_state` replaced by a placeholder card, never raw state. -- **Kin migration fails:** server start fails with the migrator's error, the same as an upstream migration failure. Nothing is half-applied, because each migration runs in a transaction. +- **Kin initialization fails:** `Layer.catchCause` disables Kin on identity or migration failures, including defects, and logs one bounded message when configured. Ordinary T3 RPCs remain available. Each Kin migration still runs in a transaction; upstream migration failures retain upstream behavior. ## Files @@ -461,20 +467,19 @@ New (all Kin-owned): Upstream files touched, hooks only: -| File | Hook | -| --------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `packages/contracts/src/index.ts` | `export * from "./kinChat.ts";` | -| `packages/contracts/src/rpc.ts` | import `KinRpcs` from `kinRpcs.ts`; `...KinRpcs,` as the last argument of `RpcGroup.make(` in `WsRpcGroup` | -| `apps/server/src/auth/RpcAuthorization.ts` | import; `...KIN_RPC_SCOPES,` in `RPC_REQUIRED_SCOPES` | -| `apps/server/src/ws.ts` | import; `const kinChatHandlers = yield* makeKinChatHandlers(currentSession);` in `layerWsRpc`; `...kinChatHandlers,` in `ServerWsRpcGroup.of({` | -| `apps/server/src/server.ts` | import; `Layer.provide(KinChat.layer),` in the pipe after the routes `Layer.mergeAll` (the one with `PeerLinkRequests.layer`), so `Ws.layer` and `McpHttpServer.layer` both see it | -| `apps/server/src/mcp/McpHttpServer.ts` | import; `makeKinChatToolkitLayer(toolkitRegistration),` in `layerTools` | -| `packages/contracts/src/auth.ts` (phase 2, CHAT-11) | `export const AuthKinChatScope = "kin:chat" as const;` and add it to `AuthEnvironmentScope` and `AuthStandardClientScopes` | +| File | Hook | +| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `packages/contracts/src/index.ts` | `export * from "./kinChat.ts";` | +| `packages/contracts/src/rpc.ts` | import `KinRpcs` from `kinRpcs.ts`; `...KinRpcs,` as the last argument of `RpcGroup.make(` in `WsRpcGroup` | +| `apps/server/src/ws.ts` | import; `const kinChatHandlers = yield* makeKinChatHandlers(currentSession);` in `layerWsRpc`; `...kinChatHandlers,` in `ServerWsRpcGroup.of({` | +| `apps/server/src/server.ts` | import; provide the shared `Kin.layer` (including request middleware) after the routes `Layer.mergeAll`, so construction and request handlers share one root | +| `apps/server/src/mcp/McpHttpServer.ts` | import; `makeKinChatToolkitLayer(toolkitRegistration),` in `layerTools` | +| `packages/contracts/src/auth.ts` (phase 2, CHAT-11) | `export const AuthKinChatScope = "kin:chat" as const;` and add it to `AuthEnvironmentScope` and `AuthStandardClientScopes` | ## Interfaces to other specs - **Lead spec:** calls `KinIdentity.registerAgentThread` / `endAgentThread` around each lead session. Subscribes to `KinChatHub` for `message.posted` and `widget_action` to build `KinEvent`s. Uses the Kin MCP tools to post. Owns `KinAgent` config beyond identity, the `deferral` card state, levels per chat (its own table keyed by `chatId`), and the brief builder that quotes non-owner text. Receives `floor.offer` wake requests from the floor manager. -- **Approvals spec:** creates `approval` widgets server-side from `KinPendingAction` and creates approval waiting rows through the server-only repository `addWaiting` in its transaction, never the public chat service or a transport. It resolves rows through its signed-grant flow; agents cannot create or resolve them. Reads `KinMessage.author` and `authorRole` as provenance for ceilings. Its signed-grant RPC is the only way to approve; `kin.widgets.act` refuses approvals. Shows "Waiting on Nishchay" to members from `KinWidget.awaiting`. +- **Approvals spec:** creates `approval` widgets server-side from `KinPendingAction` and creates approval waiting rows through the server-only repository `addApprovalWaiting` in its transaction, never the public chat service or a transport. It resolves rows through its signed-grant flow; agents cannot create or resolve them. Reads `KinMessage.author` and `authorRole` as provenance for ceilings. Its signed-grant RPC is the only way to approve; `kin.widgets.act` refuses approvals. Shows "Waiting on Nishchay" to members from `KinWidget.awaiting`. - **Triggers spec:** deferral and routine posts arrive as lead notices; nothing here. Its upstream contract tests should cover what this spec depends on: `Migrator` `table` option, `WsRpcGroup` and `RPC_REQUIRED_SCOPES` shapes, `layerWsRpc` session argument, `McpInvocationScope.thread`, `toolkitRegistration`, `getThreadShell` and `streamDomainEvents`, `OrchestratorMcpDelegateTaskResult`. - **Mobile spec:** consumes `kin.*` RPCs and both streams through the normal WS client; draws native cards from `KinWidget.type` and `state`; renders generated widgets with `ThreadMcpApp`; opens task threads with `formatThreadLink`; folds runs by `floorRunId`; avatars from `KinAuthor._tag` (rule 7). @@ -518,7 +523,7 @@ Run with `cd apps/server && vp test run src/kin` and `cd packages/contracts && v **CHAT-5. Chat service and hub.** What: `KinChatService` (createDirect, send, history, markRead, widget upsert and act, addWaiting and resolveWaiting, membership and role checks) and `KinChatHub`. Files: `KinChatService.ts`, `KinChatHub.ts`, `widgets/registry.ts`, tests. Depends on: CHAT-4. Done when: author, isolation, widget, read state and waiting tests pass. Owner: backend. -**CHAT-6. WS RPCs and streams.** Both streams must use `KinChatHub.subscribeForViewer(identity)`, which checks current membership at delivery for every event and hides suggestions from everyone except their author and the chat owner; the org-wide subscription is for trusted server consumers only. What: `makeKinChatHandlers(session)`, both stream handlers with resume, `KinChat.layer`; `packages/contracts/src/kinRpcs.ts` exporting `KinRpcs` and `KIN_RPC_SCOPES`, which combine every Kin spec's RPCs and scopes (`KinChatRpcs` first; other specs add theirs here, never to upstream files); hooks in `rpc.ts`, `ws.ts`, `RpcAuthorization.ts`, `server.ts`. Files: `kinRpcs.ts`, `apps/server/src/kin/chat/rpcHandlers.ts`, `layer.ts`. Depends on: CHAT-5. Done when: a `ws.test.ts`-style test over a real socket sends, subscribes, resumes with `afterSeq`, and gets `not_found` across orgs; `vp run typecheck` shows no `any` in the handler layer requirements (see Design corrections 6). Owner: backend. Provide one shared `KinIdentity` instance for WS handlers and the `/kin/mcp` toolkit and test that it is shared. A Kin identity failure must disable Kin without stopping T3 startup. +**CHAT-6. WS RPCs and streams.** Both streams must use `KinChatHub.subscribeForViewer(identity)`, which checks current membership at delivery except for the viewer's own resolved waiting rows and terminal removal event and hides suggestions from everyone except their author and the chat owner; the unfiltered org subscription is module-private; trusted server consumers use viewer-bound subscriptions too. What: `makeKinChatHandlers(session)`, both stream handlers with resume, `KinChat.layer`; `packages/contracts/src/kinRpcs.ts` exporting `KinRpcs` and `KIN_RPC_SCOPES`, which combine every Kin spec's RPCs and scopes (`KinChatRpcs` first; other specs add theirs here, never to upstream files); hooks in `rpc.ts`, `ws.ts`, `clientRpcPermissions.ts`, `RpcInstrumentation.ts`, `server.ts`. Files: `kinRpcs.ts`, `apps/server/src/kin/chat/rpcHandlers.ts`, `layer.ts`. Depends on: CHAT-5. Done when: a `ws.test.ts`-style test over a real socket sends, subscribes, resumes with `afterSeq`, and gets `not_found` across orgs; `vp run typecheck` shows no `any` in the handler layer requirements (see Design corrections 6). Owner: backend. Provide one shared `KinIdentity` instance for WS handlers and the `/kin/mcp` toolkit and test that it is shared. A Kin identity failure must disable Kin without stopping T3 startup. **CHAT-7. Agent MCP tools.** What: `KinChatToolkit` with `kin_chat_post`, `kin_chat_read`, `kin_widget_upsert`; `EnabledWhen` from the registry; hook in `McpHttpServer.ts`. Files: `apps/server/src/kin/mcp/*`. Depends on: CHAT-5. Done when: agent-post tests pass and `AgentContextBudget.test.ts` passes unchanged. Owner: backend. Use the same shared `KinIdentity` instance as WS handlers and test that it is shared. @@ -541,7 +546,7 @@ Run with `cd apps/server && vp test run src/kin` and `cd packages/contracts && v 3. **The MCP App bridge is bound to a thread turn item,** not a chat message (`McpAppTarget = { threadId, itemId }`; `ThreadMcpApp` needs `itemId`). Generated widgets in chat must point at the lead thread's tool call, and they work only for the owner's sessions. Showing them to members needs a Kin bridge, decided in phase 2. 4. **Guest sharing can't be reused as chat guests.** It is one browser guest per thread over T3 Connect, for 24 hours, behind the `guestSharingEnabled` beta flag. Kin reuses its suggest-only model, its limits and `PayloadSigner`. `NAME_BREAKING_CHARACTER` isn't exported, so Kin copies it. 5. **Kin's MCP toolkit can't import `toolkitRegistration` from `McpHttpServer.ts`,** because `McpHttpServer.ts` would import it back and `layerTools` is built when the module loads. `McpHttpServer.ts` passes the function in instead. -6. **`ws.ts` is near a TypeScript limit.** Its comment says that near 200 methods the handler layer's requirements degrade to `any`, which is why it splits into two groups. Kin's 12 methods land in `OtherWsRpcGroup`. If typecheck shows the degradation, Kin needs a third group: three more lines in `ws.ts`. +6. **`ws.ts` is near a TypeScript limit.** Its comment says that near 200 methods the handler layer's requirements degrade to `any`, which is why it splits into two groups. Kin's phase-1 methods land in `OtherWsRpcGroup`. If typecheck shows the degradation, Kin needs a third group: three more lines in `ws.ts`. 7. **"Each agent may ask to speak" has a cost** the design doesn't mention. Agents only act inside a woken lead session, so every bid is a session. The floor manager decides who is woken; others are offered the floor only within budget. 8. **Rules 3 and 4 would block scheduled posts** in a direct chat (a morning brief after an unanswered deferral notice). The spec splits agent posts into replies, which the rules govern, and notices, which are rate-limited instead. 9. **"Jev from TypeSafe"** could not be checked from this repo. The spec stubs the `Judge` interface only. diff --git a/packages/contracts/src/clientRpcPermissions.ts b/packages/contracts/src/clientRpcPermissions.ts index caeee25295c6f..1ede97d492240 100644 --- a/packages/contracts/src/clientRpcPermissions.ts +++ b/packages/contracts/src/clientRpcPermissions.ts @@ -1,3 +1,4 @@ +import { KIN_RPC_SCOPES } from "./kinRpcs.ts"; import * as Schema from "effect/Schema"; import { GitPreparePullRequestThreadInput, GitRunStackedActionInput } from "./git.ts"; import { @@ -16,6 +17,7 @@ import { WS_METHODS } from "./rpc.ts"; /** Incremental client enforcement; the server still authorizes every request. */ export const CLIENT_GUARDED_RPC_SCOPES = { + ...KIN_RPC_SCOPES, [WS_METHODS.providerAuthRemoveMethod]: AuthProvidersManageScope, [WS_METHODS.previewClose]: AuthPreviewOperateScope, // Stopping a server signals its processes, as closing a terminal does. diff --git a/packages/contracts/src/index.ts b/packages/contracts/src/index.ts index c8624ab601a44..5dcb4ab545a8b 100644 --- a/packages/contracts/src/index.ts +++ b/packages/contracts/src/index.ts @@ -76,3 +76,5 @@ export * from "./clientRpcPermissions.ts"; export * from "./kinChat.ts"; export * from "./kinLead.ts"; export * from "./kinTriggers.ts"; + +export * from "./kinRpcs.ts"; diff --git a/packages/contracts/src/kinChat.test.ts b/packages/contracts/src/kinChat.test.ts index 0d7de8e5d3c94..3babf53e549cf 100644 --- a/packages/contracts/src/kinChat.test.ts +++ b/packages/contracts/src/kinChat.test.ts @@ -172,7 +172,21 @@ const samples = { { _tag: "read", memberId: "member-1", seq: 1 }, ], KinOrgsListInput: [{}], - KinOrgsListResult: [{ orgs: [org], me: [{ orgId: "org-1", personId: "person-1" }] }], + KinOrgsListResult: [ + { + orgs: [ + { + ...org, + agents: [ + { agentId: "agent-1", displayName: "Lead", retired: false }, + { agentId: "agent-retired", displayName: "Retired", retired: true }, + ], + }, + ], + me: [{ orgId: "org-1", personId: "person-1" }], + }, + { orgs: [{ ...org, agents: [] }], me: [] }, + ], KinChatsSubscribeInput: [{}, { orgId: "org-1" }], KinChatsCreateDirectInput: [{ orgId: "org-1", agentId: "agent-1" }], KinChatSubscribeInput: [{ chatId: "chat-1" }, { chatId: "chat-1", afterSeq: 0 }], @@ -194,6 +208,7 @@ const samples = { KinChatError: [ "not_found", "not_allowed", + "unavailable", "widget_changed", "use_approvals", "invalid_state", diff --git a/packages/contracts/src/kinChat.ts b/packages/contracts/src/kinChat.ts index dde749e818de3..a3eb12734d7e6 100644 --- a/packages/contracts/src/kinChat.ts +++ b/packages/contracts/src/kinChat.ts @@ -265,7 +265,18 @@ export type KinChatStreamEvent = typeof KinChatStreamEvent.Type; export const KinOrgsListInput = Schema.Struct({}); export type KinOrgsListInput = typeof KinOrgsListInput.Type; export const KinOrgsListResult = Schema.Struct({ - orgs: Schema.Array(KinOrg), + orgs: Schema.Array( + Schema.Struct({ + ...KinOrg.fields, + agents: Schema.Array( + Schema.Struct({ + agentId: KinAgentId, + displayName: Schema.String, + retired: Schema.Boolean, + }), + ), + }), + ), me: Schema.Array(Schema.Struct({ orgId: KinOrgId, personId: KinPersonId })), }); export type KinOrgsListResult = typeof KinOrgsListResult.Type; @@ -315,6 +326,7 @@ export class KinChatError extends Schema.TaggedError()("KinChatErr reason: Schema.Literals([ "not_found", "not_allowed", + "unavailable", "widget_changed", "use_approvals", "invalid_state", diff --git a/packages/contracts/src/kinRpcs.ts b/packages/contracts/src/kinRpcs.ts new file mode 100644 index 0000000000000..35c30dc63732c --- /dev/null +++ b/packages/contracts/src/kinRpcs.ts @@ -0,0 +1,15 @@ +import { KinChatRpcs, KIN_CHAT_RPC_SCOPES } from "./kinChat.ts"; + +export const KinRpcs = [...KinChatRpcs] as const; +export const KIN_RPC_SCOPES = { ...KIN_CHAT_RPC_SCOPES } as const; + +export const KIN_RPC_AGGREGATES = { + "kin.orgs.list": "kin-chat", + "kin.chats.subscribe": "kin-chat", + "kin.chats.createDirect": "kin-chat", + "kin.chat.subscribe": "kin-chat", + "kin.chat.history": "kin-chat", + "kin.messages.send": "kin-chat", + "kin.messages.markRead": "kin-chat", + "kin.widgets.act": "kin-chat", +} as const; diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index a9868d64a8110..595ff218b9228 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -1,3 +1,4 @@ +import { KinRpcs } from "./kinRpcs.ts"; import { OrchestrationV2SearchThreadError, OrchestrationV2SearchThreadInput, @@ -2008,6 +2009,7 @@ export class RpcScopeAuthorization extends RpcMiddleware.Service