@a-company/sentinel 3.5.0 → 3.7.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.
@@ -1,5 +1,5 @@
1
- import { S as Sentinel } from '../sdk-BTblv--p.js';
2
- import '../storage-BqCJqZat.js';
1
+ import { S as Sentinel } from '../sdk-CzFDYYST.js';
2
+ import '../storage-BKyt7aPJ.js';
3
3
  import '../types-BmVoO1iF.js';
4
4
 
5
5
  /**
@@ -1,5 +1,5 @@
1
- import { S as Sentinel } from '../sdk-BTblv--p.js';
2
- import '../storage-BqCJqZat.js';
1
+ import { S as Sentinel } from '../sdk-CzFDYYST.js';
2
+ import '../storage-BKyt7aPJ.js';
3
3
  import '../types-BmVoO1iF.js';
4
4
 
5
5
  /**
@@ -1,5 +1,5 @@
1
- import { S as Sentinel } from '../sdk-BTblv--p.js';
2
- import '../storage-BqCJqZat.js';
1
+ import { S as Sentinel } from '../sdk-CzFDYYST.js';
2
+ import '../storage-BKyt7aPJ.js';
3
3
  import '../types-BmVoO1iF.js';
4
4
 
5
5
  /**
@@ -3,7 +3,7 @@ import initSqlJs from "sql.js";
3
3
  import { v4 as uuidv4 } from "uuid";
4
4
  import * as path from "path";
5
5
  import * as fs from "fs";
6
- var SCHEMA_VERSION = 4;
6
+ var SCHEMA_VERSION = 5;
7
7
  var DEFAULT_CONFIDENCE = {
8
8
  score: 50,
9
9
  timesMatched: 0,
@@ -434,6 +434,54 @@ var SentinelStorage = class {
434
434
  this.db.run(
435
435
  "INSERT OR REPLACE INTO metadata (key, value) VALUES ('schema_version', '4')"
436
436
  );
437
+ currentVersion = 4;
438
+ }
439
+ if (currentVersion < 5) {
440
+ try {
441
+ this.db.run(`
442
+ CREATE TABLE IF NOT EXISTS schemas (
443
+ id TEXT PRIMARY KEY,
444
+ version TEXT NOT NULL,
445
+ name TEXT NOT NULL,
446
+ description TEXT,
447
+ scope_json TEXT NOT NULL,
448
+ event_types_json TEXT NOT NULL,
449
+ causality_json TEXT,
450
+ visualization_json TEXT,
451
+ tags_json TEXT DEFAULT '[]',
452
+ registered_at TEXT NOT NULL,
453
+ updated_at TEXT NOT NULL
454
+ );
455
+
456
+ CREATE TABLE IF NOT EXISTS events (
457
+ id TEXT PRIMARY KEY,
458
+ schema_id TEXT NOT NULL,
459
+ event_type TEXT NOT NULL,
460
+ category TEXT NOT NULL,
461
+ timestamp TEXT NOT NULL,
462
+ scope_value TEXT,
463
+ scope_ordinal INTEGER,
464
+ session_id TEXT,
465
+ service TEXT NOT NULL,
466
+ data_json TEXT,
467
+ severity TEXT DEFAULT 'info',
468
+ parent_event_id TEXT,
469
+ depth INTEGER DEFAULT 0
470
+ );
471
+
472
+ CREATE INDEX IF NOT EXISTS idx_events_schema ON events(schema_id);
473
+ CREATE INDEX IF NOT EXISTS idx_events_type ON events(event_type);
474
+ CREATE INDEX IF NOT EXISTS idx_events_scope ON events(schema_id, scope_value);
475
+ CREATE INDEX IF NOT EXISTS idx_events_scope_ord ON events(schema_id, scope_ordinal);
476
+ CREATE INDEX IF NOT EXISTS idx_events_session ON events(session_id);
477
+ CREATE INDEX IF NOT EXISTS idx_events_timestamp ON events(timestamp);
478
+ CREATE INDEX IF NOT EXISTS idx_events_service ON events(service);
479
+ `);
480
+ } catch {
481
+ }
482
+ this.db.run(
483
+ "INSERT OR REPLACE INTO metadata (key, value) VALUES ('schema_version', '5')"
484
+ );
437
485
  }
438
486
  }
439
487
  /**
@@ -2022,6 +2070,341 @@ var SentinelStorage = class {
2022
2070
  logs: obj.log_ids_json ? JSON.parse(obj.log_ids_json) : []
2023
2071
  };
2024
2072
  }
2073
+ // ─── Schema Registry ─────────────────────────────────────────────
2074
+ registerSchema(schema) {
2075
+ this.initializeSync();
2076
+ const now = (/* @__PURE__ */ new Date()).toISOString();
2077
+ this.db.run(
2078
+ `INSERT INTO schemas (
2079
+ id, version, name, description, scope_json, event_types_json,
2080
+ causality_json, visualization_json, tags_json, registered_at, updated_at
2081
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2082
+ ON CONFLICT(id) DO UPDATE SET
2083
+ version = excluded.version,
2084
+ name = excluded.name,
2085
+ description = excluded.description,
2086
+ scope_json = excluded.scope_json,
2087
+ event_types_json = excluded.event_types_json,
2088
+ causality_json = excluded.causality_json,
2089
+ visualization_json = excluded.visualization_json,
2090
+ tags_json = excluded.tags_json,
2091
+ updated_at = excluded.updated_at`,
2092
+ [
2093
+ schema.id,
2094
+ schema.version,
2095
+ schema.name,
2096
+ schema.description || null,
2097
+ JSON.stringify(schema.scope),
2098
+ JSON.stringify(schema.eventTypes),
2099
+ schema.causality ? JSON.stringify(schema.causality) : null,
2100
+ schema.visualization ? JSON.stringify(schema.visualization) : null,
2101
+ JSON.stringify(schema.tags || []),
2102
+ now,
2103
+ now
2104
+ ]
2105
+ );
2106
+ this.save();
2107
+ return {
2108
+ id: schema.id,
2109
+ version: schema.version,
2110
+ name: schema.name,
2111
+ description: schema.description,
2112
+ scope: schema.scope,
2113
+ eventTypes: schema.eventTypes,
2114
+ causality: schema.causality,
2115
+ visualization: schema.visualization,
2116
+ tags: schema.tags || [],
2117
+ registeredAt: now,
2118
+ updatedAt: now
2119
+ };
2120
+ }
2121
+ getSchema(id) {
2122
+ this.initializeSync();
2123
+ const result = this.db.exec("SELECT * FROM schemas WHERE id = ?", [id]);
2124
+ if (result.length === 0 || result[0].values.length === 0) return null;
2125
+ return this.rowToSchema(result[0].columns, result[0].values[0]);
2126
+ }
2127
+ listSchemas() {
2128
+ this.initializeSync();
2129
+ const result = this.db.exec("SELECT * FROM schemas ORDER BY name ASC");
2130
+ if (result.length === 0) return [];
2131
+ return result[0].values.map(
2132
+ (row) => this.rowToSchema(result[0].columns, row)
2133
+ );
2134
+ }
2135
+ rowToSchema(columns, row) {
2136
+ const obj = {};
2137
+ columns.forEach((col, i) => {
2138
+ obj[col] = row[i];
2139
+ });
2140
+ return {
2141
+ id: obj.id,
2142
+ version: obj.version,
2143
+ name: obj.name,
2144
+ description: obj.description || void 0,
2145
+ scope: JSON.parse(obj.scope_json),
2146
+ eventTypes: JSON.parse(obj.event_types_json),
2147
+ causality: obj.causality_json ? JSON.parse(obj.causality_json) : void 0,
2148
+ visualization: obj.visualization_json ? JSON.parse(obj.visualization_json) : void 0,
2149
+ tags: JSON.parse(obj.tags_json || "[]"),
2150
+ registeredAt: obj.registered_at,
2151
+ updatedAt: obj.updated_at
2152
+ };
2153
+ }
2154
+ // ─── Generic Events ────────────────────────────────────────────
2155
+ insertEventBatch(schemaId, service, inputs) {
2156
+ this.initializeSync();
2157
+ const schema = this.getSchema(schemaId);
2158
+ const typeMap = /* @__PURE__ */ new Map();
2159
+ if (schema) {
2160
+ for (const et of schema.eventTypes) {
2161
+ typeMap.set(et.type, {
2162
+ category: et.category,
2163
+ severity: et.severity || "info"
2164
+ });
2165
+ }
2166
+ }
2167
+ let accepted = 0;
2168
+ const errors = [];
2169
+ for (const input of inputs) {
2170
+ try {
2171
+ const id = input.id || uuidv4();
2172
+ const timestamp = input.timestamp || (/* @__PURE__ */ new Date()).toISOString();
2173
+ const resolved = typeMap.get(input.type);
2174
+ const category = resolved?.category || "unknown";
2175
+ const severity = input.severity || resolved?.severity || "info";
2176
+ const scopeValue = input.scopeValue != null ? String(input.scopeValue) : null;
2177
+ const scopeOrdinal = typeof input.scopeValue === "number" ? input.scopeValue : null;
2178
+ this.db.run(
2179
+ `INSERT INTO events (
2180
+ id, schema_id, event_type, category, timestamp, scope_value,
2181
+ scope_ordinal, session_id, service, data_json, severity,
2182
+ parent_event_id, depth
2183
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2184
+ [
2185
+ id,
2186
+ schemaId,
2187
+ input.type,
2188
+ category,
2189
+ timestamp,
2190
+ scopeValue,
2191
+ scopeOrdinal,
2192
+ input.sessionId || null,
2193
+ service,
2194
+ input.data ? JSON.stringify(input.data) : null,
2195
+ severity,
2196
+ input.parentEventId || null,
2197
+ input.depth ?? 0
2198
+ ]
2199
+ );
2200
+ accepted++;
2201
+ } catch (err) {
2202
+ errors.push(err instanceof Error ? err.message : String(err));
2203
+ }
2204
+ }
2205
+ this.save();
2206
+ return { accepted, errors };
2207
+ }
2208
+ queryEvents(options = {}) {
2209
+ this.initializeSync();
2210
+ const { limit = 100, offset = 0 } = options;
2211
+ const conditions = [];
2212
+ const params = [];
2213
+ if (options.schemaId) {
2214
+ conditions.push("schema_id = ?");
2215
+ params.push(options.schemaId);
2216
+ }
2217
+ if (options.eventType) {
2218
+ conditions.push("event_type = ?");
2219
+ params.push(options.eventType);
2220
+ }
2221
+ if (options.category) {
2222
+ conditions.push("category = ?");
2223
+ params.push(options.category);
2224
+ }
2225
+ if (options.service) {
2226
+ conditions.push("service = ?");
2227
+ params.push(options.service);
2228
+ }
2229
+ if (options.sessionId) {
2230
+ conditions.push("session_id = ?");
2231
+ params.push(options.sessionId);
2232
+ }
2233
+ if (options.scopeValue) {
2234
+ conditions.push("scope_value = ?");
2235
+ params.push(options.scopeValue);
2236
+ }
2237
+ if (options.scopeFrom) {
2238
+ conditions.push("scope_value >= ?");
2239
+ params.push(options.scopeFrom);
2240
+ }
2241
+ if (options.scopeTo) {
2242
+ conditions.push("scope_value <= ?");
2243
+ params.push(options.scopeTo);
2244
+ }
2245
+ if (options.severity) {
2246
+ conditions.push("severity = ?");
2247
+ params.push(options.severity);
2248
+ }
2249
+ if (options.since) {
2250
+ conditions.push("timestamp >= ?");
2251
+ params.push(options.since);
2252
+ }
2253
+ if (options.until) {
2254
+ conditions.push("timestamp <= ?");
2255
+ params.push(options.until);
2256
+ }
2257
+ if (options.search) {
2258
+ conditions.push("data_json LIKE ?");
2259
+ params.push(`%${options.search}%`);
2260
+ }
2261
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
2262
+ const result = this.db.exec(
2263
+ `SELECT * FROM events ${whereClause} ORDER BY timestamp DESC LIMIT ? OFFSET ?`,
2264
+ [...params, limit, offset]
2265
+ );
2266
+ if (result.length === 0) return [];
2267
+ return result[0].values.map(
2268
+ (row) => this.rowToGenericEvent(result[0].columns, row)
2269
+ );
2270
+ }
2271
+ queryEventsByScope(schemaId, scopeValue) {
2272
+ this.initializeSync();
2273
+ const result = this.db.exec(
2274
+ `SELECT * FROM events
2275
+ WHERE schema_id = ? AND scope_value = ?
2276
+ ORDER BY timestamp ASC`,
2277
+ [schemaId, scopeValue]
2278
+ );
2279
+ if (result.length === 0) return [];
2280
+ return result[0].values.map(
2281
+ (row) => this.rowToGenericEvent(result[0].columns, row)
2282
+ );
2283
+ }
2284
+ getEventScopes(schemaId, options = {}) {
2285
+ this.initializeSync();
2286
+ const { limit = 100, offset = 0 } = options;
2287
+ const conditions = ["schema_id = ?"];
2288
+ const params = [schemaId];
2289
+ if (options.sessionId) {
2290
+ conditions.push("session_id = ?");
2291
+ params.push(options.sessionId);
2292
+ }
2293
+ const whereClause = `WHERE ${conditions.join(" AND ")}`;
2294
+ const result = this.db.exec(
2295
+ `SELECT
2296
+ scope_value,
2297
+ MIN(scope_ordinal) as scope_ordinal,
2298
+ COUNT(*) as event_count,
2299
+ MIN(timestamp) as first_timestamp,
2300
+ MAX(timestamp) as last_timestamp
2301
+ FROM events
2302
+ ${whereClause}
2303
+ AND scope_value IS NOT NULL
2304
+ GROUP BY scope_value
2305
+ ORDER BY MIN(COALESCE(scope_ordinal, 0)) DESC, MIN(timestamp) DESC
2306
+ LIMIT ? OFFSET ?`,
2307
+ [...params, limit, offset]
2308
+ );
2309
+ if (result.length === 0) return [];
2310
+ const scopes = [];
2311
+ for (const row of result[0].values) {
2312
+ const scopeValue = row[0];
2313
+ const scopeOrdinal = row[1] != null ? row[1] : void 0;
2314
+ const eventCount = row[2];
2315
+ const firstTimestamp = row[3];
2316
+ const lastTimestamp = row[4];
2317
+ const catResult = this.db.exec(
2318
+ `SELECT category, COUNT(*) as count FROM events
2319
+ WHERE schema_id = ? AND scope_value = ?
2320
+ GROUP BY category`,
2321
+ [schemaId, scopeValue]
2322
+ );
2323
+ const categories = {};
2324
+ if (catResult.length > 0) {
2325
+ for (const catRow of catResult[0].values) {
2326
+ categories[catRow[0]] = catRow[1];
2327
+ }
2328
+ }
2329
+ scopes.push({
2330
+ scopeValue,
2331
+ scopeOrdinal,
2332
+ eventCount,
2333
+ categories,
2334
+ firstTimestamp,
2335
+ lastTimestamp
2336
+ });
2337
+ }
2338
+ return scopes;
2339
+ }
2340
+ getEventCount(options = {}) {
2341
+ this.initializeSync();
2342
+ const conditions = [];
2343
+ const params = [];
2344
+ if (options.schemaId) {
2345
+ conditions.push("schema_id = ?");
2346
+ params.push(options.schemaId);
2347
+ }
2348
+ if (options.eventType) {
2349
+ conditions.push("event_type = ?");
2350
+ params.push(options.eventType);
2351
+ }
2352
+ if (options.service) {
2353
+ conditions.push("service = ?");
2354
+ params.push(options.service);
2355
+ }
2356
+ if (options.since) {
2357
+ conditions.push("timestamp >= ?");
2358
+ params.push(options.since);
2359
+ }
2360
+ if (options.until) {
2361
+ conditions.push("timestamp <= ?");
2362
+ params.push(options.until);
2363
+ }
2364
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
2365
+ const result = this.db.exec(
2366
+ `SELECT COUNT(*) as count FROM events ${whereClause}`,
2367
+ params
2368
+ );
2369
+ if (result.length === 0 || result[0].values.length === 0) return 0;
2370
+ return result[0].values[0][0];
2371
+ }
2372
+ pruneEvents(maxCount) {
2373
+ this.initializeSync();
2374
+ if (maxCount <= 0) return 0;
2375
+ const currentCount = this.getEventCount();
2376
+ if (currentCount <= maxCount) return 0;
2377
+ const deleteCount = currentCount - maxCount;
2378
+ this.db.run(
2379
+ `DELETE FROM events WHERE id IN (
2380
+ SELECT id FROM events ORDER BY timestamp ASC LIMIT ?
2381
+ )`,
2382
+ [deleteCount]
2383
+ );
2384
+ this.save();
2385
+ return deleteCount;
2386
+ }
2387
+ rowToGenericEvent(columns, row) {
2388
+ const obj = {};
2389
+ columns.forEach((col, i) => {
2390
+ obj[col] = row[i];
2391
+ });
2392
+ return {
2393
+ id: obj.id,
2394
+ schemaId: obj.schema_id,
2395
+ eventType: obj.event_type,
2396
+ category: obj.category,
2397
+ timestamp: obj.timestamp,
2398
+ scopeValue: obj.scope_value || void 0,
2399
+ scopeOrdinal: obj.scope_ordinal != null ? obj.scope_ordinal : void 0,
2400
+ sessionId: obj.session_id || void 0,
2401
+ service: obj.service,
2402
+ data: obj.data_json ? JSON.parse(obj.data_json) : void 0,
2403
+ severity: obj.severity || "info",
2404
+ parentEventId: obj.parent_event_id || void 0,
2405
+ depth: obj.depth || 0
2406
+ };
2407
+ }
2025
2408
  close() {
2026
2409
  if (this.db) {
2027
2410
  this.save();
package/dist/cli.js CHANGED
@@ -2,31 +2,34 @@
2
2
 
3
3
  // src/cli.ts
4
4
  import { Command } from "commander";
5
+ import { createRequire } from "module";
6
+ var require2 = createRequire(import.meta.url);
7
+ var { version: VERSION } = require2("../package.json");
5
8
  var program = new Command();
6
- program.name("sentinel").description("Semantic error monitoring \u2014 errors that speak your language").version("0.2.0");
7
- program.command("dashboard", { isDefault: true }).description("Launch the Sentinel dashboard").option("-p, --port <port>", "Port number", "3838").option("--no-open", "Don't open browser").action(async (opts) => {
8
- const { launchDashboard } = await import("./commands-7PHRWGOB.js");
9
+ program.name("sentinel").description("Semantic error monitoring \u2014 errors that speak your language").version(VERSION);
10
+ program.command("defend", { isDefault: true }).description("Launch the Sentinel dashboard").option("-p, --port <port>", "Port number", "3838").option("--no-open", "Don't open browser").action(async (opts) => {
11
+ const { launchDashboard } = await import("./commands-JU5VP345.js");
9
12
  await launchDashboard(opts);
10
13
  });
11
14
  program.command("init").description("Initialize Sentinel in current project").option("--detect", "Auto-detect symbols from codebase").action(async (opts) => {
12
- const { initProject } = await import("./commands-7PHRWGOB.js");
15
+ const { initProject } = await import("./commands-JU5VP345.js");
13
16
  await initProject(opts);
14
17
  });
15
18
  var triage = program.command("triage").description("Incident triage");
16
19
  triage.command("list").description("List incidents").option("-s, --status <status>", "Filter by status (open, investigating, resolved, wont-fix)").option("-e, --env <env>", "Filter by environment").option("--symbol <symbol>", "Filter by symbol").option("-n, --limit <n>", "Max results", "10").action(async (opts) => {
17
- const { triageList } = await import("./commands-7PHRWGOB.js");
20
+ const { triageList } = await import("./commands-JU5VP345.js");
18
21
  await triageList(opts);
19
22
  });
20
23
  triage.command("show <id>").description("Show incident details").option("--timeline", "Include flow timeline").action(async (id, opts) => {
21
- const { triageShow } = await import("./commands-7PHRWGOB.js");
24
+ const { triageShow } = await import("./commands-JU5VP345.js");
22
25
  await triageShow(id, opts);
23
26
  });
24
27
  triage.command("resolve <id>").description("Resolve an incident").option("--pattern <id>", "Pattern that resolved it").option("--commit <hash>", "Fix commit").option("--notes <text>", "Resolution notes").action(async (id, opts) => {
25
- const { triageResolve } = await import("./commands-7PHRWGOB.js");
28
+ const { triageResolve } = await import("./commands-JU5VP345.js");
26
29
  await triageResolve(id, opts);
27
30
  });
28
31
  triage.command("stats").description("Show incident statistics").option("-p, --period <period>", "Period (7d, 30d, 90d)", "7d").action(async (opts) => {
29
- const { triageStats } = await import("./commands-7PHRWGOB.js");
32
+ const { triageStats } = await import("./commands-JU5VP345.js");
30
33
  await triageStats(opts);
31
34
  });
32
35
  program.parse();