agent-relay 2.0.37 → 2.1.0
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.
- package/dist/index.cjs +31903 -33913
- package/dist/src/cli/index.js +36 -51
- package/dist/src/cli/index.js.map +1 -1
- package/package.json +18 -19
- package/packages/api-types/package.json +1 -1
- package/packages/benchmark/package.json +4 -4
- package/packages/bridge/package.json +8 -8
- package/packages/cli-tester/package.json +1 -1
- package/packages/config/dist/project-namespace.d.ts +28 -0
- package/packages/config/dist/project-namespace.d.ts.map +1 -1
- package/packages/config/dist/project-namespace.js +42 -0
- package/packages/config/dist/project-namespace.js.map +1 -1
- package/packages/config/package.json +2 -2
- package/packages/config/src/project-namespace.ts +65 -0
- package/packages/continuity/dist/formatter.d.ts +8 -2
- package/packages/continuity/dist/formatter.d.ts.map +1 -1
- package/packages/continuity/dist/formatter.js +142 -7
- package/packages/continuity/dist/formatter.js.map +1 -1
- package/packages/continuity/dist/index.d.ts +1 -0
- package/packages/continuity/dist/index.d.ts.map +1 -1
- package/packages/continuity/dist/index.js +2 -0
- package/packages/continuity/dist/index.js.map +1 -1
- package/packages/continuity/package.json +4 -1
- package/packages/continuity/src/formatter.ts +175 -10
- package/packages/continuity/src/index.ts +3 -0
- package/packages/daemon/dist/enhanced-features.d.ts +2 -3
- package/packages/daemon/dist/enhanced-features.d.ts.map +1 -1
- package/packages/daemon/dist/enhanced-features.js +1 -0
- package/packages/daemon/dist/enhanced-features.js.map +1 -1
- package/packages/daemon/dist/index.d.ts +0 -2
- package/packages/daemon/dist/index.d.ts.map +1 -1
- package/packages/daemon/dist/index.js +0 -3
- package/packages/daemon/dist/index.js.map +1 -1
- package/packages/daemon/dist/server.d.ts +0 -6
- package/packages/daemon/dist/server.d.ts.map +1 -1
- package/packages/daemon/dist/server.js +20 -119
- package/packages/daemon/dist/server.js.map +1 -1
- package/packages/daemon/package.json +12 -14
- package/packages/daemon/src/enhanced-features.ts +4 -4
- package/packages/daemon/src/index.ts +0 -4
- package/packages/daemon/src/server.ts +19 -127
- package/packages/daemon/vitest.config.ts +9 -0
- package/packages/hooks/package.json +4 -4
- package/packages/mcp/package.json +3 -3
- package/packages/memory/package.json +2 -2
- package/packages/policy/package.json +2 -2
- package/packages/protocol/package.json +1 -1
- package/packages/resiliency/package.json +1 -1
- package/packages/sdk/package.json +2 -2
- package/packages/spawner/package.json +1 -1
- package/packages/state/package.json +1 -1
- package/packages/storage/dist/adapter.d.ts +5 -5
- package/packages/storage/dist/adapter.js +9 -9
- package/packages/storage/dist/adapter.js.map +1 -1
- package/packages/storage/package.json +2 -2
- package/packages/storage/src/adapter.ts +9 -9
- package/packages/telemetry/package.json +1 -1
- package/packages/trajectory/package.json +2 -2
- package/packages/user-directory/package.json +2 -2
- package/packages/utils/package.json +2 -2
- package/packages/wrapper/package.json +6 -6
- package/scripts/build-cjs.mjs +2 -0
- package/packages/daemon/dist/migrations/index.d.ts +0 -73
- package/packages/daemon/dist/migrations/index.d.ts.map +0 -1
- package/packages/daemon/dist/migrations/index.js +0 -241
- package/packages/daemon/dist/migrations/index.js.map +0 -1
- package/packages/daemon/dist/relay-ledger.d.ts +0 -263
- package/packages/daemon/dist/relay-ledger.d.ts.map +0 -1
- package/packages/daemon/dist/relay-ledger.js +0 -538
- package/packages/daemon/dist/relay-ledger.js.map +0 -1
- package/packages/daemon/dist/relay-watchdog.d.ts +0 -125
- package/packages/daemon/dist/relay-watchdog.d.ts.map +0 -1
- package/packages/daemon/dist/relay-watchdog.js +0 -611
- package/packages/daemon/dist/relay-watchdog.js.map +0 -1
- package/packages/daemon/src/migrations/0001_initial.sql +0 -72
- package/packages/daemon/src/migrations/index.test.ts +0 -195
- package/packages/daemon/src/migrations/index.ts +0 -286
- package/packages/daemon/src/relay-ledger.test.ts +0 -358
- package/packages/daemon/src/relay-ledger.ts +0 -713
- package/packages/daemon/src/relay-watchdog.test.ts +0 -881
- package/packages/daemon/src/relay-watchdog.ts +0 -785
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
-- Relay Ledger - Initial Schema
|
|
2
|
-
-- Migration 0001: Initial tables for relay file tracking
|
|
3
|
-
--
|
|
4
|
-
-- Tables:
|
|
5
|
-
-- - relay_files: Tracks relay message files through lifecycle
|
|
6
|
-
-- - agents: Registry of known agents
|
|
7
|
-
-- - orchestrator_state: Key-value store for orchestrator persistence
|
|
8
|
-
-- - pending_operations: Crash recovery for incomplete operations
|
|
9
|
-
|
|
10
|
-
-- Main relay files table
|
|
11
|
-
CREATE TABLE IF NOT EXISTS relay_files (
|
|
12
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
13
|
-
file_id TEXT NOT NULL UNIQUE,
|
|
14
|
-
source_path TEXT NOT NULL,
|
|
15
|
-
archive_path TEXT,
|
|
16
|
-
agent_name TEXT NOT NULL,
|
|
17
|
-
message_type TEXT NOT NULL,
|
|
18
|
-
status TEXT NOT NULL DEFAULT 'pending',
|
|
19
|
-
retries INTEGER NOT NULL DEFAULT 0,
|
|
20
|
-
max_retries INTEGER NOT NULL DEFAULT 3,
|
|
21
|
-
discovered_at INTEGER NOT NULL,
|
|
22
|
-
processed_at INTEGER,
|
|
23
|
-
archived_at INTEGER,
|
|
24
|
-
error TEXT,
|
|
25
|
-
content_hash TEXT,
|
|
26
|
-
file_size INTEGER NOT NULL DEFAULT 0,
|
|
27
|
-
file_mtime_ns INTEGER,
|
|
28
|
-
file_inode INTEGER,
|
|
29
|
-
CONSTRAINT valid_status CHECK (status IN ('pending', 'processing', 'delivered', 'failed', 'archived'))
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_status ON relay_files(status);
|
|
33
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_agent ON relay_files(agent_name);
|
|
34
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_discovered ON relay_files(discovered_at);
|
|
35
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_source ON relay_files(source_path);
|
|
36
|
-
|
|
37
|
-
-- Agents registry table
|
|
38
|
-
CREATE TABLE IF NOT EXISTS agents (
|
|
39
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
40
|
-
agent_name TEXT NOT NULL UNIQUE,
|
|
41
|
-
created_at INTEGER NOT NULL,
|
|
42
|
-
last_seen_at INTEGER NOT NULL,
|
|
43
|
-
status TEXT NOT NULL DEFAULT 'active',
|
|
44
|
-
metadata TEXT,
|
|
45
|
-
CONSTRAINT valid_agent_status CHECK (status IN ('active', 'inactive'))
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
CREATE INDEX IF NOT EXISTS idx_agents_status ON agents(status);
|
|
49
|
-
CREATE INDEX IF NOT EXISTS idx_agents_last_seen ON agents(last_seen_at);
|
|
50
|
-
|
|
51
|
-
-- Orchestrator state table (key-value store for crash recovery)
|
|
52
|
-
CREATE TABLE IF NOT EXISTS orchestrator_state (
|
|
53
|
-
key TEXT PRIMARY KEY,
|
|
54
|
-
value TEXT NOT NULL,
|
|
55
|
-
updated_at INTEGER NOT NULL
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
-- Pending operations table (crash recovery atomicity)
|
|
59
|
-
CREATE TABLE IF NOT EXISTS pending_operations (
|
|
60
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
61
|
-
operation_type TEXT NOT NULL,
|
|
62
|
-
target_id TEXT NOT NULL,
|
|
63
|
-
payload TEXT,
|
|
64
|
-
created_at INTEGER NOT NULL,
|
|
65
|
-
attempts INTEGER NOT NULL DEFAULT 0,
|
|
66
|
-
last_attempt_at INTEGER,
|
|
67
|
-
error TEXT,
|
|
68
|
-
CONSTRAINT valid_operation_type CHECK (operation_type IN ('process', 'archive', 'cleanup'))
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
CREATE INDEX IF NOT EXISTS idx_pending_ops_type ON pending_operations(operation_type);
|
|
72
|
-
CREATE INDEX IF NOT EXISTS idx_pending_ops_target ON pending_operations(target_id);
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
import Database from 'better-sqlite3';
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
import os from 'node:os';
|
|
6
|
-
import {
|
|
7
|
-
ensureMigrationsTable,
|
|
8
|
-
getAppliedMigrations,
|
|
9
|
-
isMigrationApplied,
|
|
10
|
-
recordMigration,
|
|
11
|
-
calculateChecksum,
|
|
12
|
-
runMigrations,
|
|
13
|
-
getPendingMigrations,
|
|
14
|
-
verifyMigrations,
|
|
15
|
-
loadMigrationFiles,
|
|
16
|
-
} from './index.js';
|
|
17
|
-
|
|
18
|
-
describe('Migration Runner', () => {
|
|
19
|
-
let testDir: string;
|
|
20
|
-
let db: Database.Database;
|
|
21
|
-
|
|
22
|
-
beforeEach(async () => {
|
|
23
|
-
// Create temp test directory
|
|
24
|
-
testDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'relay-migrations-test-'));
|
|
25
|
-
db = new Database(path.join(testDir, 'test.sqlite'));
|
|
26
|
-
db.pragma('journal_mode = WAL');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
afterEach(() => {
|
|
30
|
-
db.close();
|
|
31
|
-
try {
|
|
32
|
-
fs.rmSync(testDir, { recursive: true, force: true });
|
|
33
|
-
} catch {
|
|
34
|
-
// Ignore cleanup errors
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe('ensureMigrationsTable', () => {
|
|
39
|
-
it('should create __migrations table if not exists', () => {
|
|
40
|
-
ensureMigrationsTable(db);
|
|
41
|
-
|
|
42
|
-
const tables = db.prepare(`
|
|
43
|
-
SELECT name FROM sqlite_master WHERE type='table' AND name='__migrations'
|
|
44
|
-
`).all();
|
|
45
|
-
|
|
46
|
-
expect(tables).toHaveLength(1);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('should be idempotent', () => {
|
|
50
|
-
ensureMigrationsTable(db);
|
|
51
|
-
ensureMigrationsTable(db);
|
|
52
|
-
|
|
53
|
-
const tables = db.prepare(`
|
|
54
|
-
SELECT name FROM sqlite_master WHERE type='table' AND name='__migrations'
|
|
55
|
-
`).all();
|
|
56
|
-
|
|
57
|
-
expect(tables).toHaveLength(1);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describe('calculateChecksum', () => {
|
|
62
|
-
it('should return consistent checksums for same input', () => {
|
|
63
|
-
const sql = 'CREATE TABLE test (id INTEGER);';
|
|
64
|
-
const hash1 = calculateChecksum(sql);
|
|
65
|
-
const hash2 = calculateChecksum(sql);
|
|
66
|
-
|
|
67
|
-
expect(hash1).toBe(hash2);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('should return different checksums for different input', () => {
|
|
71
|
-
const hash1 = calculateChecksum('CREATE TABLE test1 (id INTEGER);');
|
|
72
|
-
const hash2 = calculateChecksum('CREATE TABLE test2 (id INTEGER);');
|
|
73
|
-
|
|
74
|
-
expect(hash1).not.toBe(hash2);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should return 8-character hex string', () => {
|
|
78
|
-
const hash = calculateChecksum('some sql');
|
|
79
|
-
expect(hash).toMatch(/^[0-9a-f]{8}$/);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
describe('recordMigration / getAppliedMigrations', () => {
|
|
84
|
-
it('should record and retrieve migrations', () => {
|
|
85
|
-
ensureMigrationsTable(db);
|
|
86
|
-
|
|
87
|
-
recordMigration(db, '0001_initial', 'abc12345');
|
|
88
|
-
recordMigration(db, '0002_add_column', 'def67890');
|
|
89
|
-
|
|
90
|
-
const applied = getAppliedMigrations(db);
|
|
91
|
-
|
|
92
|
-
expect(applied).toHaveLength(2);
|
|
93
|
-
expect(applied[0].name).toBe('0001_initial');
|
|
94
|
-
expect(applied[0].checksum).toBe('abc12345');
|
|
95
|
-
expect(applied[1].name).toBe('0002_add_column');
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe('isMigrationApplied', () => {
|
|
100
|
-
it('should return true for applied migrations', () => {
|
|
101
|
-
ensureMigrationsTable(db);
|
|
102
|
-
recordMigration(db, '0001_initial', 'abc12345');
|
|
103
|
-
|
|
104
|
-
expect(isMigrationApplied(db, '0001_initial')).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should return false for unapplied migrations', () => {
|
|
108
|
-
ensureMigrationsTable(db);
|
|
109
|
-
|
|
110
|
-
expect(isMigrationApplied(db, '0001_initial')).toBe(false);
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
describe('loadMigrationFiles', () => {
|
|
115
|
-
it('should load embedded migrations', () => {
|
|
116
|
-
const migrations = loadMigrationFiles();
|
|
117
|
-
|
|
118
|
-
expect(migrations.length).toBeGreaterThanOrEqual(1);
|
|
119
|
-
expect(migrations[0].name).toBe('0001_initial');
|
|
120
|
-
expect(migrations[0].sql).toContain('CREATE TABLE');
|
|
121
|
-
expect(migrations[0].checksum).toMatch(/^[0-9a-f]{8}$/);
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
describe('runMigrations', () => {
|
|
126
|
-
it('should run initial migration successfully', () => {
|
|
127
|
-
const result = runMigrations(db);
|
|
128
|
-
|
|
129
|
-
// Should have applied the initial migration
|
|
130
|
-
expect(result.applied.length).toBeGreaterThanOrEqual(1);
|
|
131
|
-
expect(result.errors).toHaveLength(0);
|
|
132
|
-
|
|
133
|
-
// Tables should exist
|
|
134
|
-
const tables = db.prepare(`
|
|
135
|
-
SELECT name FROM sqlite_master WHERE type='table' AND name IN ('relay_files', 'agents', '__migrations')
|
|
136
|
-
`).all() as Array<{ name: string }>;
|
|
137
|
-
|
|
138
|
-
expect(tables.map(t => t.name)).toContain('relay_files');
|
|
139
|
-
expect(tables.map(t => t.name)).toContain('agents');
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('should skip already applied migrations', () => {
|
|
143
|
-
// Run migrations first time
|
|
144
|
-
const result1 = runMigrations(db);
|
|
145
|
-
const appliedCount = result1.applied.length;
|
|
146
|
-
|
|
147
|
-
// Run again
|
|
148
|
-
const result2 = runMigrations(db);
|
|
149
|
-
|
|
150
|
-
// Should skip all previously applied
|
|
151
|
-
expect(result2.skipped.length).toBe(appliedCount);
|
|
152
|
-
expect(result2.applied).toHaveLength(0);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('should be idempotent', () => {
|
|
156
|
-
runMigrations(db);
|
|
157
|
-
runMigrations(db);
|
|
158
|
-
runMigrations(db);
|
|
159
|
-
|
|
160
|
-
// Should still have valid schema
|
|
161
|
-
const result = db.prepare(`SELECT COUNT(*) as count FROM relay_files`).get() as { count: number };
|
|
162
|
-
expect(result.count).toBe(0);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
describe('getPendingMigrations', () => {
|
|
167
|
-
it('should return all migrations for fresh database', () => {
|
|
168
|
-
// Don't run migrations, just check pending
|
|
169
|
-
ensureMigrationsTable(db);
|
|
170
|
-
const pending = getPendingMigrations(db);
|
|
171
|
-
|
|
172
|
-
expect(pending.length).toBeGreaterThanOrEqual(1);
|
|
173
|
-
expect(pending[0].name).toBe('0001_initial');
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('should return empty array after all migrations applied', () => {
|
|
177
|
-
runMigrations(db);
|
|
178
|
-
const pending = getPendingMigrations(db);
|
|
179
|
-
|
|
180
|
-
expect(pending).toHaveLength(0);
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
describe('verifyMigrations', () => {
|
|
185
|
-
it('should return empty array when checksums match', () => {
|
|
186
|
-
runMigrations(db);
|
|
187
|
-
const mismatches = verifyMigrations(db);
|
|
188
|
-
|
|
189
|
-
expect(mismatches).toHaveLength(0);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
// Note: Testing checksum mismatch would require modifying embedded migrations
|
|
193
|
-
// which is not practical in unit tests
|
|
194
|
-
});
|
|
195
|
-
});
|
|
@@ -1,286 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SQLite Migration Runner for Relay Ledger
|
|
3
|
-
*
|
|
4
|
-
* Provides a lightweight migration system for the relay-ledger.db SQLite database.
|
|
5
|
-
* Tracks applied migrations in a __migrations table and runs them in order.
|
|
6
|
-
*
|
|
7
|
-
* Features:
|
|
8
|
-
* - Sequential migration execution by name
|
|
9
|
-
* - Idempotent (safe to run multiple times)
|
|
10
|
-
* - Tracks applied migrations with timestamps
|
|
11
|
-
* - Embedded SQL for portability (no file dependencies)
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type Database from 'better-sqlite3';
|
|
15
|
-
|
|
16
|
-
// ============================================================================
|
|
17
|
-
// Types
|
|
18
|
-
// ============================================================================
|
|
19
|
-
|
|
20
|
-
export interface MigrationRecord {
|
|
21
|
-
id: number;
|
|
22
|
-
name: string;
|
|
23
|
-
appliedAt: number;
|
|
24
|
-
checksum: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface MigrationFile {
|
|
28
|
-
name: string;
|
|
29
|
-
sql: string;
|
|
30
|
-
checksum: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface MigrationResult {
|
|
34
|
-
applied: string[];
|
|
35
|
-
skipped: string[];
|
|
36
|
-
errors: Array<{ name: string; error: string }>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// ============================================================================
|
|
40
|
-
// Embedded Migrations
|
|
41
|
-
// ============================================================================
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Embedded migrations - SQL is stored directly in code for portability.
|
|
45
|
-
* Add new migrations to this array in order.
|
|
46
|
-
*/
|
|
47
|
-
const EMBEDDED_MIGRATIONS: Array<{ name: string; sql: string }> = [
|
|
48
|
-
{
|
|
49
|
-
name: '0001_initial',
|
|
50
|
-
sql: `
|
|
51
|
-
-- Relay Ledger - Initial Schema
|
|
52
|
-
-- Migration 0001: Initial tables for relay file tracking
|
|
53
|
-
|
|
54
|
-
-- Main relay files table
|
|
55
|
-
CREATE TABLE IF NOT EXISTS relay_files (
|
|
56
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
57
|
-
file_id TEXT NOT NULL UNIQUE,
|
|
58
|
-
source_path TEXT NOT NULL,
|
|
59
|
-
archive_path TEXT,
|
|
60
|
-
agent_name TEXT NOT NULL,
|
|
61
|
-
message_type TEXT NOT NULL,
|
|
62
|
-
status TEXT NOT NULL DEFAULT 'pending',
|
|
63
|
-
retries INTEGER NOT NULL DEFAULT 0,
|
|
64
|
-
max_retries INTEGER NOT NULL DEFAULT 3,
|
|
65
|
-
discovered_at INTEGER NOT NULL,
|
|
66
|
-
processed_at INTEGER,
|
|
67
|
-
archived_at INTEGER,
|
|
68
|
-
error TEXT,
|
|
69
|
-
content_hash TEXT,
|
|
70
|
-
file_size INTEGER NOT NULL DEFAULT 0,
|
|
71
|
-
file_mtime_ns INTEGER,
|
|
72
|
-
file_inode INTEGER,
|
|
73
|
-
CONSTRAINT valid_status CHECK (status IN ('pending', 'processing', 'delivered', 'failed', 'archived'))
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_status ON relay_files(status);
|
|
77
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_agent ON relay_files(agent_name);
|
|
78
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_discovered ON relay_files(discovered_at);
|
|
79
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_source ON relay_files(source_path);
|
|
80
|
-
|
|
81
|
-
-- Agents registry table
|
|
82
|
-
CREATE TABLE IF NOT EXISTS agents (
|
|
83
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
84
|
-
agent_name TEXT NOT NULL UNIQUE,
|
|
85
|
-
created_at INTEGER NOT NULL,
|
|
86
|
-
last_seen_at INTEGER NOT NULL,
|
|
87
|
-
status TEXT NOT NULL DEFAULT 'active',
|
|
88
|
-
metadata TEXT,
|
|
89
|
-
CONSTRAINT valid_agent_status CHECK (status IN ('active', 'inactive'))
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
CREATE INDEX IF NOT EXISTS idx_agents_status ON agents(status);
|
|
93
|
-
CREATE INDEX IF NOT EXISTS idx_agents_last_seen ON agents(last_seen_at);
|
|
94
|
-
|
|
95
|
-
-- Orchestrator state table (key-value store for crash recovery)
|
|
96
|
-
CREATE TABLE IF NOT EXISTS orchestrator_state (
|
|
97
|
-
key TEXT PRIMARY KEY,
|
|
98
|
-
value TEXT NOT NULL,
|
|
99
|
-
updated_at INTEGER NOT NULL
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
-- Pending operations table (crash recovery atomicity)
|
|
103
|
-
CREATE TABLE IF NOT EXISTS pending_operations (
|
|
104
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
105
|
-
operation_type TEXT NOT NULL,
|
|
106
|
-
target_id TEXT NOT NULL,
|
|
107
|
-
payload TEXT,
|
|
108
|
-
created_at INTEGER NOT NULL,
|
|
109
|
-
attempts INTEGER NOT NULL DEFAULT 0,
|
|
110
|
-
last_attempt_at INTEGER,
|
|
111
|
-
error TEXT,
|
|
112
|
-
CONSTRAINT valid_operation_type CHECK (operation_type IN ('process', 'archive', 'cleanup'))
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
CREATE INDEX IF NOT EXISTS idx_pending_ops_type ON pending_operations(operation_type);
|
|
116
|
-
CREATE INDEX IF NOT EXISTS idx_pending_ops_target ON pending_operations(target_id);
|
|
117
|
-
`,
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
name: '0002_symlink_paths',
|
|
121
|
-
sql: `
|
|
122
|
-
-- Migration 0002: Add symlink path tracking for production workspace support
|
|
123
|
-
-- Workspaces in cloud/production may be symlinked for isolation.
|
|
124
|
-
-- We store both the original symlink path (for debugging) and canonical path (for operations).
|
|
125
|
-
|
|
126
|
-
-- Add symlink_path column to track original path (may be symlink)
|
|
127
|
-
ALTER TABLE relay_files ADD COLUMN symlink_path TEXT;
|
|
128
|
-
|
|
129
|
-
-- Create index for symlink lookups
|
|
130
|
-
CREATE INDEX IF NOT EXISTS idx_relay_files_symlink ON relay_files(symlink_path);
|
|
131
|
-
`,
|
|
132
|
-
},
|
|
133
|
-
];
|
|
134
|
-
|
|
135
|
-
// ============================================================================
|
|
136
|
-
// Migration Runner
|
|
137
|
-
// ============================================================================
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Create the migrations tracking table if it doesn't exist
|
|
141
|
-
*/
|
|
142
|
-
export function ensureMigrationsTable(db: Database.Database): void {
|
|
143
|
-
db.exec(`
|
|
144
|
-
CREATE TABLE IF NOT EXISTS __migrations (
|
|
145
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
146
|
-
name TEXT NOT NULL UNIQUE,
|
|
147
|
-
applied_at INTEGER NOT NULL,
|
|
148
|
-
checksum TEXT NOT NULL
|
|
149
|
-
);
|
|
150
|
-
`);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Get all applied migrations
|
|
155
|
-
*/
|
|
156
|
-
export function getAppliedMigrations(db: Database.Database): MigrationRecord[] {
|
|
157
|
-
const rows = db.prepare(`
|
|
158
|
-
SELECT id, name, applied_at, checksum FROM __migrations ORDER BY id ASC
|
|
159
|
-
`).all() as Array<{ id: number; name: string; applied_at: number; checksum: string }>;
|
|
160
|
-
|
|
161
|
-
return rows.map(row => ({
|
|
162
|
-
id: row.id,
|
|
163
|
-
name: row.name,
|
|
164
|
-
appliedAt: row.applied_at,
|
|
165
|
-
checksum: row.checksum,
|
|
166
|
-
}));
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Check if a specific migration has been applied
|
|
171
|
-
*/
|
|
172
|
-
export function isMigrationApplied(db: Database.Database, name: string): boolean {
|
|
173
|
-
const row = db.prepare(`SELECT 1 FROM __migrations WHERE name = ?`).get(name);
|
|
174
|
-
return row !== undefined;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Record a migration as applied
|
|
179
|
-
*/
|
|
180
|
-
export function recordMigration(db: Database.Database, name: string, checksum: string): void {
|
|
181
|
-
db.prepare(`
|
|
182
|
-
INSERT INTO __migrations (name, applied_at, checksum) VALUES (?, ?, ?)
|
|
183
|
-
`).run(name, Date.now(), checksum);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Calculate a simple checksum for migration content
|
|
188
|
-
*/
|
|
189
|
-
export function calculateChecksum(sql: string): string {
|
|
190
|
-
// Simple hash for migration verification
|
|
191
|
-
let hash = 0;
|
|
192
|
-
for (let i = 0; i < sql.length; i++) {
|
|
193
|
-
const char = sql.charCodeAt(i);
|
|
194
|
-
hash = ((hash << 5) - hash) + char;
|
|
195
|
-
hash = hash & hash; // Convert to 32-bit integer
|
|
196
|
-
}
|
|
197
|
-
return Math.abs(hash).toString(16).padStart(8, '0');
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Load migrations from embedded definitions
|
|
202
|
-
*/
|
|
203
|
-
export function loadMigrationFiles(): MigrationFile[] {
|
|
204
|
-
return EMBEDDED_MIGRATIONS.map(m => ({
|
|
205
|
-
name: m.name,
|
|
206
|
-
sql: m.sql,
|
|
207
|
-
checksum: calculateChecksum(m.sql),
|
|
208
|
-
}));
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Run all pending migrations
|
|
213
|
-
*/
|
|
214
|
-
export function runMigrations(db: Database.Database): MigrationResult {
|
|
215
|
-
const result: MigrationResult = {
|
|
216
|
-
applied: [],
|
|
217
|
-
skipped: [],
|
|
218
|
-
errors: [],
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
// Ensure migrations table exists
|
|
222
|
-
ensureMigrationsTable(db);
|
|
223
|
-
|
|
224
|
-
// Load all migrations
|
|
225
|
-
const migrations = loadMigrationFiles();
|
|
226
|
-
|
|
227
|
-
// Get applied migrations
|
|
228
|
-
const applied = new Set(getAppliedMigrations(db).map(m => m.name));
|
|
229
|
-
|
|
230
|
-
// Run pending migrations in order
|
|
231
|
-
for (const migration of migrations) {
|
|
232
|
-
if (applied.has(migration.name)) {
|
|
233
|
-
result.skipped.push(migration.name);
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
try {
|
|
238
|
-
// Run migration in a transaction
|
|
239
|
-
db.transaction(() => {
|
|
240
|
-
db.exec(migration.sql);
|
|
241
|
-
recordMigration(db, migration.name, migration.checksum);
|
|
242
|
-
})();
|
|
243
|
-
|
|
244
|
-
result.applied.push(migration.name);
|
|
245
|
-
} catch (err) {
|
|
246
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
247
|
-
result.errors.push({ name: migration.name, error: errorMessage });
|
|
248
|
-
// Stop on first error
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return result;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Get pending migrations that haven't been applied yet
|
|
258
|
-
*/
|
|
259
|
-
export function getPendingMigrations(db: Database.Database): MigrationFile[] {
|
|
260
|
-
ensureMigrationsTable(db);
|
|
261
|
-
const applied = new Set(getAppliedMigrations(db).map(m => m.name));
|
|
262
|
-
return loadMigrationFiles().filter(m => !applied.has(m.name));
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Verify migration checksums match what was originally applied
|
|
267
|
-
*/
|
|
268
|
-
export function verifyMigrations(db: Database.Database): Array<{ name: string; expected: string; actual: string }> {
|
|
269
|
-
const applied = getAppliedMigrations(db);
|
|
270
|
-
const files = loadMigrationFiles();
|
|
271
|
-
const fileMap = new Map(files.map(f => [f.name, f]));
|
|
272
|
-
const mismatches: Array<{ name: string; expected: string; actual: string }> = [];
|
|
273
|
-
|
|
274
|
-
for (const record of applied) {
|
|
275
|
-
const file = fileMap.get(record.name);
|
|
276
|
-
if (file && file.checksum !== record.checksum) {
|
|
277
|
-
mismatches.push({
|
|
278
|
-
name: record.name,
|
|
279
|
-
expected: record.checksum,
|
|
280
|
-
actual: file.checksum,
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
return mismatches;
|
|
286
|
-
}
|