Compare commits

...
1 Commits
Author SHA1 Message Date
tben 4852c39ad3 Scope a workspace run to its workspace
CI / check (push) Canceled after 0s
Release image / image (release) Canceled after 0s
Release image / computer-image (release) Canceled after 0s
A bot working in a workspace could reach every task in every other
workspace it belongs to, and every routine and hook it has anywhere.
botTaskScopesSql now takes the run's workspace and reaches only its
tasks: a coordinator or that workspace's admin all of them, a worker its
own and the workspace-wide ones there. grabTasks, grabRunTask and every
task tool pass it through, and create_task refuses to place a task in
another workspace from inside one.

Routines and hooks follow the same rule through the existing place
filters: list, update, delete, pause, search and read are narrowed to the
run's workspace, the coordinator's list showing every bot's there. A run
in the bot's own chat keeps its old reach, and update/delete schedule
keep no place filter when the run has none, since null there means "own
chat only".

v0.28.3
2026-11-25 03:52:51 +01:00
34 changed files with 302 additions and 69 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "cobble",
"version": "0.28.2",
"version": "0.28.3",
"module": "index.ts",
"type": "module",
"private": true,
@@ -9,18 +9,30 @@ type Params = {
* Only a call to this bot's hooks.
*/
bot_id?: number | string | null;
/**
* Only a call to a hook that runs in this workspace.
*/
workspace_id?: number | string | null;
};
/**
* One call to one of the user's hooks, whole: what it sent and what its
* run answered. Throws when it isn't theirs (or that bot's).
* run answered. Throws when it isn't theirs (or that bot's, or that
* workspace's).
*/
export default async function grabBotHookEvent({ user_id, event_id, bot_id }: Params) {
export default async function grabBotHookEvent({ user_id, event_id, bot_id, workspace_id }: Params) {
const res = await BunSQLite.sql<BotHookCall>({
sql: `SELECT e.*, h.title AS hook_title, h.bot_id, h.workspace_id
FROM bot_hook_events e JOIN bot_hooks h ON h.id = e.hook_id
WHERE e.id = ? AND h.user_id = ? AND (? IS NULL OR h.bot_id = ?)`,
values: [Number(event_id), user_id, bot_id ? Number(bot_id) : null, bot_id ? Number(bot_id) : null],
WHERE e.id = ? AND h.user_id = ? AND (? IS NULL OR h.bot_id = ?) AND (? IS NULL OR h.workspace_id = ?)`,
values: [
Number(event_id),
user_id,
bot_id ? Number(bot_id) : null,
bot_id ? Number(bot_id) : null,
workspace_id ? Number(workspace_id) : null,
workspace_id ? Number(workspace_id) : null,
],
});
const call = res.payload?.[0];
@@ -11,7 +11,9 @@ type Params = {
bot_id?: number | string;
/**
* With `bot_id`: only the hooks of that conversation (null is the
* bot's own chat), as `grabCronJobs` does for routines.
* bot's own chat), as `grabCronJobs` does for routines. Without
* `bot_id`: every bot's hooks in that workspace (a coordinator working
* there).
*/
workspace_id?: number | string | null;
/**
@@ -46,6 +48,9 @@ export default async function grabBotHooks({
where.push(scope.sql);
values.push(...scope.values);
}
} else if (workspace_id) {
where.push(`h.workspace_id = ?`);
values.push(workspace_id);
}
if (hook_id) {
@@ -4,13 +4,17 @@ import grabBotHooks from "./grab-bot-hooks";
type Params = {
user_id: number | string;
hook_id: number | string;
/**
* Only if it runs in that workspace (a run working there).
*/
workspace_id?: number | string | null;
};
/**
* One of the user's hooks. Throws when it isn't theirs.
*/
export default async function grabUserBotHook({ user_id, hook_id }: Params) {
const [hook] = await grabBotHooks({ user_id, hook_id });
export default async function grabUserBotHook({ user_id, hook_id, workspace_id }: Params) {
const [hook] = await grabBotHooks({ user_id, hook_id, workspace_id });
if (!hook?.id) {
throw new AppError(`Hook #${hook_id} not found.`);
@@ -8,6 +8,10 @@ export type SearchHookCallsParams = {
user_id: number | string;
hook_id?: number | string | null;
bot_id?: number | string | null;
/**
* Only calls to hooks that run in this workspace (a run working there).
*/
workspace_id?: number | string | null;
/**
* Words to find in what a call sent or what its run answered.
*/
@@ -33,6 +37,7 @@ export default function searchHookCallsSql({
user_id,
hook_id,
bot_id,
workspace_id,
query,
status,
before_id,
@@ -52,6 +57,11 @@ export default function searchHookCallsSql({
values.push(Number(bot_id));
}
if (workspace_id) {
where.push(`h.workspace_id = ?`);
values.push(Number(workspace_id));
}
if (status) {
where.push(`e.status = ?`);
values.push(status);
@@ -8,6 +8,10 @@ type Params = {
* Only if it's this bot's (a bot managing its own schedules).
*/
bot_id?: number | string;
/**
* Only if it runs in that workspace (a run working there).
*/
workspace_id?: number | string | null;
};
/**
@@ -15,8 +19,8 @@ type Params = {
* The sessions its runs held stay, as the user's own threads (they're
* ordinary sessions once no routine owns them).
*/
export default async function deleteCronJob({ user_id, cron_job_id, bot_id }: Params) {
const cron_job = await grabUserCronJob({ user_id, cron_job_id, bot_id });
export default async function deleteCronJob({ user_id, cron_job_id, bot_id, workspace_id }: Params) {
const cron_job = await grabUserCronJob({ user_id, cron_job_id, bot_id, workspace_id });
const detached = await BunSQLite.sql({
sql: `UPDATE bot_sessions SET routine_id = NULL, updated_at = ? WHERE routine_id = ?`,
@@ -12,7 +12,8 @@ type Params = {
/**
* With `bot_id`: only the routines of that conversation, here
* (`routineScopeSql`). `null` is the bot's own chat, so it has to be
* given rather than left out to mean "everywhere".
* given rather than left out to mean "everywhere". Without `bot_id`:
* every bot's routines in that workspace (a coordinator working there).
*/
workspace_id?: number | string | null;
/**
@@ -48,6 +49,9 @@ export default async function grabCronJobs({
where.push(scope.sql);
values.push(...scope.values);
}
} else if (workspace_id) {
where.push(`c.workspace_id = ?`);
values.push(workspace_id);
}
if (cron_job_id) {
@@ -8,13 +8,17 @@ type Params = {
* Only if it's this bot's (a bot managing its own schedules).
*/
bot_id?: number | string;
/**
* Only if it runs in that workspace (a run working there).
*/
workspace_id?: number | string | null;
};
/**
* One of the user's schedules. Throws when it isn't theirs (or the bot's).
*/
export default async function grabUserCronJob({ user_id, cron_job_id, bot_id }: Params) {
const [cron_job] = await grabCronJobs({ user_id, bot_id, cron_job_id });
export default async function grabUserCronJob({ user_id, cron_job_id, bot_id, workspace_id }: Params) {
const [cron_job] = await grabCronJobs({ user_id, bot_id, workspace_id, cron_job_id });
if (!cron_job?.id) {
throw new AppError(`Schedule #${cron_job_id} not found.`);
@@ -10,6 +10,10 @@ type Params = {
* Only if it's this bot's (a bot managing its own schedules).
*/
bot_id?: number | string;
/**
* Only if it runs in that workspace (a run working there).
*/
workspace_id?: number | string | null;
/**
* Only the fields given are changed. `max_runs: null` runs it until
* it's stopped.
@@ -28,8 +32,8 @@ type Params = {
* on, counts the next run from now; turning a finished one back on (all
* its `max_runs` done) starts its count over. Returns the schedule.
*/
export default async function updateCronJob({ user_id, cron_job_id, bot_id, data }: Params) {
const cron_job = await grabUserCronJob({ user_id, cron_job_id, bot_id });
export default async function updateCronJob({ user_id, cron_job_id, bot_id, workspace_id, data }: Params) {
const cron_job = await grabUserCronJob({ user_id, cron_job_id, bot_id, workspace_id });
const schedule = data.schedule?.trim() || cron_job.schedule || "";
const schedule_changed = Boolean(data.schedule?.trim()) && schedule != cron_job.schedule;
@@ -79,5 +83,5 @@ export default async function updateCronJob({ user_id, cron_job_id, bot_id, data
throw new Error(`Couldn't update the schedule: ${res.error}`);
}
return await grabUserCronJob({ user_id, cron_job_id: cron_job.id! });
return await grabUserCronJob({ user_id, cron_job_id: cron_job.id!, bot_id, workspace_id });
}
@@ -15,6 +15,10 @@ type Params = {
* The workspaces the bot is admin of (`grabBotRunPowers`).
*/
admin_workspace_ids?: number[];
/**
* The workspace the run is in: only its tasks are in reach.
*/
workspace_id?: number | string | null;
};
/**
@@ -30,8 +34,9 @@ export default async function grabRunTask({
bot_id,
is_coordinator,
admin_workspace_ids,
workspace_id,
}: Params): Promise<BUN_SQLITE_COBBLE_TASKS> {
const read = runTaskSql({ task_id, bot_id, is_coordinator, admin_workspace_ids });
const read = runTaskSql({ task_id, bot_id, is_coordinator, admin_workspace_ids, workspace_id });
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_TASKS>(read);
+7 -1
View File
@@ -23,6 +23,11 @@ type Params = {
* The workspaces the bot is admin of (`grabBotRunPowers`).
*/
admin_workspace_ids?: number[];
/**
* The workspace the run is in: with `bot_id`, only that workspace's
* tasks are in reach (`botTaskScopesSql`).
*/
workspace_id?: number | string | null;
/**
* Only the tasks this bot has (anywhere, unless narrowed).
*/
@@ -64,6 +69,7 @@ export default async function grabTasks({
bot_id,
is_coordinator,
admin_workspace_ids,
workspace_id,
only_bot_id,
with_workspace_wide,
only_workspace_id,
@@ -81,7 +87,7 @@ export default async function grabTasks({
where.push(access.sql);
values.push(...access.values);
} else if (bot_id != null) {
const access = botTaskScopesSql({ bot_id, is_coordinator: Boolean(is_coordinator), admin_workspace_ids });
const access = botTaskScopesSql({ bot_id, is_coordinator: Boolean(is_coordinator), admin_workspace_ids, workspace_id });
where.push(access.sql);
values.push(...access.values);
}
@@ -21,8 +21,18 @@ db.run(`INSERT INTO tasks VALUES
(7, 'Another bot''s, in a workspace it works in', 11, 5, 'todo')`);
db.run(`INSERT INTO workspace_bots VALUES (5, 10)`);
function task(bot_id: number, task_id: number, options: { is_coordinator?: boolean; admin_workspace_ids?: number[] } = {}) {
const { sql, values } = runTaskSql({ task_id, bot_id, is_coordinator: options.is_coordinator ?? false, admin_workspace_ids: options.admin_workspace_ids });
function task(
bot_id: number,
task_id: number,
options: { is_coordinator?: boolean; admin_workspace_ids?: number[]; workspace_id?: number } = {},
) {
const { sql, values } = runTaskSql({
task_id,
bot_id,
is_coordinator: options.is_coordinator ?? false,
admin_workspace_ids: options.admin_workspace_ids,
workspace_id: options.workspace_id,
});
return db.query(sql).get(...values) as { id: number } | null;
}
@@ -50,6 +60,20 @@ describe("runTaskSql", () => {
expect(task(10, 6)).toBeNull();
});
test("working in a workspace, it reaches that workspace alone", () => {
expect(task(10, 3, { workspace_id: 5 })?.id).toBe(3);
expect(task(10, 4, { workspace_id: 5 })?.id).toBe(4);
expect(task(10, 1, { workspace_id: 5 })).toBeNull();
expect(task(10, 5, { workspace_id: 5 })).toBeNull();
expect(task(10, 6, { workspace_id: 5 })).toBeNull();
expect(task(10, 3, { workspace_id: 6 })).toBeNull();
});
test("the coordinator in a workspace reaches that workspace alone", () => {
expect(task(1, 5, { is_coordinator: true, workspace_id: 6 })?.id).toBe(5);
expect(task(1, 2, { is_coordinator: true, workspace_id: 6 })).toBeNull();
});
test("the coordinator reaches every task, whoever has it", () => {
expect(task(1, 2, { is_coordinator: true })?.id).toBe(2);
expect(task(1, 5, { is_coordinator: true })?.id).toBe(5);
@@ -60,6 +84,12 @@ describe("runTaskSql", () => {
expect(task(10, 5, { admin_workspace_ids: [6] })?.id).toBe(5);
expect(task(10, 6, { admin_workspace_ids: [6] })).toBeNull();
});
test("and in a run elsewhere, only what that run's workspace holds", () => {
expect(task(10, 5, { admin_workspace_ids: [6], workspace_id: 6 })?.id).toBe(5);
expect(task(10, 6, { admin_workspace_ids: [6], workspace_id: 6 })).toBeNull();
expect(task(10, 5, { admin_workspace_ids: [6], workspace_id: 5 })).toBeNull();
});
});
describe("taskRowSql", () => {
@@ -6,14 +6,19 @@ type Params = {
bot_id: number | string;
is_coordinator: boolean;
admin_workspace_ids?: number[];
/**
* The workspace the run is in: only its tasks are in reach
* (`botTaskScopesSql`).
*/
workspace_id?: number | string | null;
};
/**
* The read `grabRunTask` makes: the task, but only while the run reaches
* it. Split out so the reach rules can be run against real SQLite.
*/
export function runTaskSql({ task_id, bot_id, is_coordinator, admin_workspace_ids }: Params): SqlCondition {
const access = botTaskScopesSql({ bot_id, is_coordinator, admin_workspace_ids });
export function runTaskSql({ task_id, bot_id, is_coordinator, admin_workspace_ids, workspace_id }: Params): SqlCondition {
const access = botTaskScopesSql({ bot_id, is_coordinator, admin_workspace_ids, workspace_id });
return {
sql: `SELECT t.* FROM tasks t WHERE t.id = ? AND ${access.sql} LIMIT 1`,
+1 -1
View File
@@ -165,7 +165,7 @@ export default function delegateTaskTool({
* worked in that workspace, so the crew comes from there.
*/
const task = args.task_id
? await grabRunTask({ task_id: args.task_id, bot_id: requester_id, is_coordinator, admin_workspace_ids })
? await grabRunTask({ task_id: args.task_id, bot_id: requester_id, is_coordinator, admin_workspace_ids, workspace_id })
: null;
const asked_workspace_id = workspace_id || args.workspace_id;
@@ -9,6 +9,10 @@ type Params = {
* The coordinator deletes any of the user's routines, not only its own.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: only a routine that runs there.
*/
workspace_id?: number | string | null;
};
type Args = {
@@ -19,7 +23,7 @@ type Args = {
* Lets a bot delete one of its own schedules, or as the coordinator any of
* the user's.
*/
export default function deleteScheduleTool({ user_id, bot_id, is_coordinator }: Params): HarnessTool<Args> {
export default function deleteScheduleTool({ user_id, bot_id, is_coordinator, workspace_id }: Params): HarnessTool<Args> {
return {
name: "delete_schedule",
description: is_coordinator
@@ -33,6 +37,7 @@ export default function deleteScheduleTool({ user_id, bot_id, is_coordinator }:
user_id,
cron_job_id: args.schedule_id,
...(is_coordinator ? {} : { bot_id }),
...(workspace_id ? { workspace_id } : {}),
});
return `Schedule #${cron_job.id} "${cron_job.title}" deleted.`;
@@ -247,6 +247,7 @@ export default async function grabBotChatTools({
powers,
workers,
grabTargets: () => askableBots({ user_id, requester_bot_id: bot_id, workspace_id }),
workspace_id,
onTask,
}),
);
+3 -3
View File
@@ -112,10 +112,10 @@ export default async function grabBotTools({
? []
: [
createScheduleTool({ user_id, bot_id, workspace_id, computers }),
updateScheduleTool({ user_id, bot_id, is_coordinator }),
deleteScheduleTool({ user_id, bot_id, is_coordinator }),
updateScheduleTool({ user_id, bot_id, is_coordinator, workspace_id }),
deleteScheduleTool({ user_id, bot_id, is_coordinator, workspace_id }),
]),
listSchedulesTool({ user_id, bot_id, is_coordinator }),
listSchedulesTool({ user_id, bot_id, is_coordinator, workspace_id }),
...grabBotHookTools({ user_id, bot_id, session_id, workspace_id, computers, hooked, is_coordinator, onPropose }),
...(session_id ? [reactToMessageTool({ bot_id, user_id, session_id })] : []),
/**
+7 -1
View File
@@ -32,6 +32,11 @@ type Params = {
* there: the ones it could ask (`askableBots`).
*/
grabTargets: () => Promise<BUN_SQLITE_COBBLE_BOTS[]>;
/**
* The workspace the run is in: a task it hands over is one of that
* workspace's, never another workspace's.
*/
workspace_id?: number | string | null;
/**
* Called with the task it hands over, so the message the run posts links
* to it (`bot_messages.task_ids`).
@@ -62,7 +67,7 @@ type Args = {
* for the move the bot asks first. The other bot works that same task, and
* the result is reported back in this session.
*/
export default function handOverTaskTool({ user_id, bot, session_id, powers, workers, grabTargets, onTask }: Params): HarnessTool<Args> {
export default function handOverTaskTool({ user_id, bot, session_id, powers, workers, grabTargets, workspace_id, onTask }: Params): HarnessTool<Args> {
const bot_id = bot.id || 0;
const crew = workers.map((worker) => `${worker.id}: ${worker.name}`).join("; ");
@@ -90,6 +95,7 @@ export default function handOverTaskTool({ user_id, bot, session_id, powers, wor
bot_id,
is_coordinator: powers.is_coordinator,
admin_workspace_ids: powers.admin_workspace_ids,
workspace_id,
});
/**
@@ -10,6 +10,10 @@ type Params = {
* The coordinator deletes any of the user's hooks, not only its own.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: only a hook that runs there.
*/
workspace_id?: number | string | null;
};
type Args = {
@@ -20,7 +24,7 @@ type Args = {
* Lets a bot delete one of its hooks for good, or as the coordinator any of
* the user's.
*/
export default function deleteHookTool({ user_id, bot_id, is_coordinator }: Params): HarnessTool<Args> {
export default function deleteHookTool({ user_id, bot_id, is_coordinator, workspace_id }: Params): HarnessTool<Args> {
return {
name: "delete_hook",
description: `Delete ${
@@ -30,7 +34,7 @@ export default function deleteHookTool({ user_id, bot_id, is_coordinator }: Para
hook_id: z.number().int().positive(),
}),
run: async ({ args }) => {
const hook = await grabOwnHook({ user_id, bot_id, hook_id: args.hook_id, is_coordinator });
const hook = await grabOwnHook({ user_id, bot_id, hook_id: args.hook_id, is_coordinator, workspace_id });
await deleteBotHook({ user_id, hook_id: hook.id! });
@@ -50,11 +50,11 @@ export default function grabBotHookTools({
if (hooked) return [];
return [
listHooksTool({ user_id, bot_id, is_coordinator }),
searchHookCallsTool({ user_id, bot_id, is_coordinator }),
readHookCallTool({ user_id, bot_id, is_coordinator }),
pauseHookTool({ user_id, bot_id, is_coordinator }),
deleteHookTool({ user_id, bot_id, is_coordinator }),
listHooksTool({ user_id, bot_id, is_coordinator, workspace_id }),
searchHookCallsTool({ user_id, bot_id, is_coordinator, workspace_id }),
readHookCallTool({ user_id, bot_id, is_coordinator, workspace_id }),
pauseHookTool({ user_id, bot_id, is_coordinator, workspace_id }),
deleteHookTool({ user_id, bot_id, is_coordinator, workspace_id }),
...(session_id && onPropose
? [proposeHookTool({ user_id, bot_id, session_id, workspace_id, computers, onPropose })]
: []),
+8 -3
View File
@@ -9,15 +9,20 @@ type Params = {
* The coordinator reaches every hook the user has, not only its own.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: only a hook that runs there.
*/
workspace_id?: number | string | null;
};
/**
* One of the user's hooks that this bot runs, or an `AppError`: a bot
* never touches another bot's hooks, except the coordinator, which the
* user made responsible for the whole crew.
* user made responsible for the whole crew. In a workspace only that
* workspace's hooks are within reach.
*/
export default async function grabOwnHook({ user_id, bot_id, hook_id, is_coordinator }: Params) {
const hook = await grabUserBotHook({ user_id, hook_id });
export default async function grabOwnHook({ user_id, bot_id, hook_id, is_coordinator, workspace_id }: Params) {
const hook = await grabUserBotHook({ user_id, hook_id, workspace_id });
if (!is_coordinator && String(hook.bot_id) != String(bot_id)) {
throw new AppError(`Hook #${hook_id} isn't one of yours.`);
+17 -7
View File
@@ -12,6 +12,10 @@ type Params = {
* crew's hooks, not only its own.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: only the hooks that run there.
*/
workspace_id?: number | string | null;
};
type Args = {
@@ -21,14 +25,14 @@ type Args = {
/**
* Lets a bot see the user's hooks it runs, everywhere it works, never
* their URLs. The coordinator sees the crew's, each naming the bot that
* runs it.
* runs it. In a workspace only that workspace's hooks are in reach.
*/
export default function listHooksTool({ user_id, bot_id, is_coordinator }: Params): HarnessTool<Args> {
export default function listHooksTool({ user_id, bot_id, is_coordinator, workspace_id }: Params): HarnessTool<Args> {
return {
name: "list_hooks",
description: is_coordinator
? `List the hooks that give the user's bots work: id, name, which bot runs it, what calls it (an outside service at its secret URL, or browser notifications on a computer), where it runs, whether it's paused, its runs, last call and last error. Pass bot_id to see only one bot's. To see what calls sent and what the runs answered, use search_hook_calls. You never see a hook's URL: only the user does. This is a list, not a census of the install: give what it holds and never conclude from it that a hook does not exist.`
: `List the user's hooks that give you work: id, name, what calls it (an outside service at its secret URL, or browser notifications on a computer), where it runs, whether it's paused, its runs, last call and last error. Only the ones that run as you: another bot's hooks are not in this list, so never say the user has no hook from it, since they may have one on another bot. To see what calls sent and what your runs answered, use search_hook_calls. You never see a hook's URL: only the user does.`,
? `List ${workspace_id ? "the hooks that run in this workspace, on any of its bots" : "the hooks that give the user's bots work"}: id, name, which bot runs it, what calls it (an outside service at its secret URL, or browser notifications on a computer), where it runs, whether it's paused, its runs, last call and last error. Pass bot_id to see only one bot's. To see what calls sent and what the runs answered, use search_hook_calls. You never see a hook's URL: only the user does. This is a list, not a census of the install: give what it holds and never conclude from it that a hook does not exist.`
: `List the hooks ${workspace_id ? "that run in this workspace and give you work" : "that give you work"}: id, name, what calls it (an outside service at its secret URL, or browser notifications on a computer), where it runs, whether it's paused, its runs, last call and last error.${workspace_id ? " Nothing outside this workspace is in reach." : " Only the ones that run as you: another bot's hooks are not in this list, so never say the user has no hook from it, since they may have one on another bot."} To see what calls sent and what your runs answered, use search_hook_calls. You never see a hook's URL: only the user does.`,
schema: is_coordinator
? z.object({
bot_id: z.number().int().positive().optional().describe("Only this bot's hooks"),
@@ -37,15 +41,21 @@ export default function listHooksTool({ user_id, bot_id, is_coordinator }: Param
run: async ({ args }) => {
const hooks = await grabBotHooks({
user_id,
...(is_coordinator ? (args.bot_id ? { bot_id: args.bot_id } : {}) : { bot_id }),
...(is_coordinator
? { ...(args.bot_id ? { bot_id: args.bot_id } : {}), ...(workspace_id ? { workspace_id } : {}) }
: { bot_id, ...(workspace_id ? { workspace_id } : {}) }),
});
if (!hooks.length) {
if (is_coordinator) {
return args.bot_id ? `Bot ${botRef({ id: args.bot_id })} has no hooks.` : `The user's crew has no hooks.`;
if (args.bot_id) return `Bot ${botRef({ id: args.bot_id })} has no hooks${workspace_id ? " in this workspace" : ""}.`;
return workspace_id ? `There are no hooks in this workspace.` : `The user's crew has no hooks.`;
}
return `You have no hooks of your own. Other bots' hooks aren't in this list.`;
return workspace_id
? `There are no hooks in this workspace. Hooks elsewhere aren't in this list.`
: `You have no hooks of your own. Other bots' hooks aren't in this list.`;
}
return hooks
@@ -11,6 +11,10 @@ type Params = {
* The coordinator pauses any of the user's hooks, not only its own.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: only a hook that runs there.
*/
workspace_id?: number | string | null;
};
type Args = {
@@ -22,7 +26,7 @@ type Args = {
* again. Only taking away needs no confirming; switching it back on is a
* proposal (`propose_hook`).
*/
export default function pauseHookTool({ user_id, bot_id, is_coordinator }: Params): HarnessTool<Args> {
export default function pauseHookTool({ user_id, bot_id, is_coordinator, workspace_id }: Params): HarnessTool<Args> {
return {
name: "pause_hook",
description: `Pause ${
@@ -32,7 +36,7 @@ export default function pauseHookTool({ user_id, bot_id, is_coordinator }: Param
hook_id: z.number().int().positive(),
}),
run: async ({ args }) => {
const hook = await grabOwnHook({ user_id, bot_id, hook_id: args.hook_id, is_coordinator });
const hook = await grabOwnHook({ user_id, bot_id, hook_id: args.hook_id, is_coordinator, workspace_id });
if (!hook.enabled) return `Hook #${hook.id} "${hook.title}" is already paused.`;
@@ -11,6 +11,10 @@ type Params = {
* The coordinator reads a call to any of the user's hooks.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: only a call to a hook that runs there.
*/
workspace_id?: number | string | null;
};
type Args = {
@@ -22,7 +26,7 @@ type Args = {
* outside data, and what its run answered. The coordinator reads any of
* the user's.
*/
export default function readHookCallTool({ user_id, bot_id, is_coordinator }: Params): HarnessTool<Args> {
export default function readHookCallTool({ user_id, bot_id, is_coordinator, workspace_id }: Params): HarnessTool<Args> {
return {
name: "read_hook_call",
description: `Read one call to ${
@@ -35,6 +39,7 @@ export default function readHookCallTool({ user_id, bot_id, is_coordinator }: Pa
const call = await grabBotHookEvent({
user_id,
...(is_coordinator ? {} : { bot_id }),
workspace_id,
event_id: args.call_id,
});
@@ -12,6 +12,10 @@ type Params = {
* The coordinator searches the calls to any of the user's hooks.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: only calls to hooks that run there.
*/
workspace_id?: number | string | null;
};
type Args = {
@@ -27,7 +31,7 @@ type Args = {
* each sent and what its run answered, newest first. Never another bot's,
* except the coordinator, which sees the crew's.
*/
export default function searchHookCallsTool({ user_id, bot_id, is_coordinator }: Params): HarnessTool<Args> {
export default function searchHookCallsTool({ user_id, bot_id, is_coordinator, workspace_id }: Params): HarnessTool<Args> {
return {
name: "search_hook_calls",
description: `Search the calls to ${
@@ -47,6 +51,7 @@ export default function searchHookCallsTool({ user_id, bot_id, is_coordinator }:
const calls = await grabBotHookEvents({
user_id,
...(is_coordinator ? {} : { bot_id }),
workspace_id,
hook_id: args.hook_id,
query: args.query,
status: args.status,
+19 -7
View File
@@ -12,6 +12,11 @@ type Params = {
* crew's routines, not only its own.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: then only the routines that run there,
* never another workspace's nor the bots' own chat.
*/
workspace_id?: number | string | null;
};
type Args = {
@@ -20,14 +25,15 @@ type Args = {
/**
* Lets a bot see its own schedules, everywhere it works. The coordinator
* sees the crew's, each naming the bot that runs it.
* sees the crew's, each naming the bot that runs it. In a workspace only
* that workspace's routines are in reach.
*/
export default function listSchedulesTool({ user_id, bot_id, is_coordinator }: Params): HarnessTool<Args> {
export default function listSchedulesTool({ user_id, bot_id, is_coordinator, workspace_id }: Params): HarnessTool<Args> {
return {
name: "list_schedules",
description: is_coordinator
? `List the schedules (routines) of every bot the user has: id, cron expression (UTC), which bot runs it, where, whether it's paused, its next run and its last error. Pass bot_id to see only one bot's. This is a list, not a census of the install: give what it holds and never conclude from it that a routine does not exist.`
: `List your own schedules (routines): id, cron expression (UTC), where each runs, whether it's paused, its next run and its last error. Only yours: another bot's routines are not in this list, so never say the user has no routine from it, since they may have one on another bot.`,
? `${workspace_id ? "List the schedules (routines) that run in this workspace, on any of its bots" : "List the schedules (routines) of every bot the user has"}: id, cron expression (UTC), which bot runs it, where, whether it's paused, its next run and its last error. Pass bot_id to see only one bot's. This is a list, not a census of the install: give what it holds and never conclude from it that a routine does not exist.`
: `List ${workspace_id ? "the schedules (routines) that run in this workspace" : "your own schedules (routines)"}: id, cron expression (UTC), where each runs, whether it's paused, its next run and its last error.${workspace_id ? " Nothing outside this workspace is in reach." : " Only yours: another bot's routines are not in this list, so never say the user has no routine from it, since they may have one on another bot."}`,
schema: is_coordinator
? z.object({
bot_id: z.number().int().positive().optional().describe("Only this bot's schedules"),
@@ -36,15 +42,21 @@ export default function listSchedulesTool({ user_id, bot_id, is_coordinator }: P
run: async ({ args }) => {
const cron_jobs = await grabCronJobs({
user_id,
...(is_coordinator ? (args.bot_id ? { bot_id: args.bot_id } : {}) : { bot_id }),
...(is_coordinator
? { ...(args.bot_id ? { bot_id: args.bot_id } : {}), ...(workspace_id ? { workspace_id } : {}) }
: { bot_id, ...(workspace_id ? { workspace_id } : {}) }),
});
if (!cron_jobs.length) {
if (is_coordinator) {
return args.bot_id ? `Bot ${botRef({ id: args.bot_id })} has no schedules.` : `The user's crew has no schedules.`;
if (args.bot_id) return `Bot ${botRef({ id: args.bot_id })} has no schedules${workspace_id ? " in this workspace" : ""}.`;
return workspace_id ? `There are no schedules in this workspace.` : `The user's crew has no schedules.`;
}
return `You have no schedules of your own. Other bots' routines aren't in this list.`;
return workspace_id
? `There are no schedules in this workspace. Routines elsewhere aren't in this list.`
: `You have no schedules of your own. Other bots' routines aren't in this list.`;
}
return cron_jobs
@@ -81,6 +81,10 @@ function resolvePlace({
}
if (is_coordinator) {
if (workspace_id && target_workspace && target_workspace != Number(workspace_id)) {
return `You're working in workspace #${workspace_id}, so a task made here goes there. Create it from your own chat to put it in another workspace.`;
}
const place_workspace = target_workspace || (workspace_id ? Number(workspace_id) : null);
if (args.workspace_wide) {
@@ -12,6 +12,10 @@ type Params = {
* The workspaces the bot is admin of (`grabBotRunPowers`).
*/
admin_workspace_ids?: number[];
/**
* The workspace the run is in: only its tasks are in reach.
*/
workspace_id?: number | string | null;
/**
* Told the task it deleted, so the message's app record names it: a bot
* read a plain "delete_task" and later described the task as still
@@ -32,7 +36,14 @@ type Args = {
* (`assertUserReachesTask`). Only chat replies get it: a routine, a hook
* call has no user asking, and a payload could ask for it.
*/
export default function deleteTaskTool({ user_id, bot_id, is_coordinator, admin_workspace_ids = [], onTask }: Params): HarnessTool<Args> {
export default function deleteTaskTool({
user_id,
bot_id,
is_coordinator,
admin_workspace_ids = [],
workspace_id,
onTask,
}: Params): HarnessTool<Args> {
return {
name: "delete_task",
description: `Delete a task from the to-do list for good, with its notes and file links (the files themselves stay). Only when the user asked for that task, or tasks like it, to be deleted; work that was dropped but should stay on record is cancelled with update_task instead. Say "deleted" only after this call says so; cancelling isn't deleting.`,
@@ -40,7 +51,7 @@ export default function deleteTaskTool({ user_id, bot_id, is_coordinator, admin_
task_id: z.number().int(),
}),
run: async ({ args }) => {
const task = await grabRunTask({ task_id: args.task_id, bot_id, is_coordinator, admin_workspace_ids });
const task = await grabRunTask({ task_id: args.task_id, bot_id, is_coordinator, admin_workspace_ids, workspace_id });
await assertUserReachesTask({ user_id, task_id: task.id! });
await deleteTask({ user_id, task_id: task.id!, deleted_by_bot_id: bot_id });
@@ -90,7 +90,7 @@ export default async function grabBotTaskTools({
members,
onTask,
}),
updateTaskTool({ user_id, bot_id, is_coordinator, admin_workspace_ids, scopes, onTask }),
...(can_delete ? [deleteTaskTool({ user_id, bot_id, is_coordinator, admin_workspace_ids, onTask })] : []),
updateTaskTool({ user_id, bot_id, is_coordinator, admin_workspace_ids, workspace_id, scopes, onTask }),
...(can_delete ? [deleteTaskTool({ user_id, bot_id, is_coordinator, admin_workspace_ids, workspace_id, onTask })] : []),
];
}
@@ -47,7 +47,7 @@ type Args = {
export default function listTasksTool({ bot_id, user_id, is_coordinator, admin_workspace_ids, workspace_id, onTask }: Params): HarnessTool<Args> {
return {
name: "list_tasks",
description: `List tasks: yours (your own list and the ones you have in workspaces), the ones in ${workspace_id ? "this workspace" : "a workspace"}, or ${is_coordinator ? "every task in the install" : "every task you can reach (yours, the workspace-wide ones in your workspaces, and as a workspace's admin every task there)"}. Each line is its id, status, title, whose it is, its files and its latest note with who wrote it. Open ones come first (blocked, then doing, then to do). Use it to plan work, and to check what's still pending when the user says hello or asks what's next. A workspace-wide task is worked on by every bot in its workspace: before you work on one, read it with task_id to see its brief and its whole log (which bot did what, and when), so you pick up where the others left off instead of redoing their part.`,
description: `List tasks: yours (your own list and the ones you have in workspaces), the ones in ${workspace_id ? "this workspace" : "a workspace"}, or ${is_coordinator ? "every task in the install" : "every task you can reach (yours, the workspace-wide ones in your workspaces, and as a workspace's admin every task there)"}.${workspace_id ? " You are working in a workspace, so only its tasks are in reach: never another workspace's, nor your own list outside it." : ""} Each line is its id, status, title, whose it is, its files and its latest note with who wrote it. Open ones come first (blocked, then doing, then to do). Use it to plan work, and to check what's still pending when the user says hello or asks what's next. A workspace-wide task is worked on by every bot in its workspace: before you work on one, read it with task_id to see its brief and its whole log (which bot did what, and when), so you pick up where the others left off instead of redoing their part.`,
schema: z.object({
task_id: z
.number()
@@ -66,14 +66,14 @@ export default function listTasksTool({ bot_id, user_id, is_coordinator, admin_w
}),
run: async ({ args }) => {
if (args.task_id) {
const task = await grabRunTask({ task_id: args.task_id, bot_id, is_coordinator, admin_workspace_ids });
const task = await grabRunTask({ task_id: args.task_id, bot_id, is_coordinator, admin_workspace_ids, workspace_id });
onTask?.({ task_id: Number(task.id) });
const [notes, files, parts] = await Promise.all([
grabTaskNotes({ task_id: task.id! }),
grabTaskMedia({ task_id: task.id! }),
grabTasks({ bot_id, is_coordinator, admin_workspace_ids, only_parent_task_id: task.id! }),
grabTasks({ bot_id, is_coordinator, admin_workspace_ids, workspace_id, only_parent_task_id: task.id! }),
]);
const parent = task.parent_task_id ? await grabTaskRow({ task_id: task.parent_task_id }) : null;
@@ -88,8 +88,7 @@ export default function listTasksTool({ bot_id, user_id, is_coordinator, admin_w
}
const scope = args.scope || "all";
const only_workspace_id =
scope == "workspace" && workspace_id ? Number(workspace_id) : undefined;
const only_workspace_id = workspace_id ? Number(workspace_id) : undefined;
const tasks = await grabTasks({
bot_id,
@@ -24,6 +24,10 @@ type Params = {
* The workspaces the bot is admin of (`grabBotRunPowers`).
*/
admin_workspace_ids?: number[];
/**
* The workspace the run is in: only its tasks are in reach.
*/
workspace_id?: number | string | null;
/**
* The run's folders, for linking a deliverable by its `[file:ID]` ref.
*/
@@ -61,7 +65,15 @@ type Args = {
* which bot did what and when. The coordinator and a workspace's admin bot
* can also turn a bot's task there into a workspace-wide one.
*/
export default function updateTaskTool({ user_id, bot_id, is_coordinator, admin_workspace_ids = [], scopes = [], onTask }: Params): HarnessTool<Args> {
export default function updateTaskTool({
user_id,
bot_id,
is_coordinator,
admin_workspace_ids = [],
workspace_id,
scopes = [],
onTask,
}: Params): HarnessTool<Args> {
/**
* Only the coordinator and workspace admins get the switch at all.
*/
@@ -131,7 +143,7 @@ export default function updateTaskTool({ user_id, bot_id, is_coordinator, admin_
),
}),
run: async ({ args }) => {
const task = await grabRunTask({ task_id: args.task_id, bot_id, is_coordinator, admin_workspace_ids });
const task = await grabRunTask({ task_id: args.task_id, bot_id, is_coordinator, admin_workspace_ids, workspace_id });
const said: string[] = [];
onTask?.({ task_id: Number(task.id) });
@@ -197,6 +209,7 @@ export default function updateTaskTool({ user_id, bot_id, is_coordinator, admin_
bot_id,
is_coordinator,
admin_workspace_ids,
workspace_id,
only_workspace_id: task.workspace_id,
own_list: !task.workspace_id,
open_only: true,
@@ -10,6 +10,10 @@ type Params = {
* The coordinator changes any of the user's routines, not only its own.
*/
is_coordinator?: boolean;
/**
* The workspace the run is in: only a routine that runs there.
*/
workspace_id?: number | string | null;
};
type Args = {
@@ -25,7 +29,7 @@ type Args = {
* Lets a bot change, pause or resume one of its own schedules, or as the
* coordinator any of the user's.
*/
export default function updateScheduleTool({ user_id, bot_id, is_coordinator }: Params): HarnessTool<Args> {
export default function updateScheduleTool({ user_id, bot_id, is_coordinator, workspace_id }: Params): HarnessTool<Args> {
return {
name: "update_schedule",
description: is_coordinator
@@ -45,6 +49,7 @@ export default function updateScheduleTool({ user_id, bot_id, is_coordinator }:
user_id,
cron_job_id: schedule_id,
...(is_coordinator ? {} : { bot_id }),
...(workspace_id ? { workspace_id } : {}),
data,
});
+23 -1
View File
@@ -12,6 +12,12 @@ type Params = {
* whoever has it.
*/
admin_workspace_ids?: number[];
/**
* The workspace the run is in (`bot_sessions.workspace_id`): then only
* that workspace's tasks are in reach, never another workspace's nor
* the bot's own list outside one.
*/
workspace_id?: number | string | null;
/**
* The table alias the condition reads.
*/
@@ -23,14 +29,30 @@ type Params = {
* worker reaches its own list and the tasks it has in the workspaces it
* still works in, never another bot's, plus every workspace-wide task (no
* bot) in those workspaces. A workspace admin also reaches every task in
* that workspace; the coordinator every task in the install.
* that workspace; the coordinator every task in the install. A run in a
* workspace is scoped to it: its session reaches nothing outside it, which
* is the whole point of working there.
*/
export default function botTaskScopesSql({
bot_id,
is_coordinator,
admin_workspace_ids = [],
workspace_id,
alias = "t",
}: Params): SqlCondition {
if (workspace_id) {
const id = Number(workspace_id);
if (is_coordinator || admin_workspace_ids.map(Number).includes(id)) {
return { sql: `${alias}.workspace_id = ?`, values: [id] };
}
return {
sql: `(${alias}.workspace_id = ? AND ${alias}.workspace_id IN (SELECT workspace_id FROM workspace_bots WHERE bot_id = ?) AND (${alias}.bot_id = ? OR ${alias}.bot_id IS NULL))`,
values: [id, Number(bot_id), Number(bot_id)],
};
}
if (is_coordinator) return { sql: `1`, values: [] };
const admin_ids = admin_workspace_ids.map(Number).filter(Boolean);
+29
View File
@@ -86,6 +86,35 @@ describe("botTaskScopesSql for a workspace admin", () => {
});
});
describe("botTaskScopesSql in a workspace", () => {
test("a worker's run there reaches that workspace's tasks and nothing else, not even its own list", () => {
expect(ids({ condition: botTaskScopesSql({ bot_id: 3, is_coordinator: false, workspace_id: 10 }) })).toEqual([110]);
expect(ids({ condition: botTaskScopesSql({ bot_id: 3, is_coordinator: false, workspace_id: 11 }) })).toEqual([112]);
});
test("the coordinator's run there reaches every task of that workspace alone", () => {
expect(ids({ condition: botTaskScopesSql({ bot_id: 1, is_coordinator: true, workspace_id: 11 }) })).toEqual([
111, 112, 113,
]);
expect(ids({ condition: botTaskScopesSql({ bot_id: 1, is_coordinator: true, workspace_id: 10 }) })).toEqual([110]);
});
test("a workspace admin's run there reaches its whole workspace and no other", () => {
db.run(`INSERT INTO workspace_bots VALUES (11, 4)`);
expect(
ids({ condition: botTaskScopesSql({ bot_id: 4, is_coordinator: false, admin_workspace_ids: [11], workspace_id: 11 }) }),
).toEqual([111, 112, 113]);
expect(
ids({ condition: botTaskScopesSql({ bot_id: 4, is_coordinator: false, admin_workspace_ids: [11], workspace_id: 10 }) }),
).toEqual([]);
db.run(`DELETE FROM workspace_bots WHERE bot_id = 4`);
});
test("workspace_id given as a string still scopes", () => {
expect(ids({ condition: botTaskScopesSql({ bot_id: 3, is_coordinator: false, workspace_id: "10" }) })).toEqual([110]);
});
});
describe("workspace-wide tasks", () => {
test("every bot working there reaches one, no bot elsewhere does, and users see it with the workspace", () => {
db.run(`INSERT INTO tasks VALUES (114, NULL, 11, NULL)`);