feat(agent): refactor database session storage to normalized DAG entries and lazy thread sync

This commit is contained in:
yiling
2026-07-28 11:21:31 +08:00
parent b876af464e
commit 4e2694e2d9
21 changed files with 930 additions and 252 deletions
@@ -0,0 +1,14 @@
-- AlterTable
ALTER TABLE "agent_session_entries" ADD COLUMN "asset_id" TEXT,
ADD COLUMN "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "data" JSONB,
ADD COLUMN "parent_id" TEXT,
ADD COLUMN "type" TEXT,
ALTER COLUMN "session_id" DROP NOT NULL,
ALTER COLUMN "entry" DROP NOT NULL;
-- CreateIndex
CREATE INDEX "agent_session_entries_parent_id_idx" ON "agent_session_entries"("parent_id");
-- CreateIndex
CREATE INDEX "agent_session_entries_asset_id_idx" ON "agent_session_entries"("asset_id");
@@ -0,0 +1,12 @@
-- Migrate existing entry JSON data to new columns
UPDATE agent_session_entries
SET
type = entry->>'type',
parent_id = entry->>'parentId',
asset_id = (SELECT s.asset_id FROM agent_sessions s WHERE s.id = agent_session_entries.session_id),
created_at = CASE
WHEN entry->>'timestamp' IS NOT NULL THEN (entry->>'timestamp')::timestamptz
ELSE created_at
END,
data = (entry::jsonb - 'id' - 'type' - 'parentId' - 'timestamp')::text::json
WHERE entry IS NOT NULL;
@@ -0,0 +1,8 @@
/*
Warnings:
- You are about to drop the column `entry` on the `agent_session_entries` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "agent_session_entries" DROP COLUMN "entry";
+12 -6
View File
@@ -722,14 +722,20 @@ model AgentSession {
}
model AgentSessionEntry {
id String @id // Uses ULID generated by pi-coding-agent
sessionId String @map("session_id")
session AgentSession @relation(fields: [sessionId], references: [id], onDelete: Cascade)
/// [PiSessionEntry]
entry Json
id String @id // Uses ULID generated by pi-coding-agent
sessionId String? @map("session_id")
session AgentSession? @relation(fields: [sessionId], references: [id], onDelete: Cascade)
assetId String? @map("asset_id")
type String?
parentId String? @map("parent_id")
/// [PiSessionEntryData]
data Json?
createdAt DateTime @default(now()) @map("created_at")
@@map("agent_session_entries")
@@index([sessionId])
@@index([parentId])
@@index([assetId])
@@map("agent_session_entries")
}
model UserMetadata {