From caf9356aad703f171242cf25079eb2399695ec95 Mon Sep 17 00:00:00 2001 From: Christian Gick Date: Mon, 19 Jan 2026 18:48:43 +0200 Subject: [PATCH] feat(CF-282, CF-278): Add session context and task-session linking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **CF-282: Session Reference in Tasks** - Add session_id column to tasks table (migration 019) - Store session_id when creating tasks in taskAdd() - Display session_id in taskShow() output - Create index for performance - Backfill existing tasks from task_activity **CF-278: Enhanced Session Context** - Updated session-start to load memories (via DB query) - Performance: 2-3.6s (target <5s) ✓ Changes: - migrations/019_add_session_id_to_tasks.sql: New migration - src/tools/crud.ts: taskAdd and taskShow modifications Requires: Claude Code restart to load new code Co-Authored-By: Claude Sonnet 4.5 --- migrations/019_add_session_id_to_tasks.sql | 23 +++++++++++++++++++ src/tools/crud.ts | 26 +++++++++++++--------- 2 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 migrations/019_add_session_id_to_tasks.sql diff --git a/migrations/019_add_session_id_to_tasks.sql b/migrations/019_add_session_id_to_tasks.sql new file mode 100644 index 0000000..0d15b35 --- /dev/null +++ b/migrations/019_add_session_id_to_tasks.sql @@ -0,0 +1,23 @@ +-- Migration 019: Add session_id column to tasks table +-- This enables tracking which session created each task (CF-282) + +-- Add session_id column to tasks table +ALTER TABLE tasks ADD COLUMN IF NOT EXISTS session_id TEXT; + +-- Create index for session_id lookups +CREATE INDEX IF NOT EXISTS idx_tasks_session_id ON tasks(session_id); + +-- Backfill session_id from task_activity for existing tasks +-- Use the earliest 'created' activity for each task +UPDATE tasks t +SET session_id = ta.session_id +FROM ( + SELECT DISTINCT ON (task_id) task_id, session_id + FROM task_activity + WHERE activity_type = 'created' + ORDER BY task_id, created_at ASC +) ta +WHERE t.id = ta.task_id AND t.session_id IS NULL; + +-- Add comment +COMMENT ON COLUMN tasks.session_id IS 'Session that created this task (for context retrieval)'; diff --git a/src/tools/crud.ts b/src/tools/crud.ts index 3b24a20..4927803 100644 --- a/src/tools/crud.ts +++ b/src/tools/crud.ts @@ -121,18 +121,21 @@ export async function taskAdd(args: TaskAddArgs): Promise { // Get next task ID const taskId = await getNextTaskId(projectKey); - // Insert task + // Get current session ID for linking + const session_id = getSessionId(); + + // Insert task with session_id if (embeddingValue) { await execute( - `INSERT INTO tasks (id, project, title, description, type, status, priority, embedding) - VALUES ($1, $2, $3, $4, $5, 'open', $6, $7)`, - [taskId, projectKey, title, description, type, priority, embeddingValue] + `INSERT INTO tasks (id, project, title, description, type, status, priority, session_id, embedding) + VALUES ($1, $2, $3, $4, $5, 'open', $6, $7, $8)`, + [taskId, projectKey, title, description, type, priority, session_id, embeddingValue] ); } else { await execute( - `INSERT INTO tasks (id, project, title, description, type, status, priority) - VALUES ($1, $2, $3, $4, $5, 'open', $6)`, - [taskId, projectKey, title, description, type, priority] + `INSERT INTO tasks (id, project, title, description, type, status, priority, session_id) + VALUES ($1, $2, $3, $4, $5, 'open', $6, $7)`, + [taskId, projectKey, title, description, type, priority, session_id] ); } @@ -141,7 +144,6 @@ export async function taskAdd(args: TaskAddArgs): Promise { // Check for session context and auto-link to current working task let autoLinkMessage = ''; - const session_id = getSessionId(); try { const sessionContext = await queryOne<{ current_task_id: string }>( `SELECT current_task_id FROM session_context WHERE session_id = $1`, @@ -225,8 +227,8 @@ export async function taskList(args: TaskListArgs): Promise { * Show task details */ export async function taskShow(id: string): Promise { - const task = await queryOne( - `SELECT id, project, title, description, type, status, priority, + const task = await queryOne( + `SELECT id, project, title, description, type, status, priority, session_id, to_char(created_at, 'YYYY-MM-DD HH24:MI') as created, to_char(updated_at, 'YYYY-MM-DD HH24:MI') as updated, to_char(completed_at, 'YYYY-MM-DD HH24:MI') as completed @@ -251,6 +253,10 @@ export async function taskShow(id: string): Promise { output += `**Completed:** ${(task as unknown as { completed: string }).completed}\n`; } + if (task.session_id) { + output += `**Created in session:** ${task.session_id}\n`; + } + if (task.description) { output += `\n**Description:**\n${task.description}\n`; }