Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
22 changes: 21 additions & 1 deletion .agents/skills/verify-kin/scripts/member-messages-teammate.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
#!/usr/bin/env bash
# Zoya messages Arjun from New: one chat, opened again the same; only the two of them read it, and it says so.
# Sent in Arjun's quiet hours, her message still rings his phone: a teammate is a person, not Kin.
source "$(dirname "$0")/lib.sh"
ARJUN="$(kv people | awk '$1 == "arjun" { print $NF }')"
ZOYA="$(kv people | awk '$1 == "zoya" { print $NF }')"
json() { node -e 'process.stdout.write(String(JSON.parse(process.argv[1])[process.argv[2]]))' "$1" "$2"; }
chat="$(json "$(kv as zoya rpc kin.chats.withPerson "{\"personId\":\"$ARJUN\"}")" chatId)"
[[ "$(json "$(kv as arjun rpc kin.chats.withPerson "{\"personId\":\"$ZOYA\"}")" chatId)" == "$chat" ]] ||
fail "Arjun's way in opened a second chat"
# 23:00 on Arjun's clock, so he is in his quiet hours (Etc/GMT-N is UTC+N).
ARJUN_ZONE="$(kv db "select time_zone from kin_people where person_id = '$ARJUN'" | tail -1)"
trap 'kv as owner rpc kin.people.updateProfile "{\"personId\":\"$ARJUN\",\"timeZone\":\"$ARJUN_ZONE\"}" >/dev/null' EXIT
NIGHT="$(node -e 'let n=((23-new Date().getUTCHours())%24+24)%24;if(n>14)n-=24;process.stdout.write(n===0?"Etc/GMT":`Etc/GMT${n>0?"-":"+"}${Math.abs(n)}`)')"
kv as owner rpc kin.people.updateProfile "{\"personId\":\"$ARJUN\",\"timeZone\":\"$NIGHT\"}" >/dev/null
TOKEN="$(node -e 'process.stdout.write(require("node:crypto").createHash("sha256").update("arjun-"+process.argv[1]).digest("hex"))' "$RUN")"
kv as arjun rpc kin.push.registerDevice "{\"token\":\"$TOKEN\",\"platform\":\"ios\",\"apsEnvironment\":\"development\"}" >/dev/null
# Lines he wrote in the last ten minutes would let Kin's line through: put them out of reach.
kv clock advance 11m >/dev/null
# Kin's own line to him waits for morning, so these are his quiet hours.
relay="{\"to\":\"Arjun Rao\",\"text\":\"Proof $RUN: the van comes at nine.\"}"
expect_in "$(kv tool chief kin_relay "$relay" --as-turn-of owner)" "their time" \
"Arjun isn't in his quiet hours, so the ring below proves nothing"
kv as zoya rpc kin.messages.send "{\"chatId\":\"$chat\",\"clientMessageId\":\"pair-$RUN\",\"body\":\"Lunch at one? $RUN\"}" >/dev/null
for _ in $(seq 1 20); do
kv push sent | rg -F "/${TOKEN:0:6}" | rg -qF "Lunch at one? $RUN" && break
sleep 1
done
expect_in "$(kv push sent | rg -F "/${TOKEN:0:6}" | rg -F "Lunch at one? $RUN" || true)" "Lunch at one? $RUN" \
"Zoya's message didn't ring Arjun's phone in his quiet hours"
page="{\"chatId\":\"$chat\",\"beforeSeq\":100000,\"limit\":50}"
expect_in "$(kv as arjun rpc kin.chat.history "$page")" "Lunch at one? $RUN" "Arjun can't read Zoya's message"
for person in owner meera dev; do
Expand All @@ -22,4 +42,4 @@ kv as zoya web wait "Only you and they can read it." >/dev/null || fail "the pic
kv as zoya web click --name "Arjun Rao" >/dev/null
kv as zoya web wait "Only you two can read this." >/dev/null || fail "the chat doesn't say only the two of them read it"
expect_in "$(kv as zoya web snapshot --full)" "Lunch at one? $RUN" "the picker opened another chat"
pass "Zoya and Arjun share one chat; owner, Meera and Dev can't read it; the chat says so"
pass "Zoya and Arjun share one chat; owner, Meera and Dev can't read it; the chat says so; it rang Arjun at 23:00 while Kin's line waited"
88 changes: 75 additions & 13 deletions .agents/skills/verify-kin/scripts/teammate-reminder.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,106 @@
#!/usr/bin/env bash
# Zoya asks Chief of staff to remind her: it is set, comes due in her own chat (not the owner's),
# rings her phone, and she taps Done herself. A reminder for someone else, or one that starts
# work, is refused on her turn. Waits about a minute of real time: stored reminders keep their own
# due time, so `kv clock advance` would leave them stale.
# rings her phone, and she taps Done herself. Her own reminders she moves and cancels by asking; the
# owner's she can't. A reminder for someone else, or one that starts work, is refused on her
# turn. Due, her card offers Change, Snooze and Tomorrow 9:00 too, and each works for her. Waits
# about a minute of real time: stored reminders keep their own due time, so `kv clock advance`
# would leave them stale.
source "$(dirname "$0")/lib.sh"
q() { kv db "$1" | tail -n +2 | tail -1; }
ZOYA="$(q "select person_id from kin_people where display_name = 'Zoya Sheikh'")"
CHIEF="$(q "select agent_id from kin_leads where domain = 'chief' limit 1")"
CHAT="$(q "select chat_id from kin_chats where direct_key = '$ZOYA:$CHIEF'")"
OWNER_CHAT="$(q "select owner_chat_id from kin_leads where agent_id = '$CHIEF'")"
WHAT="Proof m$RUN: send the Lumen mockups"
DUE="$(node -e 'process.stdout.write(new Date(Date.now()+60*1000).toISOString().replace("Z","+00:00"))')"
defer() { kv tool chief kin_defer "{\"what\":\"$1\",\"due\":\"$DUE\",\"onDue\":\"$2\",\"chatId\":\"$3\",\"sourceEventId\":\"\$event\"}" --as-turn-of zoya --say "$4" 2>&1; }
kv as zoya rpc kin.push.registerDevice "{\"token\":\"$(printf 'e%.0s' {1..64})\",\"platform\":\"ios\",\"apsEnvironment\":\"development\"}" >/dev/null
at() { node -e 'process.stdout.write(new Date(Date.now()+(+process.argv[1])*1000).toISOString().replace("Z","+00:00"))' "$1"; }
DUE="$(at 60)"
# defer <what> <onDue> <chat> <say> [replaces] [as]
defer() {
local input="{\"what\":\"$1\",\"due\":\"${LATER:-$DUE}\",\"onDue\":\"$2\",\"chatId\":\"$3\",\"sourceEventId\":\"\$event\"${5:+,\"replaces\":\"$5\"}}"
kv tool chief kin_defer "$input" --as-turn-of "${6:-zoya}" --say "$4" 2>&1
}
cancel() {
local input="{\"itemId\":\"$1\",\"sourceEventId\":\"\$event\"}"
kv tool chief kin_cancel "$input" --as-turn-of zoya --say "$2" 2>&1
}
item_of() { rg -o 'def-[0-9a-f]+' <<<"$1" | head -1; }
run_state() { q "select state from kin_deferral_runs where item_id = '$1'"; }
wait_state() {
for _ in $(seq 1 15); do
[[ "$(run_state "$1")" == "$2" ]] && return 0
sleep 1
done
fail "$3 (state $(run_state "$1"), not $2)"
}
device="{\"token\":\"$(printf 'e%.0s' {1..64})\",\"platform\":\"ios\",\"apsEnvironment\":\"development\"}"
kv as zoya rpc kin.push.registerDevice "$device" >/dev/null

set="$(defer "$WHAT" remind "$CHAT" "Remind me in a minute to send the Lumen mockups")"
expect_in "$set" "kin_defer: ok" "Zoya couldn't set her own reminder"
ITEM="$(rg -o 'def-[0-9a-f]+' <<<"$set" | head -1)"
ITEM="$(item_of "$set")"
expect_in "$(defer "Proof o$RUN: ping Rhea" remind "$OWNER_CHAT" "Remind Rhea in a minute")" "refused" \
"Zoya's turn set a reminder in the owner's chat"
expect_in "$(defer "Proof s$RUN: start the mockups" start "$CHAT" "Start the mockups in a minute")" "refused" \
"Zoya's turn set a reminder that starts work"

# Her own: moved by her next turn, and cancelled by another. These are due in an hour, so only
# the first comes due here.
LATER="$(at 3600)"
moving="$(item_of "$(defer "Proof v$RUN: call the printer" remind "$CHAT" "Remind me to call the printer")")"
moved="$(defer "Proof v$RUN: call the printer, later" remind "$CHAT" "Make that later" "$moving")"
expect_in "$moved" "kin_defer: ok" "Zoya couldn't move her own reminder"
wait_state "$moving" disarmed "moving Zoya's reminder left the old one armed"
gone="$(item_of "$(defer "Proof c$RUN: book the van" remind "$CHAT" "Remind me to book the van")")"
expect_in "$(cancel "$gone" "Never mind the van")" "kin_cancel: ok" "Zoya couldn't cancel her own reminder"
wait_state "$gone" disarmed "Zoya's cancel left her reminder armed"
# The owner's: her turn can neither move nor cancel it.
owners="$(item_of "$(defer "Proof r$RUN: Rhea's call" remind "$OWNER_CHAT" "Remind me about my call" "" owner)")"
[[ -n "$owners" ]] || fail "the owner couldn't set his own reminder"
expect_in "$(defer "Proof r$RUN: Rhea's call, later" remind "$CHAT" "Move Rhea's call" "$owners")" "not_allowed" \
"Zoya's turn moved the owner's reminder"
expect_in "$(cancel "$owners" "Cancel Rhea's call")" "not_allowed" "Zoya's turn cancelled the owner's reminder"
[[ "$(run_state "$owners")" == armed ]] || fail "the owner's reminder isn't armed after Zoya's turns"

# Before it is due there is no card: a remind posts none (the agent's reply says when), so she
# moves or cancels it by asking, as above. Undo and Cancel are on start and chase cards only.
unset LATER

for _ in $(seq 1 120); do
[[ "$(q "select count(*) from kin_messages where body like 'Reminder: $WHAT%'")" == 1 ]] && break
sleep 1
done
[[ "$(q "select count(*) from kin_messages where body like 'Reminder: $WHAT%' and chat_id = '$CHAT'")" == 1 ]] ||
fail "the reminder never came due in Zoya's chat"
[[ "$(q "select count(*) from kin_messages where chat_id = '$OWNER_CHAT' and body like '%$RUN%'")" == 0 ]] ||
[[ "$(q "select count(*) from kin_messages where chat_id = '$OWNER_CHAT' and body like '%$RUN%' and body not like '%Rhea''s call%'")" == 0 ]] ||
fail "Zoya's reminder reached the owner's chat"
sleep 2
expect_in "$(kv push sent | rg "/eeeeee" | rg -F "Reminder: $WHAT" || true)" "Reminder:" "Zoya's phone didn't ring for it"

# Due, her card offers every tap, and each one works for her.
kv as zoya web open chat:chief >/dev/null
kv as zoya web click --name "Mark this reminder done" >/dev/null || fail "Zoya has no Done on her reminder"
card="$(kv as zoya web snapshot --full)"
for name in "Change this with your agent" "Snooze this reminder for 10 minutes" \
"Snooze this reminder for an hour" "Snooze this reminder to tomorrow at 9:00" "Mark this reminder done"; do
expect_in "$card" "$name" "Zoya's due reminder has no \"$name\""
done
kv as zoya web click --name "Snooze this reminder for 10 minutes" --within "$WHAT" >/dev/null
wait_state "$ITEM" snoozed "Zoya's Snooze didn't snooze her reminder"
snoozed_until() { q "select json_extract(snapshot_json, '\$.snoozedUntil') from kin_deferral_runs where item_id = '$ITEM'"; }
soon="$(snoozed_until)"
kv as zoya web click --name "Snooze this reminder to tomorrow at 9:00" --within "$WHAT" >/dev/null || fail "no Tomorrow 9:00 after a snooze"
for _ in $(seq 1 15); do
[[ "$(snoozed_until)" != "$soon" ]] && break
sleep 1
done
# Tomorrow at 9:00 is at least nine hours past a ten-minute snooze.
(($(snoozed_until) > soon + 8 * 3600000)) || fail "Zoya's Tomorrow 9:00 didn't move her reminder ($soon to $(snoozed_until))"
kv as zoya web click --name "Change this with your agent" --within "$WHAT" >/dev/null || fail "Zoya has no Change on her reminder"
for _ in $(seq 1 15); do
[[ "$(q "select state from kin_deferral_runs where item_id = '$ITEM'")" == done ]] && break
[[ "$(q "select count(*) from kin_messages where chat_id = '$CHAT' and kind = 'widget_action' and body like '%\"change\"%'")" -ge 1 ]] && break
sleep 1
done
[[ "$(q "select state from kin_deferral_runs where item_id = '$ITEM'")" == done ]] ||
fail "Zoya's Done didn't finish her reminder"
pass "Zoya set her own reminder and nothing for anyone else; it came due in her chat, rang her phone, and her Done finished it"
[[ "$(q "select count(*) from kin_messages where chat_id = '$CHAT' and kind = 'widget_action' and body like '%\"change\"%'")" -ge 1 ]] ||
fail "Zoya's Change didn't reach her agent"
kv as zoya web click --name "Mark this reminder done" --within "$WHAT" >/dev/null || fail "Zoya has no Done on her reminder"
wait_state "$ITEM" done "Zoya's Done didn't finish her reminder"
pass "Zoya set her own reminder and nothing for anyone else; moved and cancelled her own, not the owner's; it came due in her chat, rang her phone, and her Snooze, Tomorrow 9:00, Change and Done each worked"
11 changes: 7 additions & 4 deletions apps/mobile/src/features/chats/components/KinWorkPieces.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { KIN_RETRY } from "@t3tools/client-runtime/kin/kinAnswers";
import {
KIN_WORK_MARKS,
KIN_WORK_PRIVATE,
KIN_WORK_REFRESH_FAILED,
KIN_WORK_STALE,
Expand Down Expand Up @@ -180,15 +179,19 @@ export function KinWorkTaskRow(props: {
)}
</View>
</View>
{row.mark === null ? null : (
{row.markText === null ? null : (
<Text
textRole="footnote"
className={cn(
"font-t3-medium",
row.mark === "late" ? "text-danger-foreground" : "text-warning-foreground",
row.mark === "late"
? "text-danger-foreground"
: row.mark === "handed_back"
? "text-foreground-muted"
: "text-warning-foreground",
)}
>
{KIN_WORK_MARKS[row.mark]}
{row.markText}
</Text>
)}
</Pressable>
Expand Down
11 changes: 10 additions & 1 deletion apps/mobile/src/features/chats/widgets/KinWidgetView.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { useAtomValue } from "@effect/atom-react";
import type { KinChartView } from "@t3tools/client-runtime/kin/kinChart";
import type { EnvironmentId, KinPersonId, KinWidget } from "@t3tools/contracts";
import { kinAccessAtom } from "@t3tools/client-runtime/kin/kinAccess";
import {
kinOwnerLevel,
type EnvironmentId,
type KinPersonId,
type KinWidget,
} from "@t3tools/contracts";
import { memo } from "react";
import { View } from "react-native";

import { AppText as Text } from "../../../components/ui/AppText";
import { environmentCatalog } from "../../../connection/catalog";
import { connectionAtomRuntime } from "../../../connection/runtime";
import {
KIN_MISSING_CARD_TEXT,
kinCardView,
Expand Down Expand Up @@ -44,12 +51,14 @@ export const KinWidgetView = memo(function KinWidgetView(props: {
readonly agentNames?: ReadonlyMap<string, string>;
}) {
const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
const access = useAtomValue(kinAccessAtom(connectionAtomRuntime, props.environmentId));
// Before the catalog loads, assume the environment is known; the thread screen handles the rest.
const view = kinCardView(
props.widget,
props.environmentId,
(environmentId) => !catalog.isReady || catalog.entries.has(environmentId),
props.heldFor ?? null,
access === null ? undefined : kinOwnerLevel(access),
);
switch (view.kind) {
case "task":
Expand Down
15 changes: 14 additions & 1 deletion apps/server/src/kin/approvals/ActionExecutor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1897,8 +1897,21 @@ describe("KinActionExecutor", () => {
"kin/chat/refreshes.ts",
"kin/chat/rpcHandlers.ts",
"kin/cli/lead.ts",
"kin/lead/agentPage.ts",
"kin/lead/agentUpdate.ts",
"kin/people/rpcHandlers.ts",
]);
// An agent's page reads and retires as its checked owner; a turn's model change is the
// owner's or a co-owner's own words, saved with them as the editor (`updateAgent`).
assert.match(
source("kin/lead/agentPage.ts"),
/import \{ type LeadCreator, profileIn, retirable \} from "\.\/KinLeadProvision\.ts"/,
);
assert.match(
source("kin/lead/agentUpdate.ts"),
/import \{ leadsIn, update \} from "\.\/KinLeadProvision\.ts"/,
);
assert.include(source("kin/lead/agentUpdate.ts"), "!(yield* ownerLevel(lead.orgId, asker))");
// The team agent is set with the caller's own session, checked as the editor's.
assert.notMatch(
source("kin/people/rpcHandlers.ts"),
Expand All @@ -1915,7 +1928,7 @@ describe("KinActionExecutor", () => {
]);
assert.notMatch(
source("kin/chat/rpcHandlers.ts"),
/\bLeadProvision\.(?!create\b|update\b|retire\b|restore\b|profiles\b|contacts\b|access\b|allow\b|disallow\b|teamAgent\b|KinLeadProvisionError\b|ts\b)\w+/,
/\bLeadProvision\.(?!create\b|update\b|retire\b|restore\b|setPaused\b|agentDefaults\b|setAgentDefaults\b|profiles\b|contacts\b|access\b|allow\b|disallow\b|teamAgent\b|KinLeadProvisionError\b|ts\b)\w+/,
);
// The refresh stream reads only the caller's own lead list to tell them it moved.
assert.deepStrictEqual(importers("kin/chat/refreshes.ts"), ["kin/work/rpcHandlers.ts"]);
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/kin/lead/ChatWake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ describe("ChatWake", () => {
"kin/lead/ChatWake.ts",
"kin/lead/layer.ts",
"kin/push/KinPushDispatch.ts",
// Its own week cards only, to render a preview or post what is due; nothing leaves it.
"kin/social/SocialService.ts",
]);
});
it.effect(
Expand Down
43 changes: 42 additions & 1 deletion apps/server/src/kin/lead/LeadMemory.shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
EnvironmentId,
KinAgentId,
KinChatId,
KinEventId,
KinMemoryItemId,
KinOrgId,
KinPersonId,
Expand Down Expand Up @@ -546,7 +547,6 @@ it.effect("a teammate's turn sets their own reminder, in their own chat, and not
{ chatId: "group-chat", sourceEventId: `event-${leads.a}` },
{ chatId: "owner-chat" },
{ onDue: "start" },
{ replaces: "invented-other" },
];
for (const other of others) {
const refused = yield* defer({ ...other, what: `Invented ${JSON.stringify(other)}` }).pipe(
Expand All @@ -555,5 +555,46 @@ it.effect("a teammate's turn sets their own reminder, in their own chat, and not
assert.strictEqual(refused.message, SHARED_REFUSAL, JSON.stringify(other));
}
assert.lengthOf(staged, 1);

// Moving or cancelling: only a reminder of hers, set in this same chat with this agent.
yield* sql`INSERT INTO kin_events (event_id, org_id, agent_id, tag, priority, json, received_at, queued_at, state, wakeup_id)
VALUES ('earlier', ${orgId}, ${leads.a}, 'chat.message', 'P0', '{}', 0, 0, 'done', NULL)`;
const reminder = (id: string, fields: Partial<MemoryItemHead> = {}): MemoryItem => ({
head: head(id, {
kind: "deferral",
due: "2026-11-19T10:00:00+00:00",
on_due: "remind",
chat: KinChatId.make("member-direct"),
source_event: KinEventId.make("earlier"),
said_by: "fixture-member",
trust: "member",
source: "kin:chat:member-direct:message:earlier-message",
path: `deferrals/${id}.md`,
...fields,
}),
body,
});
state.items = [
reminder("def-hers"),
reminder("def-hers-too"),
reminder("def-owners", { said_by: "fixture-owner", trust: "owner" }),
reminder("def-someone-elses", { said_by: "fixture-other" }),
reminder("def-elsewhere", { chat: KinChatId.make("group-chat") }),
reminder("def-starts", { on_due: "start" }),
];
const cancel = (itemId: string) =>
service.cancel(scopeOf(leads.a), {
itemId: KinMemoryItemId.make(itemId),
sourceEventId: "member-said",
});
assert.isString((yield* defer({ what: "Invented: moved", replaces: "def-hers" })).itemId);
assert.deepStrictEqual(yield* cancel("def-hers-too"), { itemId: "def-hers-too" });
for (const other of ["def-owners", "def-someone-elses", "def-elsewhere", "def-starts"]) {
const moved = yield* defer({ what: `Invented ${other}`, replaces: other }).pipe(Effect.flip);
assert.strictEqual(moved.message, SHARED_REFUSAL, other);
assert.strictEqual((yield* Effect.flip(cancel(other))).message, SHARED_REFUSAL, other);
}
// Her replacement and her cancel, nothing for anyone else.
assert.lengthOf(staged, 4);
}).pipe(Effect.provide(layer)),
);
Loading