feat(db): add personalities + agent-direct prompts schema (Stage 1)

A Personality is a named overlay on top of an Agent — same agent,
same LLM, but a different bundle of prompts injected into the system
block at chat time. VLAN-on-ethernet semantics: ethernet still works
without VLAN; with a VLAN tag, frames are segmented but still ethernet.

Schema additions:
- Prompt.agentId (nullable FK + index, cascade on delete) so prompts
  can attach directly to an agent without going through a project.
- Personality { id, name, description, agentId, priority } with
  unique (name, agentId).
- PersonalityPrompt join table with per-binding priority override.
- Agent.defaultPersonalityId (SetNull on delete) so an agent can pick
  one personality as the default when no --personality flag is passed.

Backwards-compatible by construction: every new column is nullable;
existing rows are valid as-is; the chat.service systemBlock changes
land in Stage 3.

8 new prisma-level assertions in agent-schema.test.ts cover unique
constraints, cascade behavior, the SetNull on defaultPersonalityId,
and shared-prompt-across-personalities. All 16 db tests pass; mcpd
typecheck + 777 mcpd unit tests still green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Michal
2026-04-26 19:12:22 +01:00
parent 9389ffff3c
commit f60f00f1fd
4 changed files with 347 additions and 86 deletions

View File

@@ -0,0 +1,59 @@
-- Add agent-direct prompts and per-agent personalities (with personality<->prompt join).
-- ── Prompt: optional agentId, mirrors projectId pattern ──
ALTER TABLE "Prompt" ADD COLUMN "agentId" TEXT;
CREATE UNIQUE INDEX "Prompt_name_agentId_key" ON "Prompt"("name", "agentId");
CREATE INDEX "Prompt_agentId_idx" ON "Prompt"("agentId");
ALTER TABLE "Prompt" ADD CONSTRAINT "Prompt_agentId_fkey"
FOREIGN KEY ("agentId") REFERENCES "Agent"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- ── Agent: defaultPersonalityId (optional FK; SET NULL on delete to keep agent alive) ──
ALTER TABLE "Agent" ADD COLUMN "defaultPersonalityId" TEXT;
CREATE INDEX "Agent_defaultPersonalityId_idx" ON "Agent"("defaultPersonalityId");
-- ── Personality table ──
CREATE TABLE "Personality" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT NOT NULL DEFAULT '',
"agentId" TEXT NOT NULL,
"priority" INTEGER NOT NULL DEFAULT 5,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Personality_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "Personality_name_agentId_key" ON "Personality"("name", "agentId");
CREATE INDEX "Personality_agentId_idx" ON "Personality"("agentId");
ALTER TABLE "Personality" ADD CONSTRAINT "Personality_agentId_fkey"
FOREIGN KEY ("agentId") REFERENCES "Agent"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- Now that Personality exists, the Agent.defaultPersonalityId FK can point at it.
ALTER TABLE "Agent" ADD CONSTRAINT "Agent_defaultPersonalityId_fkey"
FOREIGN KEY ("defaultPersonalityId") REFERENCES "Personality"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- ── PersonalityPrompt join table ──
CREATE TABLE "PersonalityPrompt" (
"id" TEXT NOT NULL,
"personalityId" TEXT NOT NULL,
"promptId" TEXT NOT NULL,
"priority" INTEGER NOT NULL DEFAULT 5,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "PersonalityPrompt_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "PersonalityPrompt_personalityId_promptId_key" ON "PersonalityPrompt"("personalityId", "promptId");
CREATE INDEX "PersonalityPrompt_personalityId_idx" ON "PersonalityPrompt"("personalityId");
CREATE INDEX "PersonalityPrompt_promptId_idx" ON "PersonalityPrompt"("promptId");
ALTER TABLE "PersonalityPrompt" ADD CONSTRAINT "PersonalityPrompt_personalityId_fkey"
FOREIGN KEY ("personalityId") REFERENCES "Personality"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "PersonalityPrompt" ADD CONSTRAINT "PersonalityPrompt_promptId_fkey"
FOREIGN KEY ("promptId") REFERENCES "Prompt"("id") ON DELETE CASCADE ON UPDATE CASCADE;