@compilr-dev/sdk 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ /**
2
+ * SQLite Project Repository — Concrete implementation of IProjectRepository.
3
+ *
4
+ * All methods return Promises (wrapping synchronous better-sqlite3 calls)
5
+ * to satisfy the async IProjectRepository interface.
6
+ */
7
+ import type Database from 'better-sqlite3';
8
+ import type { IProjectRepository } from '../repositories.js';
9
+ import type { Project, ProjectStatus, CreateProjectInput, UpdateProjectInput, ProjectListOptions, ProjectListResult } from '../types.js';
10
+ /**
11
+ * Options for customizing project deletion behavior.
12
+ * Consumers can provide a callback to clean up platform-specific files.
13
+ */
14
+ export interface ProjectDeleteHooks {
15
+ onDelete?: (projectId: number) => void;
16
+ }
17
+ export declare class SQLiteProjectRepository implements IProjectRepository {
18
+ private readonly db;
19
+ private readonly deleteHooks?;
20
+ constructor(db: Database.Database, hooks?: ProjectDeleteHooks);
21
+ create(input: CreateProjectInput): Promise<Project>;
22
+ getById(id: number): Promise<Project | null>;
23
+ getByName(name: string): Promise<Project | null>;
24
+ getByPath(path: string): Promise<Project | null>;
25
+ getByDocsPath(docsPath: string): Promise<Project | null>;
26
+ findByPath(searchPath: string): Promise<Project | null>;
27
+ list(options?: ProjectListOptions): Promise<ProjectListResult>;
28
+ update(id: number, input: UpdateProjectInput): Promise<Project | null>;
29
+ touch(id: number): Promise<void>;
30
+ delete(id: number): Promise<boolean>;
31
+ archive(id: number): Promise<Project | null>;
32
+ isNameAvailable(name: string, excludeId?: number): Promise<boolean>;
33
+ getStatusCounts(): Promise<Record<ProjectStatus, number>>;
34
+ }
@@ -0,0 +1,282 @@
1
+ /**
2
+ * SQLite Project Repository — Concrete implementation of IProjectRepository.
3
+ *
4
+ * All methods return Promises (wrapping synchronous better-sqlite3 calls)
5
+ * to satisfy the async IProjectRepository interface.
6
+ */
7
+ function recordToProject(record) {
8
+ return {
9
+ id: record.id,
10
+ name: record.name,
11
+ displayName: record.display_name,
12
+ description: record.description,
13
+ type: record.type,
14
+ status: record.status,
15
+ path: record.path,
16
+ docsPath: record.docs_path,
17
+ repoPattern: record.repo_pattern,
18
+ language: record.language,
19
+ framework: record.framework,
20
+ packageManager: record.package_manager,
21
+ runtimeVersion: record.runtime_version,
22
+ commands: record.commands ? JSON.parse(record.commands) : null,
23
+ gitRemote: record.git_remote,
24
+ gitBranch: record.git_branch,
25
+ workflowMode: record.workflow_mode,
26
+ lifecycleState: record.lifecycle_state,
27
+ currentItemId: record.current_item_id,
28
+ lastContext: record.last_context
29
+ ? JSON.parse(record.last_context)
30
+ : null,
31
+ metadata: record.metadata ? JSON.parse(record.metadata) : null,
32
+ createdAt: new Date(record.created_at),
33
+ updatedAt: new Date(record.updated_at),
34
+ lastActivityAt: record.last_activity_at ? new Date(record.last_activity_at) : null,
35
+ };
36
+ }
37
+ export class SQLiteProjectRepository {
38
+ db;
39
+ deleteHooks;
40
+ constructor(db, hooks) {
41
+ this.db = db;
42
+ this.deleteHooks = hooks;
43
+ }
44
+ create(input) {
45
+ const now = new Date().toISOString();
46
+ const result = this.db
47
+ .prepare(`INSERT INTO projects (
48
+ name, display_name, description, type, path, docs_path, repo_pattern,
49
+ language, framework, package_manager, runtime_version, commands,
50
+ git_remote, git_branch, workflow_mode, metadata,
51
+ created_at, updated_at, last_activity_at
52
+ ) VALUES (
53
+ @name, @display_name, @description, @type, @path, @docs_path, @repo_pattern,
54
+ @language, @framework, @package_manager, @runtime_version, @commands,
55
+ @git_remote, @git_branch, @workflow_mode, @metadata,
56
+ @created_at, @updated_at, @last_activity_at
57
+ )`)
58
+ .run({
59
+ name: input.name,
60
+ display_name: input.display_name,
61
+ description: input.description ?? null,
62
+ type: input.type ?? 'general',
63
+ path: input.path,
64
+ docs_path: input.docs_path ?? null,
65
+ repo_pattern: input.repo_pattern ?? 'single',
66
+ language: input.language ?? null,
67
+ framework: input.framework ?? null,
68
+ package_manager: input.package_manager ?? null,
69
+ runtime_version: input.runtime_version ?? null,
70
+ commands: input.commands ? JSON.stringify(input.commands) : null,
71
+ git_remote: input.git_remote ?? null,
72
+ git_branch: input.git_branch ?? 'main',
73
+ workflow_mode: input.workflow_mode ?? 'flexible',
74
+ metadata: input.metadata ? JSON.stringify(input.metadata) : null,
75
+ created_at: now,
76
+ updated_at: now,
77
+ last_activity_at: now,
78
+ });
79
+ const record = this.db
80
+ .prepare('SELECT * FROM projects WHERE id = ?')
81
+ .get(Number(result.lastInsertRowid));
82
+ if (!record)
83
+ throw new Error('Failed to create project');
84
+ return Promise.resolve(recordToProject(record));
85
+ }
86
+ getById(id) {
87
+ const record = this.db.prepare('SELECT * FROM projects WHERE id = ?').get(id);
88
+ return Promise.resolve(record ? recordToProject(record) : null);
89
+ }
90
+ getByName(name) {
91
+ const record = this.db.prepare('SELECT * FROM projects WHERE name = ?').get(name);
92
+ return Promise.resolve(record ? recordToProject(record) : null);
93
+ }
94
+ getByPath(path) {
95
+ const record = this.db.prepare('SELECT * FROM projects WHERE path = ?').get(path);
96
+ return Promise.resolve(record ? recordToProject(record) : null);
97
+ }
98
+ getByDocsPath(docsPath) {
99
+ const record = this.db.prepare('SELECT * FROM projects WHERE docs_path = ?').get(docsPath);
100
+ return Promise.resolve(record ? recordToProject(record) : null);
101
+ }
102
+ async findByPath(searchPath) {
103
+ let project = await this.getByPath(searchPath);
104
+ if (project)
105
+ return project;
106
+ project = await this.getByDocsPath(searchPath);
107
+ if (project)
108
+ return project;
109
+ const parts = searchPath.split('/').filter(Boolean);
110
+ for (let i = parts.length - 1; i >= 0; i--) {
111
+ const parentPath = '/' + parts.slice(0, i).join('/');
112
+ if (!parentPath || parentPath === '/')
113
+ break;
114
+ project = await this.getByPath(parentPath);
115
+ if (project)
116
+ return project;
117
+ project = await this.getByDocsPath(parentPath);
118
+ if (project)
119
+ return project;
120
+ }
121
+ return null;
122
+ }
123
+ list(options) {
124
+ const conditions = [];
125
+ const params = {};
126
+ if (options?.status && options.status !== 'all') {
127
+ conditions.push('status = @status');
128
+ params.status = options.status;
129
+ }
130
+ if (options?.type && options.type !== 'all') {
131
+ conditions.push('type = @type');
132
+ params.type = options.type;
133
+ }
134
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
135
+ const countResult = this.db
136
+ .prepare(`SELECT COUNT(*) as count FROM projects ${whereClause}`)
137
+ .get(params);
138
+ let query = `SELECT * FROM projects ${whereClause} ORDER BY last_activity_at DESC NULLS LAST, updated_at DESC`;
139
+ if (options?.limit) {
140
+ query += ` LIMIT @limit`;
141
+ params.limit = options.limit;
142
+ }
143
+ if (options?.offset) {
144
+ query += ` OFFSET @offset`;
145
+ params.offset = options.offset;
146
+ }
147
+ const records = this.db.prepare(query).all(params);
148
+ return Promise.resolve({
149
+ projects: records.map(recordToProject),
150
+ total: countResult.count,
151
+ });
152
+ }
153
+ update(id, input) {
154
+ const updates = [];
155
+ const params = { id };
156
+ if (input.display_name !== undefined) {
157
+ updates.push('display_name = @display_name');
158
+ params.display_name = input.display_name;
159
+ }
160
+ if (input.description !== undefined) {
161
+ updates.push('description = @description');
162
+ params.description = input.description;
163
+ }
164
+ if (input.type !== undefined) {
165
+ updates.push('type = @type');
166
+ params.type = input.type;
167
+ }
168
+ if (input.status !== undefined) {
169
+ updates.push('status = @status');
170
+ params.status = input.status;
171
+ }
172
+ if (input.docs_path !== undefined) {
173
+ updates.push('docs_path = @docs_path');
174
+ params.docs_path = input.docs_path;
175
+ }
176
+ if (input.repo_pattern !== undefined) {
177
+ updates.push('repo_pattern = @repo_pattern');
178
+ params.repo_pattern = input.repo_pattern;
179
+ }
180
+ if (input.language !== undefined) {
181
+ updates.push('language = @language');
182
+ params.language = input.language;
183
+ }
184
+ if (input.framework !== undefined) {
185
+ updates.push('framework = @framework');
186
+ params.framework = input.framework;
187
+ }
188
+ if (input.package_manager !== undefined) {
189
+ updates.push('package_manager = @package_manager');
190
+ params.package_manager = input.package_manager;
191
+ }
192
+ if (input.runtime_version !== undefined) {
193
+ updates.push('runtime_version = @runtime_version');
194
+ params.runtime_version = input.runtime_version;
195
+ }
196
+ if (input.commands !== undefined) {
197
+ updates.push('commands = @commands');
198
+ params.commands = JSON.stringify(input.commands);
199
+ }
200
+ if (input.git_remote !== undefined) {
201
+ updates.push('git_remote = @git_remote');
202
+ params.git_remote = input.git_remote;
203
+ }
204
+ if (input.git_branch !== undefined) {
205
+ updates.push('git_branch = @git_branch');
206
+ params.git_branch = input.git_branch;
207
+ }
208
+ if (input.workflow_mode !== undefined) {
209
+ updates.push('workflow_mode = @workflow_mode');
210
+ params.workflow_mode = input.workflow_mode;
211
+ }
212
+ if (input.lifecycle_state !== undefined) {
213
+ updates.push('lifecycle_state = @lifecycle_state');
214
+ params.lifecycle_state = input.lifecycle_state;
215
+ }
216
+ if (input.current_item_id !== undefined) {
217
+ updates.push('current_item_id = @current_item_id');
218
+ params.current_item_id = input.current_item_id;
219
+ }
220
+ if (input.last_context !== undefined) {
221
+ updates.push('last_context = @last_context');
222
+ params.last_context = JSON.stringify(input.last_context);
223
+ }
224
+ if (input.metadata !== undefined) {
225
+ updates.push('metadata = @metadata');
226
+ params.metadata = JSON.stringify(input.metadata);
227
+ }
228
+ if (updates.length === 0) {
229
+ return this.getById(id);
230
+ }
231
+ updates.push('updated_at = @updated_at');
232
+ params.updated_at = new Date().toISOString();
233
+ this.db.prepare(`UPDATE projects SET ${updates.join(', ')} WHERE id = @id`).run(params);
234
+ return this.getById(id);
235
+ }
236
+ touch(id) {
237
+ const now = new Date().toISOString();
238
+ this.db
239
+ .prepare('UPDATE projects SET last_activity_at = ?, updated_at = ? WHERE id = ?')
240
+ .run(now, now, id);
241
+ return Promise.resolve();
242
+ }
243
+ delete(id) {
244
+ const project = this.db.prepare('SELECT status FROM projects WHERE id = ?').get(id);
245
+ if (!project)
246
+ return Promise.resolve(false);
247
+ if (project.status !== 'archived') {
248
+ throw new Error('Project must be archived before deletion. Use archive() first.');
249
+ }
250
+ const result = this.db.prepare('DELETE FROM projects WHERE id = ?').run(id);
251
+ if (result.changes > 0) {
252
+ this.deleteHooks?.onDelete?.(id);
253
+ }
254
+ return Promise.resolve(result.changes > 0);
255
+ }
256
+ archive(id) {
257
+ return this.update(id, { status: 'archived' });
258
+ }
259
+ isNameAvailable(name, excludeId) {
260
+ const query = excludeId
261
+ ? 'SELECT id FROM projects WHERE name = ? AND id != ?'
262
+ : 'SELECT id FROM projects WHERE name = ?';
263
+ const params = excludeId ? [name, excludeId] : [name];
264
+ const result = this.db.prepare(query).get(...params);
265
+ return Promise.resolve(!result);
266
+ }
267
+ getStatusCounts() {
268
+ const results = this.db
269
+ .prepare('SELECT status, COUNT(*) as count FROM projects GROUP BY status')
270
+ .all();
271
+ const counts = {
272
+ active: 0,
273
+ paused: 0,
274
+ completed: 0,
275
+ archived: 0,
276
+ };
277
+ for (const row of results) {
278
+ counts[row.status] = row.count;
279
+ }
280
+ return Promise.resolve(counts);
281
+ }
282
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Database Schema for compilr-dev workflow system
3
+ *
4
+ * Shared between CLI and Desktop — both access ~/.compilr-dev/projects.db
5
+ * Schema version must be kept in sync across all consumers.
6
+ */
7
+ export declare const SCHEMA_VERSION = 6;
8
+ export declare const SCHEMA_SQL = "\n-- Schema version tracking\nCREATE TABLE IF NOT EXISTS schema_version (\n version INTEGER PRIMARY KEY,\n applied_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);\n\n-- Projects table\nCREATE TABLE IF NOT EXISTS projects (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT UNIQUE NOT NULL,\n display_name TEXT NOT NULL,\n description TEXT,\n type TEXT DEFAULT 'general',\n status TEXT DEFAULT 'active',\n path TEXT NOT NULL,\n docs_path TEXT,\n repo_pattern TEXT DEFAULT 'single',\n language TEXT,\n framework TEXT,\n package_manager TEXT,\n runtime_version TEXT,\n commands TEXT,\n git_remote TEXT,\n git_branch TEXT DEFAULT 'main',\n workflow_mode TEXT DEFAULT 'flexible',\n lifecycle_state TEXT DEFAULT 'setup',\n current_item_id TEXT,\n last_context TEXT,\n metadata TEXT,\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n last_activity_at DATETIME\n);\n\n-- Work items (backlog items, tasks, bugs)\nCREATE TABLE IF NOT EXISTS work_items (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n project_id INTEGER NOT NULL,\n item_number INTEGER NOT NULL,\n item_id TEXT NOT NULL,\n type TEXT NOT NULL,\n status TEXT DEFAULT 'backlog',\n priority TEXT DEFAULT 'medium',\n guided_step TEXT,\n owner TEXT,\n title TEXT NOT NULL,\n description TEXT,\n estimated_effort TEXT,\n actual_minutes INTEGER,\n completed_at DATETIME,\n completed_by TEXT,\n commit_hash TEXT,\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,\n UNIQUE (project_id, item_id)\n);\n\n-- Project documents (PRD, architecture, plans, etc.)\nCREATE TABLE IF NOT EXISTS project_documents (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n project_id INTEGER NOT NULL,\n doc_type TEXT NOT NULL,\n title TEXT NOT NULL,\n content TEXT NOT NULL,\n status TEXT,\n work_item_id INTEGER,\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,\n FOREIGN KEY (work_item_id) REFERENCES work_items(id) ON DELETE SET NULL\n);\n\n-- Work item history (audit trail)\nCREATE TABLE IF NOT EXISTS work_item_history (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n work_item_id INTEGER NOT NULL,\n project_id INTEGER NOT NULL,\n action TEXT NOT NULL,\n old_value TEXT,\n new_value TEXT,\n notes TEXT,\n changed_by TEXT,\n changed_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (work_item_id) REFERENCES work_items(id) ON DELETE CASCADE,\n FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE\n);\n\n-- Indexes\nCREATE INDEX IF NOT EXISTS idx_projects_path ON projects(path);\nCREATE INDEX IF NOT EXISTS idx_projects_docs_path ON projects(docs_path);\nCREATE INDEX IF NOT EXISTS idx_projects_status ON projects(status);\nCREATE INDEX IF NOT EXISTS idx_work_items_project ON work_items(project_id);\nCREATE INDEX IF NOT EXISTS idx_work_items_status ON work_items(status);\nCREATE INDEX IF NOT EXISTS idx_work_items_priority ON work_items(priority);\nCREATE INDEX IF NOT EXISTS idx_work_items_owner ON work_items(owner);\nCREATE INDEX IF NOT EXISTS idx_project_documents_project ON project_documents(project_id);\nCREATE INDEX IF NOT EXISTS idx_project_documents_type ON project_documents(doc_type);\nCREATE INDEX IF NOT EXISTS idx_project_documents_status ON project_documents(status);\nCREATE INDEX IF NOT EXISTS idx_project_documents_work_item ON project_documents(work_item_id);\nCREATE INDEX IF NOT EXISTS idx_work_item_history_item ON work_item_history(work_item_id);\n\n-- Terminal sessions (multi-terminal awareness)\nCREATE TABLE IF NOT EXISTS terminal_sessions (\n id TEXT PRIMARY KEY,\n project_id INTEGER,\n pid INTEGER NOT NULL,\n tty_path TEXT,\n label TEXT,\n started_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n last_heartbeat DATETIME DEFAULT CURRENT_TIMESTAMP,\n active_agent TEXT DEFAULT 'default',\n agents_json TEXT DEFAULT '[]',\n status TEXT DEFAULT 'active',\n FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE SET NULL\n);\nCREATE INDEX IF NOT EXISTS idx_terminal_sessions_project ON terminal_sessions(project_id);\nCREATE INDEX IF NOT EXISTS idx_terminal_sessions_status ON terminal_sessions(status);\n\n-- File locks (multi-terminal file lock awareness)\nCREATE TABLE IF NOT EXISTS file_locks (\n path TEXT NOT NULL,\n project_id INTEGER NOT NULL,\n session_id TEXT NOT NULL,\n agent_id TEXT NOT NULL,\n locked_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (path, project_id),\n FOREIGN KEY (session_id) REFERENCES terminal_sessions(id) ON DELETE CASCADE,\n FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE\n);\nCREATE INDEX IF NOT EXISTS idx_file_locks_session ON file_locks(session_id);\n\n-- Session notifications (cross-session notifications)\nCREATE TABLE IF NOT EXISTS session_notifications (\n id TEXT PRIMARY KEY,\n project_id INTEGER NOT NULL,\n from_session_id TEXT NOT NULL,\n to_session_id TEXT,\n type TEXT NOT NULL,\n title TEXT NOT NULL,\n message TEXT,\n payload_json TEXT,\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n read_at DATETIME,\n FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,\n FOREIGN KEY (from_session_id) REFERENCES terminal_sessions(id) ON DELETE CASCADE\n);\nCREATE INDEX IF NOT EXISTS idx_session_notifications_project ON session_notifications(project_id);\nCREATE INDEX IF NOT EXISTS idx_session_notifications_to_session ON session_notifications(to_session_id);\nCREATE INDEX IF NOT EXISTS idx_session_notifications_unread ON session_notifications(read_at);\n";
9
+ export interface ProjectRecord {
10
+ id: number;
11
+ name: string;
12
+ display_name: string;
13
+ description: string | null;
14
+ type: string;
15
+ status: string;
16
+ path: string;
17
+ docs_path: string | null;
18
+ repo_pattern: string;
19
+ language: string | null;
20
+ framework: string | null;
21
+ package_manager: string | null;
22
+ runtime_version: string | null;
23
+ commands: string | null;
24
+ git_remote: string | null;
25
+ git_branch: string;
26
+ workflow_mode: string;
27
+ lifecycle_state: string;
28
+ current_item_id: string | null;
29
+ last_context: string | null;
30
+ metadata: string | null;
31
+ created_at: string;
32
+ updated_at: string;
33
+ last_activity_at: string | null;
34
+ }
35
+ export interface WorkItemRecord {
36
+ id: number;
37
+ project_id: number;
38
+ item_number: number;
39
+ item_id: string;
40
+ type: string;
41
+ status: string;
42
+ priority: string;
43
+ guided_step: string | null;
44
+ owner: string | null;
45
+ title: string;
46
+ description: string | null;
47
+ estimated_effort: string | null;
48
+ actual_minutes: number | null;
49
+ completed_at: string | null;
50
+ completed_by: string | null;
51
+ commit_hash: string | null;
52
+ created_at: string;
53
+ updated_at: string;
54
+ }
55
+ export interface ProjectDocumentRecord {
56
+ id: number;
57
+ project_id: number;
58
+ doc_type: string;
59
+ title: string;
60
+ content: string;
61
+ status: string | null;
62
+ work_item_id: number | null;
63
+ created_at: string;
64
+ updated_at: string;
65
+ }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * Database Schema for compilr-dev workflow system
3
+ *
4
+ * Shared between CLI and Desktop — both access ~/.compilr-dev/projects.db
5
+ * Schema version must be kept in sync across all consumers.
6
+ */
7
+ export const SCHEMA_VERSION = 6;
8
+ export const SCHEMA_SQL = `
9
+ -- Schema version tracking
10
+ CREATE TABLE IF NOT EXISTS schema_version (
11
+ version INTEGER PRIMARY KEY,
12
+ applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
13
+ );
14
+
15
+ -- Projects table
16
+ CREATE TABLE IF NOT EXISTS projects (
17
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
18
+ name TEXT UNIQUE NOT NULL,
19
+ display_name TEXT NOT NULL,
20
+ description TEXT,
21
+ type TEXT DEFAULT 'general',
22
+ status TEXT DEFAULT 'active',
23
+ path TEXT NOT NULL,
24
+ docs_path TEXT,
25
+ repo_pattern TEXT DEFAULT 'single',
26
+ language TEXT,
27
+ framework TEXT,
28
+ package_manager TEXT,
29
+ runtime_version TEXT,
30
+ commands TEXT,
31
+ git_remote TEXT,
32
+ git_branch TEXT DEFAULT 'main',
33
+ workflow_mode TEXT DEFAULT 'flexible',
34
+ lifecycle_state TEXT DEFAULT 'setup',
35
+ current_item_id TEXT,
36
+ last_context TEXT,
37
+ metadata TEXT,
38
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
39
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
40
+ last_activity_at DATETIME
41
+ );
42
+
43
+ -- Work items (backlog items, tasks, bugs)
44
+ CREATE TABLE IF NOT EXISTS work_items (
45
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
46
+ project_id INTEGER NOT NULL,
47
+ item_number INTEGER NOT NULL,
48
+ item_id TEXT NOT NULL,
49
+ type TEXT NOT NULL,
50
+ status TEXT DEFAULT 'backlog',
51
+ priority TEXT DEFAULT 'medium',
52
+ guided_step TEXT,
53
+ owner TEXT,
54
+ title TEXT NOT NULL,
55
+ description TEXT,
56
+ estimated_effort TEXT,
57
+ actual_minutes INTEGER,
58
+ completed_at DATETIME,
59
+ completed_by TEXT,
60
+ commit_hash TEXT,
61
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
62
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
63
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
64
+ UNIQUE (project_id, item_id)
65
+ );
66
+
67
+ -- Project documents (PRD, architecture, plans, etc.)
68
+ CREATE TABLE IF NOT EXISTS project_documents (
69
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
70
+ project_id INTEGER NOT NULL,
71
+ doc_type TEXT NOT NULL,
72
+ title TEXT NOT NULL,
73
+ content TEXT NOT NULL,
74
+ status TEXT,
75
+ work_item_id INTEGER,
76
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
77
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
78
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
79
+ FOREIGN KEY (work_item_id) REFERENCES work_items(id) ON DELETE SET NULL
80
+ );
81
+
82
+ -- Work item history (audit trail)
83
+ CREATE TABLE IF NOT EXISTS work_item_history (
84
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
85
+ work_item_id INTEGER NOT NULL,
86
+ project_id INTEGER NOT NULL,
87
+ action TEXT NOT NULL,
88
+ old_value TEXT,
89
+ new_value TEXT,
90
+ notes TEXT,
91
+ changed_by TEXT,
92
+ changed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
93
+ FOREIGN KEY (work_item_id) REFERENCES work_items(id) ON DELETE CASCADE,
94
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
95
+ );
96
+
97
+ -- Indexes
98
+ CREATE INDEX IF NOT EXISTS idx_projects_path ON projects(path);
99
+ CREATE INDEX IF NOT EXISTS idx_projects_docs_path ON projects(docs_path);
100
+ CREATE INDEX IF NOT EXISTS idx_projects_status ON projects(status);
101
+ CREATE INDEX IF NOT EXISTS idx_work_items_project ON work_items(project_id);
102
+ CREATE INDEX IF NOT EXISTS idx_work_items_status ON work_items(status);
103
+ CREATE INDEX IF NOT EXISTS idx_work_items_priority ON work_items(priority);
104
+ CREATE INDEX IF NOT EXISTS idx_work_items_owner ON work_items(owner);
105
+ CREATE INDEX IF NOT EXISTS idx_project_documents_project ON project_documents(project_id);
106
+ CREATE INDEX IF NOT EXISTS idx_project_documents_type ON project_documents(doc_type);
107
+ CREATE INDEX IF NOT EXISTS idx_project_documents_status ON project_documents(status);
108
+ CREATE INDEX IF NOT EXISTS idx_project_documents_work_item ON project_documents(work_item_id);
109
+ CREATE INDEX IF NOT EXISTS idx_work_item_history_item ON work_item_history(work_item_id);
110
+
111
+ -- Terminal sessions (multi-terminal awareness)
112
+ CREATE TABLE IF NOT EXISTS terminal_sessions (
113
+ id TEXT PRIMARY KEY,
114
+ project_id INTEGER,
115
+ pid INTEGER NOT NULL,
116
+ tty_path TEXT,
117
+ label TEXT,
118
+ started_at DATETIME DEFAULT CURRENT_TIMESTAMP,
119
+ last_heartbeat DATETIME DEFAULT CURRENT_TIMESTAMP,
120
+ active_agent TEXT DEFAULT 'default',
121
+ agents_json TEXT DEFAULT '[]',
122
+ status TEXT DEFAULT 'active',
123
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE SET NULL
124
+ );
125
+ CREATE INDEX IF NOT EXISTS idx_terminal_sessions_project ON terminal_sessions(project_id);
126
+ CREATE INDEX IF NOT EXISTS idx_terminal_sessions_status ON terminal_sessions(status);
127
+
128
+ -- File locks (multi-terminal file lock awareness)
129
+ CREATE TABLE IF NOT EXISTS file_locks (
130
+ path TEXT NOT NULL,
131
+ project_id INTEGER NOT NULL,
132
+ session_id TEXT NOT NULL,
133
+ agent_id TEXT NOT NULL,
134
+ locked_at DATETIME DEFAULT CURRENT_TIMESTAMP,
135
+ PRIMARY KEY (path, project_id),
136
+ FOREIGN KEY (session_id) REFERENCES terminal_sessions(id) ON DELETE CASCADE,
137
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
138
+ );
139
+ CREATE INDEX IF NOT EXISTS idx_file_locks_session ON file_locks(session_id);
140
+
141
+ -- Session notifications (cross-session notifications)
142
+ CREATE TABLE IF NOT EXISTS session_notifications (
143
+ id TEXT PRIMARY KEY,
144
+ project_id INTEGER NOT NULL,
145
+ from_session_id TEXT NOT NULL,
146
+ to_session_id TEXT,
147
+ type TEXT NOT NULL,
148
+ title TEXT NOT NULL,
149
+ message TEXT,
150
+ payload_json TEXT,
151
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
152
+ read_at DATETIME,
153
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
154
+ FOREIGN KEY (from_session_id) REFERENCES terminal_sessions(id) ON DELETE CASCADE
155
+ );
156
+ CREATE INDEX IF NOT EXISTS idx_session_notifications_project ON session_notifications(project_id);
157
+ CREATE INDEX IF NOT EXISTS idx_session_notifications_to_session ON session_notifications(to_session_id);
158
+ CREATE INDEX IF NOT EXISTS idx_session_notifications_unread ON session_notifications(read_at);
159
+ `;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * SQLite Work Item Repository — Concrete implementation of IWorkItemRepository.
3
+ */
4
+ import type Database from 'better-sqlite3';
5
+ import type { IWorkItemRepository } from '../repositories.js';
6
+ import type { WorkItem, WorkItemType, WorkItemStatus, HistoryEntry, CreateWorkItemInput, UpdateWorkItemInput, QueryWorkItemsInput, WorkItemQueryResult, BulkCreateItem } from '../types.js';
7
+ export declare class SQLiteWorkItemRepository implements IWorkItemRepository {
8
+ private readonly db;
9
+ constructor(db: Database.Database);
10
+ create(input: CreateWorkItemInput): Promise<WorkItem>;
11
+ getById(id: number): Promise<WorkItem | null>;
12
+ getByItemId(projectId: number, itemId: string): Promise<WorkItem | null>;
13
+ query(input: QueryWorkItemsInput): Promise<WorkItemQueryResult>;
14
+ getNext(projectId: number, type?: WorkItemType): Promise<WorkItem | null>;
15
+ update(id: number, input: UpdateWorkItemInput): Promise<WorkItem | null>;
16
+ delete(id: number): Promise<boolean>;
17
+ getByOwner(projectId: number, owner: string, status?: WorkItemStatus): Promise<WorkItem[]>;
18
+ getOwnerCounts(projectId: number): Promise<Record<string, number>>;
19
+ getStatusCounts(projectId: number): Promise<Record<WorkItemStatus, number>>;
20
+ getHistory(workItemId: number): Promise<HistoryEntry[]>;
21
+ bulkCreate(projectId: number, items: BulkCreateItem[]): Promise<WorkItem[]>;
22
+ private recordHistory;
23
+ }