-
-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathindex.ts
More file actions
540 lines (488 loc) · 21.2 KB
/
Copy pathindex.ts
File metadata and controls
540 lines (488 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import { Database } from 'bun:sqlite'
import { chmodSync, closeSync, existsSync, mkdirSync, openSync } from 'node:fs'
import { dirname } from 'node:path'
import { MachineStore } from './machineStore'
import { MessageStore } from './messageStore'
import { PushStore } from './pushStore'
import { ScratchlistStore } from './scratchlistStore'
import { SessionStore } from './sessionStore'
import { UserStore } from './userStore'
export type {
StoredMachine,
StoredMessage,
StoredPushSubscription,
StoredScratchlistEntry,
StoredSession,
StoredUser,
VersionedUpdateResult
} from './types'
export type { CancelQueuedMessageResult, LookupQueuedMessageResult } from './messages'
export { MachineStore } from './machineStore'
export { MessageStore } from './messageStore'
export { PushStore } from './pushStore'
export { ScratchlistStore } from './scratchlistStore'
export { SessionStore } from './sessionStore'
export { UserStore } from './userStore'
const SCHEMA_VERSION: number = 10
const REQUIRED_TABLES = [
'sessions',
'machines',
'messages',
'users',
'push_subscriptions',
'session_scratchlist'
] as const
export class Store {
private db: Database
private readonly _dbPath: string
private closed: boolean = false
readonly sessions: SessionStore
readonly machines: MachineStore
readonly messages: MessageStore
readonly users: UserStore
readonly push: PushStore
readonly scratchlist: ScratchlistStore
/**
* Filesystem path of the underlying SQLite database, or ':memory:' for
* in-memory stores. Used by the legacy → ACP migrator (#824) to take a
* backup before a bulk run; treat as read-only.
*/
get dbPath(): string {
return this._dbPath
}
constructor(dbPath: string) {
this._dbPath = dbPath
if (dbPath !== ':memory:' && !dbPath.startsWith('file::memory:')) {
const dir = dirname(dbPath)
mkdirSync(dir, { recursive: true, mode: 0o700 })
try {
chmodSync(dir, 0o700)
} catch {
}
if (!existsSync(dbPath)) {
try {
const fd = openSync(dbPath, 'a', 0o600)
closeSync(fd)
} catch {
}
}
}
this.db = new Database(dbPath, { create: true, readwrite: true, strict: true })
this.db.exec('PRAGMA journal_mode = WAL')
this.db.exec('PRAGMA synchronous = NORMAL')
this.db.exec('PRAGMA foreign_keys = ON')
this.db.exec('PRAGMA busy_timeout = 5000')
this.initSchema()
if (dbPath !== ':memory:' && !dbPath.startsWith('file::memory:')) {
for (const path of [dbPath, `${dbPath}-wal`, `${dbPath}-shm`]) {
try {
chmodSync(path, 0o600)
} catch {
}
}
}
this.sessions = new SessionStore(this.db)
this.machines = new MachineStore(this.db)
this.messages = new MessageStore(this.db)
this.users = new UserStore(this.db)
this.push = new PushStore(this.db)
this.scratchlist = new ScratchlistStore(this.db)
}
close(): void {
if (this.closed) return
this.db.close()
this.closed = true
// Bun's SQLite close uses sqlite3_close_v2 by default, so prepared
// statements that are already unreachable may keep the underlying file
// handle alive until the next GC cycle. Windows refuses to remove a
// directory while those SQLite WAL/SHM handles are still pending.
if (process.platform === 'win32') {
Bun.gc(true)
}
}
private initSchema(): void {
const currentVersion = this.getUserVersion()
// V1/V2/V3 entries cover legacy DBs that pre-date our migration ladder.
// Each step is idempotent (column-existence guards inside) so we can
// safely run the full V1→V8 chain in the legacy branch where the DB
// shape is unknown.
const buildStepMigrations = (legacy: boolean): Record<number, () => void> => ({
1: () => this.migrateFromV1ToV2(legacy),
2: () => this.migrateFromV2ToV3(),
3: () => this.migrateFromV3ToV4(),
4: () => this.migrateFromV4ToV5(),
5: () => this.migrateFromV5ToV6(),
6: () => this.migrateFromV6ToV7(),
7: () => this.migrateFromV7ToV8(),
8: () => this.migrateFromV8ToV9(),
9: () => this.migrateFromV9ToV10(),
})
if (currentVersion === 0) {
if (this.hasAnyUserTables()) {
this.migrateLegacySchemaIfNeeded()
// Run the full step ladder BEFORE createSchema so legacy tables
// pick up every later-version column (e.g. invoked_at) via ALTER
// TABLE. Without this, createSchema below would try to build
// idx_messages_session_position over a column that does not
// exist yet, and CREATE TABLE IF NOT EXISTS would not add the
// missing column to the existing table.
const legacySteps = buildStepMigrations(true)
for (let v = 1; v < SCHEMA_VERSION; v++) {
legacySteps[v]?.()
}
// Backfill any *missing* tables (sessions, machines, ...) that
// a partially-built legacy DB may not have yet.
this.createSchema()
this.setUserVersion(SCHEMA_VERSION)
return
}
this.createSchema()
this.setUserVersion(SCHEMA_VERSION)
return
}
const stepMigrations = buildStepMigrations(false)
if (currentVersion < SCHEMA_VERSION && stepMigrations[currentVersion]) {
for (let v = currentVersion; v < SCHEMA_VERSION; v++) {
const step = stepMigrations[v]
if (!step) throw this.buildSchemaMismatchError(currentVersion)
step()
}
this.setUserVersion(SCHEMA_VERSION)
return
}
if (currentVersion !== SCHEMA_VERSION) {
throw this.buildSchemaMismatchError(currentVersion)
}
this.assertRequiredTablesPresent()
}
private createSchema(): void {
this.db.exec(`
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
tag TEXT,
namespace TEXT NOT NULL DEFAULT 'default',
machine_id TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
metadata TEXT,
metadata_version INTEGER DEFAULT 1,
agent_state TEXT,
agent_state_version INTEGER DEFAULT 1,
model TEXT,
model_reasoning_effort TEXT,
effort TEXT,
todos TEXT,
todos_updated_at INTEGER,
team_state TEXT,
team_state_updated_at INTEGER,
active INTEGER DEFAULT 0,
active_at INTEGER,
seq INTEGER DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_sessions_tag ON sessions(tag);
CREATE INDEX IF NOT EXISTS idx_sessions_tag_namespace ON sessions(tag, namespace);
CREATE TABLE IF NOT EXISTS machines (
id TEXT PRIMARY KEY,
namespace TEXT NOT NULL DEFAULT 'default',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
metadata TEXT,
metadata_version INTEGER DEFAULT 1,
runner_state TEXT,
runner_state_version INTEGER DEFAULT 1,
active INTEGER DEFAULT 0,
active_at INTEGER,
seq INTEGER DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_machines_namespace ON machines(namespace);
CREATE TABLE IF NOT EXISTS messages (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
content TEXT NOT NULL,
created_at INTEGER NOT NULL,
seq INTEGER NOT NULL,
local_id TEXT,
invoked_at INTEGER,
scheduled_at INTEGER,
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id, seq);
CREATE UNIQUE INDEX IF NOT EXISTS idx_messages_local_id ON messages(session_id, local_id) WHERE local_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_messages_session_position
ON messages(session_id, COALESCE(invoked_at, created_at) DESC, seq DESC);
CREATE INDEX IF NOT EXISTS idx_messages_scheduled_pending
ON messages(scheduled_at)
WHERE scheduled_at IS NOT NULL AND invoked_at IS NULL;
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT NOT NULL,
platform_user_id TEXT NOT NULL,
namespace TEXT NOT NULL DEFAULT 'default',
created_at INTEGER NOT NULL,
UNIQUE(platform, platform_user_id)
);
CREATE INDEX IF NOT EXISTS idx_users_platform ON users(platform);
CREATE INDEX IF NOT EXISTS idx_users_platform_namespace ON users(platform, namespace);
CREATE TABLE IF NOT EXISTS push_subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
namespace TEXT NOT NULL,
endpoint TEXT NOT NULL,
p256dh TEXT NOT NULL,
auth TEXT NOT NULL,
created_at INTEGER NOT NULL,
UNIQUE(namespace, endpoint)
);
CREATE INDEX IF NOT EXISTS idx_push_subscriptions_namespace ON push_subscriptions(namespace);
CREATE TABLE IF NOT EXISTS session_scratchlist (
session_id TEXT NOT NULL,
entry_id TEXT NOT NULL,
text TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
PRIMARY KEY (session_id, entry_id),
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_session_scratchlist_session_created
ON session_scratchlist(session_id, created_at DESC);
`)
}
private migrateLegacySchemaIfNeeded(): void {
const columns = this.getMachineColumnNames()
if (columns.size === 0) {
return
}
const hasDaemon = columns.has('daemon_state') || columns.has('daemon_state_version')
const hasRunner = columns.has('runner_state') || columns.has('runner_state_version')
if (hasDaemon && hasRunner) {
throw new Error('SQLite schema has both daemon_state and runner_state columns in machines; manual cleanup required.')
}
if (hasDaemon && !hasRunner) {
this.migrateFromV1ToV2()
}
}
private migrateFromV1ToV2(legacy: boolean = false): void {
const columns = this.getMachineColumnNames()
if (columns.size === 0) {
// In the legacy branch the table may not exist yet — createSchema
// will build the up-to-date one. When invoked from the regular
// upgrade path (user_version >= 1), missing the machines table is
// still an error.
if (legacy) return
throw new Error('SQLite schema missing machines table for v1 to v2 migration.')
}
const hasDaemon = columns.has('daemon_state') && columns.has('daemon_state_version')
const hasRunner = columns.has('runner_state') && columns.has('runner_state_version')
if (hasRunner && !hasDaemon) {
return
}
if (!hasDaemon) {
if (legacy) return
throw new Error('SQLite schema missing daemon_state columns for v1 to v2 migration.')
}
try {
this.db.exec('BEGIN')
this.db.exec('ALTER TABLE machines RENAME COLUMN daemon_state TO runner_state')
this.db.exec('ALTER TABLE machines RENAME COLUMN daemon_state_version TO runner_state_version')
this.db.exec('COMMIT')
return
} catch (error) {
this.db.exec('ROLLBACK')
}
try {
this.db.exec('BEGIN')
this.db.exec(`
CREATE TABLE machines_new (
id TEXT PRIMARY KEY,
namespace TEXT NOT NULL DEFAULT 'default',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
metadata TEXT,
metadata_version INTEGER DEFAULT 1,
runner_state TEXT,
runner_state_version INTEGER DEFAULT 1,
active INTEGER DEFAULT 0,
active_at INTEGER,
seq INTEGER DEFAULT 0
);
`)
this.db.exec(`
INSERT INTO machines_new (
id, namespace, created_at, updated_at,
metadata, metadata_version,
runner_state, runner_state_version,
active, active_at, seq
)
SELECT id, namespace, created_at, updated_at,
metadata, metadata_version,
daemon_state, daemon_state_version,
active, active_at, seq
FROM machines;
`)
this.db.exec('DROP TABLE machines')
this.db.exec('ALTER TABLE machines_new RENAME TO machines')
this.db.exec('CREATE INDEX IF NOT EXISTS idx_machines_namespace ON machines(namespace)')
this.db.exec('COMMIT')
} catch (error) {
this.db.exec('ROLLBACK')
const message = error instanceof Error ? error.message : String(error)
throw new Error(`SQLite schema migration v1->v2 failed: ${message}`)
}
}
private migrateFromV2ToV3(): void {
return
}
private migrateFromV3ToV4(): void {
const columns = this.getSessionColumnNames()
// When the legacy branch invokes the full step ladder, an upstream-only
// DB may not have the sessions table yet — createSchema runs after the
// ladder. Skip ALTERs in that case; createSchema will build the table
// with the up-to-date columns.
if (columns.size === 0) return
if (!columns.has('team_state')) {
this.db.exec('ALTER TABLE sessions ADD COLUMN team_state TEXT')
}
if (!columns.has('team_state_updated_at')) {
this.db.exec('ALTER TABLE sessions ADD COLUMN team_state_updated_at INTEGER')
}
}
private migrateFromV4ToV5(): void {
const columns = this.getSessionColumnNames()
if (columns.size === 0) return
if (!columns.has('model')) {
this.db.exec('ALTER TABLE sessions ADD COLUMN model TEXT')
}
}
private migrateFromV5ToV6(): void {
const columns = this.getSessionColumnNames()
if (columns.size === 0) return
if (!columns.has('effort')) {
this.db.exec('ALTER TABLE sessions ADD COLUMN effort TEXT')
}
}
private migrateFromV6ToV7(): void {
const columns = this.getSessionColumnNames()
if (columns.size === 0) return
if (!columns.has('model_reasoning_effort')) {
this.db.exec('ALTER TABLE sessions ADD COLUMN model_reasoning_effort TEXT')
}
}
private migrateFromV7ToV8(): void {
const columns = this.getMessageColumnNames()
if (columns.size === 0) {
// No messages table yet — createSchema will build the up-to-date one.
return
}
if (!columns.has('invoked_at')) {
this.db.exec('ALTER TABLE messages ADD COLUMN invoked_at INTEGER')
}
// Idempotent (WHERE invoked_at IS NULL); safe to re-run if a previous attempt
// crashed between ALTER and UPDATE before user_version was bumped.
this.db.exec('UPDATE messages SET invoked_at = created_at WHERE invoked_at IS NULL')
// Position index for byPosition pagination — idempotent via IF NOT EXISTS.
this.db.exec(`
CREATE INDEX IF NOT EXISTS idx_messages_session_position
ON messages(session_id, COALESCE(invoked_at, created_at) DESC, seq DESC)
`)
}
private migrateFromV8ToV9(): void {
const columns = this.getMessageColumnNames()
if (columns.size === 0) {
// No messages table yet — createSchema will build the up-to-date one.
return
}
if (!columns.has('scheduled_at')) {
this.db.exec('ALTER TABLE messages ADD COLUMN scheduled_at INTEGER')
}
// Partial index for efficient mature scheduled message lookup.
// Idempotent via IF NOT EXISTS.
this.db.exec(`
CREATE INDEX IF NOT EXISTS idx_messages_scheduled_pending
ON messages(scheduled_at)
WHERE scheduled_at IS NOT NULL AND invoked_at IS NULL
`)
}
/**
* tiann/hapi#893 (scratchlist v2): introduce the per-session
* `session_scratchlist` typed table. Operator-decided schema choice
* over an opaque metadata blob - the eventual overseer-context use
* case wants `(sessionId, createdAt)` queryability without parsing
* JSON.
*
* Idempotent via `CREATE TABLE IF NOT EXISTS` + `CREATE INDEX IF NOT
* EXISTS`. Cascade-delete from `sessions(id)` handles delete-session
* cleanup. No data backfill: pre-v10 hubs never had this data; the
* web client's first-run migration (`hapi.scratchlist.v2.migrated.*`
* flag) pushes any existing `localStorage` entries up via the REST
* endpoint.
*
* Rollback: `DROP TABLE session_scratchlist; PRAGMA user_version = 9;`
* - the table is independent, so the drop is safe and loses only the
* v2 hub-side entries (web client retains its localStorage offline
* cache).
*/
private migrateFromV9ToV10(): void {
this.db.exec(`
CREATE TABLE IF NOT EXISTS session_scratchlist (
session_id TEXT NOT NULL,
entry_id TEXT NOT NULL,
text TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
PRIMARY KEY (session_id, entry_id),
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_session_scratchlist_session_created
ON session_scratchlist(session_id, created_at DESC);
`)
}
private getSessionColumnNames(): Set<string> {
const rows = this.db.prepare('PRAGMA table_info(sessions)').all() as Array<{ name: string }>
return new Set(rows.map((row) => row.name))
}
private getMachineColumnNames(): Set<string> {
const rows = this.db.prepare('PRAGMA table_info(machines)').all() as Array<{ name: string }>
return new Set(rows.map((row) => row.name))
}
private getMessageColumnNames(): Set<string> {
const rows = this.db.prepare('PRAGMA table_info(messages)').all() as Array<{ name: string }>
return new Set(rows.map((row) => row.name))
}
private getUserVersion(): number {
const row = this.db.prepare('PRAGMA user_version').get() as { user_version: number } | undefined
return row?.user_version ?? 0
}
private setUserVersion(version: number): void {
this.db.exec(`PRAGMA user_version = ${version}`)
}
private hasAnyUserTables(): boolean {
const row = this.db.prepare(
"SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' LIMIT 1"
).get() as { name?: string } | undefined
return Boolean(row?.name)
}
private assertRequiredTablesPresent(): void {
const placeholders = REQUIRED_TABLES.map(() => '?').join(', ')
const rows = this.db.prepare(
`SELECT name FROM sqlite_master WHERE type = 'table' AND name IN (${placeholders})`
).all(...REQUIRED_TABLES) as Array<{ name: string }>
const existing = new Set(rows.map((row) => row.name))
const missing = REQUIRED_TABLES.filter((table) => !existing.has(table))
if (missing.length > 0) {
throw new Error(
`SQLite schema is missing required tables (${missing.join(', ')}). ` +
'Back up and rebuild the database, or run an offline migration to the expected schema version.'
)
}
}
private buildSchemaMismatchError(currentVersion: number): Error {
const location = (this._dbPath === ':memory:' || this._dbPath.startsWith('file::memory:'))
? 'in-memory database'
: this._dbPath
return new Error(
`SQLite schema version mismatch for ${location}. ` +
`Expected ${SCHEMA_VERSION}, found ${currentVersion}. ` +
'This build does not run compatibility migrations. ' +
'Back up and rebuild the database, or run an offline migration to the expected schema version.'
)
}
}