コンテンツにスキップ

Slack MCP Agent — データモデル設計

v1.1 / 2026-04-20

既存プロトタイプの DDL (Schema Version 1) を基盤に、MVP 追加要件を反映した完全なデータモデルを定義する。
docs/architecture.md Section 7、docs/mvp-spec.md Section 11 と整合。


1. ER 図

erDiagram
    tenants ||--o{ tasks : "1:N"
    tenants ||--|| settings : "1:1"
    tenants ||--o{ integrations : "1:N"
    tenants ||--o{ tool_permissions : "1:N"
    tenants ||--o{ audit_logs : "1:N"
    tasks ||--o{ prompts : "1:N (versioned)"
    tasks ||--o{ processes : "1:N (versioned)"
    tasks ||--o{ executions : "1:N"
    tasks ||--o{ slack_messages : "1:N"
    prompts ||--|| processes : "1:1"
    processes ||--o{ executions : "1:N"

    tenants {
        TEXT id PK "ULID"
        TEXT name
        TEXT slug UK
        TEXT agent_id "将来用"
        TEXT vault_id "将来用"
    }
    tasks {
        TEXT id PK "ULID"
        TEXT tenant_id FK
        TEXT title
        TEXT status "TaskStatus enum"
        TEXT source "channel / agent_container"
        TEXT slack_channel
        TEXT slack_thread_ts
    }
    prompts {
        TEXT id PK "ULID"
        TEXT task_id FK
        INTEGER version
        TEXT content
        TEXT status "PromptStatus enum"
        TEXT rejection_reason
    }
    processes {
        TEXT id PK "ULID"
        TEXT task_id FK
        TEXT prompt_id FK
        INTEGER version
        TEXT steps "JSON"
        TEXT status "ProcessStatus enum"
        TEXT rejection_reason
    }
    executions {
        TEXT id PK "ULID"
        TEXT task_id FK
        TEXT process_id FK
        TEXT session_id
        TEXT status "ExecutionStatus enum"
        TEXT results "JSON"
        TEXT cancelled_by
    }
    slack_messages {
        TEXT id PK "ULID"
        TEXT task_id FK
        TEXT card_type
        TEXT message_ts
    }
    audit_logs {
        TEXT id PK "ULID"
        TEXT tenant_id FK
        TEXT timestamp
        TEXT action
        TEXT details "JSON"
    }
    settings {
        TEXT id PK "ULID"
        TEXT tenant_id FK "UNIQUE"
        TEXT config "JSON"
    }
    integrations {
        TEXT id PK "ULID"
        TEXT tenant_id FK
        TEXT provider
        TEXT status
        TEXT credentials "JSON encrypted"
    }
    tool_permissions {
        TEXT id PK "ULID"
        TEXT tenant_id FK
        TEXT tool_name
        INTEGER allowed
    }

リレーション一覧

関係 FK
tenants tasks 1:N tasks.tenant_id → tenants.id
tenants settings 1:1 settings.tenant_id → tenants.id (UNIQUE)
tenants integrations 1:N integrations.tenant_id → tenants.id
tenants tool_permissions 1:N tool_permissions.tenant_id → tenants.id
tenants audit_logs 1:N audit_logs.tenant_id → tenants.id
tasks prompts 1:N prompts.task_id → tasks.id
tasks processes 1:N processes.task_id → tasks.id
tasks executions 1:N executions.task_id → tasks.id
tasks slack_messages 1:N slack_messages.task_id → tasks.id
prompts processes 1:1 processes.prompt_id → prompts.id
processes executions 1:N executions.process_id → processes.id

2. テーブル定義

2.1 tenants

テナント管理。MVP はシングルテナント (default)。

カラム 制約 説明
id TEXT PK (ULID) テナント ID
name TEXT NOT NULL 表示名
slug TEXT NOT NULL, UNIQUE URL スラッグ
agent_id TEXT Managed Agent 定義 ID (将来用)
vault_id TEXT Vault ID (将来用)
created_at TEXT NOT NULL, DEFAULT now 作成日時 (ISO 8601)
updated_at TEXT NOT NULL, DEFAULT now 更新日時

2.2 tasks

L1 出力のタスク。パイプラインのルートエンティティ。

カラム 制約 説明
id TEXT PK (ULID) タスク ID
tenant_id TEXT NOT NULL, FK → tenants 所属テナント
title TEXT NOT NULL L1 が抽出したタイトル
description TEXT NOT NULL, DEFAULT '' L1 が抽出した説明
priority TEXT NOT NULL, DEFAULT 'medium' low / medium / high / urgent
task_type TEXT NOT NULL, DEFAULT 'standard' standard / urgent
status TEXT NOT NULL, DEFAULT 'extracted' TaskStatus enum
source TEXT NOT NULL, DEFAULT 'channel' channel / agent_container (MVP は channel のみ書き込み、agent_container は Post-MVP)
slack_channel TEXT NOT NULL Slack channel ID
slack_thread_ts TEXT NOT NULL Slack thread timestamp
created_at TEXT NOT NULL, DEFAULT now 作成日時
updated_at TEXT NOT NULL, DEFAULT now 更新日時

インデックス: - idx_tasks_tenant ON (tenant_id) - idx_tasks_status ON (status) - idx_tasks_created_at ON (created_at DESC) -- 一覧ソート用

2.3 prompts

L2 出力の実行方針。タスクごとにバージョン管理。

カラム 制約 説明
id TEXT PK (ULID) プロンプト ID
task_id TEXT NOT NULL, FK → tasks 親タスク
version INTEGER NOT NULL, DEFAULT 1 バージョン番号
content TEXT NOT NULL, DEFAULT '' 実行方針テキスト
status TEXT NOT NULL, DEFAULT 'generating' PromptStatus enum
approved_by TEXT 承認者 (Slack user ID)
approved_at TEXT 承認日時
rejection_reason TEXT 却下理由
rejected_by TEXT 却下者 (Slack user ID)
created_at TEXT NOT NULL, DEFAULT now 作成日時

制約: - UNIQUE (task_id, version) -- タスク内で版番号は一意

インデックス: - idx_prompts_task ON (task_id)

2.4 processes

L3 出力のステップ計画。タスクごとにバージョン管理。

カラム 制約 説明
id TEXT PK (ULID) プロセス ID
task_id TEXT NOT NULL, FK → tasks 親タスク
prompt_id TEXT NOT NULL, FK → prompts 基にしたプロンプト
version INTEGER NOT NULL, DEFAULT 1 バージョン番号
steps TEXT NOT NULL, DEFAULT '[]' JSON: ステップ配列
status TEXT NOT NULL, DEFAULT 'generating' ProcessStatus enum
approved_by TEXT 承認者
approved_at TEXT 承認日時
rejection_reason TEXT 却下理由
rejected_by TEXT 却下者
created_at TEXT NOT NULL, DEFAULT now 作成日時

steps JSON 構造:

[
  {
    "stepId": "step-1",
    "order": 1,
    "tool": "list_employees",
    "toolInput": { "department": null, "status": "active" },
    "description": "従業員一覧を取得する",
    "expectedOutput": "従業員のリスト"
  }
]

制約: - UNIQUE (task_id, version)

インデックス: - idx_processes_task ON (task_id)

2.5 executions

L4 の実行記録。1 プロセスに対して複数回実行可能 (再実行)。

カラム 制約 説明
id TEXT PK (ULID) 実行 ID
task_id TEXT NOT NULL, FK → tasks 親タスク
process_id TEXT NOT NULL, FK → processes 実行したプロセス
session_id TEXT Managed Agent Session ID
status TEXT NOT NULL, DEFAULT 'pending' ExecutionStatus enum
current_step INTEGER NOT NULL, DEFAULT 0 現在のステップ番号
results TEXT NOT NULL, DEFAULT '[]' JSON: ステップ実行結果
error TEXT エラーメッセージ
cancelled_by TEXT 中止者 (Slack user ID)
cancelled_at TEXT 中止日時
started_at TEXT 実行開始日時
completed_at TEXT 実行完了日時

results JSON 構造:

[
  {
    "stepId": "step-1",
    "tool": "list_employees",
    "status": "completed",
    "result": { "count": 42 },
    "duration_ms": 800,
    "started_at": "2026-04-18T14:35:00Z",
    "completed_at": "2026-04-18T14:35:00.800Z"
  }
]

インデックス: - idx_executions_task ON (task_id) - idx_executions_process ON (process_id)

2.6 slack_messages

Slack カードの message_ts と DB エンティティのマッピング。chat.update で書き換える際に使用。

カラム 制約 説明
id TEXT PK (ULID) レコード ID
task_id TEXT NOT NULL, FK → tasks 親タスク
card_type TEXT NOT NULL task / prompt / process / execution
channel TEXT NOT NULL Slack channel ID
message_ts TEXT NOT NULL Slack message timestamp
created_at TEXT NOT NULL, DEFAULT now 作成日時

インデックス: - idx_slack_messages_task ON (task_id)

2.7 audit_logs (MVP 新規)

全操作の監査ログ。

カラム 制約 説明
id TEXT PK (ULID) ログ ID
tenant_id TEXT NOT NULL, FK → tenants テナント
timestamp TEXT NOT NULL イベント発生日時
actor_type TEXT NOT NULL user / system / agent
actor_id TEXT Slack user ID or 空
action TEXT NOT NULL アクション種別 (下記参照)
resource_type TEXT NOT NULL task / prompt / process / execution / settings / integration
resource_id TEXT 対象リソースの ID
details TEXT DEFAULT '{}' JSON: 補足情報

アクション種別:

action actor_type トリガー
task.created system L1 完了
prompt.approved user Slack [承認]
prompt.rejected user Slack [却下]
process.approved user Slack [承認]
process.rejected user Slack [却下]
execution.started system L4 開始
execution.completed system L4 正常完了
execution.failed system L4 異常終了
execution.cancelled user [中止]
settings.updated user Web 設定保存
integration.connected user OAuth 接続完了
integration.disconnected user [切断]

インデックス: - idx_audit_logs_tenant_ts ON (tenant_id, timestamp DESC) -- 一覧フィルタ用 - idx_audit_logs_action ON (action)

2.8 settings (MVP 新規)

テナント設定。1 テナント 1 レコード。

カラム 制約 説明
id TEXT PK (ULID) レコード ID
tenant_id TEXT NOT NULL, FK → tenants, UNIQUE テナント (1:1)
config TEXT NOT NULL, DEFAULT '{}' JSON: 設定値
updated_at TEXT NOT NULL, DEFAULT now 最終更新日時
updated_by TEXT 更新者

config JSON 構造:

{
  "name": "HR AI Agent",
  "slug": "default",
  "prompt_approval_required": true,
  "process_approval_required": true,
  "model": "claude-sonnet-4-6",
  "l4_timeout_seconds": 360,
  "auto_approve_tools": true
}

2.9 integrations (MVP 新規)

外部サービス接続情報。

カラム 制約 説明
id TEXT PK (ULID) レコード ID
tenant_id TEXT NOT NULL, FK → tenants テナント
provider TEXT NOT NULL freee / google / slack
status TEXT NOT NULL, DEFAULT 'disconnected' connected / disconnected / error
credentials TEXT DEFAULT '{}' JSON (暗号化): OAuth トークン等
metadata TEXT DEFAULT '{}' JSON: プロバイダー固有情報
connected_at TEXT 接続日時
updated_at TEXT NOT NULL, DEFAULT now 更新日時

制約: - UNIQUE (tenant_id, provider) -- テナント内でプロバイダーは一意

2.10 tool_permissions

ツール実行権限 (Post-MVP で活用)。

カラム 制約 説明
id TEXT PK (ULID) レコード ID
tenant_id TEXT NOT NULL, FK → tenants テナント
tool_name TEXT NOT NULL ツール名
allowed INTEGER NOT NULL, DEFAULT 1 1=許可, 0=禁止
constraints TEXT NOT NULL, DEFAULT '{}' JSON: 制約条件
created_at TEXT NOT NULL, DEFAULT now 作成日時
updated_at TEXT NOT NULL, DEFAULT now 更新日時

制約: - UNIQUE (tenant_id, tool_name)


3. 状態遷移図

3.1 TaskStatus

stateDiagram-v2
    [*] --> extracted
    extracted --> running : L4 実行開始
    extracted --> cancelled : タスク取消
    running --> completed : L4 正常完了
    running --> failed : L4 異常終了
    running --> cancelled : 中止ボタン
    failed --> running : 再実行ボタン
    completed --> [*]
    failed --> [*]
    cancelled --> [*]
遷移元 遷移先 トリガー
extracted running L4 実行開始
extracted cancelled 承認前にタスク取消
running completed L4 正常完了
running failed L4 異常終了
running cancelled [中止] ボタン
failed running [再実行] ボタン

3.2 PromptStatus

stateDiagram-v2
    [*] --> generating
    generating --> pending_approval : L2 完了
    pending_approval --> approved : 承認ボタン
    pending_approval --> rejected : 却下ボタン
    rejected --> [*] : 新 version を generating で作成
    approved --> [*]
遷移元 遷移先 トリガー
generating pending_approval L2 完了、content 書込
pending_approval approved [承認] ボタン
pending_approval rejected [却下] ボタン

3.3 ProcessStatus

PromptStatus と同一パターン。

遷移元 遷移先 トリガー
generating pending_approval L3 完了、steps 書込
pending_approval approved [承認] ボタン
pending_approval rejected [却下] ボタン

3.4 ExecutionStatus

stateDiagram-v2
    [*] --> pending
    pending --> running : Session 作成完了
    running --> completed : Session idle + summary
    running --> failed : Session error / timeout
    running --> cancelled : 中止ボタン
    completed --> [*]
    failed --> [*]
    cancelled --> [*]
遷移元 遷移先 トリガー
pending running Session 作成完了
running completed Session idle + summary
running failed Session error / timeout
running cancelled [中止] ボタン

4. マイグレーション戦略

4.1 現状 (Schema Version 1)

プロトタイプの DDL。tenants, tasks, prompts, processes, executions, slack_messages, tool_permissions の 7 テーブル + インデックス。

4.2 MVP (Schema Version 2)

Version 1 からの差分:

-- Schema Version 2: MVP additions

-- tasks: source カラム追加
ALTER TABLE tasks ADD COLUMN source TEXT NOT NULL DEFAULT 'channel';

-- prompts: 却下理由カラム追加
ALTER TABLE prompts ADD COLUMN rejection_reason TEXT;
ALTER TABLE prompts ADD COLUMN rejected_by TEXT;

-- processes: 却下理由カラム追加
ALTER TABLE processes ADD COLUMN rejection_reason TEXT;
ALTER TABLE processes ADD COLUMN rejected_by TEXT;

-- executions: 中止カラム・ステータス追加
ALTER TABLE executions ADD COLUMN cancelled_by TEXT;
ALTER TABLE executions ADD COLUMN cancelled_at TEXT;

-- audit_logs (新規)
CREATE TABLE IF NOT EXISTS audit_logs (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL REFERENCES tenants(id),
    timestamp TEXT NOT NULL,
    actor_type TEXT NOT NULL,
    actor_id TEXT,
    action TEXT NOT NULL,
    resource_type TEXT NOT NULL,
    resource_id TEXT,
    details TEXT NOT NULL DEFAULT '{}'
);
CREATE INDEX IF NOT EXISTS idx_audit_logs_tenant_ts
    ON audit_logs(tenant_id, timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_audit_logs_action
    ON audit_logs(action);

-- settings (新規)
CREATE TABLE IF NOT EXISTS settings (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL REFERENCES tenants(id),
    config TEXT NOT NULL DEFAULT '{}',
    updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%f+00:00', 'now')),
    updated_by TEXT
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_settings_tenant
    ON settings(tenant_id);

-- integrations (新規)
CREATE TABLE IF NOT EXISTS integrations (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL REFERENCES tenants(id),
    provider TEXT NOT NULL,
    status TEXT NOT NULL DEFAULT 'disconnected',
    credentials TEXT NOT NULL DEFAULT '{}',
    metadata TEXT NOT NULL DEFAULT '{}',
    connected_at TEXT,
    updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%f+00:00', 'now'))
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_integrations_tenant_provider
    ON integrations(tenant_id, provider);

-- tasks: 作成日時降順インデックス追加
CREATE INDEX IF NOT EXISTS idx_tasks_created_at
    ON tasks(created_at DESC);

-- executions: process_id インデックス追加
CREATE INDEX IF NOT EXISTS idx_executions_process
    ON executions(process_id);

4.3 マイグレーション実行

既存の _MIGRATIONS dict にバージョン 2 を追加し、migrate() で自動適用:

SCHEMA_VERSION = 2

_MIGRATIONS: dict[int, str] = {
    1: "...(既存)...",
    2: "...(上記 SQL)...",
}

5. データフロー

5.1 パイプライン実行時の書込順序

1. tasks       INSERT (status: extracted)
2. slack_messages INSERT (card_type: task)
3. prompts     INSERT (status: generating)
4. slack_messages INSERT (card_type: prompt)
5. prompts     UPDATE (content, status: pending_approval)
6. audit_logs  INSERT (action: task.created)

--- 承認後 ---
7. prompts     UPDATE (status: approved, approved_by, approved_at)
8. audit_logs  INSERT (action: prompt.approved)
9. processes   INSERT (status: generating)
10. slack_messages INSERT (card_type: process)
11. processes   UPDATE (steps, status: pending_approval)

--- 承認後 ---
12. processes   UPDATE (status: approved)
13. audit_logs  INSERT (action: process.approved)
14. tasks       UPDATE (status: running)
15. executions  INSERT (status: pending)
16. slack_messages INSERT (card_type: execution)
17. executions  UPDATE (status: running, started_at)
18. audit_logs  INSERT (action: execution.started)

--- 完了 ---
19. executions  UPDATE (status: completed, results, completed_at)
20. tasks       UPDATE (status: completed)
21. audit_logs  INSERT (action: execution.completed)

変更履歴

日付 バージョン 変更内容
2026-04-18 v1.0 初版作成
2026-04-20 v1.1 tasks.source の利用範囲(MVP は channel のみ)を注記。実装の SCHEMA_VERSION=2 に整合済みであることを確認