refactor(agent)!: migrate to Pi Durable and embed the agent queue (#525)

* refactor(agent)!: migrate sessions to Pi Durable

Replace patched AgentHarness storage with per-session SQLite and PostgreSQL commit mirroring. Migrate legacy history automatically and run agent queues with the main server.

BREAKING CHANGE: Remove standalone agent workers and return native Pi entry records from chat history APIs.

* fix(agent): constrain session log dialog scrolling

Keep the header visible and wrap long output while log entries and expanded records scroll within the dialog. Cover desktop and narrow viewports through the dashboard Sessions flow.

* fix(agent): hide entry IDs from session log card headers

Retain internal entry IDs in expanded raw records for debugging. Verify that card headers omit the IDs while raw JSON preserves them.

* test(e2e): handle browser wheel limits in session log checks

Verify that a normal wheel event moves the viewport before explicitly scrolling to the final record. Keep the dialog bounds, fixed header, wrapping, and expanded-record assertions.

* fix(agent): select model providers without inheriting unrelated auth

* fix(webui): prevent resize feedback when toggling agent panel

* test(agent): reproduce concurrent comment session races

* fix(agent): serialize comment sessions and deduplicate history

* docs(changelog): consolidate Pi Durable changes into one entry
This commit is contained in:
Yiling-J
2026-11-11 16:05:18 +08:00
committed by GitHub
parent 843a56304c
commit 35a0e82284
120 changed files with 7593 additions and 5972 deletions
+18 -74
View File
@@ -1,39 +1,20 @@
---
title: Deploy with Distributed Workers
description: Configure a high-scale production deployment using dedicated background workers, DBOS, and S3-compatible storage.
description: Run media transcoding on dedicated workers while agents run with the main server.
---
For production environments or workloads with high-volume video processing and heavy AI agent usage, running background tasks on a single server can cause bottlenecks. Shumai resolves this by decoupling the web dashboard from background processing through dedicated worker nodes orchestrated by **DBOS Transact**.
All background tasks (media transcoding, AI embeddings, agent tasks) are dispatched to durable task queues (`transcode_queue` and `agent_queue`) hosted directly in PostgreSQL. No separate external cluster (like Temporal) is required.
---
## How it Works
In a distributed deployment, the core Shumai web application handles user requests, database updates, and file uploads, while delegating durable background workflows to specialized worker instances via PostgreSQL task queues.
Shumai runs agent, embedding, and archive queues with the main web server. Media transcoding can run on dedicated workers through PostgreSQL queues managed by DBOS Transact.
```mermaid
graph TD
UserUpload[User Uploads Video / Triggers Agent] --> WebApp[Shumai Web UI / API]
WebApp -->|Enqueue Task| DB[(PostgreSQL / DBOS Queues)]
DB -->|Queue: transcode_queue| TranscodeWorker[Transcode Worker Machine]
DB -->|Queue: agent_queue| AgentWorker[Agent Worker Machine]
User[User] --> Server[Shumai Web UI / API + Agents + Archives]
Server --> DB[(PostgreSQL / DBOS Queues)]
DB --> Transcode[Dedicated Transcode Workers]
```
- **Transcoding Workers**: Can be run on GPU-accelerated or high-CPU compute instances.
- **AI Agent Workers**: Can be run on isolated, sandboxed hosts to safely execute automation scripts.
## Main Server
> [!IMPORTANT]
> **Storage Requirement**: Because the core application and the background workers run on separate machines in a distributed setup, **you must configure an S3-compatible storage backend** (such as AWS S3, Cloudflare R2, or MinIO). Local storage (`STORAGE_BACKEND=local`) is not supported in this mode because the workers and the web application do not share a common filesystem.
---
## Step 1: Deploy the Core Web Application
On your primary host machine, run PostgreSQL and the main Shumai web application.
To instruct the main web application to handle only API and UI traffic without running background worker queues locally, set `SHUMAI_WORKERS=none`:
Set `SHUMAI_WORKERS=none` (or `--workers=none`) to run agents and archives with the main server and send transcoding jobs to dedicated workers. `all` and `transcode` also run the transcode queue in the server. The older `agent`, `ai`, and `archive` values remain accepted and run agents and archives.
```env
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
@@ -47,67 +28,30 @@ SHUMAI_WORKERS=none
BETTER_AUTH_SECRET=your-random-32-character-secret-key
```
When started with `SHUMAI_WORKERS=none` (or `--workers=none`), the web server starts immediately without polling or consuming tasks from the transcode or agent queues.
Agent tools run on the server, so install its sandbox dependencies (`bubblewrap` on Linux or `sandbox-exec` on macOS). Standalone agent workers are no longer supported.
---
## Dedicated Transcode Workers
## Step 2: Configure & Run Dedicated Workers
On your specialized worker machines, configure each worker with a `.env` file referencing the shared PostgreSQL database and S3 storage:
```env
DATABASE_URL=postgresql://shumai:shumai_password@<primary-host-ip>:5432/shumai_db?schema=public
STORAGE_BACKEND=s3
AWS_ENDPOINT_URL_S3=https://your-s3-endpoint.com
S3_BUCKET=shumai
S3_REGION=us-east-1
S3_ACCESS_KEY_ID=your-access-key-id
S3_SECRET_ACCESS_KEY=your-secret-access-key
CONCURRENCY_TRANSCODE=1
CONCURRENCY_AGENT=5
```
### Media Transcoding Worker
Run this on your media processing nodes (requires system installations of `ffmpeg` and `ffprobe`):
Configure workers with the same PostgreSQL database and S3 storage as the server. A shared S3-compatible backend is required when machines do not share a filesystem.
```bash
npm install -g @shumai-one/shumai-transcode
shumai-transcode
```
Alternatively, you can run the transcode worker using the dedicated lightweight Docker image:
Or use the dedicated image:
```bash
docker run -d \
--name shumai-transcode-worker \
--env-file .env \
shumaione/shumai-transcode:latest
docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai-transcode:latest
```
(You can also use the all-in-one image: `docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai:latest shumai-transcode`.)
Bun users can run `bun run --bun shumai-transcode`.
### AI Agent Worker
## Upgrading Existing Agent Sessions
Run this on your sandboxed agent execution nodes (requires sandbox dependencies like `bubblewrap` or `sandbox-exec`):
Stop and drain old agent workers before upgrading the server. The normal database migration copies existing session context into independent histories automatically. No separate migration command is required. Legacy records remain in the database during this transition.
```bash
npm install -g @shumai-one/shumai-agent
shumai-agent
```
Alternatively, you can run the agent worker using the official Docker image:
```bash
docker run -d \
--name shumai-agent-worker \
--env-file .env \
--cap-add=SYS_ADMIN \
shumaione/shumai:latest shumai-agent
```
> [!IMPORTANT]
> **For Bun Users:** Since the worker package binaries contain a `#!/usr/bin/env node` shebang, running `shumai-transcode` or `shumai-agent` directly will run under Node.js. If you wish to run them using Bun, prefix the commands with `bun run --bun` (e.g., `bun run --bun shumai-transcode` or `bun run --bun shumai-agent`).
Each session uses a local SQLite working copy, restored from PostgreSQL when missing. `AGENT_SESSION_CACHE_DIR` optionally selects the cache directory. The server mirrors compact session commits to PostgreSQL before acknowledging them. Keep the database in your backup policy; local SQLite copies are disposable. Image and skill placeholders remain compact in both copies and are resolved when the agent needs them.
---
@@ -118,11 +62,11 @@ docker run -d \
Workers pull tasks from PostgreSQL queues based on configurable concurrency limits:
- `CONCURRENCY_TRANSCODE`: Maximum concurrent video transcoding jobs per worker (defaults to `1`).
- `CONCURRENCY_AGENT`: Maximum concurrent agent and embedding workflows per worker (defaults to `5`, recommend setting to `1` when paired with a local `embedding-server`).
- `CONCURRENCY_AGENT`: Maximum concurrent agent and embedding workflows per main server (defaults to `5`, recommend setting to `1` when paired with a local `embedding-server`).
### Replica Identity
When running multiple replicas of the transcode or agent worker across multiple machines or pods:
When running multiple replicas of the transcode workers or main servers across multiple machines or pods:
- `DbosExecutor` automatically derives a unique per-instance ID from the machine hostname and process ID to ensure startup recovery only re-enqueues tasks belonging to that specific instance.
- **Custom Worker ID**: Set `SHUMAI_WORKERS_ID` (or `SHUMAI_WORKER_ID`) if you wish to assign an explicit identifier.
+1 -1
View File
@@ -99,7 +99,7 @@ Docker Compose is the fastest way to get Shumai running. You do not need to clon
### Production Deployments (Distributed Workers)
If you are deploying Shumai for a production team with high-volume video uploads, we recommend setting up a distributed deployment using dedicated workers and an S3-compatible storage backend. This offloads resource-heavy video transcoding and AI agents to separate, dedicated worker nodes to keep your main dashboard fast and responsive. For setup instructions, see the [Deploy with Distributed Workers](/configuration/distributed) guide.
If you are deploying Shumai for a production team with high-volume video uploads, we recommend setting up a distributed deployment using dedicated workers and an S3-compatible storage backend. This offloads resource-heavy video transcoding to dedicated worker nodes to keep your main dashboard fast and responsive. For setup instructions, see the [Deploy with Distributed Workers](/configuration/distributed) guide.
---
+15 -90
View File
@@ -1,113 +1,38 @@
---
title: 分布式 Worker 部署
description: 通过独立的后台 Worker、DBOS 与兼容 S3 的对象存储配置大规模生产级部署。
description: 将媒体转码部署到独立 Worker,智能体随主服务器运行。
---
在面对高并发视频处理或重度 AI 智能体调用的生产环境中,若将所有后台任务集中在单台服务器上运行,极易引发性能瓶颈。Shumai 通过由 **DBOS Transact** 统一编排的独立 Worker 节点,将 Web 控制台与后台处理彻底解耦,轻松化解瓶颈。
智能体、嵌入和归档队列随 Shumai 主服务器一起启动。媒体转码仍可通过 PostgreSQL 和 DBOS 队列交给独立 Worker。
所有后台任务(媒体转码、AI 向量嵌入、智能体任务)均分发至直接托管在 PostgreSQL 中的持久化任务队列(`transcode_queue` 与 `agent_queue`)。无需部署额外的外部集群(如 Temporal)。
## 主服务器
---
设置 `SHUMAI_WORKERS=none`(或 `--workers=none`)时,主服务器处理智能体和归档任务,转码交给独立 Worker。`all` 和 `transcode` 会同时在主服务器运行转码队列。旧的 `agent`、`ai`、`archive` 参数仍可使用,均启动智能体和归档队列。
## 核心工作原理
主服务器需安装智能体沙箱依赖(Linux 使用 `bubblewrap`,macOS 使用 `sandbox-exec`)。不再支持独立启动智能体 Worker。
在分布式部署中,Shumai 核心 Web 应用负责处理用户请求、数据库更新和文件上传,并通过 PostgreSQL 任务队列将持久化后台工作流委派给专用的 Worker 实例。
## 独立转码 Worker
```mermaid
graph TD
UserUpload[用户上传视频 / 触发智能体] --> WebApp[Shumai Web UI / API]
WebApp -->|入队任务| DB[(PostgreSQL / DBOS 队列)]
DB -->|队列: transcode_queue| TranscodeWorker[转码 Worker 机器]
DB -->|队列: agent_queue| AgentWorker[智能体 Worker 机器]
```
- **转码 Worker**:可在配备 GPU 加速或高配 CPU 的计算实例上运行。
- **AI 智能体 Worker**:可在隔离沙箱环境中运行,以安全执行自动化脚本。
> [!IMPORTANT]
> **存储配置硬性要求**:由于在分布式部署下核心应用与后台 Worker 分别运行在不同机器上,**必须配置兼容 S3 的对象存储后端**(例如 AWS S3、Cloudflare R2 或 MinIO)。此模式下不支持本地存储(`STORAGE_BACKEND=local`),因为 Worker 节点与 Web 应用无法共享本地文件系统。
---
## 第 1 步:部署核心 Web 应用
在主服务器上运行 PostgreSQL 和 Shumai 核心 Web 应用。
若需指示核心 Web 应用仅处理 API 和前端 UI 流量,而不本地消费后台 Worker 队列任务,请设置 `SHUMAI_WORKERS=none`:
```env
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
STORAGE_BACKEND=s3
AWS_ENDPOINT_URL_S3=https://your-s3-endpoint.com
S3_BUCKET=shumai
S3_REGION=us-east-1
S3_ACCESS_KEY_ID=your-access-key-id
S3_SECRET_ACCESS_KEY=your-secret-access-key
SHUMAI_WORKERS=none
BETTER_AUTH_SECRET=your-random-32-character-secret-key
```
当使用 `SHUMAI_WORKERS=none`(或启动参数 `--workers=none`)启动时,Web 服务器将即刻启动,不会轮询或消费来自转码或智能体队列的任务。
---
## 第 2 步:配置并运行专属 Worker
在专用 Worker 机器上,配置各 Worker 的 `.env` 文件以指向共享的 PostgreSQL 数据库与 S3 存储:
```env
DATABASE_URL=postgresql://shumai:shumai_password@<primary-host-ip>:5432/shumai_db?schema=public
STORAGE_BACKEND=s3
AWS_ENDPOINT_URL_S3=https://your-s3-endpoint.com
S3_BUCKET=shumai
S3_REGION=us-east-1
S3_ACCESS_KEY_ID=your-access-key-id
S3_SECRET_ACCESS_KEY=your-secret-access-key
CONCURRENCY_TRANSCODE=1
CONCURRENCY_AGENT=5
```
### 媒体转码 Worker
在媒体处理计算节点上运行(需要预先在系统上安装 `ffmpeg` 与 `ffprobe`):
在 Worker 上配置与主服务器相同的 PostgreSQL 数据库和 S3 存储。多台机器不共享文件系统时,必须使用 S3 兼容存储。
```bash
npm install -g @shumai-one/shumai-transcode
shumai-transcode
```
或者,可以使用轻量级专属 Docker 镜像运行转码 Worker:
也可以使用 Docker:
```bash
docker run -d \
--name shumai-transcode-worker \
--env-file .env \
shumaione/shumai-transcode:latest
docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai-transcode:latest
```
(亦可使用一体化全功能镜像:`docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai:latest shumai-transcode`。)
Bun 用户可运行 `bun run --bun shumai-transcode`。
### AI 智能体 Worker
## 升级现有智能体会话
在沙箱化智能体执行节点上运行(需要安装 `bubblewrap` 或 `sandbox-exec` 等沙箱依赖项):
升级主服务器前,请等待旧智能体 Worker 完成任务并将其停止。正常数据库迁移会自动将已有会话上下文复制为独立记录,无需额外运行迁移脚本。过渡期间保留旧记录表。
```bash
npm install -g @shumai-one/shumai-agent
shumai-agent
```
或者,可以使用官方 Docker 镜像运行智能体 Worker:
```bash
docker run -d \
--name shumai-agent-worker \
--env-file .env \
--cap-add=SYS_ADMIN \
shumaione/shumai:latest shumai-agent
```
> [!IMPORTANT]
> **Bun 用户须知**:由于 Worker 依赖包的二进制可执行文件包含 `#!/usr/bin/env node` Shebang,直接运行 `shumai-transcode` 或 `shumai-agent` 将在 Node.js 下执行。若希望使用 Bun 运行,请在命令前加上 `bun run --bun`(例如 `bun run --bun shumai-transcode` 或 `bun run --bun shumai-agent`)。
每个会话使用独立的本地 SQLite 工作副本,缺失时自动从 PostgreSQL 恢复。可用 `AGENT_SESSION_CACHE_DIR` 指定缓存目录。主服务器在确认提交前将精简记录同步至 PostgreSQL。本地副本可重建,备份应包含 PostgreSQL。图片和技能内容在两种存储中继续使用动态占位符,在智能体需要时解析。
---
@@ -118,11 +43,11 @@ docker run -d \
Worker 根据可配置的并发上限从 PostgreSQL 队列中拉取任务:
- `CONCURRENCY_TRANSCODE`:单个 Worker 最大并发视频转码任务数(默认 `1`)。
- `CONCURRENCY_AGENT`:单个 Worker 最大并发智能体及向量嵌入工作流数(默认 `5`,若搭配本地 `embedding-server` 建议设为 `1`)。
- `CONCURRENCY_AGENT`:单个主服务器最大并发智能体及向量嵌入工作流数(默认 `5`,若搭配本地 `embedding-server` 建议设为 `1`)。
### 副本实例标识
当在多台机器或 Pod 上运行转码或智能体 Worker 的多个副本时:
当在多台机器或 Pod 上运行转码 Worker 或主服务器 的多个副本时:
- `DbosExecutor` 会自动基于机器主机名与进程 ID 派生唯一的单实例标识,确保启动恢复时仅重新入队属于该实例的任务。
- **自定义 Worker ID**:若需显式指定标识符,可设置 `SHUMAI_WORKERS_ID`(或 `SHUMAI_WORKER_ID`)。