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
177 changes: 175 additions & 2 deletions apps/server/src/kin/lead/LeadMemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { KinHandoff } from "./Handoff.ts";
import { KinTeam } from "../team/KinTeam.ts";
import { SHARED_REFUSAL } from "./Audience.ts";
import { ownerOnlyWakeup } from "./Audience.testkit.ts";
import { markOutside, stampReads, storedStamp } from "./OutsideContent.ts";
import { markOutside, outsideToolAllowed, stampReads, storedStamp } from "./OutsideContent.ts";
import * as Lead from "./LeadMemory.ts";
import * as KinSchedules from "../triggers/kinSchedules.ts";
import { recordingSchedules } from "../triggers/kinSchedules.testkit.ts";
Expand Down Expand Up @@ -1234,6 +1234,139 @@ it.live(
)).message,
"That id is a feed's.",
);
const outsideTurn = { ...fixtureTurn, agentId };
const setOwnerWords = (body: string) =>
sql`UPDATE kin_messages SET body = ${body} WHERE org_id = ${orgId}
AND message_id = ${fixtureMessage}`;
const mail = { k: "mail", from: "arjun@junipercold.example" } as const;
const dailyInput = {
...scheduleInput,
what: "Check the inbox and flag what needs the owner.",
at: "09:00",
days: [0, 1, 2, 3, 4, 5, 6],
checkIn: true,
} as const;
const [daily] = yield* Effect.forEach(
[
"Check my inbox every morning at 9 and tell me what needs me.",
"Überprüfe jeden Morgen um 9 Uhr meinen Posteingang.",
"हर सुबह नौ बजे मेरा इनबॉक्स देखो।",
] as const,
Effect.fnUntraced(function* (body) {
yield* setOwnerWords(body);
const request = { ...dailyInput, title: body };
const made = yield* service.schedule(scope, request);
assert.deepInclude(nativeSaves.pop(), { scheduleId: made.itemId, checkIn: true });
yield* markOutside(outsideTurn, "invented-mail", [mail]);
expect
.soft(yield* outsideToolAllowed(outsideTurn, "kin_schedule", request), body)
.toBe(false);
yield* sql`DELETE FROM kin_wakeup_outside_reads WHERE wakeup_id = 'fixture-wakeup'`;
return made;
}),
);
yield* markOutside(outsideTurn, "invented-mail", [mail]);
assert.strictEqual(yield* madeUnder(daily.itemId), "");

const native = (itemId: string, source: string) =>
sql`INSERT INTO kin_schedules (task_id, org_id, schedule_id, agent_id, chat_id,
source_event, said_by, audience, content_sha256, inbox_thread_id, created_at, updated_at)
VALUES (${KinSchedules.scheduleTaskId(orgId, itemId)}, ${orgId}, ${itemId}, ${agentId},
${chat.chatId}, ${source}, ${person!.personId}, 'owner_only', 'hash', 'inbox', 0, 0)`;
const cancelling = (itemId: KinMemoryItemId) => ({
itemId,
sourceEventId: "fixture-event",
});
const olderRoutine = KinMemoryItemId.make("older-routine");
yield* native(olderRoutine, "fixture-event");
const oldCancel = yield* service
.cancel(scope, cancelling(olderRoutine))
.pipe(Effect.result);
expect
.soft(
oldCancel._tag === "Failure" ? oldCancel.failure.message : oldCancel.success.outcome,
)
.toContain("Offer it to the owner with kin_ask");
const oldReplace = yield* service
.defer(scope, {
...deferred,
what: "Take another look",
replaces: olderRoutine,
})
.pipe(Effect.result);
expect
.soft(
oldReplace._tag === "Failure"
? oldReplace.failure.message
: oldReplace.success.scheduled,
)
.toContain("Offer it to the owner with kin_ask");
expect
.soft((yield* KinSchedules.readSchedule(sql, orgId, olderRoutine))?.deletedAt)
.toBeNull();
yield* sql`DELETE FROM kin_schedules WHERE org_id = ${orgId} AND schedule_id = ${olderRoutine}`;
if (nativeCancels.at(-1) === olderRoutine) nativeCancels.pop();
const ownCancel = yield* service.cancel(scope, cancelling(daily.itemId));
assert.strictEqual(ownCancel.outcome, "stopped");
assert.strictEqual(nativeCancels.pop(), daily.itemId);

const pendingInput = {
...deferred,
what: "Recheck the daily mail",
onDue: "remind",
} as const;
const pending = yield* service.defer(scope, pendingInput);
const pendingCancel = yield* service
.cancel(scope, cancelling(pending.itemId))
.pipe(Effect.result);
expect
.soft(
pendingCancel._tag === "Success"
? pendingCancel.success.outcome
: pendingCancel.failure.message,
)
.toBe("stopped");

expect
.soft((yield* service.defer(scope, pendingInput).pipe(Effect.result))._tag)
.toBe("Failure");

const checkIn = {
...routineEvent,
_tag: "pulse",
checkIn: {
scheduleId: daily.itemId,
title: "Inbox check",
what: "Check the inbox.",
slot: "morning",
},
factKeys: [],
};
yield* sql`UPDATE kin_events SET tag = 'pulse', json = ${JSON.stringify(checkIn)}
WHERE event_id = 'routine-event'`;
yield* sql`UPDATE kin_events SET wakeup_id = NULL WHERE event_id = 'fixture-event'`;
const later = yield* service.defer(scope, {
...deferred,
what: "Look again at 3",
onDue: "start",
sourceEventId: "routine-event",
});
assert.equal(later.scheduled, "start");
assert.deepEqual(stampReads((yield* madeUnder(later.itemId)) ?? ""), [mail]);
expect
.soft(
yield* outsideToolAllowed(outsideTurn, "kin_schedule", {
...scheduleInput,
sourceEventId: "routine-event",
}),
)
.toBe(false);
yield* sql`UPDATE kin_events SET tag = 'routine', json = ${JSON.stringify(routineEvent)}
WHERE event_id = 'routine-event'`;
yield* sql`UPDATE kin_events SET wakeup_id = 'fixture-wakeup' WHERE event_id = 'fixture-event'`;
yield* setOwnerWords(message.body);
yield* sql`DELETE FROM kin_wakeup_outside_reads WHERE wakeup_id = 'fixture-wakeup'`;

// The owner's own time, as he said it, is kept; the native task runs it on the host's clock.
const scheduled = yield* Effect.gen(function* () {
using _host = vi.spyOn(KinSchedules, "hostTimeZone").mockReturnValue("Asia/Tokyo");
Expand Down Expand Up @@ -1305,6 +1438,7 @@ it.live(
yield* git(remote, ["show", `${committed.commit}:deferrals/${asked.itemId}.md`]),
"owner: fixture-member",
);
assert.notInclude(files, `deferrals/${pending.itemId}.md`);
const content = yield* git(remote, [
"show",
`${committed.commit}:deferrals/${first.itemId}.md`,
Expand Down Expand Up @@ -1386,7 +1520,7 @@ it.live(
yield* sql`UPDATE kin_wakeups SET status = 'completed' WHERE wakeup_id = 'fixture-wakeup'`;
// A turn that only set up a schedule or posted a review saved it at once: its end is no restart.
yield* sql`INSERT INTO kin_wakeups (wakeup_id, org_id, agent_id, thread_id, run_id, model, started_at, status, event_ids, audience) SELECT 'schedule-wakeup', org_id, agent_id, thread_id, run_id, model, started_at, 'running', event_ids, audience FROM kin_wakeups WHERE wakeup_id = 'fixture-wakeup'`;
yield* sql`INSERT INTO kin_lead_tool_calls (org_id, wakeup_id, call_key, request_hash) VALUES (${orgId}, 'schedule-wakeup', 'schedule:sch-fixture', 'fixture-hash'), (${orgId}, 'schedule-wakeup', 'github-review:evt-fixture', 'posting')`;
yield* sql`INSERT INTO kin_lead_tool_calls (org_id, wakeup_id, call_key, request_hash) VALUES (${orgId}, 'schedule-wakeup', 'schedule:sch-fixture', 'fixture-hash'), (${orgId}, 'schedule-wakeup', 'github-review:evt-fixture', 'posting'), (${orgId}, 'schedule-wakeup', 'setup:sch-fixture', 'schedule')`;
yield* writerService.commitWakeup(orgId, "schedule-wakeup", "completed");
assert.isFalse(notices.some((notice) => notice.includes("lost on restart")));
yield* sql`UPDATE kin_wakeups SET status = 'completed' WHERE wakeup_id = 'schedule-wakeup'`;
Expand Down Expand Up @@ -1801,6 +1935,45 @@ it.live(
const cleared = yield* git(remote, ["show", "main:claims/amount-claim.md"]);
assert.include(cleared, "sensitive: false");
assert.include(cleared, `person: ${person!.personId}`);
yield* sql`UPDATE kin_chat_members SET left_at = '2026-11-08T00:00:00.000Z'
WHERE member_id = 'fixture-member-membership'`;
yield* markOutside({ orgId, wakeupId: "team-wakeup" }, "invented-mail", [mail]);
const nextMessage = yield* chats.send(person!, {
chatId: chat.chatId,
body: "हर सुबह नौ बजे मेरा इनबॉक्स देखो।",
clientMessageId: "next-own-ask",
});
yield* sql`UPDATE kin_wakeups SET status = 'completed' WHERE wakeup_id = 'team-wakeup'`;
yield* (yield* Orchestrator.OrchestratorV2).dispatch({
type: "run.interrupt",
commandId: CommandId.make("interrupt:team-wakeup"),
threadId,
runId: teamRun.id,
});
const nextId = KinWakeupId.make("next-owner-wakeup");
const nextRun = yield* startWakeupRun(threadId, nextId);
yield* sql`INSERT INTO kin_wakeups (wakeup_id, org_id, agent_id, thread_id, run_id, model,
started_at, status, event_ids, audience)
VALUES (${nextId}, ${orgId}, ${agentId}, ${threadId}, ${nextRun.id}, 'fixture',
${yield* Clock.currentTimeMillis}, 'running', '["next-owner-event"]', 'owner_only')`;
const nextEvent = {
...event,
eventId: "next-owner-event",
messageId: nextMessage.messageId,
provenance: nextMessage.author,
};
yield* sql`INSERT INTO kin_events (event_id, org_id, agent_id, tag, priority, json,
received_at, queued_at, state, wakeup_id)
VALUES ('next-owner-event', ${orgId}, ${agentId}, 'chat.message', 'P0',
${JSON.stringify(nextEvent)}, 0, 0, 'claimed', ${nextId})`;
const nextDaily = yield* service.schedule(scope, {
...dailyInput,
sourceEventId: nextEvent.eventId,
});
assert.deepInclude(nativeSaves.at(-1), {
scheduleId: nextDaily.itemId,
sourceEvent: nextEvent.eventId,
});
}).pipe(Effect.provide(toolkit));
}).pipe(
Effect.provide(
Expand Down
Loading
Loading