From 33722b183f170ab8e16c724ef78edbaad5ad7ce7 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 09:24:07 +0530 Subject: [PATCH 1/2] fix(kin): resume offline chat changes and reconcile author sends --- apps/server/src/kin/chat/KinChatHub.ts | 6 + .../src/kin/chat/KinChatRepository.test.ts | 7 +- apps/server/src/kin/chat/KinChatRepository.ts | 76 +++++- .../src/kin/chat/KinChatService.test.ts | 9 +- apps/server/src/kin/chat/KinChatService.ts | 16 +- apps/server/src/kin/chat/messageView.ts | 18 ++ apps/server/src/kin/chat/rpcHandlers.test.ts | 228 +++++++++++++++++- apps/server/src/kin/chat/rpcHandlers.ts | 27 ++- .../server/src/kin/mcp/KinChatToolkit.test.ts | 2 +- .../src/kin/persistence/KinMigrations.test.ts | 33 +++ .../src/kin/persistence/KinMigrations.ts | 3 + .../migrations/K010_ChatChanges.ts | 32 +++ apps/server/src/kin/rpcHandlers.ts | 15 ++ apps/server/src/ws.ts | 4 +- docs/kin/specs/chat.md | 11 +- packages/contracts/src/kinChat.test.ts | 13 +- packages/contracts/src/kinChat.ts | 9 +- packages/contracts/src/kinRpcs.ts | 3 +- 18 files changed, 471 insertions(+), 41 deletions(-) create mode 100644 apps/server/src/kin/chat/messageView.ts create mode 100644 apps/server/src/kin/persistence/migrations/K010_ChatChanges.ts create mode 100644 apps/server/src/kin/rpcHandlers.ts diff --git a/apps/server/src/kin/chat/KinChatHub.ts b/apps/server/src/kin/chat/KinChatHub.ts index d93c98d10f8bb..ec5d5cce89105 100644 --- a/apps/server/src/kin/chat/KinChatHub.ts +++ b/apps/server/src/kin/chat/KinChatHub.ts @@ -13,6 +13,7 @@ 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 { messageForViewer } from "./messageView.ts"; import * as Repository from "./KinChatRepository.ts"; import type { AgentIdentity, PersonIdentity } from "./KinIdentity.ts"; import * as PubSub from "effect/PubSub"; @@ -128,6 +129,11 @@ const make = Effect.gen(function* () { ); }), ), + Stream.map((event) => + event.type === "message.posted" + ? { ...event, message: messageForViewer(event.message, identity) } + : event, + ), ); }), }); diff --git a/apps/server/src/kin/chat/KinChatRepository.test.ts b/apps/server/src/kin/chat/KinChatRepository.test.ts index 27e1c6b5652c2..c2a28b39fcd80 100644 --- a/apps/server/src/kin/chat/KinChatRepository.test.ts +++ b/apps/server/src/kin/chat/KinChatRepository.test.ts @@ -107,7 +107,7 @@ const message = (id: string, overrides: Partial> = {}) = editedAt: null, ...overrides, clientMessageId: { kind: "send", id }, - }) satisfies Omit & { + }) satisfies Omit & { caller: McpThreadCaller; clientMessageId: { kind: "send" | "act"; id: string }; }; @@ -355,7 +355,10 @@ describe("KinChatRepository", () => { ); } const first = yield* repo.insertMessage(message("first")); - assert.deepStrictEqual(first, decodeMessage({ ...message("first"), seq: 1 })); + assert.deepStrictEqual( + first, + decodeMessage({ ...message("first"), clientMessageId: "first", seq: 1, changeSeq: 1 }), + ); const repeated = yield* Effect.all( Array.from({ length: 5 }, (_, i) => repo.insertMessage({ diff --git a/apps/server/src/kin/chat/KinChatRepository.ts b/apps/server/src/kin/chat/KinChatRepository.ts index 19ab3853e59a9..c95a261731bb4 100644 --- a/apps/server/src/kin/chat/KinChatRepository.ts +++ b/apps/server/src/kin/chat/KinChatRepository.ts @@ -31,6 +31,8 @@ import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; +import * as SchemaTransformation from "effect/SchemaTransformation"; +import { messageForViewer } from "./messageView.ts"; import * as SqlClient from "effect/sql/SqlClient"; import * as SqlSchema from "effect/sql/SqlSchema"; import { @@ -59,7 +61,7 @@ export type RepositoryError = PersistenceSqlError | PersistenceDecodeError | Kin type Scope = { readonly orgId: KinOrgId }; type ChatScope = Scope & { readonly chatId: KinChatId }; type MemberScope = ChatScope & { readonly memberId: KinMemberId }; -type InsertMessage = Omit & { +type InsertMessage = Omit & { readonly caller?: McpThreadCaller | undefined; // Only the server-only task card creator may attribute a message outside an agent wake-up. readonly serverTaskCard?: true; @@ -119,10 +121,28 @@ const memberColumns = `org_id AS "orgId", chat_id AS "chatId", member_id AS "mem ELSE json_object('_tag', principal_kind, 'agentId', agent_id) END AS principal`; const MessageRow = Schema.Struct({ ...KinMessage.fields, + storedClientMessageId: Schema.NullOr(Schema.String), author: Schema.fromJsonString(KinMessage.fields.author), mentions: Schema.fromJsonString(KinMessage.fields.mentions), -}); -const messageColumns = `org_id AS "orgId", chat_id AS "chatId", message_id AS "messageId", seq AS "seq", author_role AS "authorRole", kind AS "kind", body AS "body", intent AS "intent", mentions_json AS "mentions", reply_to AS "replyTo", widget_id AS "widgetId", floor_run_id AS "floorRunId", status AS "status", sent_by AS "sentBy", created_at AS "createdAt", edited_at AS "editedAt", CASE author_kind WHEN 'human' THEN json_object('_tag', 'human', 'personId', author_person_id, 'memberId', author_member_id) +}).pipe( + Schema.decodeTo( + Schema.toType(KinMessage), + SchemaTransformation.transform({ + decode: ({ storedClientMessageId, ...message }) => ({ + ...message, + ...(storedClientMessageId?.startsWith("send:") + ? { clientMessageId: storedClientMessageId.slice(5) } + : {}), + }), + encode: (message) => ({ + ...message, + storedClientMessageId: + message.clientMessageId === undefined ? null : `send:${message.clientMessageId}`, + }), + }), + ), +); +const messageColumns = `org_id AS "orgId", chat_id AS "chatId", message_id AS "messageId", seq AS "seq", change_seq AS "changeSeq", client_message_id AS "storedClientMessageId", author_role AS "authorRole", kind AS "kind", body AS "body", intent AS "intent", mentions_json AS "mentions", reply_to AS "replyTo", widget_id AS "widgetId", floor_run_id AS "floorRunId", status AS "status", sent_by AS "sentBy", created_at AS "createdAt", edited_at AS "editedAt", CASE author_kind WHEN 'human' THEN json_object('_tag', 'human', 'personId', author_person_id, 'memberId', author_member_id) WHEN 'agent' THEN json_object('_tag', 'agent', 'agentId', author_agent_id, 'memberId', author_member_id, 'ownerPersonId', author_owner_person_id, 'threadId', author_thread_id) ELSE json_object('_tag', author_kind) END AS author`; @@ -231,11 +251,16 @@ export class KinChatRepository extends Context.Service< input: ChatScope & { readonly messageId: KinMessageId }, ) => Effect.Effect; readonly resume: ( - input: ChatScope & { readonly memberId: KinMemberId; readonly afterSeq: number }, + input: ChatScope & { + readonly memberId: KinMemberId; + readonly afterSeq: number; + readonly afterChangeSeq?: number | undefined; + }, ) => Effect.Effect< { readonly reset: boolean; readonly lastSeq: number; + readonly lastChangeSeq: number; readonly messages: ReadonlyArray; readonly widgets: ReadonlyArray; }, @@ -788,14 +813,29 @@ const make = Effect.gen(function* () { 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 counters = yield* sql<{ + change_seq: number; + }>`SELECT change_seq FROM kin_chats WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId}`; + const lastChangeSeq = counters[0]!.change_seq; + const afterChangeSeq = input.afterChangeSeq ?? input.afterSeq; + const visible = sql`(${member.role === "owner" ? 1 : 0} = 1 OR status NOT IN ('suggested', 'dismissed') OR author_member_id = ${member.memberId})`; 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`, + sql`SELECT ${sql.literal(messageColumns)} FROM kin_messages + WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND message_id IN ( + SELECT message_id FROM (SELECT message_id FROM kin_messages + WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND seq > ${input.afterSeq} AND ${visible} + ORDER BY seq LIMIT 201) + UNION + SELECT message_id FROM (SELECT message_id FROM kin_messages + WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND change_seq > ${afterChangeSeq} AND ${visible} + ORDER BY change_seq, seq LIMIT 201) + ) ORDER BY seq LIMIT 201`, })(input); if (messages.length > 200) - return { reset: true, lastSeq: chat.lastSeq, messages: [], widgets: [] }; + return { reset: true, lastSeq: chat.lastSeq, lastChangeSeq, messages: [], widgets: [] }; const widgets = messages.length === 0 ? [] @@ -805,7 +845,13 @@ const make = Effect.gen(function* () { 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 }; + return { + reset: false, + messages: messages.map((m) => messageForViewer(m, member.principal)), + widgets, + lastSeq: chat.lastSeq, + lastChangeSeq, + }; }, (effect) => sql.withTransaction(effect).pipe(NodeSqliteClient.readOnly), Effect.mapError(toRepositoryError("KinChatRepository.resume")), @@ -1256,7 +1302,10 @@ const make = Effect.gen(function* () { AND (${viewer === null || viewer.role === "owner" ? 1 : 0} = 1 OR status NOT IN ('suggested', 'dismissed') OR author_member_id = ${viewer?.memberId ?? null}) ORDER BY seq DESC LIMIT ${request.limit + 1}`, })(input); - const messages = rows.slice(0, request.limit).reverse(); + const messages = rows + .slice(0, request.limit) + .reverse() + .map((m) => messageForViewer(m, viewer?.principal)); const widgets = messages.length === 0 ? [] @@ -1277,6 +1326,12 @@ const make = Effect.gen(function* () { messagesAfter: Effect.fn("KinChatRepository.messagesAfter")( function* (input) { const request = yield* decodeResume(input); + const viewer = + input.viewerMemberId === undefined + ? undefined + : yield* requireRecord( + memberOne({ orgId: input.orgId, memberId: input.viewerMemberId }), + ); return yield* SqlSchema.findAll({ Request: Schema.Unknown, Result: MessageRow, @@ -1284,7 +1339,10 @@ const make = Effect.gen(function* () { () => sql`SELECT ${sql.literal(messageColumns)} FROM kin_messages WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND seq > ${request.afterSeq} AND (${input.viewerMemberId ?? null} IS NULL OR status NOT IN ('suggested', 'dismissed') OR author_member_id = ${input.viewerMemberId ?? null} OR EXISTS (SELECT 1 FROM kin_chat_members WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId} AND member_id = ${input.viewerMemberId ?? null} AND role = 'owner')) ORDER BY seq LIMIT ${request.limit}`, - })(input).pipe(NodeSqliteClient.readOnly); + })(input).pipe( + NodeSqliteClient.readOnly, + Effect.map((messages) => messages.map((m) => messageForViewer(m, viewer?.principal))), + ); }, Effect.mapError(toRepositoryError("KinChatRepository.messagesAfter")), ), diff --git a/apps/server/src/kin/chat/KinChatService.test.ts b/apps/server/src/kin/chat/KinChatService.test.ts index e35248d3d71e0..1a3fb26790b7c 100644 --- a/apps/server/src/kin/chat/KinChatService.test.ts +++ b/apps/server/src/kin/chat/KinChatService.test.ts @@ -221,10 +221,11 @@ describe("KinChatService", () => { service.send(person, { chatId: chat.chatId, clientMessageId: "blank", body: " " }), ]) assert.include(yield* Effect.flip(operation), { reason: "invalid_state" }); + const { clientMessageId: _postedId, ...postedForPerson } = posted; assert.deepStrictEqual( (yield* service.history(person, { chatId: chat.chatId, beforeSeq: 4, limit: 1 })) .messages, - [posted], + [postedForPerson], ); const widget = yield* service.createTaskCard({ orgId: person.orgId, @@ -877,7 +878,11 @@ describe("KinChatService", () => { for (const event of events) yield* hub.publish(event); assert.deepStrictEqual( yield* Stream.runCollect(viewer.pipe(Stream.take(events.length))), - events, + events.map((event) => { + if (event.type !== "message.posted") return event; + const { clientMessageId: _id, ...visible } = event.message; + return { ...event, message: visible }; + }), ); const agentViewer = yield* hub.subscribeForViewer(agent); const suggestion: Hub.KinChatHubEvent = { diff --git a/apps/server/src/kin/chat/KinChatService.ts b/apps/server/src/kin/chat/KinChatService.ts index 4c04ef40bca20..03cb68cf5dcfc 100644 --- a/apps/server/src/kin/chat/KinChatService.ts +++ b/apps/server/src/kin/chat/KinChatService.ts @@ -564,16 +564,21 @@ const make = 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 resumed = yield* repo.resume({ + ...member, + afterSeq: input.afterSeq ?? 0, + afterChangeSeq: input.afterChangeSeq, + }); const backlog: KinChatStreamEvent = resumed.reset - ? { _tag: "reset", lastSeq: resumed.lastSeq } + ? { _tag: "reset", lastSeq: resumed.lastSeq, lastChangeSeq: resumed.lastChangeSeq } : { _tag: "backlog", lastSeq: resumed.lastSeq, + lastChangeSeq: resumed.lastChangeSeq, messages: resumed.messages, widgets: resumed.widgets, }; - const lastHistorySeq = backlog.lastSeq; + const messageChanges = new Map(); const revisions = new Map( backlog._tag === "backlog" ? backlog.widgets.map((w) => [w.widgetId, w.revision]) : [], ); @@ -583,11 +588,14 @@ const make = Effect.gen(function* () { Stream.map((event) => { switch (event.type) { case "message.posted": { + const change = event.message.changeSeq ?? event.message.seq; if ( event.message.chatId !== input.chatId || - event.message.seq <= lastHistorySeq + change <= resumed.lastChangeSeq || + change <= (messageChanges.get(event.message.messageId) ?? 0) ) return Option.none(); + messageChanges.set(event.message.messageId, change); return Option.some({ _tag: "message", message: event.message, diff --git a/apps/server/src/kin/chat/messageView.ts b/apps/server/src/kin/chat/messageView.ts new file mode 100644 index 0000000000000..ff2c0161a605d --- /dev/null +++ b/apps/server/src/kin/chat/messageView.ts @@ -0,0 +1,18 @@ +import type { KinChatMember, KinMessage } from "@t3tools/contracts"; + +export function messageForViewer( + message: KinMessage, + viewer: KinChatMember["principal"] | undefined, +): KinMessage { + if ( + (viewer?._tag === "person" && + message.author._tag === "human" && + viewer.personId === message.author.personId) || + (viewer?._tag === "agent" && + message.author._tag === "agent" && + viewer.agentId === message.author.agentId) + ) + return message; + const { clientMessageId: _, ...visible } = message; + return visible; +} diff --git a/apps/server/src/kin/chat/rpcHandlers.test.ts b/apps/server/src/kin/chat/rpcHandlers.test.ts index ad3bd593c0b8e..55ee7320636de 100644 --- a/apps/server/src/kin/chat/rpcHandlers.test.ts +++ b/apps/server/src/kin/chat/rpcHandlers.test.ts @@ -51,7 +51,7 @@ 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"; +import { makeKinHandlers } from "../rpcHandlers.ts"; const decodeTaskState = Schema.decodeEffect(KinTaskCardState); @@ -115,7 +115,7 @@ it.effect("serves real WS sends, subscriptions and afterSeq resume with org isol assert.isFalse(toolConsumer(threadId)); yield* runnerConsumer({ ...person, agentId: KinAgentId.make("lead"), threadId }); assert.isTrue(toolConsumer(threadId)); - const handlers = yield* makeKinChatHandlers(session); + const handlers = yield* makeKinHandlers(session); const serverHandlers = group.toLayer({ ...handlers, "server.probe": () => Effect.succeed({}) }); const route = Layer.effectDiscard( Effect.gen(function* () { @@ -150,6 +150,7 @@ it.effect("serves real WS sends, subscriptions and afterSeq resume with org isol author: { _tag: "system" }, }; const first = yield* client["kin.messages.send"](forged); + assert.strictEqual(first.clientMessageId, "first"); assert.include(first.author, { _tag: "human", personId: person.personId }); const events = yield* Stream.toQueue( client["kin.chat.subscribe"]({ chatId: direct.chatId, afterSeq: 0 }), @@ -375,6 +376,7 @@ it.effect( 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: 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 }); @@ -402,7 +404,7 @@ it.effect("resets large backlogs and bounds a resume to the current snapshot", ( Option.getOrThrow( yield* Stream.runHead(chat.subscribe(person, { chatId: direct.chatId, afterSeq: 0 })), ), - { _tag: "reset", lastSeq: 201 }, + { _tag: "reset", lastSeq: 201, lastChangeSeq: 201 }, ); const resume = Option.getOrThrow( yield* Stream.runHead(chat.subscribe(person, { chatId: direct.chatId, afterSeq: 1 })), @@ -413,6 +415,20 @@ it.effect("resets large backlogs and bounds a resume to the current snapshot", ( assert.strictEqual(resume.messages[0]?.seq, 2); assert.strictEqual(resume.lastSeq, 201); } + const sql = yield* SqlClient.SqlClient; + yield* sql`UPDATE kin_messages SET body = 'Offline edit' WHERE org_id = ${person.orgId} AND chat_id = ${direct.chatId}`; + assert.deepStrictEqual( + Option.getOrThrow( + yield* Stream.runHead( + chat.subscribe(person, { + chatId: direct.chatId, + afterSeq: 201, + afterChangeSeq: 201, + }), + ), + ), + { _tag: "reset", lastSeq: 201, lastChangeSeq: 402 }, + ); }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), ); @@ -468,7 +484,7 @@ it.effect("subscribes before the list snapshot and drops buffered state already it.effect("denies settings-only sessions before Kin handlers execute", () => Effect.gen(function* () { - const handlers = yield* makeKinChatHandlers({ ...session, scopes: [AuthSettingsWriteScope] }); + const handlers = yield* makeKinHandlers({ ...session, scopes: [AuthSettingsWriteScope] }); const client = yield* RpcTest.makeClient(group).pipe( Effect.provide( Layer.merge( @@ -538,7 +554,7 @@ it.effect( () => Effect.gen(function* () { const { person, repo } = yield* seed; - const handlers = yield* makeKinChatHandlers(session); + const handlers = yield* makeKinHandlers(session); const client = yield* RpcTest.makeClient(group).pipe( Effect.provide( Layer.merge( @@ -571,7 +587,7 @@ it.effect( const kin = yield* Kin.KinServer; const services = Option.getOrThrow(kin.services); const identity = Context.get(services, KinIdentity); - const memberHandlers = yield* makeKinChatHandlers(session).pipe( + const memberHandlers = yield* makeKinHandlers(session).pipe( Effect.provideService(Kin.KinServer, { ...kin, services: Option.some( @@ -608,3 +624,203 @@ it.effect( ); }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), ); + +it.effect("replays offline edits, deletions and widget revisions once with afterSeq", () => + Effect.gen(function* () { + const { chat, repo, person, direct } = yield* seed; + const sql = yield* SqlClient.SqlClient; + const old = yield* chat.send(person, { + chatId: direct.chatId, + body: "Old", + clientMessageId: "old", + }); + const removed = yield* chat.send(person, { + chatId: direct.chatId, + body: "Remove", + clientMessageId: "remove", + }); + const latest = yield* chat.send(person, { + chatId: direct.chatId, + body: "Latest", + clientMessageId: "latest", + }); + const widgetId = KinWidgetId.make("offline-widget"); + yield* repo.insertWidget({ + ...person, + chatId: direct.chatId, + messageId: old.messageId, + widgetId, + widgetClass: "native", + type: "task", + state: {}, + source: null, + feed: null, + awaiting: [], + pinnedAt: null, + closedAt: null, + updatedAt: direct.createdAt, + }); + yield* sql`UPDATE kin_messages SET body = 'Edited', edited_at = '1970-01-01T00:00:01.000Z' WHERE org_id = ${person.orgId} AND message_id = ${old.messageId}`; + yield* sql`UPDATE kin_messages SET body = '', status = 'deleted' WHERE org_id = ${person.orgId} AND message_id = ${removed.messageId}`; + yield* sql`UPDATE kin_widgets SET revision = 2 WHERE org_id = ${person.orgId} AND widget_id = ${widgetId}`; + const event = Option.getOrThrow( + yield* Stream.runHead( + chat.subscribe(person, { chatId: direct.chatId, afterSeq: latest.seq }), + ), + ); + assert.strictEqual(event._tag, "backlog"); + if (event._tag !== "backlog") return; + assert.deepStrictEqual( + event.messages.map((m) => [m.messageId, m.body, m.status]), + [ + [old.messageId, "Edited", "posted"], + [removed.messageId, "", "deleted"], + ], + ); + assert.deepStrictEqual( + event.widgets.map((w) => [w.widgetId, w.revision]), + [[widgetId, 2]], + ); + assert.strictEqual(new Set(event.messages.map((m) => m.messageId)).size, event.messages.length); + assert.isDefined(event.lastChangeSeq); + const cursor = { + chatId: direct.chatId, + afterSeq: event.lastSeq, + afterChangeSeq: event.lastChangeSeq, + }; + const unchanged = Option.getOrThrow(yield* Stream.runHead(chat.subscribe(person, cursor))); + assert.include(unchanged, { _tag: "backlog" }); + if (unchanged._tag === "backlog") + assert.deepStrictEqual([unchanged.messages, unchanged.widgets], [[], []]); + yield* sql`UPDATE kin_widgets SET revision = 3 WHERE org_id = ${person.orgId} AND widget_id = ${widgetId}`; + const changed = Option.getOrThrow(yield* Stream.runHead(chat.subscribe(person, cursor))); + assert.strictEqual(changed._tag, "backlog"); + if (changed._tag === "backlog") { + assert.deepStrictEqual( + changed.messages.map((m) => m.messageId), + [old.messageId], + ); + assert.deepStrictEqual( + changed.widgets.map((w) => w.revision), + [3], + ); + } + }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), +); + +it.effect("maps a real persistence failure to retryable unavailable and preserves refusals", () => + Effect.gen(function* () { + const { direct } = yield* seed; + const handlers = yield* makeKinHandlers(session); + assert.include( + yield* handlers["kin.chat.history"]({ + chatId: KinChatId.make("missing"), + beforeSeq: 100, + limit: 10, + }).pipe(Effect.flip), + { reason: "not_found" }, + ); + const kin = yield* Kin.KinServer; + const services = Option.getOrThrow(kin.services); + const identity = Context.get(services, KinIdentity); + const broken = yield* makeKinHandlers(session).pipe( + Effect.provideService(Kin.KinServer, { + ...kin, + services: Option.some( + Context.add(services, KinIdentity, { + ...identity, + resolve: () => Effect.die("identity defect"), + }), + ), + }), + ); + assert.include(yield* broken["kin.orgs.list"]().pipe(Effect.flip), { reason: "unavailable" }); + assert.include( + yield* Stream.runHead(broken["kin.chat.subscribe"]({ chatId: direct.chatId })).pipe( + Effect.flip, + ), + { reason: "unavailable" }, + ); + const sql = yield* SqlClient.SqlClient; + yield* sql`DROP TABLE kin_widgets`; + yield* sql`DROP TABLE kin_messages`; + assert.include( + yield* handlers["kin.chat.history"]({ + chatId: direct.chatId, + beforeSeq: 100, + limit: 10, + }).pipe(Effect.flip), + { reason: "unavailable" }, + ); + }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), +); + +it.effect("exposes a send id only to its author in live, backlog, history and summaries", () => + Effect.gen(function* () { + const { chat, repo, person, direct } = yield* seed; + const other = { ...person, personId: KinPersonId.make("second-viewer") }; + yield* repo.insertPerson({ + ...other, + handle: "second", + displayName: "Second", + githubLogin: null, + createdAt: direct.createdAt, + }); + yield* repo.addMember({ + ...direct, + memberId: KinMemberId.make("second-member"), + principal: other, + role: "member", + joinedAt: direct.createdAt, + leftAt: null, + mutedUntil: null, + }); + const ownEvents = yield* Stream.toQueue(chat.subscribe(person, { chatId: direct.chatId }), { + capacity: "unbounded", + }); + const otherEvents = yield* Stream.toQueue(chat.subscribe(other, { chatId: direct.chatId }), { + capacity: "unbounded", + }); + yield* Queue.take(ownEvents); + yield* Queue.take(otherEvents); + const sent = yield* chat.send(person, { + chatId: direct.chatId, + body: "Hello", + clientMessageId: "pending-bubble", + }); + assert.strictEqual(sent.clientMessageId, "pending-bubble"); + for (const [viewer, events, expected] of [ + [person, ownEvents, "pending-bubble"], + [other, otherEvents, undefined], + ] as const) { + const live = yield* Queue.take(events); + assert.strictEqual(live._tag, "message"); + if (live._tag === "message") { + assert.strictEqual(live.message.clientMessageId, expected); + assert.strictEqual(Object.hasOwn(live.message, "clientMessageId"), expected !== undefined); + } + const history = yield* chat.history(viewer, { + chatId: direct.chatId, + beforeSeq: 100, + limit: 10, + }); + assert.strictEqual(history.messages[0]?.clientMessageId, expected); + const backlog = Option.getOrThrow( + yield* Stream.runHead(chat.subscribe(viewer, { chatId: direct.chatId, afterSeq: 0 })), + ); + if (backlog._tag !== "backlog") return assert.fail("Expected backlog"); + assert.strictEqual(backlog.messages[0]?.clientMessageId, expected); + const list = Option.getOrThrow(yield* Stream.runHead(chat.subscribeChats([viewer], {}))); + if (list._tag !== "snapshot") return assert.fail("Expected snapshot"); + assert.strictEqual(list.chats[0]?.lastMessage?.clientMessageId, expected); + } + assert.strictEqual( + (yield* chat.send(person, { + chatId: direct.chatId, + body: "Hello", + clientMessageId: "pending-bubble", + })).clientMessageId, + "pending-bubble", + ); + }).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 index 7479ee779cb74..77f35b2997c8e 100644 --- a/apps/server/src/kin/chat/rpcHandlers.ts +++ b/apps/server/src/kin/chat/rpcHandlers.ts @@ -1,4 +1,4 @@ -import { KinChatError, KinRpcs } from "@t3tools/contracts"; +import { KinChatError, KinChatAndBudgetRpcs } from "@t3tools/contracts"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; @@ -11,13 +11,10 @@ import { KinBudget } from "../lead/KinBudget.ts"; import { KinIdentity } from "./KinIdentity.ts"; import { KinChatService } from "./KinChatService.ts"; -const group = RpcGroup.make(...KinRpcs); +const group = RpcGroup.make(...KinChatAndBudgetRpcs); 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." }); +const mapError = (error: unknown) => (isKinChatError(error) ? error : unavailable); export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( session: AuthenticatedSession, @@ -44,6 +41,7 @@ export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( .pipe(Effect.flatMap((people) => budget.view(people, input.orgId))), ), Effect.mapError(mapError), + Effect.catchDefect(() => Effect.fail(unavailable)), ), "kin.budget.set": (input) => services.pipe( @@ -51,6 +49,7 @@ export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( identity.resolve(session).pipe(Effect.flatMap((people) => budget.set(people, input))), ), Effect.mapError(mapError), + Effect.catchDefect(() => Effect.fail(unavailable)), ), "kin.orgs.list": () => services.pipe( @@ -58,6 +57,7 @@ export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( identity.resolve(session).pipe(Effect.flatMap(chat.orgs)), ), Effect.mapError(mapError), + Effect.catchDefect(() => Effect.fail(unavailable)), ), "kin.chats.subscribe": (input) => Stream.unwrap( @@ -68,36 +68,47 @@ export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( .pipe(Effect.map((people) => chat.subscribeChats(people, input))), ), ), - ).pipe(Stream.mapError(mapError)), + ).pipe( + Stream.mapError(mapError), + Stream.catchDefect(() => Stream.fail(unavailable)), + ), "kin.chat.subscribe": (input) => Stream.unwrap( viewer.pipe(Effect.map(({ chat, people }) => chat.subscribe(people, input))), - ).pipe(Stream.mapError(mapError)), + ).pipe( + Stream.mapError(mapError), + Stream.catchDefect(() => Stream.fail(unavailable)), + ), "kin.chats.createDirect": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.createDirect(people, input)), Effect.mapError(mapError), + Effect.catchDefect(() => Effect.fail(unavailable)), ), "kin.chat.history": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.history(people, input)), Effect.mapError(mapError), + Effect.catchDefect(() => Effect.fail(unavailable)), ), "kin.messages.send": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.send(people, input)), Effect.mapError(mapError), + Effect.catchDefect(() => Effect.fail(unavailable)), ), "kin.messages.markRead": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.markRead(people, input)), Effect.as({}), Effect.mapError(mapError), + Effect.catchDefect(() => Effect.fail(unavailable)), ), "kin.widgets.act": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.act(people, input)), Effect.mapError(mapError), + Effect.catchDefect(() => Effect.fail(unavailable)), ), }); }); diff --git a/apps/server/src/kin/mcp/KinChatToolkit.test.ts b/apps/server/src/kin/mcp/KinChatToolkit.test.ts index fe1daae9d4361..942d565891d3a 100644 --- a/apps/server/src/kin/mcp/KinChatToolkit.test.ts +++ b/apps/server/src/kin/mcp/KinChatToolkit.test.ts @@ -334,7 +334,7 @@ it.effect("hides foreign chats with the same not_found error", () => Effect.gen(function* () { const { sql } = yield* seed(); yield* sql`INSERT INTO kin_orgs VALUES ('foreign', 'Foreign', '1970-01-01T00:00:00.000Z')`; - yield* sql`INSERT INTO kin_chats VALUES ('foreign-chat', 'foreign', 'team_group', NULL, NULL, NULL, 0, NULL, '1970-01-01T00:00:00.000Z', NULL)`; + yield* sql`INSERT INTO kin_chats (chat_id, org_id, kind, title, team_id, direct_key, last_seq, last_message_at, created_at, archived_at) VALUES ('foreign-chat', 'foreign', 'team_group', NULL, NULL, NULL, 0, NULL, '1970-01-01T00:00:00.000Z', NULL)`; for (const chatId of ["missing", "foreign-chat"]) { const result = yield* call("kin_chat_read", { chatId }); assert.include(errorOf(result), { reason: "not_found" }); diff --git a/apps/server/src/kin/persistence/KinMigrations.test.ts b/apps/server/src/kin/persistence/KinMigrations.test.ts index 1c67fefcdc3e2..01d5c377b555c 100644 --- a/apps/server/src/kin/persistence/KinMigrations.test.ts +++ b/apps/server/src/kin/persistence/KinMigrations.test.ts @@ -95,6 +95,7 @@ describe("Kin migrations", () => { { migration_id: 7, name: "Budget" }, { migration_id: 8, name: "Floor" }, { migration_id: 9, name: "JudgeLog" }, + { migration_id: 10, name: "ChatChanges" }, ]); assert.deepStrictEqual( upstreamBefore.map((row) => [row.migration_id, row.name] as const), @@ -146,6 +147,7 @@ describe("Kin migrations", () => { { migration_id: 7, name: "Budget" }, { migration_id: 8, name: "Floor" }, { migration_id: 9, name: "JudgeLog" }, + { migration_id: 10, name: "ChatChanges" }, ]); assert.deepStrictEqual(yield* upstreamHistory, upstreamBefore); assert.deepStrictEqual(yield* sql`SELECT * FROM kin_orgs`, [ @@ -170,6 +172,7 @@ describe("Kin migrations", () => { { name: "idx_kin_floor_latest" }, { name: "idx_kin_floor_notices_rate" }, { name: "idx_kin_floor_open" }, + { name: "idx_kin_messages_changes" }, { name: "idx_kin_people_org_person" }, { name: "idx_kin_ping_items_item" }, { name: "idx_kin_ping_items_pending" }, @@ -221,6 +224,7 @@ describe("Kin migrations", () => { [7, "Budget"], [8, "Floor"], [9, "JudgeLog"], + [10, "ChatChanges"], ]); const before = yield* kinSchema; assert.deepStrictEqual( @@ -238,6 +242,17 @@ describe("Kin migrations", () => { 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})`; + for (const trigger of [ + "kin_message_insert_change", + "kin_message_update_change", + "kin_widget_insert_change", + "kin_widget_update_change", + "kin_widget_delete_change", + ]) + yield* sql.unsafe(`DROP TRIGGER ${trigger}`); + yield* sql`DROP INDEX idx_kin_messages_changes`; + yield* sql`ALTER TABLE kin_messages DROP COLUMN change_seq`; + yield* sql`ALTER TABLE kin_chats DROP COLUMN change_seq`; yield* sql`DROP TABLE kin_judge_log`; yield* sql`DROP TABLE kin_floor_notices`; yield* sql`DROP TABLE kin_floor_grants`; @@ -248,6 +263,9 @@ describe("Kin migrations", () => { yield* sql`DROP INDEX kin_chat_members_one_owner`; yield* sql`DROP TABLE kin_budget_charges`; yield* sql`ALTER TABLE kin_events DROP COLUMN budget_deferred`; + yield* sql`UPDATE kin_chats SET last_seq = 1 WHERE org_id = 'org' AND chat_id = 'existing-chat'`; + yield* sql`INSERT INTO kin_messages (org_id, chat_id, message_id, seq, author_kind, author_role, kind, body, mentions_json, status, created_at) + VALUES ('org', 'existing-chat', 'existing-message', 1, 'system', 'member', 'system', 'Before upgrade', '[]', 'posted', ${timestamp})`; yield* sql`DELETE FROM kin_sql_migrations WHERE migration_id >= 5`; assert.deepStrictEqual(yield* runKinMigrations(), [ [5, "OneChatOwner"], @@ -255,11 +273,25 @@ describe("Kin migrations", () => { [7, "Budget"], [8, "Floor"], [9, "JudgeLog"], + [10, "ChatChanges"], ]); assert.deepStrictEqual( yield* sql`SELECT member_id FROM kin_chat_members WHERE org_id = 'org' ORDER BY member_id`, [{ member_id: "existing-owner" }], ); + assert.deepStrictEqual( + yield* sql`SELECT change_seq FROM kin_chats WHERE org_id = 'org' ORDER BY chat_id`, + [{ change_seq: 2 }], + ); + assert.deepStrictEqual( + yield* sql`SELECT change_seq FROM kin_messages WHERE org_id = 'org' ORDER BY seq`, + [{ change_seq: 2 }], + ); + yield* sql`UPDATE kin_messages SET body = 'After upgrade' WHERE org_id = 'org' AND message_id = 'existing-message'`; + assert.deepStrictEqual( + yield* sql`SELECT change_seq FROM kin_messages WHERE org_id = 'org' ORDER BY seq`, + [{ change_seq: 3 }], + ); yield* runMigrations(); assert.deepStrictEqual( (yield* upstreamHistory).map((row) => [row.migration_id, row.name] as const), @@ -276,6 +308,7 @@ describe("Kin migrations", () => { { migration_id: 7, name: "Budget" }, { migration_id: 8, name: "Floor" }, { migration_id: 9, name: "JudgeLog" }, + { migration_id: 10, name: "ChatChanges" }, ]); assert.deepStrictEqual(yield* runKinMigrations(), []); assert.deepStrictEqual(yield* runMigrations(), []); diff --git a/apps/server/src/kin/persistence/KinMigrations.ts b/apps/server/src/kin/persistence/KinMigrations.ts index d140ffa0b4398..ab0034ce1c786 100644 --- a/apps/server/src/kin/persistence/KinMigrations.ts +++ b/apps/server/src/kin/persistence/KinMigrations.ts @@ -12,6 +12,8 @@ import Floor from "./migrations/K008_Floor.ts"; import JudgeLog from "./migrations/K009_JudgeLog.ts"; +import ChatChanges from "./migrations/K010_ChatChanges.ts"; + const run = Migrator.make({}); const loader = Migrator.fromRecord({ "1_ChatCore": ChatCore, @@ -23,6 +25,7 @@ const loader = Migrator.fromRecord({ "7_Budget": Budget, "8_Floor": Floor, "9_JudgeLog": JudgeLog, + "10_ChatChanges": ChatChanges, }); export const runKinMigrations = Effect.fn("KinMigrations.run")(function* () { diff --git a/apps/server/src/kin/persistence/migrations/K010_ChatChanges.ts b/apps/server/src/kin/persistence/migrations/K010_ChatChanges.ts new file mode 100644 index 0000000000000..dab24a08f78e2 --- /dev/null +++ b/apps/server/src/kin/persistence/migrations/K010_ChatChanges.ts @@ -0,0 +1,32 @@ +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`ALTER TABLE kin_chats ADD COLUMN change_seq INTEGER NOT NULL DEFAULT 0`; + yield* sql`ALTER TABLE kin_messages ADD COLUMN change_seq INTEGER NOT NULL DEFAULT 0`; + // Old clients may have missed edits before this migration; replay existing rows once. + yield* sql`UPDATE kin_chats SET change_seq = last_seq + 1`; + yield* sql`UPDATE kin_messages SET change_seq = (SELECT change_seq FROM kin_chats WHERE kin_chats.org_id = kin_messages.org_id AND kin_chats.chat_id = kin_messages.chat_id)`; + yield* sql`CREATE INDEX idx_kin_messages_changes ON kin_messages(org_id, chat_id, change_seq, seq)`; + yield* sql`CREATE TRIGGER kin_message_insert_change AFTER INSERT ON kin_messages BEGIN + UPDATE kin_chats SET change_seq = change_seq + 1 WHERE org_id = NEW.org_id AND chat_id = NEW.chat_id; + UPDATE kin_messages SET change_seq = (SELECT change_seq FROM kin_chats WHERE org_id = NEW.org_id AND chat_id = NEW.chat_id) + WHERE org_id = NEW.org_id AND message_id = NEW.message_id; + END`; + // The internal watermark update must not recursively allocate another change. + yield* sql`CREATE TRIGGER kin_message_update_change AFTER UPDATE ON kin_messages + WHEN NEW.change_seq = OLD.change_seq BEGIN + UPDATE kin_chats SET change_seq = change_seq + 1 WHERE org_id = NEW.org_id AND chat_id = NEW.chat_id; + UPDATE kin_messages SET change_seq = (SELECT change_seq FROM kin_chats WHERE org_id = NEW.org_id AND chat_id = NEW.chat_id) + WHERE org_id = NEW.org_id AND message_id = NEW.message_id; + END`; + for (const event of ["INSERT", "UPDATE", "DELETE"] as const) { + const row = event === "DELETE" ? "OLD" : "NEW"; + yield* sql.unsafe(`CREATE TRIGGER kin_widget_${event.toLowerCase()}_change AFTER ${event} ON kin_widgets BEGIN + UPDATE kin_chats SET change_seq = change_seq + 1 WHERE org_id = ${row}.org_id AND chat_id = ${row}.chat_id; + UPDATE kin_messages SET change_seq = (SELECT change_seq FROM kin_chats WHERE org_id = ${row}.org_id AND chat_id = ${row}.chat_id) + WHERE org_id = ${row}.org_id AND message_id = ${row}.message_id; + END`); + } +}); diff --git a/apps/server/src/kin/rpcHandlers.ts b/apps/server/src/kin/rpcHandlers.ts new file mode 100644 index 0000000000000..1cf89ddebea30 --- /dev/null +++ b/apps/server/src/kin/rpcHandlers.ts @@ -0,0 +1,15 @@ +import { KinRpcs } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as RpcGroup from "effect/rpc/RpcGroup"; +import type { AuthenticatedSession } from "../auth/EnvironmentAuth.ts"; +import { makeKinChatHandlers } from "./chat/rpcHandlers.ts"; + +const group = RpcGroup.make(...KinRpcs); + +export const makeKinHandlers = Effect.fn("makeKinHandlers")(function* ( + session: AuthenticatedSession, +) { + return group.of({ + ...(yield* makeKinChatHandlers(session)), + }); +}); diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index f3d1edf2d858d..bbab4fe72da3e 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -1,4 +1,4 @@ -import { makeKinChatHandlers } from "./kin/chat/rpcHandlers.ts"; +import { makeKinHandlers } from "./kin/rpcHandlers.ts"; import { OrchestrationDispatchCommandError } from "@t3tools/contracts"; import * as Context from "effect/Context"; import * as SecureRandom from "effect/crypto/SecureRandom"; @@ -2041,7 +2041,7 @@ export const layerWsRpc = ( }); const handlers = ServerWsRpcGroup.of({ - ...(yield* makeKinChatHandlers(currentSession)), + ...(yield* makeKinHandlers(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 d11675c9871d8..5cf13e7f9324c 100644 --- a/docs/kin/specs/chat.md +++ b/docs/kin/specs/chat.md @@ -307,7 +307,7 @@ Method names live in `KIN_CHAT_WS_METHODS` in `kinChat.ts`. `kinChat.ts` must no | `kin.orgs.list` | `{}` | `{ orgs: KinOrg[], me: { orgId, personId }[] }` | 1 | `orchestration:read` | | `kin.chats.subscribe` (stream) | `{ orgId?: KinOrgId }` | `KinChatListEvent` | 1 | `orchestration:read` | | `kin.chats.createDirect` | `{ orgId, agentId }` | `KinChat` | 1 | `orchestration:operate` | -| `kin.chat.subscribe` (stream) | `{ chatId, afterSeq?: number }` | `KinChatStreamEvent` | 1 | `orchestration:read` | +| `kin.chat.subscribe` (stream) | `{ chatId, afterSeq?: number, afterChangeSeq?: number }` | `KinChatStreamEvent` | 1 | `orchestration:read` | | `kin.chat.history` | `{ chatId, beforeSeq, limit ≤ 100 }` | `{ messages, widgets, hasMore }` | 1 | `orchestration:read` | | `kin.messages.send` | `{ chatId, clientMessageId, body, mentions?, replyTo? }` | `KinMessage` | 1 | `orchestration:operate` | | `kin.messages.markRead` | `{ chatId, seq }` | `{}` | 1 | `orchestration:read` | @@ -337,8 +337,12 @@ export const KinChatStreamEvent = Schema.Union([ messages: Schema.Array(KinMessage), widgets: Schema.Array(KinWidget), lastSeq: NonNegativeInt, + lastChangeSeq: Schema.optional(NonNegativeInt), }), - Schema.TaggedStruct("reset", { lastSeq: NonNegativeInt }), // gap too big: refetch with history + Schema.TaggedStruct("reset", { + lastSeq: NonNegativeInt, + lastChangeSeq: Schema.optional(NonNegativeInt), + }), // gap too big: refetch with history Schema.TaggedStruct("message", { message: KinMessage }), // new or changed (edit, delete, send of a suggestion) Schema.TaggedStruct("widget", { widget: KinWidget }), Schema.TaggedStruct("member", { member: KinChatMember }), @@ -346,7 +350,8 @@ 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. +- **Resume:** `kin.chat.subscribe` subscribes to the hub first, then reads one SQLite snapshot. `seq` remains the message's immutable ordering key. K010 adds a per-chat integer `change_seq`: message inserts/updates and widget writes advance it transactionally; widget writes mark their parent message changed. The indexed backlog selects the union of new messages (`seq > afterSeq`) and changed messages (`change_seq > afterChangeSeq`), once per message, with current associated widgets and normal viewer filtering. More than 200 affected visible messages sends `reset` instead. Both `backlog` and `reset` include `lastSeq` and `lastChangeSeq` from that snapshot. The migration marks all existing messages changed beyond `lastSeq`, so a client offline across the upgrade cannot miss an earlier edit. +- **Client resume contract:** upsert messages by `messageId` (do not discard an older `seq`) and widgets by `widgetId`/`revision`. Persist `backlog.lastChangeSeq` only after applying the whole backlog; send it as optional `afterChangeSeq` with the last message `afterSeq` on reconnect. Keep this snapshot cursor unchanged while receiving live events: their delivery order need not match commit order, so taking the maximum live `changeSeq` could skip another committed event. Replaying already-held rows across reconnects is intentional and idempotent. `reset` invalidates the entire cached chat, not just the latest page; refetch with history and resume from the reset cursors. Legacy clients omitting `afterChangeSeq` use `afterSeq` as a conservative lower bound, which can replay more rows or reset more often but cannot miss an offline edit. Clients must treat a backlog as upserts, not append-only. Live message events carry optional `changeSeq`; the server drops buffered changes already represented by the snapshot and repeated versions. These optional fields preserve older payload decoding. `clientMessageId` is an optional unprefixed send id, present only for the message's author in send replies, history, backlogs, live events and chat summaries; owning the chat does not reveal another author's id. - **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`. diff --git a/packages/contracts/src/kinChat.test.ts b/packages/contracts/src/kinChat.test.ts index e30a55496cd6c..f60d3b56a3f89 100644 --- a/packages/contracts/src/kinChat.test.ts +++ b/packages/contracts/src/kinChat.test.ts @@ -146,7 +146,10 @@ const samples = { KinPrincipal: principals, KinChatMember: [member], KinAuthor: authors, - KinMessage: authors.map((author) => ({ ...message, author })), + KinMessage: [ + ...authors.map((author) => ({ ...message, author })), + { ...message, changeSeq: 2, clientMessageId: "pending" }, + ], KinWidget: [ widget, { ...widget, widgetClass: "generated", source: { threadId: "thread-1", itemId: "item-1" } }, @@ -166,6 +169,8 @@ const samples = { KinChatStreamEvent: [ { _tag: "backlog", messages: [message], widgets: [widget], lastSeq: 1 }, { _tag: "reset", lastSeq: 1 }, + { _tag: "backlog", messages: [message], widgets: [widget], lastSeq: 1, lastChangeSeq: 2 }, + { _tag: "reset", lastSeq: 1, lastChangeSeq: 2 }, { _tag: "message", message }, { _tag: "widget", widget }, { _tag: "member", member }, @@ -189,7 +194,11 @@ const samples = { ], KinChatsSubscribeInput: [{}, { orgId: "org-1" }], KinChatsCreateDirectInput: [{ orgId: "org-1", agentId: "agent-1" }], - KinChatSubscribeInput: [{ chatId: "chat-1" }, { chatId: "chat-1", afterSeq: 0 }], + KinChatSubscribeInput: [ + { chatId: "chat-1" }, + { chatId: "chat-1", afterSeq: 0 }, + { chatId: "chat-1", afterSeq: 1, afterChangeSeq: 2 }, + ], KinChatHistoryInput: [{ chatId: "chat-1", beforeSeq: 2, limit: 100 }], KinChatHistoryResult: [{ messages: [message], widgets: [widget], hasMore: false }], KinMessagesSendInput: [send, { ...send, mentions: ["member-2"], replyTo: "message-1" }], diff --git a/packages/contracts/src/kinChat.ts b/packages/contracts/src/kinChat.ts index 7804928709471..ee985d2b45bac 100644 --- a/packages/contracts/src/kinChat.ts +++ b/packages/contracts/src/kinChat.ts @@ -146,6 +146,8 @@ export const KinMessage = Schema.Struct({ chatId: KinChatId, messageId: KinMessageId, seq: PositiveInt, + changeSeq: Schema.optional(NonNegativeInt), + clientMessageId: Schema.optional(TrimmedNonEmptyString), author: KinAuthor, authorRole: KinRole, kind: KinMessageKind, @@ -260,8 +262,12 @@ export const KinChatStreamEvent = Schema.Union([ messages: Schema.Array(KinMessage), widgets: Schema.Array(KinWidget), lastSeq: NonNegativeInt, + lastChangeSeq: Schema.optional(NonNegativeInt), + }), + Schema.TaggedStruct("reset", { + lastSeq: NonNegativeInt, + lastChangeSeq: Schema.optional(NonNegativeInt), }), - Schema.TaggedStruct("reset", { lastSeq: NonNegativeInt }), Schema.TaggedStruct("message", { message: KinMessage }), Schema.TaggedStruct("widget", { widget: KinWidget }), Schema.TaggedStruct("member", { member: KinChatMember }), @@ -294,6 +300,7 @@ export type KinChatsCreateDirectInput = typeof KinChatsCreateDirectInput.Type; export const KinChatSubscribeInput = Schema.Struct({ chatId: KinChatId, afterSeq: Schema.optional(NonNegativeInt), + afterChangeSeq: Schema.optional(NonNegativeInt), }); export type KinChatSubscribeInput = typeof KinChatSubscribeInput.Type; export const KinChatHistoryInput = Schema.Struct({ diff --git a/packages/contracts/src/kinRpcs.ts b/packages/contracts/src/kinRpcs.ts index 01aee69c132b2..f9aaf7228d157 100644 --- a/packages/contracts/src/kinRpcs.ts +++ b/packages/contracts/src/kinRpcs.ts @@ -20,7 +20,8 @@ const KinBudgetRpcs = [ error: Schema.Union([KinChatError, EnvironmentAuthorizationError]), }), ] as const; -export const KinRpcs = [...KinChatRpcs, ...KinBudgetRpcs] as const; +export const KinChatAndBudgetRpcs = [...KinChatRpcs, ...KinBudgetRpcs] as const; +export const KinRpcs = [...KinChatAndBudgetRpcs] as const; export const KIN_RPC_SCOPES = { ...KIN_CHAT_RPC_SCOPES, "kin.budget.get": AuthOrchestrationReadScope, From d61b6761fa6fe95c1aaaba8e249fc342c8ef73dc Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 09:53:17 +0530 Subject: [PATCH 2/2] fix(kin): resume replays only new messages without a change cursor; log RPC infra failures Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/chat/KinChatRepository.ts | 3 +- apps/server/src/kin/chat/rpcHandlers.test.ts | 21 +++++++++++- apps/server/src/kin/chat/rpcHandlers.ts | 34 +++++++++++++------ docs/kin/specs/chat.md | 4 +-- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/apps/server/src/kin/chat/KinChatRepository.ts b/apps/server/src/kin/chat/KinChatRepository.ts index c95a261731bb4..659fcf472d33e 100644 --- a/apps/server/src/kin/chat/KinChatRepository.ts +++ b/apps/server/src/kin/chat/KinChatRepository.ts @@ -817,7 +817,8 @@ const make = Effect.gen(function* () { change_seq: number; }>`SELECT change_seq FROM kin_chats WHERE org_id = ${input.orgId} AND chat_id = ${input.chatId}`; const lastChangeSeq = counters[0]!.change_seq; - const afterChangeSeq = input.afterChangeSeq ?? input.afterSeq; + // Without a change cursor, replay only new messages; change_seq runs ahead of seq. + const afterChangeSeq = input.afterChangeSeq ?? lastChangeSeq; const visible = sql`(${member.role === "owner" ? 1 : 0} = 1 OR status NOT IN ('suggested', 'dismissed') OR author_member_id = ${member.memberId})`; const messages = yield* SqlSchema.findAll({ Request: Schema.Unknown, diff --git a/apps/server/src/kin/chat/rpcHandlers.test.ts b/apps/server/src/kin/chat/rpcHandlers.test.ts index 55ee7320636de..da29d1c7ffb03 100644 --- a/apps/server/src/kin/chat/rpcHandlers.test.ts +++ b/apps/server/src/kin/chat/rpcHandlers.test.ts @@ -625,7 +625,7 @@ it.effect( }).pipe(Effect.provide(root.pipe(Layer.provideMerge(Sqlite.layerMemory)))), ); -it.effect("replays offline edits, deletions and widget revisions once with afterSeq", () => +it.effect("replays offline edits, deletions and widget revisions once with afterChangeSeq", () => Effect.gen(function* () { const { chat, repo, person, direct } = yield* seed; const sql = yield* SqlClient.SqlClient; @@ -660,14 +660,33 @@ it.effect("replays offline edits, deletions and widget revisions once with after closedAt: null, updatedAt: direct.createdAt, }); + const baseline = Option.getOrThrow( + yield* Stream.runHead( + chat.subscribe(person, { chatId: direct.chatId, afterSeq: latest.seq }), + ), + ); + assert.strictEqual(baseline._tag, "backlog"); + if (baseline._tag !== "backlog") return; yield* sql`UPDATE kin_messages SET body = 'Edited', edited_at = '1970-01-01T00:00:01.000Z' WHERE org_id = ${person.orgId} AND message_id = ${old.messageId}`; yield* sql`UPDATE kin_messages SET body = '', status = 'deleted' WHERE org_id = ${person.orgId} AND message_id = ${removed.messageId}`; yield* sql`UPDATE kin_widgets SET revision = 2 WHERE org_id = ${person.orgId} AND widget_id = ${widgetId}`; const event = Option.getOrThrow( + yield* Stream.runHead( + chat.subscribe(person, { + chatId: direct.chatId, + afterSeq: latest.seq, + afterChangeSeq: baseline.lastChangeSeq, + }), + ), + ); + const legacy = Option.getOrThrow( yield* Stream.runHead( chat.subscribe(person, { chatId: direct.chatId, afterSeq: latest.seq }), ), ); + assert.include(legacy, { _tag: "backlog" }); + if (legacy._tag === "backlog") + assert.deepStrictEqual([legacy.messages, legacy.widgets], [[], []]); assert.strictEqual(event._tag, "backlog"); if (event._tag !== "backlog") return; assert.deepStrictEqual( diff --git a/apps/server/src/kin/chat/rpcHandlers.ts b/apps/server/src/kin/chat/rpcHandlers.ts index 77f35b2997c8e..adfb83f5c1ac6 100644 --- a/apps/server/src/kin/chat/rpcHandlers.ts +++ b/apps/server/src/kin/chat/rpcHandlers.ts @@ -15,6 +15,10 @@ const group = RpcGroup.make(...KinChatAndBudgetRpcs); const unavailable = new KinChatError({ reason: "unavailable", message: "Kin is unavailable." }); const isKinChatError = Schema.is(KinChatError); const mapError = (error: unknown) => (isKinChatError(error) ? error : unavailable); +const logInfra = (error: unknown) => + isKinChatError(error) ? Effect.void : Effect.logWarning("Kin RPC failed.", error); +const defect = (cause: unknown) => + Effect.logWarning("Kin RPC defect.", cause).pipe(Effect.andThen(Effect.fail(unavailable))); export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( session: AuthenticatedSession, @@ -40,24 +44,27 @@ export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( .resolve(session) .pipe(Effect.flatMap((people) => budget.view(people, input.orgId))), ), + Effect.tapError(logInfra), Effect.mapError(mapError), - Effect.catchDefect(() => Effect.fail(unavailable)), + Effect.catchDefect(defect), ), "kin.budget.set": (input) => services.pipe( Effect.flatMap(({ identity, budget }) => identity.resolve(session).pipe(Effect.flatMap((people) => budget.set(people, input))), ), + Effect.tapError(logInfra), Effect.mapError(mapError), - Effect.catchDefect(() => Effect.fail(unavailable)), + Effect.catchDefect(defect), ), "kin.orgs.list": () => services.pipe( Effect.flatMap(({ identity, chat }) => identity.resolve(session).pipe(Effect.flatMap(chat.orgs)), ), + Effect.tapError(logInfra), Effect.mapError(mapError), - Effect.catchDefect(() => Effect.fail(unavailable)), + Effect.catchDefect(defect), ), "kin.chats.subscribe": (input) => Stream.unwrap( @@ -69,46 +76,53 @@ export const makeKinChatHandlers = Effect.fn("makeKinChatHandlers")(function* ( ), ), ).pipe( + Stream.tapError(logInfra), Stream.mapError(mapError), - Stream.catchDefect(() => Stream.fail(unavailable)), + Stream.catchDefect((cause) => Stream.fromEffect(defect(cause))), ), "kin.chat.subscribe": (input) => Stream.unwrap( viewer.pipe(Effect.map(({ chat, people }) => chat.subscribe(people, input))), ).pipe( + Stream.tapError(logInfra), Stream.mapError(mapError), - Stream.catchDefect(() => Stream.fail(unavailable)), + Stream.catchDefect((cause) => Stream.fromEffect(defect(cause))), ), "kin.chats.createDirect": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.createDirect(people, input)), + Effect.tapError(logInfra), Effect.mapError(mapError), - Effect.catchDefect(() => Effect.fail(unavailable)), + Effect.catchDefect(defect), ), "kin.chat.history": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.history(people, input)), + Effect.tapError(logInfra), Effect.mapError(mapError), - Effect.catchDefect(() => Effect.fail(unavailable)), + Effect.catchDefect(defect), ), "kin.messages.send": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.send(people, input)), + Effect.tapError(logInfra), Effect.mapError(mapError), - Effect.catchDefect(() => Effect.fail(unavailable)), + Effect.catchDefect(defect), ), "kin.messages.markRead": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.markRead(people, input)), Effect.as({}), + Effect.tapError(logInfra), Effect.mapError(mapError), - Effect.catchDefect(() => Effect.fail(unavailable)), + Effect.catchDefect(defect), ), "kin.widgets.act": (input) => viewer.pipe( Effect.flatMap(({ chat, people }) => chat.act(people, input)), + Effect.tapError(logInfra), Effect.mapError(mapError), - Effect.catchDefect(() => Effect.fail(unavailable)), + Effect.catchDefect(defect), ), }); }); diff --git a/docs/kin/specs/chat.md b/docs/kin/specs/chat.md index 5cf13e7f9324c..7ed4f5a892289 100644 --- a/docs/kin/specs/chat.md +++ b/docs/kin/specs/chat.md @@ -351,7 +351,7 @@ export const KinChatStreamEvent = Schema.Union([ ``` - **Resume:** `kin.chat.subscribe` subscribes to the hub first, then reads one SQLite snapshot. `seq` remains the message's immutable ordering key. K010 adds a per-chat integer `change_seq`: message inserts/updates and widget writes advance it transactionally; widget writes mark their parent message changed. The indexed backlog selects the union of new messages (`seq > afterSeq`) and changed messages (`change_seq > afterChangeSeq`), once per message, with current associated widgets and normal viewer filtering. More than 200 affected visible messages sends `reset` instead. Both `backlog` and `reset` include `lastSeq` and `lastChangeSeq` from that snapshot. The migration marks all existing messages changed beyond `lastSeq`, so a client offline across the upgrade cannot miss an earlier edit. -- **Client resume contract:** upsert messages by `messageId` (do not discard an older `seq`) and widgets by `widgetId`/`revision`. Persist `backlog.lastChangeSeq` only after applying the whole backlog; send it as optional `afterChangeSeq` with the last message `afterSeq` on reconnect. Keep this snapshot cursor unchanged while receiving live events: their delivery order need not match commit order, so taking the maximum live `changeSeq` could skip another committed event. Replaying already-held rows across reconnects is intentional and idempotent. `reset` invalidates the entire cached chat, not just the latest page; refetch with history and resume from the reset cursors. Legacy clients omitting `afterChangeSeq` use `afterSeq` as a conservative lower bound, which can replay more rows or reset more often but cannot miss an offline edit. Clients must treat a backlog as upserts, not append-only. Live message events carry optional `changeSeq`; the server drops buffered changes already represented by the snapshot and repeated versions. These optional fields preserve older payload decoding. `clientMessageId` is an optional unprefixed send id, present only for the message's author in send replies, history, backlogs, live events and chat summaries; owning the chat does not reveal another author's id. +- **Client resume contract:** upsert messages by `messageId` (do not discard an older `seq`) and widgets by `widgetId`/`revision`. Persist `backlog.lastChangeSeq` only after applying the whole backlog; send it as optional `afterChangeSeq` with the last message `afterSeq` on reconnect. Keep this snapshot cursor unchanged while receiving live events: their delivery order need not match commit order, so taking the maximum live `changeSeq` could skip another committed event. Replaying already-held rows across reconnects is intentional and idempotent. `reset` invalidates the entire cached chat, not just the latest page; refetch with history and resume from the reset cursors. Clients omitting `afterChangeSeq` get only new messages (`seq > afterSeq`), the pre-K010 behaviour, and can miss offline edits. Clients must treat a backlog as upserts, not append-only. Live message events carry optional `changeSeq`; the server drops buffered changes already represented by the snapshot and repeated versions. These optional fields preserve older payload decoding. `clientMessageId` is an optional unprefixed send id, present only for the message's author in send replies, history, backlogs, live events and chat summaries; owning the chat does not reveal another author's id. - **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`. @@ -526,7 +526,7 @@ Run with `cd apps/server && vp test run src/kin` and `cd packages/contracts && v - Isolation: a person in org A gets `not_found` for a chat in org B, on every method. - Widgets: `act` with a stale revision fails `widget_changed`; on an approval widget fails `use_approvals`; `kin_widget_upsert` with `type: "approval"` fails. - Task card: a fake orchestrator emitting thread status changes moves the card through `working → needs_decision → done` with rising revisions and no new messages. -- Stream: subscribe with `afterSeq` gets exactly the missed messages, then live ones, with no duplicates across a concurrent send; more than 200 missed gives `reset`. +- Stream: subscribe with `afterSeq` and `afterChangeSeq` gets the missed messages plus messages and widgets changed while away, then live ones, with no duplicates across a concurrent send; more than 200 missed gives `reset`. - Read state: `markRead` never moves back; unread counts exclude the reader's own messages. - Waiting: an agent question in a direct chat adds a row; the owner's reply resolves it; the list stream shows both changes. - Floor manager: table tests for rules 1 to 5, including a mention of the last speaker, the 6-turn cap, `answer` refused on a second turn, and notices outside runs.