Add workspaces, computers, bot memory and the bots queue

- Schema: workspaces (+ workspace_bots, workspace_computers), containers,
  computers, bot_computers, bots.memory, workspaces.system_prompt, and
  bots_queue as a bun-sqlite isQueue table
- Main computer (and its container record) ensured with the coordinator on
  signup, seed and /admin/bots; the coordinator's access to it is locked
- Effective computer access: direct grants plus workspace computers
- Full bot prompt: core instructions, workspace prompts, then memory;
  bots can rewrite their memory and system prompt
- Queue helpers on the native BunSQLite.queue API, plus a handler that
  heartbeats, serializes per computer, recovers after restarts, reports
  lost jobs, supports cancelling and pushes outcomes over the WebSocket
- Enable SQLite foreign keys at startup so schema cascades fire
- Prune finished jobs daily

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-18 18:33:54 +01:00
co-authored by Claude Opus 5.5
parent 694c83346f
commit d49f524fe8
72 changed files with 2961 additions and 31 deletions
+399 -3
View File
@@ -8,6 +8,12 @@ import {
BotTaskStatuses,
BotTaskStepStates,
} from "@/src/dict/bot-dict";
import {
ComputerKinds,
ContainerPurposes,
ContainerStatuses,
} from "@/src/dict/computer-dict";
import { AppData } from "@/src/data/app-data";
import type { BUN_SQLITE_DatabaseSchemaType } from "@moduletrace/bun-sqlite/types";
const schema: BUN_SQLITE_DatabaseSchemaType = {
@@ -197,7 +203,7 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
fields: [
{
fieldName: "user_id",
fieldDescription: `Workspace owner`,
fieldDescription: `Bot owner`,
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
@@ -213,7 +219,7 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
},
{
fieldName: "kind",
fieldDescription: `One coordinator per workspace; everything else is a worker`,
fieldDescription: `One coordinator per owner; everything else is a worker`,
dataType: "TEXT",
options: BotKinds.map((bk) => bk.value),
defaultValue: "worker",
@@ -243,7 +249,12 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
},
{
fieldName: "system_prompt",
fieldDescription: `The bot's core instructions, in markdown`,
fieldDescription: `The bot's core instructions, in markdown. The bot can rewrite it`,
dataType: "TEXT",
},
{
fieldName: "memory",
fieldDescription: `What the bot must always remember, in markdown. The bot keeps it up to date`,
dataType: "TEXT",
},
{
@@ -377,6 +388,391 @@ const schema: BUN_SQLITE_DatabaseSchemaType = {
},
],
},
{
tableName: "bots_queue",
isQueue: {
maxAttempts: AppData.BotQueueMaxAttempts,
lockTimeout: AppData.BotQueueLockTimeoutMs,
backoff: AppData.BotQueueBackoffMs,
},
fields: [
{
fieldName: "bot_id",
fieldDescription: `The bot doing the work`,
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "bots",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "bots_queue_bot_fk",
},
},
{
fieldName: "message_id",
fieldDescription: `The user message that asked for the work, if any`,
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "bot_messages",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "bots_queue_message_fk",
},
},
{
fieldName: "task_id",
fieldDescription: `The task shown in the working-on panel, if any`,
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "bot_tasks",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "bots_queue_task_fk",
},
},
{
fieldName: "computer_id",
fieldDescription: `The computer to run on, if any. Checked against the bot's access when queued`,
dataType: "INTEGER",
},
{
fieldName: "title",
fieldDescription: `Short label for the job`,
dataType: "TEXT",
},
{
fieldName: "instructions",
fieldDescription: `The brief the bot wrote for itself, in markdown`,
dataType: "TEXT",
},
{
fieldName: "params",
fieldDescription: `Structured parameters the handler needs`,
dataType: "TEXT",
json: true,
},
{
fieldName: "progress",
fieldDescription: `Latest progress note from the bot, in markdown`,
dataType: "TEXT",
},
{
fieldName: "checkpoint",
fieldDescription: `State to resume from if the job is picked up again`,
dataType: "TEXT",
json: true,
},
{
fieldName: "result",
fieldDescription: `What the bot reported when it finished, in markdown`,
dataType: "TEXT",
},
{
fieldName: "started_at",
fieldDescription: `Unix timestamp of the first claim`,
dataType: "INTEGER",
},
{
fieldName: "finished_at",
fieldDescription: `Unix timestamp of completion or final failure`,
dataType: "INTEGER",
},
],
indexes: [
{
indexName: "idx_bots_queue_bot_id_status",
indexTableFields: ["bot_id", "status"],
},
],
},
{
tableName: "workspaces",
fields: [
{
fieldName: "user_id",
fieldDescription: `Workspace owner`,
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "users",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "workspace_user_fk",
},
},
{
fieldName: "name",
dataType: "TEXT",
},
{
fieldName: "description",
dataType: "TEXT",
},
{
fieldName: "system_prompt",
fieldDescription: `Shared context in markdown, appended to the prompt of every bot in the workspace`,
dataType: "TEXT",
},
{
fieldName: "archived",
dataType: "INTEGER",
options: [0, 1],
defaultValue: 0,
},
],
indexes: [
{
indexName: "idx_workspaces_user_id",
indexTableFields: ["user_id"],
},
],
},
{
tableName: "workspace_bots",
fields: [
{
fieldName: "workspace_id",
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "workspaces",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "workspace_bot_workspace_fk",
},
},
{
fieldName: "bot_id",
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "bots",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "workspace_bot_bot_fk",
},
},
],
uniqueConstraints: [
{
alias: "one_bot_per_workspace",
constraintName: "A bot joins a workspace once",
constraintTableFields: [
{ value: "workspace_id" },
{ value: "bot_id" },
],
},
],
indexes: [
{
indexName: "idx_workspace_bots_bot_id",
indexTableFields: ["bot_id"],
},
],
},
{
tableName: "containers",
fields: [
{
fieldName: "name",
fieldDescription: `Docker container name, also its hostname on the network`,
dataType: "TEXT",
unique: true,
},
{
fieldName: "docker_id",
fieldDescription: `Docker container id, once known`,
dataType: "TEXT",
},
{
fieldName: "purpose",
dataType: "TEXT",
options: ContainerPurposes.map((cp) => cp.value),
defaultValue: "computer",
},
{
fieldName: "image",
fieldDescription: `Image the container runs, e.g. ghcr.io/agent-infra/sandbox:latest`,
dataType: "TEXT",
},
{
fieldName: "network",
fieldDescription: `Docker network shared with the main container`,
dataType: "TEXT",
},
{
fieldName: "hostname",
fieldDescription: `Address the main container reaches it on`,
dataType: "TEXT",
},
{
fieldName: "port",
fieldDescription: `Port the service listens on inside the container`,
dataType: "INTEGER",
},
{
fieldName: "status",
fieldDescription: `Last known docker state`,
dataType: "TEXT",
options: ContainerStatuses.map((cs) => cs.value),
defaultValue: "unknown",
},
{
fieldName: "metadata",
fieldDescription: `Extra docker details (ports, volumes, env names, labels)`,
dataType: "TEXT",
json: true,
},
{
fieldName: "last_seen_at",
fieldDescription: `Unix timestamp of the last docker inspect`,
dataType: "INTEGER",
},
],
},
{
tableName: "computers",
fields: [
{
fieldName: "user_id",
fieldDescription: `Computer owner`,
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "users",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "computer_user_fk",
},
},
{
fieldName: "container_id",
fieldDescription: `The container this computer runs in`,
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "containers",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "computer_container_fk",
},
},
{
fieldName: "name",
dataType: "TEXT",
},
{
fieldName: "kind",
fieldDescription: `One main computer per owner; everything else is a sandbox`,
dataType: "TEXT",
options: ComputerKinds.map((ck) => ck.value),
defaultValue: "sandbox",
},
{
fieldName: "description",
dataType: "TEXT",
},
],
indexes: [
{
indexName: "idx_computers_user_id_kind",
indexTableFields: ["user_id", "kind"],
},
{
indexName: "idx_computers_container_id",
indexTableFields: ["container_id"],
},
],
},
{
tableName: "bot_computers",
fields: [
{
fieldName: "bot_id",
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "bots",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "bot_computer_bot_fk",
},
},
{
fieldName: "computer_id",
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "computers",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "bot_computer_computer_fk",
},
},
],
uniqueConstraints: [
{
alias: "one_access_per_bot_computer",
constraintName: "A bot gets access to a computer once",
constraintTableFields: [
{ value: "bot_id" },
{ value: "computer_id" },
],
},
],
indexes: [
{
indexName: "idx_bot_computers_computer_id",
indexTableFields: ["computer_id"],
},
],
},
{
tableName: "workspace_computers",
fields: [
{
fieldName: "workspace_id",
fieldDescription: `Every bot in this workspace gets access to the computer`,
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "workspaces",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "workspace_computer_workspace_fk",
},
},
{
fieldName: "computer_id",
dataType: "INTEGER",
foreignKey: {
cascadeDelete: true,
destinationTableName: "computers",
destinationTableColumnName: "id",
destinationTableColumnType: "INTEGER",
foreignKeyName: "workspace_computer_computer_fk",
},
},
],
uniqueConstraints: [
{
alias: "one_computer_per_workspace",
constraintName: "A computer joins a workspace once",
constraintTableFields: [
{ value: "workspace_id" },
{ value: "computer_id" },
],
},
],
indexes: [
{
indexName: "idx_workspace_computers_computer_id",
indexTableFields: ["computer_id"],
},
],
},
{
tableName: "logs",
fields: [
+263 -4
View File
@@ -9,6 +9,13 @@ export const BunSQLiteTables = [
"bot_messages",
"bot_tasks",
"bot_task_steps",
"bots_queue",
"workspaces",
"workspace_bots",
"containers",
"computers",
"bot_computers",
"workspace_computers",
"logs",
] as const
@@ -143,12 +150,12 @@ export type BUN_SQLITE_COBBLE_BOTS = {
*/
updated_at?: number | "";
/**
* Workspace owner
* Bot owner
*/
user_id?: number | "";
name?: string;
/**
* One coordinator per workspace; everything else is a worker
* One coordinator per owner; everything else is a worker
*/
kind?: "worker" | "coordinator" | "";
/**
@@ -168,9 +175,13 @@ export type BUN_SQLITE_COBBLE_BOTS = {
*/
status_line?: string;
/**
* The bot's core instructions, in markdown
* The bot's core instructions, in markdown. The bot can rewrite it
*/
system_prompt?: string;
/**
* What the bot must always remember, in markdown. The bot keeps it up to date
*/
memory?: string;
unread?: 0 | 1 | "";
/**
* Unix timestamp of the latest message, for rail ordering
@@ -241,6 +252,254 @@ export type BUN_SQLITE_COBBLE_BOT_TASK_STEPS = {
position?: number | "";
}
export type BUN_SQLITE_COBBLE_BOTS_QUEUE = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
/**
* The bot doing the work
*/
bot_id?: number | "";
/**
* The user message that asked for the work, if any
*/
message_id?: number | "";
/**
* The task shown in the working-on panel, if any
*/
task_id?: number | "";
/**
* The computer to run on, if any. Checked against the bot's access when queued
*/
computer_id?: number | "";
/**
* Short label for the job
*/
title?: string;
/**
* The brief the bot wrote for itself, in markdown
*/
instructions?: string;
/**
* Structured parameters the handler needs
*/
params?: string;
/**
* Latest progress note from the bot, in markdown
*/
progress?: string;
/**
* State to resume from if the job is picked up again
*/
checkpoint?: string;
/**
* What the bot reported when it finished, in markdown
*/
result?: string;
/**
* Unix timestamp of the first claim
*/
started_at?: number | "";
/**
* Unix timestamp of completion or final failure
*/
finished_at?: number | "";
/**
* The state of the job. (pending | processing | completed | failed)
*/
status?: string;
/**
* The number of times the job has been claimed.
*/
attempts?: number | "";
/**
* The time from which the job can be claimed. (Unix Timestamp)
*/
available_at?: number | "";
/**
* The time when the job was claimed. (Unix Timestamp)
*/
locked_at?: number | "";
/**
* The worker that claimed the job.
*/
locked_by?: string;
/**
* The error from the last failed attempt.
*/
last_error?: string;
}
export type BUN_SQLITE_COBBLE_WORKSPACES = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
/**
* Workspace owner
*/
user_id?: number | "";
name?: string;
description?: string;
/**
* Shared context in markdown, appended to the prompt of every bot in the workspace
*/
system_prompt?: string;
archived?: 0 | 1 | "";
}
export type BUN_SQLITE_COBBLE_WORKSPACE_BOTS = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
workspace_id?: number | "";
bot_id?: number | "";
}
export type BUN_SQLITE_COBBLE_CONTAINERS = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
/**
* Docker container name, also its hostname on the network
*/
name?: string;
/**
* Docker container id, once known
*/
docker_id?: string;
purpose?: "computer" | "";
/**
* Image the container runs, e.g. ghcr.io/agent-infra/sandbox:latest
*/
image?: string;
/**
* Docker network shared with the main container
*/
network?: string;
/**
* Address the main container reaches it on
*/
hostname?: string;
/**
* Port the service listens on inside the container
*/
port?: number | "";
/**
* Last known docker state
*/
status?: "created" | "running" | "paused" | "restarting" | "exited" | "removing" | "dead" | "unknown" | "";
/**
* Extra docker details (ports, volumes, env names, labels)
*/
metadata?: string;
/**
* Unix timestamp of the last docker inspect
*/
last_seen_at?: number | "";
}
export type BUN_SQLITE_COBBLE_COMPUTERS = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
/**
* Computer owner
*/
user_id?: number | "";
/**
* The container this computer runs in
*/
container_id?: number | "";
name?: string;
/**
* One main computer per owner; everything else is a sandbox
*/
kind?: "main" | "sandbox" | "";
description?: string;
}
export type BUN_SQLITE_COBBLE_BOT_COMPUTERS = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
bot_id?: number | "";
computer_id?: number | "";
}
export type BUN_SQLITE_COBBLE_WORKSPACE_COMPUTERS = {
/**
* The unique identifier of the record.
*/
id?: number | "";
/**
* The time when the record was created. (Unix Timestamp)
*/
created_at?: number | "";
/**
* The time when the record was updated. (Unix Timestamp)
*/
updated_at?: number | "";
/**
* Every bot in this workspace gets access to the computer
*/
workspace_id?: number | "";
computer_id?: number | "";
}
export type BUN_SQLITE_COBBLE_LOGS = {
/**
* The unique identifier of the record.
@@ -288,4 +547,4 @@ export type BUN_SQLITE_COBBLE_LOGS = {
ip_address?: string;
}
export type BUN_SQLITE_COBBLE_ALL_TYPEDEFS = BUN_SQLITE_COBBLE_USERS & BUN_SQLITE_COBBLE_USER_TYPES & BUN_SQLITE_COBBLE_SSO_LOGIN_CODES & BUN_SQLITE_COBBLE_USER_PASSWORDS & BUN_SQLITE_COBBLE_MEDIA & BUN_SQLITE_COBBLE_MEDIA_PARADIGMS & BUN_SQLITE_COBBLE_BOTS & BUN_SQLITE_COBBLE_BOT_MESSAGES & BUN_SQLITE_COBBLE_BOT_TASKS & BUN_SQLITE_COBBLE_BOT_TASK_STEPS & BUN_SQLITE_COBBLE_LOGS
export type BUN_SQLITE_COBBLE_ALL_TYPEDEFS = BUN_SQLITE_COBBLE_USERS & BUN_SQLITE_COBBLE_USER_TYPES & BUN_SQLITE_COBBLE_SSO_LOGIN_CODES & BUN_SQLITE_COBBLE_USER_PASSWORDS & BUN_SQLITE_COBBLE_MEDIA & BUN_SQLITE_COBBLE_MEDIA_PARADIGMS & BUN_SQLITE_COBBLE_BOTS & BUN_SQLITE_COBBLE_BOT_MESSAGES & BUN_SQLITE_COBBLE_BOT_TASKS & BUN_SQLITE_COBBLE_BOT_TASK_STEPS & BUN_SQLITE_COBBLE_BOTS_QUEUE & BUN_SQLITE_COBBLE_WORKSPACES & BUN_SQLITE_COBBLE_WORKSPACE_BOTS & BUN_SQLITE_COBBLE_CONTAINERS & BUN_SQLITE_COBBLE_COMPUTERS & BUN_SQLITE_COBBLE_BOT_COMPUTERS & BUN_SQLITE_COBBLE_WORKSPACE_COMPUTERS & BUN_SQLITE_COBBLE_LOGS