Files
goclaw/migrations/000018_team_tasks_workspace_followup.up.sql
Viet TranandNam Nguyen Ngoc 1a42dc93a6 feat(teams): team system v2 with bug fixes, workspace scope, versioning, and prompt optimization (#183)
* feat(workspace): add team shared workspace for file collaboration

- Add workspace_write and workspace_read tools for agents to share files across team members
- Create team_workspaces DB table with migration 000017 (file metadata, pinning, tags)
- Implement PostgreSQL store layer for workspace CRUD operations
- Add RPC handlers for workspace list/read/delete from web UI
- Build React workspace tab with file listing, content preview, and delete
- Propagate workspace channel/chatID scope through delegation chain
- Auto-allow workspace tools in agent tool policy when agent belongs to a team
- Inject team workspace guidance into system prompt for team agents
- Add /reset command handler for clearing session history
- Harden MCP bridge context middleware to reject headers when no gateway token
- Add i18n strings for workspace UI in en/vi/zh locales

* feat(teams): add comprehensive task management with followup reminders and recovery

- Add task followup/reminder system with auto-set on lead agent reply and auto-clear when user responds on channel
- Add task recovery ticker to re-dispatch stale/pending tasks periodically
- Add task scopes, filtering by status/channel/chatID, and task events
- Add WS RPC handlers for task CRUD, assignments, comments, events, and bulk operations (teams_tasks.go)
- Add task detail dialog, settings UI for followup config, and scope filtering in web dashboard
- Add migrations 000018 (team_tasks_v2) and 000019 (task_followup)
- Extend team_tasks_tool with await_reply, clear_followup actions
- Auto-complete/fail team tasks when delegate agent finishes
- Add workspace file listing and team tool manager enhancements

* docs(teams): add team system architecture and playbook ideas documentation

- Add TEAM_SYSTEM.md with full architecture design covering task management, shared workspace, and delegation engine subsystems
- Add TEAM_PLAYBOOK_IDEAS.md outlining future team coordination layers (playbook, member capabilities, auto-learned patterns)
- Document data models, status flows, tool actions, followup reminder system, task ticker, execution locking, and workspace scope model

* fix(teams): resolve 6 critical bugs in team task system

- Fix unblock SQL: check array_length after array_remove (not before)
- Enforce single-team leadership in team creation
- Add requireLead() for approve/reject tool actions
- Validate cross-team dependency references in blocked_by
- Add team_id to handoff route for multi-team isolation
- Set blocked_by DEFAULT '{}' to prevent NULL array issues

* refactor(workspace): use stable userID as scope key instead of connection UUID

Workspace scope changed from (team_id, channel, chat_id) to (team_id, userID).
Fixes workspace fragmentation across WS tab refreshes and reconnections.

* feat(teams): add V1/V2 versioning with feature gating and optimized prompts

- IsTeamV2() helper gates advanced features (locking, followup, review, audit)
- V2 tool actions rejected for V1 teams with clear error message
- Ticker, gateway consumer, delegation hooks respect version flag
- TEAM.md renders v1/v2 sections conditionally
- Tool descriptions and params optimized (~38% token reduction)
- UI: version toggle in settings, V2 Beta badge, conditional rendering
- i18n: version modal keys for en/vi/zh

* fix(migration): use VARCHAR(255) for user ID columns and add metadata JSONB

- assignee_user_id, user_id, actor_id: TEXT → VARCHAR(255)
- Add metadata JSONB to team_task_comments and team_task_attachments

---------

Co-authored-by: Nam Nguyen Ngoc <namnn.0911@gmail.com>
2026-03-13 22:41:32 +07:00

170 lines
7.4 KiB
SQL

-- ============================================================
-- Part 1: Team workspace (shared file storage)
-- ============================================================
-- Team workspace: shared file storage scoped by (team, chat_id).
-- chat_id stores the system-derived userID (stable across WS reconnects).
CREATE TABLE team_workspace_files (
id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
team_id UUID NOT NULL REFERENCES agent_teams(id) ON DELETE CASCADE,
channel VARCHAR(50) NOT NULL DEFAULT '',
chat_id VARCHAR(255) NOT NULL DEFAULT '',
file_name VARCHAR(255) NOT NULL,
mime_type VARCHAR(100),
file_path TEXT NOT NULL,
size_bytes BIGINT NOT NULL DEFAULT 0,
uploaded_by UUID NOT NULL REFERENCES agents(id),
task_id UUID REFERENCES team_tasks(id) ON DELETE SET NULL,
pinned BOOLEAN NOT NULL DEFAULT false,
tags TEXT[] NOT NULL DEFAULT '{}',
metadata JSONB DEFAULT '{}',
archived_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(team_id, chat_id, file_name)
);
CREATE INDEX idx_twf_team_scope ON team_workspace_files(team_id, chat_id);
CREATE INDEX idx_twf_uploaded_by ON team_workspace_files(uploaded_by);
CREATE INDEX idx_twf_task ON team_workspace_files(task_id) WHERE task_id IS NOT NULL;
CREATE INDEX idx_twf_archived ON team_workspace_files(archived_at) WHERE archived_at IS NOT NULL;
CREATE INDEX idx_twf_pinned ON team_workspace_files(team_id, pinned) WHERE pinned = true;
CREATE INDEX idx_twf_tags ON team_workspace_files USING GIN(tags);
-- File version history.
CREATE TABLE team_workspace_file_versions (
id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
file_id UUID NOT NULL REFERENCES team_workspace_files(id) ON DELETE CASCADE,
version INT NOT NULL,
file_path TEXT NOT NULL,
size_bytes BIGINT NOT NULL DEFAULT 0,
uploaded_by UUID NOT NULL REFERENCES agents(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(file_id, version)
);
CREATE INDEX idx_twfv_file ON team_workspace_file_versions(file_id);
-- File comments / annotations.
CREATE TABLE team_workspace_comments (
id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
file_id UUID NOT NULL REFERENCES team_workspace_files(id) ON DELETE CASCADE,
agent_id UUID NOT NULL REFERENCES agents(id),
content TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_twfc_file ON team_workspace_comments(file_id);
-- ============================================================
-- Part 2: Team tasks v2 (locking, progress, audit, comments)
-- ============================================================
-- New columns on team_tasks
ALTER TABLE team_tasks ADD COLUMN task_type VARCHAR(30) NOT NULL DEFAULT 'general';
ALTER TABLE team_tasks ADD COLUMN task_number INT NOT NULL DEFAULT 0;
ALTER TABLE team_tasks ADD COLUMN identifier VARCHAR(20);
ALTER TABLE team_tasks ADD COLUMN created_by_agent_id UUID REFERENCES agents(id);
ALTER TABLE team_tasks ADD COLUMN assignee_user_id VARCHAR(255);
ALTER TABLE team_tasks ADD COLUMN parent_id UUID REFERENCES team_tasks(id) ON DELETE SET NULL;
ALTER TABLE team_tasks ADD COLUMN chat_id VARCHAR(255) DEFAULT '';
ALTER TABLE team_tasks ADD COLUMN locked_at TIMESTAMPTZ;
ALTER TABLE team_tasks ADD COLUMN lock_expires_at TIMESTAMPTZ;
ALTER TABLE team_tasks ADD COLUMN progress_percent INT DEFAULT 0 CHECK (progress_percent BETWEEN 0 AND 100);
ALTER TABLE team_tasks ADD COLUMN progress_step TEXT;
-- Indexes
CREATE INDEX idx_tt_parent ON team_tasks(parent_id) WHERE parent_id IS NOT NULL;
CREATE INDEX idx_tt_scope ON team_tasks(team_id, channel, chat_id);
CREATE INDEX idx_tt_type ON team_tasks(team_id, task_type);
CREATE INDEX idx_tt_lock ON team_tasks(lock_expires_at) WHERE lock_expires_at IS NOT NULL AND status = 'in_progress';
CREATE UNIQUE INDEX idx_tt_identifier ON team_tasks(team_id, identifier) WHERE identifier IS NOT NULL;
-- Task comments
CREATE TABLE team_task_comments (
id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
task_id UUID NOT NULL REFERENCES team_tasks(id) ON DELETE CASCADE,
agent_id UUID REFERENCES agents(id),
user_id VARCHAR(255),
content TEXT NOT NULL,
metadata JSONB DEFAULT '{}',
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_ttc_task ON team_task_comments(task_id);
-- Audit history
CREATE TABLE team_task_events (
id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
task_id UUID NOT NULL REFERENCES team_tasks(id) ON DELETE CASCADE,
event_type VARCHAR(30) NOT NULL,
actor_type VARCHAR(10) NOT NULL,
actor_id VARCHAR(255) NOT NULL,
data JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_tte_task ON team_task_events(task_id);
-- Task-workspace attachments
CREATE TABLE team_task_attachments (
id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
task_id UUID NOT NULL REFERENCES team_tasks(id) ON DELETE CASCADE,
file_id UUID NOT NULL REFERENCES team_workspace_files(id) ON DELETE CASCADE,
added_by UUID REFERENCES agents(id),
metadata JSONB DEFAULT '{}',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(task_id, file_id)
);
CREATE INDEX idx_tta_task ON team_task_attachments(task_id);
-- Backfill task_number (per-team sequential) and identifiers for existing tasks
DO $$
DECLARE
r RECORD;
seq INT;
prev_team UUID := '00000000-0000-0000-0000-000000000000';
BEGIN
FOR r IN
SELECT t.id, t.team_id,
UPPER(LEFT(COALESCE(tm.name, 'TSK'), 3)) AS team_prefix
FROM team_tasks t
JOIN agent_teams tm ON tm.id = t.team_id
WHERE t.identifier IS NULL
ORDER BY t.team_id, t.created_at
LOOP
IF r.team_id != prev_team THEN
seq := 0;
prev_team := r.team_id;
END IF;
seq := seq + 1;
UPDATE team_tasks SET task_number = seq, identifier = r.team_prefix || '-' || seq WHERE id = r.id;
END LOOP;
END $$;
-- ============================================================
-- Part 3: Task followup reminders
-- ============================================================
ALTER TABLE team_tasks ADD COLUMN followup_at TIMESTAMPTZ;
ALTER TABLE team_tasks ADD COLUMN followup_count INT NOT NULL DEFAULT 0;
ALTER TABLE team_tasks ADD COLUMN followup_max INT NOT NULL DEFAULT 0;
ALTER TABLE team_tasks ADD COLUMN followup_message TEXT;
ALTER TABLE team_tasks ADD COLUMN followup_channel VARCHAR(60);
ALTER TABLE team_tasks ADD COLUMN followup_chat_id VARCHAR(255);
CREATE INDEX idx_tt_followup ON team_tasks(followup_at)
WHERE followup_at IS NOT NULL AND status = 'in_progress';
-- ============================================================
-- Part 4: Fix blocked_by DEFAULT (was NULL, should be empty array)
-- ============================================================
ALTER TABLE team_tasks ALTER COLUMN blocked_by SET DEFAULT '{}'::uuid[];
UPDATE team_tasks SET blocked_by = '{}' WHERE blocked_by IS NULL;
-- ============================================================
-- Part 5: Add team_id to handoff_routes
-- ============================================================
ALTER TABLE handoff_routes ADD COLUMN team_id UUID REFERENCES agent_teams(id) ON DELETE SET NULL;
CREATE INDEX idx_hr_team ON handoff_routes(team_id) WHERE team_id IS NOT NULL;