Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions apps/server/src/kin/chat/KinGroups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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,
Expand All @@ -898,19 +898,19 @@ 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.",
],
},
{
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)",
Expand Down Expand Up @@ -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)(
Expand Down Expand Up @@ -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",
],
);
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/kin/chat/KinGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<KinChat, "orgId" | "chatId">,
Expand Down
35 changes: 19 additions & 16 deletions apps/server/src/kin/lead/ChatSharing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ 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.";
? "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. */
const movedCardTitle = (lead: string) => `Moved to your private chat with ${lead}`;
Expand Down Expand Up @@ -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,
Expand All @@ -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<string, boolean>();
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: {
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/kin/lead/WakeupRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions apps/server/src/kin/people/KinPeople.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
48 changes: 35 additions & 13 deletions apps/server/src/kin/reply/KinReplyBridge.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pairSession } from "../auth/ProofKeySessions.testkit.ts";
import { kinSecretsTest } from "../lead/layer.testkit.ts";
import { assert, it } from "@effect/vitest";
import { assert, it, vi } from "@effect/vitest";
import {
AuthSessionId,
EnvironmentId,
Expand Down Expand Up @@ -2249,49 +2249,57 @@ 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.",
],
},
{
access: "member",
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.",
],
},
{
access: "member",
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.",
],
},
{
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 it 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)",
Expand All @@ -2304,19 +2312,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],
Expand Down Expand Up @@ -2349,7 +2368,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,
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/kin/reply/KinReplyBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/kin/team/KinTeam.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
3 changes: 2 additions & 1 deletion verification/kin/reply/Reply.tla
Original file line number Diff line number Diff line change
Expand Up @@ -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) ==
Expand Down
Loading