@anmol-srv/sigil 0.13.0 → 0.15.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anmol-srv/sigil",
3
- "version": "0.13.0",
3
+ "version": "0.15.3",
4
4
  "type": "module",
5
5
  "description": "Local-first memory infrastructure for AI coding agents. One brain shared across Claude Code, Codex CLI, Cursor, Kiro, Continue, Cline, Windsurf, or any MCP client. Organized in pluggable pods, stored in your own Postgres. No cloud, no telemetry. Auto-captured from Claude Code via hooks; surfaced everywhere else as a 9-tool MCP server.",
6
6
  "bin": {
@@ -28,6 +28,7 @@
28
28
  "dist/",
29
29
  "prompts/",
30
30
  "src/db/migrations/",
31
+ "src/db/drivers/",
31
32
  "src/gui/web/",
32
33
  "integrations/",
33
34
  "knexfile.js",
@@ -83,14 +84,14 @@
83
84
  "dependencies": {
84
85
  "@iarna/toml": "^2.2.5",
85
86
  "@modelcontextprotocol/sdk": "^1.27.1",
86
- "@number0/iroh": "^0.35.0",
87
87
  "dotenv": "^17.3.1",
88
88
  "knex": "^3.1.0",
89
89
  "pg": "^8.20.0",
90
90
  "ws": "^8.21.0"
91
91
  },
92
92
  "optionalDependencies": {
93
- "@anthropic-ai/sdk": "^0.30.0"
93
+ "@anthropic-ai/sdk": "^0.30.0",
94
+ "@number0/iroh": "^0.35.0"
94
95
  },
95
96
  "devDependencies": {
96
97
  "@clack/prompts": "^1.2.0",
@@ -0,0 +1,41 @@
1
+ /**
2
+ * DB driver selection.
3
+ *
4
+ * - If SIGIL_DATABASE_URL or DATABASE_URL is set, use the URL driver
5
+ * (Neon, Supabase, RDS, Render, Railway, self-hosted, etc.).
6
+ * - Otherwise, use the local-postgres driver (back-compat with the
7
+ * SIGIL_DB_HOST / PORT / NAME / USER / PASSWORD env vars).
8
+ *
9
+ * Returns a pg-shape connection object suitable for passing to knex's
10
+ * `connection` field. Pool config and post-processing are applied by
11
+ * the caller (cortex.js).
12
+ */
13
+ import { buildLocalConnection, buildLocalConnectionFromFields } from './local-postgres.js';
14
+ import { buildUrlConnection, classifyProvider } from './url.js';
15
+
16
+ export function selectDriver(config) {
17
+ const url = config.db.url;
18
+ if (url) {
19
+ return {
20
+ kind: 'url',
21
+ provider: classifyProvider(url),
22
+ connection: normalizeConnection(buildUrlConnection(url)),
23
+ };
24
+ }
25
+ return {
26
+ kind: 'local',
27
+ provider: 'local',
28
+ connection: normalizeConnection(buildLocalConnection(config)),
29
+ };
30
+ }
31
+
32
+ // pg throws "SASL: client password must be a string" if password is null/
33
+ // undefined. Coerce credentials to strings so a missing value degrades to a
34
+ // clear auth error (or a no-password connect) instead of a cryptic SASL crash.
35
+ function normalizeConnection(conn) {
36
+ if (conn.password == null) conn.password = '';
37
+ if (conn.user == null) delete conn.user;
38
+ return conn;
39
+ }
40
+
41
+ export { buildLocalConnection, buildLocalConnectionFromFields, buildUrlConnection, classifyProvider };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Local-Postgres driver: builds a pg connection from the discrete
3
+ * SIGIL_DB_HOST / PORT / NAME / USER / PASSWORD env vars. This is the
4
+ * original Sigil behavior and remains the default when no URL is set.
5
+ */
6
+ export function buildLocalConnection(config) {
7
+ return {
8
+ host: config.db.host,
9
+ port: config.db.port,
10
+ database: config.db.database,
11
+ user: config.db.user,
12
+ password: config.db.password,
13
+ };
14
+ }
15
+
16
+ /**
17
+ * Build a pg connection from discrete fields (with sigil defaults). Used by
18
+ * setup steps that have host/port/db/user/password directly rather than a
19
+ * `config`-shaped object.
20
+ */
21
+ export function buildLocalConnectionFromFields({ host = 'localhost', port = 5432, database = 'sigil', user = 'sigil_app', password = '' } = {}) {
22
+ return { host, port: Number(port) || 5432, database, user, password };
23
+ }
@@ -0,0 +1,155 @@
1
+ /**
2
+ * URL driver: turns a Postgres connection URL into pg-shape config.
3
+ *
4
+ * Provider hints:
5
+ * - Neon (*.neon.tech) → SSL required, root CA trust default
6
+ * - Supabase (*.supabase.co | pooler) → SSL required
7
+ * - AWS RDS (*.rds.amazonaws.com) → SSL required (AWS root)
8
+ * - Render (*.render.com) → SSL required
9
+ * - Railway (*.railway.app) → SSL required
10
+ * - localhost / 127.0.0.1 / *.local → no SSL by default
11
+ *
12
+ * Explicit sslmode in the URL query string wins:
13
+ * sslmode=disable → no SSL
14
+ * sslmode=require → SSL, rejectUnauthorized=true
15
+ * sslmode=no-verify → SSL, rejectUnauthorized=false (compatibility for self-signed)
16
+ * sslmode=verify-full → SSL, rejectUnauthorized=true
17
+ *
18
+ * Falls back to provider heuristics if sslmode is absent.
19
+ */
20
+
21
+ const REQUIRE_SSL_HOSTS = [
22
+ /\.neon\.tech$/i,
23
+ /\.supabase\.co$/i,
24
+ /\.supabase\.com$/i,
25
+ /\.pooler\.supabase\.com$/i,
26
+ /\.rds\.amazonaws\.com$/i,
27
+ /\.render\.com$/i,
28
+ /\.railway\.app$/i,
29
+ /\.cockroachlabs\.cloud$/i,
30
+ ];
31
+
32
+ const NO_SSL_HOSTS = [
33
+ /^localhost$/i,
34
+ /^127\.0\.0\.1$/,
35
+ /^::1$/,
36
+ /\.local$/i,
37
+ ];
38
+
39
+ export function buildUrlConnection(url) {
40
+ if (!url) {
41
+ throw new Error('url driver: SIGIL_DATABASE_URL is empty');
42
+ }
43
+
44
+ let parsed;
45
+ try {
46
+ parsed = new URL(url);
47
+ } catch (err) {
48
+ throw new Error(`url driver: invalid URL — ${err.message}`);
49
+ }
50
+
51
+ if (!/^postgres(ql)?:$/i.test(parsed.protocol)) {
52
+ throw new Error(`url driver: expected postgres:// or postgresql:// scheme, got ${parsed.protocol}`);
53
+ }
54
+
55
+ const sslmode = parsed.searchParams.get('sslmode');
56
+ const ssl = resolveSsl(parsed.hostname, sslmode);
57
+
58
+ // pg accepts a connectionString directly, but we expand into discrete
59
+ // fields so knex can inject pool/post-process config alongside it.
60
+ const conn = {
61
+ host: parsed.hostname,
62
+ port: parsed.port ? Number(parsed.port) : 5432,
63
+ database: parsed.pathname.replace(/^\//, '') || 'postgres',
64
+ user: decodeURIComponent(parsed.username),
65
+ password: decodeURIComponent(parsed.password),
66
+ };
67
+ if (ssl !== undefined) conn.ssl = ssl;
68
+
69
+ // Application name surfaces in Postgres logs / pg_stat_activity — useful
70
+ // for ops to identify Sigil connections vs other clients on a shared DB.
71
+ if (!parsed.searchParams.get('application_name')) {
72
+ conn.application_name = 'sigil';
73
+ }
74
+
75
+ return conn;
76
+ }
77
+
78
+ function resolveSsl(hostname, sslmode) {
79
+ if (sslmode === 'disable') return false;
80
+ if (sslmode === 'require' || sslmode === 'verify-full' || sslmode === 'verify-ca') {
81
+ return { rejectUnauthorized: true };
82
+ }
83
+ if (sslmode === 'no-verify' || sslmode === 'prefer') {
84
+ return { rejectUnauthorized: false };
85
+ }
86
+ // Heuristics (no explicit sslmode set)
87
+ if (NO_SSL_HOSTS.some((re) => re.test(hostname))) return undefined;
88
+ if (REQUIRE_SSL_HOSTS.some((re) => re.test(hostname))) return { rejectUnauthorized: true };
89
+ // Unknown remote host: be safe — require SSL, allow self-signed.
90
+ return { rejectUnauthorized: false };
91
+ }
92
+
93
+ /**
94
+ * Pooled-connection detection + direct-host rewrite for migrations.
95
+ *
96
+ * Connection poolers (PgBouncer in transaction mode — Neon's `-pooler`
97
+ * endpoint, Supabase's pooler host) reject the advisory locks and prepared
98
+ * statements knex migrations rely on. Migrations must run against the DIRECT
99
+ * endpoint; only runtime traffic should use the pooled URL.
100
+ *
101
+ * isPooledUrl(url) → true if the host looks like a pooler
102
+ * directMigrationUrl(url) → a URL safe to migrate against, or null if we
103
+ * can't safely derive one (caller must then ask
104
+ * the user for a direct connection string)
105
+ *
106
+ * Neon is the only provider whose direct host is a deterministic transform
107
+ * of the pooled host (drop "-pooler"). Supabase's direct host is a different
108
+ * shape (db.<ref>.supabase.co) we can't reconstruct from the pooled URL, so
109
+ * we return null rather than fabricate a wrong host.
110
+ */
111
+ export function isPooledUrl(url) {
112
+ try {
113
+ const host = new URL(url).hostname;
114
+ return /-pooler\./i.test(host) || /\.pooler\.supabase\.com$/i.test(host);
115
+ } catch {
116
+ return false;
117
+ }
118
+ }
119
+
120
+ export function directMigrationUrl(url) {
121
+ let parsed;
122
+ try {
123
+ parsed = new URL(url);
124
+ } catch {
125
+ return null;
126
+ }
127
+ const host = parsed.hostname;
128
+ // Neon: ep-xxx-pooler.region.aws.neon.tech → ep-xxx.region.aws.neon.tech
129
+ if (/\.neon\.tech$/i.test(host) && /-pooler\./i.test(host)) {
130
+ parsed.hostname = host.replace('-pooler.', '.');
131
+ return parsed.toString();
132
+ }
133
+ // Already-direct Neon host (or any non-pooled host) needs no rewrite.
134
+ if (!isPooledUrl(url)) return url;
135
+ // Pooled, but not a shape we can safely rewrite (e.g. Supabase pooler).
136
+ return null;
137
+ }
138
+
139
+ /** Identify the most-likely provider for diagnostics / hints. */
140
+ export function classifyProvider(url) {
141
+ try {
142
+ const host = new URL(url).hostname;
143
+ if (/\.neon\.tech$/i.test(host)) return 'neon';
144
+ if (/\.pooler\.supabase\.com$/i.test(host)) return 'supabase-pooler';
145
+ if (/\.supabase\.co$/i.test(host) || /\.supabase\.com$/i.test(host)) return 'supabase';
146
+ if (/\.rds\.amazonaws\.com$/i.test(host)) return 'aws-rds';
147
+ if (/\.render\.com$/i.test(host)) return 'render';
148
+ if (/\.railway\.app$/i.test(host)) return 'railway';
149
+ if (/\.cockroachlabs\.cloud$/i.test(host)) return 'cockroachdb';
150
+ if (NO_SSL_HOSTS.some((re) => re.test(host))) return 'local';
151
+ return 'unknown';
152
+ } catch {
153
+ return 'unknown';
154
+ }
155
+ }
@@ -0,0 +1,94 @@
1
+ import { describe, it, expect } from 'vitest';
2
+
3
+ import { buildUrlConnection, classifyProvider } from './url.js';
4
+
5
+ describe('url driver', () => {
6
+ it('parses basic postgres:// URL', () => {
7
+ const c = buildUrlConnection('postgres://user:pass@host.example.com:5432/db');
8
+ expect(c.host).toBe('host.example.com');
9
+ expect(c.port).toBe(5432);
10
+ expect(c.database).toBe('db');
11
+ expect(c.user).toBe('user');
12
+ expect(c.password).toBe('pass');
13
+ });
14
+
15
+ it('decodes URI-encoded passwords (special chars)', () => {
16
+ const c = buildUrlConnection('postgres://u:p%40ss%3Aword@h.example.com/db');
17
+ expect(c.password).toBe('p@ss:word');
18
+ });
19
+
20
+ it('defaults port to 5432 when omitted', () => {
21
+ const c = buildUrlConnection('postgresql://u:p@h.example.com/db');
22
+ expect(c.port).toBe(5432);
23
+ });
24
+
25
+ it('accepts both postgres:// and postgresql:// schemes', () => {
26
+ expect(buildUrlConnection('postgres://u:p@h.example.com/db').host).toBe('h.example.com');
27
+ expect(buildUrlConnection('postgresql://u:p@h.example.com/db').host).toBe('h.example.com');
28
+ });
29
+
30
+ it('rejects non-postgres schemes', () => {
31
+ expect(() => buildUrlConnection('mysql://u:p@h.example.com/db')).toThrow(/scheme/);
32
+ });
33
+
34
+ it('does not enable SSL for localhost', () => {
35
+ expect(buildUrlConnection('postgres://u:p@localhost:5432/db').ssl).toBeUndefined();
36
+ expect(buildUrlConnection('postgres://u:p@127.0.0.1/db').ssl).toBeUndefined();
37
+ });
38
+
39
+ it('enables strict SSL for Neon URLs', () => {
40
+ const c = buildUrlConnection('postgres://u:p@ep-foo-123.us-east-2.aws.neon.tech/db');
41
+ expect(c.ssl).toEqual({ rejectUnauthorized: true });
42
+ });
43
+
44
+ it('enables strict SSL for Supabase pooler URLs', () => {
45
+ const c = buildUrlConnection('postgres://u:p@aws-0-us-east-1.pooler.supabase.com:6543/db');
46
+ expect(c.ssl).toEqual({ rejectUnauthorized: true });
47
+ });
48
+
49
+ it('enables strict SSL for AWS RDS URLs', () => {
50
+ const c = buildUrlConnection('postgres://u:p@my-instance.abc123.us-east-1.rds.amazonaws.com/db');
51
+ expect(c.ssl).toEqual({ rejectUnauthorized: true });
52
+ });
53
+
54
+ it('explicit sslmode=disable wins over heuristics', () => {
55
+ const c = buildUrlConnection('postgres://u:p@host.neon.tech/db?sslmode=disable');
56
+ expect(c.ssl).toBe(false);
57
+ });
58
+
59
+ it('explicit sslmode=no-verify produces relaxed SSL', () => {
60
+ const c = buildUrlConnection('postgres://u:p@host.example.com/db?sslmode=no-verify');
61
+ expect(c.ssl).toEqual({ rejectUnauthorized: false });
62
+ });
63
+
64
+ it('unknown remote host defaults to permissive SSL', () => {
65
+ const c = buildUrlConnection('postgres://u:p@unknown-host.example.com/db');
66
+ expect(c.ssl).toEqual({ rejectUnauthorized: false });
67
+ });
68
+
69
+ it('sets application_name=sigil by default', () => {
70
+ const c = buildUrlConnection('postgres://u:p@h.example.com/db');
71
+ expect(c.application_name).toBe('sigil');
72
+ });
73
+
74
+ it('rejects empty input', () => {
75
+ expect(() => buildUrlConnection('')).toThrow(/empty/);
76
+ });
77
+
78
+ it('rejects malformed URLs', () => {
79
+ expect(() => buildUrlConnection('not a url')).toThrow(/invalid URL/);
80
+ });
81
+ });
82
+
83
+ describe('classifyProvider', () => {
84
+ it('identifies common providers', () => {
85
+ expect(classifyProvider('postgres://u:p@ep-x.neon.tech/db')).toBe('neon');
86
+ expect(classifyProvider('postgres://u:p@x.supabase.co/db')).toBe('supabase');
87
+ expect(classifyProvider('postgres://u:p@x.pooler.supabase.com/db')).toBe('supabase-pooler');
88
+ expect(classifyProvider('postgres://u:p@x.rds.amazonaws.com/db')).toBe('aws-rds');
89
+ expect(classifyProvider('postgres://u:p@x.render.com/db')).toBe('render');
90
+ expect(classifyProvider('postgres://u:p@x.railway.app/db')).toBe('railway');
91
+ expect(classifyProvider('postgres://u:p@localhost/db')).toBe('local');
92
+ expect(classifyProvider('postgres://u:p@some-host.example.com/db')).toBe('unknown');
93
+ });
94
+ });
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Trigram index for entity name search.
3
+ *
4
+ * entities/store.js searchByName() runs `WHERE LOWER(name) LIKE '%q%'` on
5
+ * every search for short queries. A leading-wildcard LIKE can't use a normal
6
+ * B-tree index, so this was a sequential scan of the entity table that grew
7
+ * linearly with memory size. A GIN trigram index on LOWER(name) turns it into
8
+ * an index scan.
9
+ *
10
+ * The index expression must match the query expression exactly (LOWER(name)),
11
+ * otherwise the planner won't use it.
12
+ */
13
+ exports.up = async (knex) => {
14
+ await knex.raw('CREATE EXTENSION IF NOT EXISTS pg_trgm');
15
+ await knex.raw(`
16
+ CREATE INDEX IF NOT EXISTS entity_name_trgm_idx
17
+ ON entity USING GIN (LOWER(name) gin_trgm_ops)
18
+ `);
19
+ };
20
+
21
+ exports.down = async (knex) => {
22
+ await knex.raw('DROP INDEX IF EXISTS entity_name_trgm_idx');
23
+ // Leave the pg_trgm extension installed — other indexes or queries may rely
24
+ // on it, and dropping a shared extension on rollback is surprising.
25
+ };