From b530d47bf5790eafce4413aaf67f2f86ba3852a4 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Mon, 30 Nov 2026 03:20:20 +0530 Subject: [PATCH 1/3] fix(kin): post one unanswered notice per join MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Group expired unanswered asks into one notice without exposing their count. Recheck the wake’s abort mark inside the bridge notice transaction so a concurrent join cannot produce a second notice. People-change: When someone joins, the chat sees at most one line about unanswered asks, without showing how many asks were hidden. Co-Authored-By: GPT-6.2 Sol --- apps/server/src/kin/chat/KinGroups.test.ts | 19 ++++----- apps/server/src/kin/chat/KinGroups.ts | 4 +- apps/server/src/kin/lead/ChatSharing.ts | 33 ++++++++------- apps/server/src/kin/lead/WakeupRunner.test.ts | 2 +- apps/server/src/kin/people/KinPeople.test.ts | 3 +- .../src/kin/reply/KinReplyBridge.test.ts | 41 +++++++++++++++---- apps/server/src/kin/reply/KinReplyBridge.ts | 1 + apps/server/src/kin/team/KinTeam.test.ts | 2 +- verification/kin/reply/Reply.tla | 3 +- 9 files changed, 67 insertions(+), 41 deletions(-) diff --git a/apps/server/src/kin/chat/KinGroups.test.ts b/apps/server/src/kin/chat/KinGroups.test.ts index 846066600933a..d6815c737edfe 100644 --- a/apps/server/src/kin/chat/KinGroups.test.ts +++ b/apps/server/src/kin/chat/KinGroups.test.ts @@ -853,27 +853,27 @@ it.effect( .map((row) => row.state), ["expired"], ); - // What it was asked goes unanswered, and the chat hears it once, where Sam reads too. - const told = "A message sent just before someone joined went unanswered. Send it again."; + // The join expires three asks but posts one line, visible to Sam without the dropped count. + const told = "What was sent just before someone joined went unanswered. Send it again."; const lines = sql<{ messageId: KinMessageId; body: string }>`SELECT message_id AS "messageId", body FROM kin_messages WHERE chat_id = ${chatId} AND author_kind = 'system' ORDER BY seq`; assert.deepStrictEqual( (yield* lines).map((row) => row.body), - [told, told, told], + [told], ); assert.deepStrictEqual( (yield* chats.history([sam], { chatId, beforeSeq: 1000, limit: 50 })).messages.map( (message) => message.body, ), - [told, told, told], + [told], ); // The join handled again, or its line retried, posts nothing more. const again = yield* sql.withTransaction( ChatSharing.onPersonJoined({ orgId, chatId, personId: sam.personId }), ); assert.deepStrictEqual(again.stopped, []); - assert.lengthOf(yield* lines, 3); + assert.lengthOf(yield* lines, 1); yield* queue.finish({ orgId, wakeupId: running.wakeupId, status: "completed" }); // Said after Sam joined, and a tap there on the card from before: only the first is his. const after = yield* say(sam, chatId, "Invented-sam-question"); @@ -882,7 +882,7 @@ it.effect( WHERE message_id = ${tap.messageId}`; const turn = yield* wake(tech); yield* sql`UPDATE kin_wakeups SET audience = 'shared' WHERE wakeup_id = ${turn.wakeupId}`; - assert.deepStrictEqual(yield* seen(turn), [told, told, told, "Invented-sam-question"]); + assert.deepStrictEqual(yield* seen(turn), [told, "Invented-sam-question"]); assert.deepStrictEqual( (yield* chats.read(turn, { chatId, afterSeq: after.seq - 1, limit: 1 })).map( (message) => message.messageId, @@ -904,13 +904,13 @@ it.effect.each([ { joiner: "member", before: "still queued", - lines: ["A message sent just before someone joined went unanswered. Send it again."], + lines: ["What was sent just before someone joined went unanswered. Send it again."], }, // `historyFloor` exempts only the chat's own owner member: a co-owner's join moves it too. { joiner: "co_owner", before: "nothing", - lines: ["A message sent just before someone joined went unanswered. Send it again."], + lines: ["What was sent just before someone joined went unanswered. Send it again."], }, ] as const)( "a join that leaves a person's question unanswered says so in the chat once ($joiner, $before)", @@ -1124,8 +1124,7 @@ it.effect( (message) => message.body, ), [ - "A message sent just before someone joined went unanswered. Send it again.", - "A message sent just before someone joined went unanswered. Send it again.", + "What was sent just before someone joined went unanswered. Send it again.", "Invented-priya-is-back", ], ); diff --git a/apps/server/src/kin/chat/KinGroups.ts b/apps/server/src/kin/chat/KinGroups.ts index 44f37d239bfb3..f70b30b6be977 100644 --- a/apps/server/src/kin/chat/KinGroups.ts +++ b/apps/server/src/kin/chat/KinGroups.ts @@ -276,8 +276,8 @@ const make = Effect.gen(function* () { /** * In the join's transaction (`onPersonJoined`): no turn that may hold the owner's context, or - * read the chat before it was shared, continues once someone else can read it. What was asked - * there before stays the owner's: unanswered, it expires and the chat hears once who didn't. + * read the chat before it was shared, continues once someone else can read it. Unanswered asks + * below the new reader's history floor expire and the chat gets one plain line. */ const shareWith = Effect.fn("KinGroups.shareWith")(function* ( chat: Pick, diff --git a/apps/server/src/kin/lead/ChatSharing.ts b/apps/server/src/kin/lead/ChatSharing.ts index f9a7a11674ef3..8cb689925d497 100644 --- a/apps/server/src/kin/lead/ChatSharing.ts +++ b/apps/server/src/kin/lead/ChatSharing.ts @@ -44,7 +44,7 @@ const PERSON_ASK_TAGS = ["chat.message", "widget.action"] as const; export const stoppedByJoinNotice = (partway: boolean) => partway ? "A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer." - : "A message sent just before someone joined went unanswered. Send it again."; + : "What was sent just before someone joined went unanswered. Send it again."; /** The fixed line a delegated task's card in a shared chat shows instead of its own. */ const movedCardTitle = (lead: string) => `Moved to your private chat with ${lead}`; @@ -152,7 +152,7 @@ export const finishPersonJoin = Effect.fn("KinChatSharing.finishPersonJoin")(fun ); }); -/** One line per unanswered human message, even when it woke several leads. */ +/** One line for the join, without revealing how many unanswered human messages expired. */ const stoppedLines = Effect.fnUntraced(function* ( sql: SqlClient.SqlClient, orgId: KinOrgId, @@ -170,20 +170,23 @@ const stoppedLines = Effect.fnUntraced(function* ( AND answered.tag IN ${sql.in(PERSON_ASK_TAGS)} AND json_extract(answered.json, '$.messageId') = m.message_id) ORDER BY m.seq, m.message_id, e.wakeup_id`; - const lines = new Map(); - for (const row of asked) { - const partway = - lines.get(row.messageId) === true || - (row.wakeupId !== null && - (yield* didVisibleWork({ orgId, wakeupId: KinWakeupId.make(row.wakeupId) }))); - lines.set(row.messageId, partway); + if (asked.length === 0) return []; + const messageIds = [...new Set(asked.map(({ messageId }) => messageId))].sort(); + let partway = false; + for (const wakeupId of new Set(asked.map((row) => row.wakeupId))) { + if (wakeupId === null) continue; + if (yield* didVisibleWork({ orgId, wakeupId: KinWakeupId.make(wakeupId) })) { + partway = true; + break; + } } - return [...lines].map(([messageId, partway]) => ({ - messageId: KinMessageId.make( - `kin-join-stopped:${NodeCrypto.createHash("sha256").update(messageId).digest("hex")}`, - ), - body: stoppedByJoinNotice(partway), - })); + const hash = NodeCrypto.createHash("sha256").update(messageIds.join("\n")).digest("hex"); + return [ + { + messageId: KinMessageId.make(`kin-join-stopped:${hash}`), + body: stoppedByJoinNotice(partway), + }, + ]; }); const tellStopped = Effect.fn("KinChatSharing.tellStopped")(function* (input: { diff --git a/apps/server/src/kin/lead/WakeupRunner.test.ts b/apps/server/src/kin/lead/WakeupRunner.test.ts index aa659e8a65179..6b4f4a0d0ea81 100644 --- a/apps/server/src/kin/lead/WakeupRunner.test.ts +++ b/apps/server/src/kin/lead/WakeupRunner.test.ts @@ -1232,7 +1232,7 @@ describe("WakeupRunner", () => { const notices = sql<{ body: string }>`SELECT body FROM kin_messages WHERE org_id = ${orgId} AND chat_id = ${chatId} AND author_kind = 'system' ORDER BY seq`; const expected = joinFirst - ? "A message sent just before someone joined went unanswered. Send it again." + ? "What was sent just before someone joined went unanswered. Send it again." : "Lead took too long and was stopped. Nothing it hadn't finished was sent. Ask again to start fresh."; if (joinFirst) { yield* join; diff --git a/apps/server/src/kin/people/KinPeople.test.ts b/apps/server/src/kin/people/KinPeople.test.ts index a4349514bf690..6914aad6d7fd1 100644 --- a/apps/server/src/kin/people/KinPeople.test.ts +++ b/apps/server/src/kin/people/KinPeople.test.ts @@ -1641,8 +1641,7 @@ it.effect( ); // Leaving lowers the group's floor only to what Tomas, still there, could always read; each // join said once that it left a question unanswered. - const unanswered = - "A message sent just before someone joined went unanswered. Send it again."; + const unanswered = "What was sent just before someone joined went unanswered. Send it again."; const turn = yield* wake(); assert.deepStrictEqual( (yield* chats.read(turn, { chatId: group.chatId, afterSeq: 0, limit: 100 })).map( diff --git a/apps/server/src/kin/reply/KinReplyBridge.test.ts b/apps/server/src/kin/reply/KinReplyBridge.test.ts index cf4259dde1d6b..8f85572fed133 100644 --- a/apps/server/src/kin/reply/KinReplyBridge.test.ts +++ b/apps/server/src/kin/reply/KinReplyBridge.test.ts @@ -1,6 +1,7 @@ import { pairSession } from "../auth/ProofKeySessions.testkit.ts"; import { kinSecretsTest } from "../lead/layer.testkit.ts"; import { assert, it } from "@effect/vitest"; +import { vi } from "vitest"; import { AuthSessionId, EnvironmentId, @@ -2272,26 +2273,34 @@ it.effect.each([ access: "member", before: "a join already", then: "completed", - expected: ["system:A message sent just before someone joined went unanswered. Send it again."], + expected: ["system:What was sent just before someone joined went unanswered. Send it again."], }, { access: "member", before: "a join already", then: "failed", - expected: ["system:A message sent just before someone joined went unanswered. Send it again."], + expected: ["system:What was sent just before someone joined went unanswered. Send it again."], + }, + { + access: "member", + before: "a join during refusal", + then: "completed", + expected: [ + "system:A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer.", + ], }, // The join through Add says it, and the turn it aborted says nothing more. { access: "member", before: "a join through Add", then: "completed", - expected: ["system:A message sent just before someone joined went unanswered. Send it again."], + expected: ["system:What was sent just before someone joined went unanswered. Send it again."], }, { access: "member", before: "a join through Add", then: "timed_out", - expected: ["system:A message sent just before someone joined went unanswered. Send it again."], + expected: ["system:What was sent just before someone joined went unanswered. Send it again."], }, ] as const)( "an owner-only reply posts where everyone reading is owner-level, never silently held ($access, $before, $then)", @@ -2304,19 +2313,30 @@ it.effect.each([ }); yield* sql`UPDATE kin_people SET access = ${access} WHERE person_id = 'mate'`; yield* sql`UPDATE kin_wakeups SET audience = 'owner_only' WHERE wakeup_id = ${wakeupId}`; - if (before === "a tool call") + if (before === "a tool call" || before === "a join during refusal") yield* sql`INSERT INTO kin_lead_tool_calls (org_id, wakeup_id, call_key, request_hash) VALUES (${owner.orgId}, ${wakeupId}, 'item:task-1', 'fixture-hash')`; if (before === "a join already") yield* sql`UPDATE kin_events SET json = json_set(json, '$._audienceMoved', 1) WHERE wakeup_id = ${wakeupId}`; - if (before === "a join through Add") { + const join = Effect.gen(function* () { yield* sql`INSERT INTO kin_people (person_id, org_id, handle, display_name, created_at) VALUES ('pat', ${owner.orgId}, 'pat', 'Pat', '1970-01-01T00:00:00.000Z')`; yield* Context.get(services, KinGroups).addMember([owner], { chatId, principal: { _tag: "person", personId: KinPersonId.make("pat") }, }); - } + }).pipe(Effect.orDie); fake.items = [text("a", 1, "Draft INV-2 is ready: ₹40,000 + GST.", runId)]; - yield* complete; + if (before === "a join during refusal") { + const didVisibleWork = KinEventQueue.didVisibleWork; + // The bridge has read the abort mark and has not yet opened the notice transaction. + using _join = vi + .spyOn(KinEventQueue, "didVisibleWork") + .mockImplementationOnce((wake) => didVisibleWork(wake).pipe(Effect.tap(() => join))); + yield* complete; + assert.isTrue(_join.mock.calls.length > 0); + } else { + if (before === "a join through Add") yield* join; + yield* complete; + } assert.deepStrictEqual( (yield* posted).map((row) => `${row.authorKind}:${row.body}`), [...expected], @@ -2349,7 +2369,10 @@ it.effect.each([ requeued ? { state: "queued", attempts: 0, wakeupId: null, moved: 1 } : { - state: before === "a join through Add" ? "expired" : "done", + state: + before === "a join through Add" || before === "a join during refusal" + ? "expired" + : "done", attempts: 0, wakeupId, moved: before === "a join already" ? 1 : null, diff --git a/apps/server/src/kin/reply/KinReplyBridge.ts b/apps/server/src/kin/reply/KinReplyBridge.ts index 558fcc44d14d8..330406016857a 100644 --- a/apps/server/src/kin/reply/KinReplyBridge.ts +++ b/apps/server/src/kin/reply/KinReplyBridge.ts @@ -1126,6 +1126,7 @@ const make = Effect.gen(function* () { ); yield* sql.withTransaction( Effect.gen(function* () { + if (yield* aborted(wake).pipe(Effect.provideService(SqlClient.SqlClient, sql))) return; yield* endLostToJoin({ ...wake, chatId: wake.lane.chatId }).pipe( Effect.provideService(SqlClient.SqlClient, sql), ); diff --git a/apps/server/src/kin/team/KinTeam.test.ts b/apps/server/src/kin/team/KinTeam.test.ts index 5134296bcf60a..47ec4dff756ed 100644 --- a/apps/server/src/kin/team/KinTeam.test.ts +++ b/apps/server/src/kin/team/KinTeam.test.ts @@ -557,7 +557,7 @@ it.effect.each(["admission", "promotion"] as const)( assert.deepStrictEqual(yield* readWake, [{ status: "running", aborted: 0, audience }]); assert.isTrue(identity.isLeadThread(threadId)); - const failure = "A message sent just before someone joined went unanswered. Send it again."; + const failure = "What was sent just before someone joined went unanswered. Send it again."; yield* sql.withTransaction( Effect.gen(function* () { if (road === "admission") { diff --git a/verification/kin/reply/Reply.tla b/verification/kin/reply/Reply.tla index a51ba3de111a1..389d1f7602e57 100644 --- a/verification/kin/reply/Reply.tla +++ b/verification/kin/reply/Reply.tla @@ -385,7 +385,8 @@ Owed == {m \in Claimed : ~covered[m]} (* Every join, in its transaction (chat/KinGroups.ts shareWith → lead/ChatSharing.ts onPersonJoined): each ask waiting in the chat, or claimed unanswered by a turn aborted for its audience, expires if it is below the new reader's history floor. The same transaction stores - the plain failure line once for each unanswered message; tellStopped publishes after commit. + one plain failure line for the join, counted here for each unanswered message; + tellStopped publishes after commit. In this reply branch the join advances the floor; a return that preserves the floor can requeue an eligible event. *) Expire(exp) == From 2b000db4b47ec863bf9d24c5f12421f2971c39fb Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Mon, 30 Nov 2026 03:21:17 +0530 Subject: [PATCH 2/3] =?UTF-8?q?test(kin):=20use=20the=20server=E2=80=99s?= =?UTF-8?q?=20Vitest=20export=20for=20the=20join=20race?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Import the spy from the server’s declared Effect Vitest dependency so the isolated test run and server typecheck resolve the same test API. People-change: None; this corrects the join-race test import. Co-Authored-By: GPT-6.2 Sol --- apps/server/src/kin/reply/KinReplyBridge.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/server/src/kin/reply/KinReplyBridge.test.ts b/apps/server/src/kin/reply/KinReplyBridge.test.ts index 8f85572fed133..bf12022c573e0 100644 --- a/apps/server/src/kin/reply/KinReplyBridge.test.ts +++ b/apps/server/src/kin/reply/KinReplyBridge.test.ts @@ -1,7 +1,6 @@ import { pairSession } from "../auth/ProofKeySessions.testkit.ts"; import { kinSecretsTest } from "../lead/layer.testkit.ts"; -import { assert, it } from "@effect/vitest"; -import { vi } from "vitest"; +import { assert, it, vi } from "@effect/vitest"; import { AuthSessionId, EnvironmentId, From ff6811048257019369e36371c29bbe726066ae90 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Mon, 30 Nov 2026 03:34:34 +0530 Subject: [PATCH 3/3] fix(kin): the interrupted-reply line fits one message or several People-change: When someone joins while a reply is under way, the chat's one line says "Send it again for an answer." and no longer implies there was exactly one message. Co-Authored-By: Claude Fable 5.2 --- apps/server/src/kin/chat/KinGroups.test.ts | 4 ++-- apps/server/src/kin/lead/ChatSharing.ts | 2 +- apps/server/src/kin/reply/KinReplyBridge.test.ts | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/server/src/kin/chat/KinGroups.test.ts b/apps/server/src/kin/chat/KinGroups.test.ts index d6815c737edfe..04d1871294ddb 100644 --- a/apps/server/src/kin/chat/KinGroups.test.ts +++ b/apps/server/src/kin/chat/KinGroups.test.ts @@ -898,7 +898,7 @@ it.effect.each([ joiner: "member", before: "a tool call", lines: [ - "A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer.", + "A reply was interrupted when someone joined. Work already done stays. Send it again for an answer.", ], }, { @@ -1007,7 +1007,7 @@ it.effect.each([ { unanswered: true, lines: [ - "A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer.", + "A reply was interrupted when someone joined. Work already done stays. Send it again for an answer.", ], }, ] as const)( diff --git a/apps/server/src/kin/lead/ChatSharing.ts b/apps/server/src/kin/lead/ChatSharing.ts index 8cb689925d497..6662b6faf8263 100644 --- a/apps/server/src/kin/lead/ChatSharing.ts +++ b/apps/server/src/kin/lead/ChatSharing.ts @@ -43,7 +43,7 @@ const PERSON_ASK_TAGS = ["chat.message", "widget.action"] as const; */ export const stoppedByJoinNotice = (partway: boolean) => partway - ? "A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer." + ? "A reply was interrupted when someone joined. Work already done stays. Send it again for an answer." : "What was sent just before someone joined went unanswered. Send it again."; /** The fixed line a delegated task's card in a shared chat shows instead of its own. */ diff --git a/apps/server/src/kin/reply/KinReplyBridge.test.ts b/apps/server/src/kin/reply/KinReplyBridge.test.ts index bf12022c573e0..b73fd7d3682e1 100644 --- a/apps/server/src/kin/reply/KinReplyBridge.test.ts +++ b/apps/server/src/kin/reply/KinReplyBridge.test.ts @@ -2249,7 +2249,7 @@ it.effect.each([ before: "a tool call", then: "completed", expected: [ - "system:A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer.", + "system:A reply was interrupted when someone joined. Work already done stays. Send it again for an answer.", ], }, { @@ -2257,7 +2257,7 @@ it.effect.each([ before: "a tool call", then: "failed", expected: [ - "system:A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer.", + "system:A reply was interrupted when someone joined. Work already done stays. Send it again for an answer.", ], }, { @@ -2265,7 +2265,7 @@ it.effect.each([ before: "a tool call", then: "restart", expected: [ - "system:A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer.", + "system:A reply was interrupted when someone joined. Work already done stays. Send it again for an answer.", ], }, { @@ -2285,7 +2285,7 @@ it.effect.each([ before: "a join during refusal", then: "completed", expected: [ - "system:A reply was interrupted when someone joined. Work already done stays. Send the message again for an answer.", + "system:A reply was interrupted when someone joined. Work already done stays. Send it again for an answer.", ], }, // The join through Add says it, and the turn it aborted says nothing more.