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
6 changes: 4 additions & 2 deletions apps/server/src/kin/cli/lead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ const charterFlag = Flag.String("charter").pipe(

const workerProjectsFlag = Flag.String("worker-project").pipe(
Flag.withSchema(ProjectId),
Flag.withDescription("A project the lead's workers may run in. Repeat for more."),
Flag.withDescription(
"A project the lead is held to in chats its owner didn't put it in. Repeat for more. The owner's own chats with it reach every repository in the worker environment.",
),
Flag.atLeast(0),
);

Expand Down Expand Up @@ -270,7 +272,7 @@ const projectsCommand = Command.make("projects", {
json: jsonFlag,
}).pipe(
Command.withDescription(
"Replace a live lead's worker projects. Pass none to clear them. Applies to its next tool call.",
"Replace the projects a live lead is held to in chats its owner didn't put it in. Pass none to clear them. Applies to its next tool call.",
),
Command.withHandler((flags) =>
runOnHost(flags, (provision) =>
Expand Down
38 changes: 32 additions & 6 deletions apps/server/src/kin/lead/KinLevels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,49 @@ export const originLevel = (orgId: KinOrgId, origin: KinOrigin) =>
);

/**
* The lowest level of the chats the wake-up's due deferrals were asked from, or undefined when
* none was asked from outside its owner chat. Read from the stored events only.
* Whether the lead's owner placed it in `chatId` (`ownerPlacedSql`). False for a retired lead or a
* chat in another org.
*/
export const originCap = Effect.fn("KinLevels.originCap")(function* (input: {
export const ownerPlaced = Effect.fn("KinLevels.ownerPlaced")(function* (
orgId: KinOrgId,
origin: KinOrigin,
) {
const sql = yield* SqlClient.SqlClient;
const [row] = yield* sql<{ ownerPlaced: number }>`
SELECT ${ownerPlacedSql(sql, origin.chatId)} AS "ownerPlaced" FROM kin_leads l
JOIN kin_agents a ON a.org_id = l.org_id AND a.agent_id = l.agent_id
WHERE l.org_id = ${orgId} AND l.agent_id = ${origin.agentId}
AND a.role = 'lead' AND a.retired_at IS NULL
ORDER BY l.agent_id`.pipe(Effect.mapError(toPersistenceSqlError("KinLevels.ownerPlaced")));
return row?.ownerPlaced === 1;
});

/** The chats the wake-up's due deferrals were asked from, read from the stored events only. */
export const origins = Effect.fn("KinLevels.origins")(function* (input: {
readonly orgId: KinOrgId;
readonly agentId: KinAgentId;
readonly wakeupId: string;
}) {
const sql = yield* SqlClient.SqlClient;
const origins = yield* sql<{ agentId: KinAgentId; chatId: KinChatId }>`
return yield* sql<KinOrigin>`
SELECT DISTINCT json_extract(json, '$.origin.agentId') AS "agentId",
json_extract(json, '$.origin.chatId') AS "chatId" FROM kin_events
WHERE org_id = ${input.orgId} AND agent_id = ${input.agentId} AND wakeup_id = ${input.wakeupId}
AND tag = 'deferral.due' AND json_extract(json, '$.origin.chatId') IS NOT NULL
ORDER BY 1, 2`.pipe(Effect.mapError(toPersistenceSqlError("KinLevels.originCap")));
ORDER BY 1, 2`.pipe(Effect.mapError(toPersistenceSqlError("KinLevels.origins")));
});

/**
* The lowest level of the chats the wake-up's due deferrals were asked from, or undefined when
* none was asked from outside its owner chat.
*/
export const originCap = Effect.fn("KinLevels.originCap")(function* (input: {
readonly orgId: KinOrgId;
readonly agentId: KinAgentId;
readonly wakeupId: string;
}) {
let cap: KinLevel | undefined;
for (const origin of origins) {
for (const origin of yield* origins(input)) {
const level = yield* originLevel(input.orgId, origin);
cap = cap === undefined ? level : lowest(cap, level);
}
Expand Down
Loading