Artifact a9a2a8a83138fe4f53d0e9e4822cda2e343f3d321e446e4a9f3a320383eb9442:
- File
src/userdb.c
— part of check-in
[34868fb74a]
at
2026-11-19 17:09:37
on branch rows
— Conversations as rows of picu_pi_session_line for ctx (ticket 467ff3edef)
The Pi v3 lines pic exports for ctx now go into picu_pi_session_line in ~/.pic/pic.sqlite: one row per line, append-only per conversation, deflated when smaller by the SQLite Archive rule, with the line's Pi type and the journal entry it came from. Exporting again adds only what is new; a rename adds one line. Until ctx reads the rows the ~/.pic/sessions files are still written; piSessionFiles=0 stops them. pic export --pi DIR still writes files there, and - writes to standard output. (user: dmestas, size: 9705) [annotate] [blame] [check-ins using] [more...]
/* pic's user database: ~/.pic/pic.sqlite. Tables are prefixed picu_ (the * journal's are pic_, libfossil's fsl*) so one file could hold them all. */ #include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <time.h> #include <unistd.h> #include "sqlar.h" #include "userdb.h" #define USERDB_VERSION "1" static sqlite3 *conn; static const char schema[] = "CREATE TABLE IF NOT EXISTS picu_meta(key TEXT PRIMARY KEY, value TEXT NOT NULL);" "CREATE TABLE IF NOT EXISTS picu_setting(key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_ms INTEGER NOT NULL);" /* Pi's model catalog per provider from pi.dev; models: JSON array, sqlar rule. */ "CREATE TABLE IF NOT EXISTS picu_catalog(provider TEXT PRIMARY KEY, sz INTEGER NOT NULL, models BLOB NOT NULL," " checked_ms INTEGER NOT NULL, last_modified_ms INTEGER NOT NULL, etag TEXT);" "CREATE TABLE IF NOT EXISTS picu_trust(path TEXT PRIMARY KEY, trusted INTEGER NOT NULL CHECK(trusted IN (0,1))," " decided_ms INTEGER NOT NULL);" "CREATE TABLE IF NOT EXISTS picu_scoped_model(position INTEGER PRIMARY KEY, pattern TEXT NOT NULL);" "CREATE TABLE IF NOT EXISTS picu_workspace(path TEXT PRIMARY KEY, journal TEXT NOT NULL, last_used_ms INTEGER NOT NULL);" /* Pi v3 session lines of pic's conversations, for ctx: append-only per * conversation; line is the JSON line, deflated when smaller (sqlar rule). * kind is the line's Pi type, entry the journal entry it came from. */ "CREATE TABLE IF NOT EXISTS picu_pi_session_line(conversation TEXT NOT NULL, seq INTEGER NOT NULL," " workspace TEXT NOT NULL, kind TEXT NOT NULL, entry INTEGER, sz INTEGER NOT NULL, line BLOB NOT NULL," " PRIMARY KEY(conversation, seq));" "INSERT OR IGNORE INTO picu_meta(key, value) VALUES('schema_version', '" USERDB_VERSION "');"; static int home_path(const char *name, char *out, size_t cap) { const char *home = getenv("HOME"); return home && *home && snprintf(out, cap, "%s/.pic/%s", home, name) < (int)cap ? 0 : -1; } /* A whole small file (malloc'd), or NULL. */ static char *slurp(const char *path) { FILE *f = fopen(path, "rb"); char *buf = NULL; long n; if (!f) { return NULL; } if (fseek(f, 0, SEEK_END) == 0 && (n = ftell(f)) >= 0 && n < (64L << 20) && fseek(f, 0, SEEK_SET) == 0 && (buf = malloc((size_t)n + 1))) { if (fread(buf, 1, (size_t)n, f) == (size_t)n) { buf[n] = '\0'; } else { free(buf); buf = NULL; } } fclose(f); return buf; } static long long now_ms(void) { struct timespec t; clock_gettime(CLOCK_REALTIME, &t); return (long long)t.tv_sec * 1000 + t.tv_nsec / 1000000; } static int run(sqlite3 *db, const char *sql, const char *text) { sqlite3_stmt *q = NULL; int rc; if (sqlite3_prepare_v2(db, sql, -1, &q, NULL) != SQLITE_OK) { return -1; } if (text) { sqlite3_bind_text(q, 1, text, -1, SQLITE_STATIC); } sqlite3_bind_int64(q, sqlite3_bind_parameter_count(q), now_ms()); rc = sqlite3_step(q); sqlite3_finalize(q); return rc == SQLITE_DONE || rc == SQLITE_ROW ? 0 : -1; } static int valid_json(sqlite3 *db, const char *text) { sqlite3_stmt *q = NULL; int valid = 0; if (sqlite3_prepare_v2(db, "SELECT json_valid(?1)", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, text, -1, SQLITE_STATIC); valid = sqlite3_step(q) == SQLITE_ROW && sqlite3_column_int(q, 0); sqlite3_finalize(q); } return valid; } /* The legacy JSON files, imported in one transaction, then deleted. A file * that is not valid JSON is left in place. */ static int migrate(sqlite3 *db) { char spath[PATH_MAX], mpath[PATH_MAX]; char *settings = home_path("settings.json", spath, sizeof spath) == 0 ? slurp(spath) : NULL; char *store = home_path("models-store.json", mpath, sizeof mpath) == 0 ? slurp(mpath) : NULL; int rc = 0; if (!settings && !store) { return 0; } if (sqlite3_exec(db, "BEGIN IMMEDIATE", NULL, NULL, NULL) != SQLITE_OK) { free(settings); free(store); return -1; } if (settings) { rc |= run(db, "INSERT OR IGNORE INTO picu_setting(key, value, updated_ms)" " SELECT key, CAST(value AS TEXT), ?2 FROM json_each(?1) WHERE json_valid(?1) AND value IS NOT NULL", settings); } if (store) { rc |= run(db, "INSERT OR IGNORE INTO picu_catalog(provider, sz, models, checked_ms, last_modified_ms, etag)" " SELECT key, length(CAST(json_extract(value, '$.models') AS BLOB))," " sqlar_compress(CAST(json_extract(value, '$.models') AS BLOB))," " coalesce(json_extract(value, '$.checkedAt'), 0), coalesce(json_extract(value, '$.lastModified'), 0)," " json_extract(value, '$.etag')" " FROM json_each(?1) WHERE json_valid(?1) AND json_type(value, '$.models') = 'array' AND ?2 IS NOT NULL", store); } if (rc != 0 || sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) != SQLITE_OK) { (void)sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL); free(settings); free(store); return -1; } /* Committed: the files are no longer the record. Invalid JSON stays put. */ if (settings && valid_json(db, settings)) { (void)unlink(spath); } if (store && valid_json(db, store)) { (void)unlink(mpath); } free(settings); free(store); return 0; } sqlite3 *pic_userdb(char *err, size_t errcap) { char path[PATH_MAX], dir[PATH_MAX]; const char *home = getenv("HOME"); sqlite3 *db = NULL; if (conn) { return conn; } if (!home || !*home || snprintf(dir, sizeof dir, "%s/.pic", home) >= (int)sizeof dir || home_path("pic.sqlite", path, sizeof path) != 0) { if (err) { (void)snprintf(err, errcap, "no home directory for ~/.pic/pic.sqlite"); } return NULL; } if (mkdir(dir, 0700) != 0 && errno != EEXIST) { if (err) { (void)snprintf(err, errcap, "cannot create %s: %s", dir, strerror(errno)); } return NULL; } if (sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK || sqlite3_busy_timeout(db, 5000) != SQLITE_OK || sqlite3_exec(db, "PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=OFF", NULL, NULL, NULL) != SQLITE_OK || pic_sqlar_register(db) != 0 || sqlite3_exec(db, schema, NULL, NULL, NULL) != SQLITE_OK || migrate(db) != 0) { if (err) { (void)snprintf(err, errcap, "%s: %s", path, db ? sqlite3_errmsg(db) : "cannot open"); } sqlite3_close(db); return NULL; } (void)chmod(path, 0600); conn = db; return conn; } void pic_userdb_close(void) { sqlite3_close(conn); conn = NULL; } char *pic_setting(const char *key) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; char *out = NULL; if (db && sqlite3_prepare_v2(db, "SELECT value FROM picu_setting WHERE key = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, key, -1, SQLITE_STATIC); if (sqlite3_step(q) == SQLITE_ROW) { out = strdup((const char *)sqlite3_column_text(q, 0)); } } sqlite3_finalize(q); return out; } int pic_setting_set(const char *key, const char *value) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, value ? "INSERT INTO picu_setting(key, value, updated_ms) VALUES(?1, ?2, ?3)" " ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_ms = excluded.updated_ms" : "DELETE FROM picu_setting WHERE key = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, key, -1, SQLITE_STATIC); if (value) { sqlite3_bind_text(q, 2, value, -1, SQLITE_STATIC); sqlite3_bind_int64(q, 3, now_ms()); } rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; } sqlite3_finalize(q); return rc; } int pic_settings_each(int (*fn)(void *ctx, const char *key, const char *value), void *ctx) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; if (!db || sqlite3_prepare_v2(db, "SELECT key, value FROM picu_setting ORDER BY key", -1, &q, NULL) != SQLITE_OK) { return -1; } while (sqlite3_step(q) == SQLITE_ROW && !fn(ctx, (const char *)sqlite3_column_text(q, 0), (const char *)sqlite3_column_text(q, 1))) { } sqlite3_finalize(q); return 0; } int pic_workspace_touch(const char *workspace, const char *journal) { sqlite3 *db = pic_userdb(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, "INSERT INTO picu_workspace(path, journal, last_used_ms) VALUES(?1, ?2, ?3)" " ON CONFLICT(path) DO UPDATE SET journal = excluded.journal, last_used_ms = excluded.last_used_ms", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, workspace, -1, SQLITE_STATIC); sqlite3_bind_text(q, 2, journal, -1, SQLITE_STATIC); sqlite3_bind_int64(q, 3, now_ms()); rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; } sqlite3_finalize(q); return rc; }