kernel / core / itx / src / context / context-catalog.tsmain at 949894f
// context-catalog.ts — THE MIND'S CATALOG OF ITS CONTEXTS: one SQLite row per context, in the
// storage of the context that accepts the awake sockets (awake.ts). A row is the newest summary
// its context sent (context-summary.ts). A list joins the rows with the open sockets, so "awake"
// and live `activities` are never stored in SQLite: when a socket closes, both vanish at once.
// A watched query reads a copy of these rows, kept beside them (reactive-catalog.ts).
import { z } from "zod";
import type { ContextListing, ContextListOptions } from "iterate/api";
import type { AwakeContextState } from "./awake.ts";
import { activeContextActivities, ContextActivity } from "./context-activity-stack.ts";
import { ContextSummary } from "./context-summary.ts";
import type { CatalogRowInput } from "./reactive-catalog.ts";
/** Durable fields stored in SQLite `context_catalog.summary` (never live `activities`). */
const StoredContextSummary = ContextSummary.extend({
processors: z.array(z.string()),
storageBytes: z.number().int().nonnegative(),
});
/** What a context sends over its awake socket: its durable summary plus live in-memory activities. */
export const ContextSummaryMessage = StoredContextSummary.extend({
activities: z.array(ContextActivity).optional(),
});
export type ContextSummaryMessage = z.infer<typeof ContextSummaryMessage>;
/** A message on an awake socket that is no summary: saying it again would be refused again. Any
* other failure of `store` is the catalog's own, and the summary is owed. */
export class NotASummary extends Error {}
export class ContextCatalog {
readonly #sql: SqlStorage;
#tableMade = false;
constructor(sql: SqlStorage) {
this.#sql = sql;
}
#table(): void {
if (this.#tableMade) return;
// `active_at` is what a list sorts by: the newest activity, or the newest event without one
this.#sql.exec(
`CREATE TABLE IF NOT EXISTS context_catalog (
path TEXT PRIMARY KEY,
through_offset INTEGER NOT NULL,
active_at TEXT NOT NULL,
summary TEXT NOT NULL
)`,
);
this.#sql.exec(
"CREATE INDEX IF NOT EXISTS context_catalog_active_at ON context_catalog (active_at DESC)",
);
this.#tableMade = true;
}
/** Keeps the durable part of `message` as `path`'s row unless the row is of a later offset, and
* returns the live `activities` carried on the message so the caller can attach them to the
* open awake socket (never SQLite). `row` is the row as a watched query serves it, when the
* message was kept: the copy beside this table takes it (reactive-catalog.ts). A message that
* is no summary is refused with `NotASummary`. */
store(
path: string,
message: string,
): { activities: ContextActivity[] | undefined; row: CatalogRowInput | undefined } {
let parsed: ContextSummaryMessage;
try {
parsed = ContextSummaryMessage.parse(JSON.parse(message));
} catch (error) {
throw new NotASummary(error instanceof Error ? error.message : String(error));
}
const { activities, ...durable } = parsed;
this.#table();
const { rowsWritten } = this.#sql.exec(
`INSERT INTO context_catalog (path, through_offset, active_at, summary) VALUES (?, ?, ?, ?)
ON CONFLICT (path) DO UPDATE SET
through_offset = excluded.through_offset,
active_at = excluded.active_at,
summary = excluded.summary
WHERE excluded.through_offset >= context_catalog.through_offset`,
path,
durable.throughOffset,
durable.lastActivityAt || durable.lastEventAt || "",
JSON.stringify(durable),
);
return { activities, row: rowsWritten > 0 ? watchedRowOf(path, durable) : undefined };
}
/** One context's row as a watched query serves it: what the copy settles a write from that it
* could not make. Undefined when the catalog has none, or one that no longer parses. */
row(path: string): CatalogRowInput | undefined {
this.#table();
const stored = this.#sql
.exec<{ summary: string }>("SELECT summary FROM context_catalog WHERE path = ?", path)
.toArray()[0];
const summary = stored && StoredContextSummary.safeParse(JSON.parse(stored.summary));
return summary?.success ? watchedRowOf(path, summary.data) : undefined;
}
/** A page of the rows, in path order, after the path `after`: what the copy for watched queries
* is first filled from. A row that no longer parses as a summary is left out. */
rowsAfter(after: string, limit: number): CatalogRowInput[] {
this.#table();
return this.#sql
.exec<{ path: string; summary: string }>(
"SELECT path, summary FROM context_catalog WHERE path > ? ORDER BY path LIMIT ?",
after,
limit,
)
.toArray()
.flatMap(({ path, summary }) => {
const stored = StoredContextSummary.safeParse(JSON.parse(summary));
return stored.success ? [watchedRowOf(path, stored.data)] : [];
});
}
/** The contexts that match, the most recently active first. `awake` is the open sockets, by
* path, with their live in-memory activities. Sleeping contexts never carry `activities`. */
list(
options: ContextListOptions,
awake: ReadonlyMap<string, AwakeContextState>,
now = Date.now(),
): ContextListing[] {
this.#table();
const limit = Math.min(Math.max(1, options.limit ?? 200), 1_000);
const terms = (options.search || "").toLowerCase().split(/\s+/).filter(Boolean);
const under = options.under && options.under !== "/" ? options.under : undefined;
const matchesPath = (path: string) => !under || path === under || path.startsWith(`${under}/`);
const where: string[] = [];
const bindings: (string | number)[] = [];
if (under) {
where.push("(path = ? OR substr(path, 1, ?) = ?)");
bindings.push(under, under.length + 1, `${under}/`);
}
for (const term of terms) {
where.push("(instr(lower(path), ?) > 0 OR instr(lower(summary), ?) > 0)");
bindings.push(term, term);
}
if (options.awake) {
const paths = [...awake.keys()].filter(matchesPath);
if (paths.length === 0) return [];
where.push(`path IN (SELECT value FROM json_each(?))`);
bindings.push(JSON.stringify(paths));
}
const rows = this.#sql
.exec<{ path: string; summary: string }>(
`SELECT path, summary FROM context_catalog
${where.length ? `WHERE ${where.join(" AND ")}` : ""}
ORDER BY active_at DESC, path LIMIT ?`,
...bindings,
limit,
)
.toArray();
const toListing = (path: string, summaryJson: string): ContextListing => {
const { throughOffset: _, ...stored } = StoredContextSummary.parse(JSON.parse(summaryJson));
const awakeState = awake.get(path);
if (!awakeState) return { path, ...stored };
const liveActivities = activeContextActivities(awakeState.activities, now);
return {
path,
...stored,
awake: { since: awakeState.since },
...(liveActivities.length > 0 && { activities: liveActivities }),
};
};
const toUnlistedAwake = (path: string): ContextListing => {
const awakeState = awake.get(path);
const liveActivities = activeContextActivities(awakeState?.activities, now);
return {
path,
awake: { since: awakeState?.since ?? now },
eventCount: 0,
eventTypes: {},
recentTypes: [],
processors: [],
...(liveActivities.length > 0 && { activities: liveActivities }),
};
};
const listed = rows.map(({ path, summary }) => toListing(path, summary));
const candidates = [...awake.keys()].filter(matchesPath);
if (candidates.length === 0) return listed;
const storedRows = this.#sql
.exec<{ path: string; summary: string }>(
"SELECT path, summary FROM context_catalog WHERE path IN (SELECT value FROM json_each(?))",
JSON.stringify(candidates),
)
.toArray();
const storedByPath = new Map(storedRows.map((row) => [row.path, row.summary]));
if (terms.length > 0) {
const listedPaths = new Set(listed.map((row) => row.path));
const extraAwakeMatches: ContextListing[] = [];
for (const path of candidates) {
if (listedPaths.has(path)) continue;
const storedSummary = storedByPath.get(path);
const candidateListing = storedSummary
? toListing(path, storedSummary)
: toUnlistedAwake(path);
const haystack = [
path,
storedSummary || "",
...(candidateListing.activities || []).flatMap((item) => [
item.text,
item.key,
...(item.tags || []),
]),
]
.join(" ")
.toLowerCase();
if (terms.every((term) => haystack.includes(term))) {
extraAwakeMatches.push(candidateListing);
}
}
return [...extraAwakeMatches, ...listed].slice(0, limit);
}
// An awake context with no row at all: one that has a row past this page is not one.
const unlisted = candidates.filter((path) => !storedByPath.has(path)).map(toUnlistedAwake);
return [...unlisted, ...listed].slice(0, limit);
}
}
/** A context's row as a watched query serves it (iterate/api `CatalogRow`): its listing, and the
* two times that the orders sort by, as numbers. The order `active` is this table's own order:
* the newest activity, or the newest event without one. A context with no time yet sorts last. */
function watchedRowOf(
path: string,
{ throughOffset: _, ...listing }: z.infer<typeof StoredContextSummary>,
): CatalogRowInput {
return {
path,
activeMs: Date.parse(listing.lastActivityAt || listing.lastEventAt || "") || 0,
createdMs: Date.parse(listing.createdAt || "") || 0,
...listing,
};
}