diff --git a/robot-docs/agent-sessions.md b/robot-docs/agent-sessions.md index 266068179..1e9548bb9 100644 --- a/robot-docs/agent-sessions.md +++ b/robot-docs/agent-sessions.md @@ -101,12 +101,16 @@ or replace a verifier whose condition is no longer relevant. The `subagents` settings section defaults to 12 open children per parent and three generations below a root. `-1` removes either cap. The declarations are in [`family.limits_section`](../src/albedo/daemon/family.gleam), and -`family.link_in` enforces them when it records a child. Family tables and the -reads extensions use belong to -[`sdk/family`](../src/albedo/harness/sdk/family.gleam). Remote lookups check the -family's own `family_remote` stubs first. Correspondents, sessions on another -daemon that wrote to the caller, come from mail storage, so the daemon supplies -that lookup as `family.Correspondents` +`family.link_in` enforces them when it records a child. The family tables, their +schema, and the statements on them belong to +[`daemon/family`](../src/albedo/daemon/family.gleam), which the daemon +initialises at startup. The reads and writes extensions use, with their +capability checks, are in +[`sdk/family`](../src/albedo/harness/sdk/family.gleam), which calls core's +operations ([core table operations](extensions.md#core-table-operations)). +Remote lookups check the family's own `family_remote` stubs first. +Correspondents, sessions on another daemon that wrote to the caller, come from +mail storage, so the daemon supplies that lookup as `family.Correspondents` ([`family.correspondents`](../src/albedo/daemon/family.gleam)). Use mail by session ID to reach another session rather than adding nesting solely for communication. diff --git a/robot-docs/extensions.md b/robot-docs/extensions.md index 0e8bf6b66..bc6afd61a 100644 --- a/robot-docs/extensions.md +++ b/robot-docs/extensions.md @@ -706,7 +706,17 @@ operations that core injects. The contract is [`sdk/core_tables`](../src/albedo/harness/sdk/core_tables.gleam): one record of operations per area, gathered in `core_tables.Tables`. Core implements each area's record next to that area's schema, and -[`daemon/core_store`](../src/albedo/daemon/core_store.gleam) gathers them. +[`daemon/core_store`](../src/albedo/daemon/core_store.gleam) gathers them: + +- `Sessions`, the reads of `sdk/sessions` on the `sessions` table: + [`daemon/session_rows`](../src/albedo/daemon/session_rows.gleam). The values + they answer, such as `Names`, `Summary`, and `Standing`, are typed in + [`sdk/session_info`](../src/albedo/harness/sdk/session_info.gleam), below the + store, and `sdk/sessions` names them by alias. +- `Family`, the reads and writes of `sdk/family` on `session_family`, + `family_remote`, and the family columns of `sessions`: + [`daemon/family`](../src/albedo/daemon/family.gleam), which also creates those + tables. The operations reach SDK code through the storage handle, so there is one path. The host starts the store with the record (`store.start(path, schema, tables)`), @@ -1019,13 +1029,12 @@ which only a transcript row names. Finding another session's ID still takes a A callback that holds a connection, from `storage.snapshot`, `storage.query`, or a plugin that receives one, has no handle to check. So the SDK readers that take a connection and read a session's stored data are core's alone: -`history.read_info` and `snapshot_in`, `request_history.last_turn_in`, -`family.member_in`, and `journal.traces_in` and `session_hashes`. The -`albedo_extension_raw_store` lint refuses them in extension code. Read through -their handle-taking counterparts, such as `history.get`, `history.snapshot`, or -`request_history.last_turn`. A transfer export collects the images that the -moved sessions' kernel cells name in core (`transfers.cell_images`), not in the -kernel's transfer adapter. +`history.read_info` and `snapshot_in`, `request_history.last_turn_in`, and +`journal.traces_in` and `session_hashes`. The `albedo_extension_raw_store` lint +refuses them in extension code. Read through their handle-taking counterparts, +such as `history.get`, `history.snapshot`, or `request_history.last_turn`. A +transfer export collects the images that the moved sessions' kernel cells name +in core (`transfers.cell_images`), not in the kernel's transfer adapter. `History` is separate from `Sessions` so that discovery does not imply reading. `links` and `worktrees` list sessions and their metadata but read no transcript. @@ -1181,10 +1190,11 @@ typed session operations. It also defines the coordinator's `Op` and `Reply` values, which only the daemon's session registry handles. Each typed function takes the `session_control.Control` that `host.Context.control` and `extension.Daemon.control` carry. See [agents](agent-sessions.md). -[`sdk/family`](../src/albedo/harness/sdk/family.gleam) owns the family tables, -their schema, and the extension-facing family operations. -[`daemon/family`](../src/albedo/daemon/family.gleam) keeps child admission, -limits, closing, family revisions, remote stubs, and deletion claims. +[`sdk/family`](../src/albedo/harness/sdk/family.gleam) owns the extension-facing +family operations and their capability checks. +[`daemon/family`](../src/albedo/daemon/family.gleam) owns the family tables, +their schema, and every statement on them, and keeps child admission, limits, +closing, family revisions, remote stubs, and deletion claims. [`sdk/session_identity`](../src/albedo/harness/sdk/session_identity.gleam) owns the shared family member, address, stub, and local or remote destination values. `host.Context.ask_moved`, `extension.Daemon.tell_refused`, and diff --git a/robot-docs/gleam-lint.md b/robot-docs/gleam-lint.md index 982f5fe5d..fdc0a1595 100644 --- a/robot-docs/gleam-lint.md +++ b/robot-docs/gleam-lint.md @@ -184,10 +184,10 @@ reads modules under `src/albedo/harness/extensions`. It flags an import of operations over core's tables. It flags the SDK readers that take a raw connection and read a session's stored data, which its `host_readers` constant lists: `history.read_info` and `snapshot_in`, `request_history.last_turn_in`, -`family.member_in`, and `journal.traces_in` and `session_hashes`. No capability -check covers a connection. It finds them all qualified under any alias, imported -unqualified, or referenced as a value. It flags `owned_by` from that module -outside the public feature modules that +and `journal.traces_in` and `session_hashes`. No capability check covers a +connection. It finds them all qualified under any alias, imported unqualified, +or referenced as a value. It flags `owned_by` from that module outside the +public feature modules that [`extension_cross_import`](../tools/lint/src/albedo_lint/rules/extension_cross_import.gleam) lists. diff --git a/src/albedo/daemon/core_store.gleam b/src/albedo/daemon/core_store.gleam index 85434cdf2..fd6f0c82c 100644 --- a/src/albedo/daemon/core_store.gleam +++ b/src/albedo/daemon/core_store.gleam @@ -2,9 +2,14 @@ //// with (`sdk/core_tables`). Each area's operations live with that area's //// schema and migrations; this module only gathers them. +import albedo/daemon/family +import albedo/daemon/session_rows import albedo/harness/sdk/core_tables /// The operations every store the daemon starts carries. pub fn tables() -> core_tables.Tables { - core_tables.Tables + core_tables.Tables( + sessions: session_rows.operations(), + family: family.operations(), + ) } diff --git a/src/albedo/daemon/family.gleam b/src/albedo/daemon/family.gleam index b9ffb66f8..1fdf42ebc 100644 --- a/src/albedo/daemon/family.gleam +++ b/src/albedo/daemon/family.gleam @@ -1,10 +1,15 @@ -//// The daemon's side of session families: admission and limits for new +//// The daemon's side of session families: the `session_family` and +//// `family_remote` tables and their schema, admission and limits for new //// children, closing, the revision a family change bumps, remote stubs, and -//// deletion claims. The tables, their schema and the reads extensions use -//// belong to `sdk/family`. +//// deletion claims. The reads and writes extensions use run here too +//// (`operations`); `sdk/family` checks a caller's capabilities before it +//// calls them and resolves addresses over them. +import albedo/harness/sdk/core_tables import albedo/harness/sdk/family as families -import albedo/harness/sdk/session_identity.{type Stub, Member} as identity +import albedo/harness/sdk/session_identity.{ + type Address, type Stub, Address, Member, Stub, +} as identity import albedo_clock import albedo/daemon/operations @@ -32,6 +37,232 @@ pub type Facts { ) } +const schema = + " +CREATE TABLE IF NOT EXISTS session_family ( + session TEXT PRIMARY KEY REFERENCES sessions(id) ON DELETE CASCADE, + parent TEXT NOT NULL REFERENCES sessions(id), + name TEXT NOT NULL, + depth INTEGER NOT NULL CHECK(depth >= 1), + created_at INTEGER NOT NULL, + closed_at INTEGER, + UNIQUE(parent,name) +); +CREATE INDEX IF NOT EXISTS session_family_parent ON session_family(parent); +CREATE TABLE IF NOT EXISTS family_remote ( + session TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE, + member TEXT NOT NULL, + relation TEXT NOT NULL CHECK(relation IN ('parent','child','sibling')), + name TEXT NOT NULL, + peer TEXT NOT NULL, + host TEXT NOT NULL, + PRIMARY KEY(session,member) +) WITHOUT ROWID; +" + +/// Create or upgrade the family tables. Used during installation, not for +/// each family lookup. Storage failures propagate. +pub fn initialise(db: store.Store) -> Result(Nil, String) { + store.query(db, fn(connection) { + store.transaction(connection, fn() { + use _ <- result.try(store.exec(connection, schema)) + store.add_columns(connection, "session_family", [ + #("verifier", "TEXT NOT NULL DEFAULT ''"), + ]) + }) + }) +} + +/// The reads and writes `core_store.tables` gathers for `sdk/family`. +pub fn operations() -> core_tables.Family { + core_tables.Family( + member: member_in, + children:, + named:, + address:, + remote:, + started_by:, + role:, + set_role:, + verifier:, + set_verifier:, + peer_closed:, + ) +} + +const columns = "session,parent,name,depth,closed_at IS NOT NULL" + +/// Every member query: the shared column list, with the given filter. +fn members(filter: String) -> String { + "SELECT " <> columns <> " FROM session_family " <> filter +} + +fn member_decoder() -> decode.Decoder(Member) { + use session <- decode.field(0, decode.string) + use parent <- decode.field(1, decode.string) + use name <- decode.field(2, decode.string) + use depth <- decode.field(3, decode.int) + use closed <- decode.field(4, sqlight.decode_bool()) + decode.success(Member(session, parent, name, depth, closed)) +} + +fn member_rows( + connection: sqlight.Connection, + sql: String, + args: List(sqlight.Value), +) -> Result(List(Member), String) { + store.rows(connection, sql, args, member_decoder()) +} + +/// A child's family membership on the caller's connection, or `None` for a +/// root. +pub fn member_in( + connection: sqlight.Connection, + session: String, +) -> Result(Option(Member), String) { + member_rows(connection, members("WHERE session=?"), [sqlight.text(session)]) + |> result.map(fn(found) { list.first(found) |> option.from_result }) +} + +fn children( + connection: sqlight.Connection, + parent: String, +) -> Result(List(Member), String) { + member_rows(connection, members("WHERE parent=? ORDER BY created_at"), [ + sqlight.text(parent), + ]) +} + +fn named( + connection: sqlight.Connection, + parent: String, + name: String, +) -> Result(List(Member), String) { + member_rows(connection, members("WHERE parent=? AND name=?"), [ + sqlight.text(parent), + sqlight.text(name), + ]) +} + +fn address( + connection: sqlight.Connection, + session: String, +) -> Result(Option(Address), String) { + store.rows( + connection, + "SELECT s.id,COALESCE(f.name,NULLIF(s.name,''),s.title) FROM sessions s LEFT JOIN session_family f ON f.session=s.id WHERE s.id=?", + [sqlight.text(session)], + { + use id <- decode.field(0, decode.string) + use name <- decode.field(1, decode.string) + decode.success(Address(id, name)) + }, + ) + |> result.map(fn(found) { list.first(found) |> option.from_result }) +} + +fn remote( + connection: sqlight.Connection, + caller: String, + to: String, +) -> Result(Option(Stub), String) { + store.rows( + connection, + "SELECT member,relation,name,peer,host FROM family_remote WHERE session=?1 AND (member=?2 OR (?2='parent' AND relation='parent') OR name=?2) LIMIT 1", + [sqlight.text(caller), sqlight.text(to)], + { + use member <- decode.field(0, decode.string) + use relation <- decode.field(1, decode.string) + use name <- decode.field(2, decode.string) + use peer <- decode.field(3, decode.string) + use host <- decode.field(4, decode.string) + decode.success(Stub(member, relation, name, peer, host)) + }, + ) + |> result.map(fn(found) { list.first(found) |> option.from_result }) +} + +fn started_by( + connection: sqlight.Connection, + session: String, +) -> Result(Option(String), String) { + store.rows( + connection, + "SELECT started_by FROM sessions WHERE id=?", + [sqlight.text(session)], + decode.field(0, decode.optional(decode.string), decode.success), + ) + |> result.map(fn(rows) { + list.first(rows) |> option.from_result |> option.flatten + }) +} + +fn role( + connection: sqlight.Connection, + session: String, +) -> Result(Option(String), String) { + store.rows( + connection, + "SELECT role FROM sessions WHERE id=?", + [sqlight.text(session)], + decode.field(0, decode.string, decode.success), + ) + |> result.map(fn(rows) { list.first(rows) |> option.from_result }) +} + +fn set_role( + connection: sqlight.Connection, + session: String, + value: String, +) -> Result(Nil, String) { + store.run(connection, "UPDATE sessions SET role=? WHERE id=?", [ + sqlight.text(value), + sqlight.text(session), + ]) +} + +fn verifier( + connection: sqlight.Connection, + session: String, +) -> Result(Option(#(String, String)), String) { + store.rows( + connection, + "SELECT COALESCE(s.desired_workspace,s.cwd),f.verifier FROM session_family f JOIN sessions s ON s.id=f.session WHERE f.session=? AND f.verifier<>''", + [sqlight.text(session)], + { + use cwd <- decode.field(0, decode.string) + use command <- decode.field(1, decode.string) + decode.success(#(cwd, command)) + }, + ) + |> result.map(fn(found) { list.first(found) |> option.from_result }) +} + +fn set_verifier( + connection: sqlight.Connection, + session: String, + command: String, +) -> Result(Nil, String) { + store.run(connection, "UPDATE session_family SET verifier=? WHERE session=?", [ + sqlight.text(command), + sqlight.text(session), + ]) +} + +fn peer_closed( + connection: sqlight.Connection, + session: String, +) -> Result(Bool, String) { + store.one( + connection, + "SELECT peer_closed_at IS NOT NULL FROM sessions WHERE id=?", + [sqlight.text(session)], + decode.field(0, decode.int, decode.success), + "session not found", + ) + |> result.map(fn(value) { value == 1 }) +} + /// The `families.Correspondents` lookup over this daemon's mail: a session on /// another daemon, named by its ID, that wrote to the caller. pub fn correspondents(db: store.Store) -> families.Correspondents { @@ -41,7 +272,7 @@ pub fn correspondents(db: store.Store) -> families.Correspondents { use name <- decode.field(1, decode.string) use peer <- decode.field(2, decode.string) use host <- decode.field(3, decode.string) - decode.success(identity.Stub(member, "", name, peer, host)) + decode.success(Stub(member, "", name, peer, host)) } store.read( db, @@ -66,7 +297,7 @@ pub fn capture_in( decode.field(0, decode.int, decode.success), "family root not found", )) - use member <- result.try(families.member_in(connection, session)) + use member <- result.try(member_in(connection, session)) let role = store.one( connection, @@ -193,9 +424,7 @@ pub fn link_in( refusal.ChildAddressInvalid(families.name_text(failure)) }), ) - use above <- result.try( - refusal.storage(families.member_in(connection, parent)), - ) + use above <- result.try(refusal.storage(member_in(connection, parent))) let depth = case above { Some(member) -> member.depth + 1 None -> 1 @@ -249,7 +478,7 @@ pub fn link_in( pub fn close(db: store.Store, session: String) -> Result(Nil, String) { store.query(db, fn(connection) { store.transaction(connection, fn() { - use found <- result.try(families.member_in(connection, session)) + use found <- result.try(member_in(connection, session)) case found { Some(Member(closed: False, ..)) -> { use _ <- result.try( diff --git a/src/albedo/daemon/registry.gleam b/src/albedo/daemon/registry.gleam index 8268c046a..b1c7c1b2f 100644 --- a/src/albedo/daemon/registry.gleam +++ b/src/albedo/daemon/registry.gleam @@ -647,7 +647,7 @@ pub fn prepare_storage( ) use _ <- result.try(quota.initialise(ledger)) use _ <- result.try(mail.initialise(ledger)) - use _ <- result.try(families.initialise(storage.host(ledger))) + use _ <- result.try(family.initialise(ledger)) use _ <- result.try(worktree.initialise(storage.host(ledger))) use _ <- result.try(transfers.initialise(ledger)) moves.initialise(ledger) diff --git a/src/albedo/daemon/session_rows.gleam b/src/albedo/daemon/session_rows.gleam new file mode 100644 index 000000000..b783b9868 --- /dev/null +++ b/src/albedo/daemon/session_rows.gleam @@ -0,0 +1,138 @@ +//// The reads of the `sessions` table that core injects for the SDK +//// (`core_tables.Sessions`). `albedo/daemon/conversation` owns the table's +//// schema and migrations; `sdk/sessions` checks a caller's capabilities +//// before it calls these. + +import albedo/harness/sdk/core_tables +import albedo/harness/sdk/session_info.{ + type Names, type Standing, type Summary, Names, Standing, Summary, +} +import albedo/harness/sdk/store +import gleam/dynamic/decode +import gleam/json +import gleam/list +import gleam/option.{type Option} +import gleam/result +import gleam/set.{type Set} +import sqlight + +/// The operations `core_store.tables` gathers. +pub fn operations() -> core_tables.Sessions { + core_tables.Sessions( + names:, + summaries:, + find:, + all:, + occupied:, + standing:, + importing:, + ) +} + +const summary_select = + "SELECT s.id,COALESCE(s.name,''),f.name,s.title,s.cwd,COALESCE(s.desired_workspace,s.cwd) FROM sessions s LEFT JOIN session_family f ON f.session=s.id" + +fn names_decoder() -> decode.Decoder(Names) { + use id <- decode.field(0, decode.string) + use given <- decode.field(1, decode.string) + use family <- decode.field(2, decode.optional(decode.string)) + use title <- decode.field(3, decode.string) + decode.success(Names(id, given, family, title)) +} + +fn summary_decoder() -> decode.Decoder(Summary) { + use names <- decode.then(names_decoder()) + use cwd <- decode.field(4, decode.string) + use workspace <- decode.field(5, decode.string) + decode.success(Summary(names, cwd, workspace)) +} + +fn id_list(ids: List(String)) -> sqlight.Value { + sqlight.text(json.to_string(json.array(ids, json.string))) +} + +fn names( + db: sqlight.Connection, + ids: List(String), +) -> Result(List(Names), String) { + store.rows( + db, + "SELECT s.id,COALESCE(s.name,''),f.name,s.title FROM sessions s LEFT JOIN session_family f ON f.session=s.id WHERE s.id IN (SELECT value FROM json_each(?))", + [id_list(ids)], + names_decoder(), + ) +} + +fn summaries( + db: sqlight.Connection, + ids: List(String), +) -> Result(List(Summary), String) { + store.rows( + db, + summary_select <> " WHERE s.id IN (SELECT value FROM json_each(?))", + [id_list(ids)], + summary_decoder(), + ) +} + +fn find( + db: sqlight.Connection, + key: String, +) -> Result(Option(Summary), String) { + store.rows( + db, + summary_select + <> " WHERE s.id=?1 OR f.name=?1 OR s.name=?1 OR s.title=?1 ORDER BY s.id=?1 DESC,s.activity_seq DESC,s.rowid DESC LIMIT 1", + [sqlight.text(key)], + summary_decoder(), + ) + |> result.map(fn(found) { list.first(found) |> option.from_result }) +} + +fn all(db: sqlight.Connection) -> Result(List(Summary), String) { + store.rows(db, summary_select, [], summary_decoder()) +} + +fn occupied(db: sqlight.Connection, workspace: String) -> Result(Bool, String) { + store.rows( + db, + "SELECT 1 FROM sessions WHERE cwd=?1 OR desired_workspace=?1 LIMIT 1", + [sqlight.text(workspace)], + decode.field(0, decode.int, decode.success), + ) + |> result.map(fn(found) { found != [] }) +} + +fn standing( + db: sqlight.Connection, + session: String, +) -> Result(Option(Standing), String) { + store.rows( + db, + "SELECT stage=" + <> session_info.stage_sql(session_info.Importing) + <> ",moved IS NOT NULL FROM sessions WHERE id=?", + [sqlight.text(session)], + { + use importing <- decode.field(0, sqlight.decode_bool()) + use moved <- decode.field(1, sqlight.decode_bool()) + decode.success(Standing(importing, moved)) + }, + ) + |> result.map(fn(found) { list.first(found) |> option.from_result }) +} + +fn importing( + db: sqlight.Connection, + ids: List(String), +) -> Result(Set(String), String) { + store.rows( + db, + "SELECT id FROM sessions WHERE stage=" + <> session_info.stage_sql(session_info.Importing) + <> " AND id IN (SELECT value FROM json_each(?))", + [id_list(ids)], + decode.field(0, decode.string, decode.success), + ) + |> result.map(set.from_list) +} diff --git a/src/albedo/harness/extensions/agents/extension.gleam b/src/albedo/harness/extensions/agents/extension.gleam index 84d70ae3b..69d44f62f 100644 --- a/src/albedo/harness/extensions/agents/extension.gleam +++ b/src/albedo/harness/extensions/agents/extension.gleam @@ -66,8 +66,8 @@ pub fn extension() -> extension.Extension { // The hook is cheap for roots with no requests and keeps prompt context stable. extension.OwnedManagedPlugin(prepare), ], - // Its tables, whether or not the daemon made them first. - extension.storage_initialise(fn(scope) { family.initialise(scope) }), + // The family tables are core's: the daemon creates them. + extension.no_initialise, tables: [], uses:, ) diff --git a/src/albedo/harness/extensions/webhooks/ledger.gleam b/src/albedo/harness/extensions/webhooks/ledger.gleam index baa542a37..c667630af 100644 --- a/src/albedo/harness/extensions/webhooks/ledger.gleam +++ b/src/albedo/harness/extensions/webhooks/ledger.gleam @@ -4,6 +4,7 @@ import albedo/harness/sdk/letter as mail_value import albedo/harness/sdk/input import albedo/harness/sdk/mail +import albedo/harness/sdk/session_info import albedo/harness/sdk/sessions import albedo/harness/sdk/storage import albedo/harness/sdk/transfer @@ -1262,7 +1263,7 @@ pub fn accept( |> result.map_error(Storage), ) let #(importing, session_moved) = case standing { - Some(sessions.Standing(importing:, moved:)) -> #(importing, moved) + Some(session_info.Standing(importing:, moved:)) -> #(importing, moved) None -> #(False, False) } use <- bool.guard(importing, Error(NotFound)) diff --git a/src/albedo/harness/sdk/core_tables.gleam b/src/albedo/harness/sdk/core_tables.gleam index dede78e9d..c01c18d45 100644 --- a/src/albedo/harness/sdk/core_tables.gleam +++ b/src/albedo/harness/sdk/core_tables.gleam @@ -14,9 +14,76 @@ //// //// Every operation takes the connection to run on, so the SDK function //// that calls several of them in one snapshot or transaction keeps them in -//// it, and an error is the store's message as a `String`. +//// it, and an error is the store's message as a `String`. The values they +//// answer are typed in modules below the store, such as `sdk/session_info` +//// and `sdk/session_identity`, so the SDK modules that hand them on can +//// name them. + +import albedo/harness/sdk/session_identity.{type Address, type Member, type Stub} +import albedo/harness/sdk/session_info.{type Names, type Standing, type Summary} +import gleam/option.{type Option} +import gleam/set.{type Set} +import sqlight.{type Connection} /// Every record of operations core injects. pub type Tables { - Tables + Tables(sessions: Sessions, family: Family) +} + +/// Reads of core's `sessions` table, with each child's family name. +pub type Sessions { + Sessions( + /// The names of each listed session that has a row, in no particular + /// order. + names: fn(Connection, List(String)) -> Result(List(Names), String), + /// The names and workspace of each listed session that has a row, in + /// no particular order. + summaries: fn(Connection, List(String)) -> Result(List(Summary), String), + /// The session with the ID `key`, else the most recently active one + /// whose family name, given name, or title is `key`. + find: fn(Connection, String) -> Result(Option(Summary), String), + /// Every session, importing ones included, in storage order. + all: fn(Connection) -> Result(List(Summary), String), + /// Whether any session works in the workspace, as its `cwd` or the + /// workspace it asked to move to. + occupied: fn(Connection, String) -> Result(Bool, String), + /// The session's standing, or `None` when it has no row. + standing: fn(Connection, String) -> Result(Option(Standing), String), + /// Those of the listed sessions a transfer is still importing. + importing: fn(Connection, List(String)) -> Result(Set(String), String), + ) +} + +/// Reads and writes of core's family tables, `session_family` and +/// `family_remote`, and the family facts the `sessions` table keeps. +pub type Family { + Family( + /// The session's family membership, or `None` for a root. + member: fn(Connection, String) -> Result(Option(Member), String), + /// The parent's children in creation order, closed ones included. + children: fn(Connection, String) -> Result(List(Member), String), + /// The children of the parent (the second argument) named the third. + named: fn(Connection, String, String) -> Result(List(Member), String), + /// The session's ID with its family name, else the name it was given, + /// else its title; `None` when it has no row. + address: fn(Connection, String) -> Result(Option(Address), String), + /// The remote relative of the caller (the second argument) a move left + /// behind, named by the third: "parent", its name, or its ID. + remote: fn(Connection, String, String) -> Result(Option(Stub), String), + /// The session that started the session as a peer, if it recorded one. + started_by: fn(Connection, String) -> Result(Option(String), String), + /// The session's role metadata, or `None` when it has no row. + role: fn(Connection, String) -> Result(Option(String), String), + /// Saves the role (the third argument) of an existing session. + set_role: fn(Connection, String, String) -> Result(Nil, String), + /// The child's effective workspace and verifier command, when it + /// records a nonempty one. + verifier: fn(Connection, String) -> + Result(Option(#(String, String)), String), + /// Saves a child's verifier command (the third argument). + set_verifier: fn(Connection, String, String) -> Result(Nil, String), + /// Whether a session outside every family was closed as a peer; the + /// error `session not found` when it has no row. + peer_closed: fn(Connection, String) -> Result(Bool, String), + ) } diff --git a/src/albedo/harness/sdk/family.gleam b/src/albedo/harness/sdk/family.gleam index c69342c44..f9e9b90a4 100644 --- a/src/albedo/harness/sdk/family.gleam +++ b/src/albedo/harness/sdk/family.gleam @@ -4,14 +4,16 @@ //// a family: anything further away is addressed by session id, so a title //// shared by two unrelated sessions cannot misdirect. //// -//// The daemon keeps family admission, limits, closing, revisions, remote stubs -//// and deletion claims in `albedo/daemon/family`, on these same tables. +//// The daemon keeps the family tables, their schema, admission, limits, +//// closing, revisions, remote stubs and deletion claims in +//// `albedo/daemon/family`, which runs every statement on them +//// (`core_tables.Family`). This module checks the caller's capabilities and +//// resolves addresses over those reads. -import albedo/harness/sdk/session_identity.{Address, Here, Member, Stub, There} as identity +import albedo/harness/sdk/core_tables +import albedo/harness/sdk/session_identity.{Address, Here, There} as identity import albedo/harness/sdk/storage -import albedo/harness/sdk/store import albedo_bytes/scalars -import gleam/dynamic/decode import gleam/list import gleam/option.{type Option, None, Some} import gleam/result @@ -21,76 +23,8 @@ import sqlight /// This module's path, as `capability.functions` names it. const module = "albedo/harness/sdk/family" -const schema = - " -CREATE TABLE IF NOT EXISTS session_family ( - session TEXT PRIMARY KEY REFERENCES sessions(id) ON DELETE CASCADE, - parent TEXT NOT NULL REFERENCES sessions(id), - name TEXT NOT NULL, - depth INTEGER NOT NULL CHECK(depth >= 1), - created_at INTEGER NOT NULL, - closed_at INTEGER, - UNIQUE(parent,name) -); -CREATE INDEX IF NOT EXISTS session_family_parent ON session_family(parent); -CREATE TABLE IF NOT EXISTS family_remote ( - session TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE, - member TEXT NOT NULL, - relation TEXT NOT NULL CHECK(relation IN ('parent','child','sibling')), - name TEXT NOT NULL, - peer TEXT NOT NULL, - host TEXT NOT NULL, - PRIMARY KEY(session,member) -) WITHOUT ROWID; -" - -/// Create or upgrade the family tables. Used during installation, not for -/// each family lookup. Storage failures propagate. -pub fn initialise(db: storage.Storage) -> Result(Nil, String) { - storage.query(db, fn(connection) { - store.transaction(connection, fn() { - use _ <- result.try(store.exec(connection, schema)) - store.add_columns(connection, "session_family", [ - #("verifier", "TEXT NOT NULL DEFAULT ''"), - ]) - }) - }) -} - -const columns = "session,parent,name,depth,closed_at IS NOT NULL" - -/// Every member query: the shared column list, with the given filter. -fn members(filter: String) -> String { - "SELECT " <> columns <> " FROM session_family " <> filter -} - -fn decoder() -> decode.Decoder(identity.Member) { - use session <- decode.field(0, decode.string) - use parent <- decode.field(1, decode.string) - use name <- decode.field(2, decode.string) - use depth <- decode.field(3, decode.int) - use closed <- decode.field(4, sqlight.decode_bool()) - decode.success(Member(session, parent, name, depth, closed)) -} - -fn rows( - connection: sqlight.Connection, - sql: String, - args: List(sqlight.Value), -) -> Result(List(identity.Member), String) { - store.rows(connection, sql, args, decoder()) -} - -/// `get` on the caller's connection, for reads inside its transaction. -/// Only core calls this: it reads through a raw connection, which no -/// capability check covers, so the `albedo_extension_raw_store` lint -/// refuses it in extension code. -pub fn member_in( - connection: sqlight.Connection, - session: String, -) -> Result(Option(identity.Member), String) { - rows(connection, members("WHERE session=?"), [sqlight.text(session)]) - |> result.map(fn(found) { list.first(found) |> option.from_result }) +fn rows(db: storage.Storage) -> core_tables.Family { + storage.core(db).family } /// Why a family address is refused. @@ -144,15 +78,7 @@ fn initiator( db: storage.Storage, session: String, ) -> Result(Option(String), String) { - storage.read( - db, - "SELECT started_by FROM sessions WHERE id=?", - [sqlight.text(session)], - decode.field(0, decode.optional(decode.string), decode.success), - ) - |> result.map(fn(rows) { - list.first(rows) |> option.from_result |> option.flatten - }) + storage.reading(db, rows(db).started_by(_, session)) } /// Whether `caller` is the target's parent or started it as a peer, so it may @@ -167,13 +93,8 @@ pub fn owns(db: storage.Storage, caller: String, target: String) -> Bool { /// Read free-form role metadata, or an empty string if the session is absent. pub fn role(db: storage.Storage, session: String) -> Result(String, String) { - storage.read( - db, - "SELECT role FROM sessions WHERE id=?", - [sqlight.text(session)], - decode.field(0, decode.string, decode.success), - ) - |> result.map(fn(rows) { list.first(rows) |> result.unwrap("") }) + storage.reading(db, rows(db).role(_, session)) + |> result.map(option.unwrap(_, "")) } /// Trim and save role metadata. Reject more than 512 Unicode scalar values. @@ -187,11 +108,7 @@ pub fn set_role( let value = string.trim(value) case scalars.length(value) <= 512 { False -> Error("role is at most 512 characters") - True -> - storage.write(db, "UPDATE sessions SET role=? WHERE id=?", [ - sqlight.text(value), - sqlight.text(session), - ]) + True -> storage.writing(db, rows(db).set_role(_, session, value)) } } @@ -212,13 +129,7 @@ fn member( db: storage.Storage, session: String, ) -> Result(Option(identity.Member), String) { - storage.read( - db, - members("WHERE session=?"), - [sqlight.text(session)], - decoder(), - ) - |> result.map(fn(found) { list.first(found) |> option.from_result }) + storage.reading(db, rows(db).member(_, session)) } /// Trim and save the command that must pass before a child answers its parent. @@ -232,11 +143,7 @@ pub fn set_verifier( let command = string.trim(command) case scalars.length(command) <= 4096 { False -> Error("verifier is at most 4096 characters") - True -> - storage.write(db, "UPDATE session_family SET verifier=? WHERE session=?", [ - sqlight.text(command), - sqlight.text(session), - ]) + True -> storage.writing(db, rows(db).set_verifier(_, session, command)) } } @@ -246,17 +153,7 @@ pub fn verifier( db: storage.Storage, session: String, ) -> Result(Option(#(String, String)), String) { - storage.read( - db, - "SELECT COALESCE(s.desired_workspace,s.cwd),f.verifier FROM session_family f JOIN sessions s ON s.id=f.session WHERE f.session=? AND f.verifier<>''", - [sqlight.text(session)], - { - use cwd <- decode.field(0, decode.string) - use command <- decode.field(1, decode.string) - decode.success(#(cwd, command)) - }, - ) - |> result.map(fn(found) { list.first(found) |> option.from_result }) + storage.reading(db, rows(db).verifier(_, session)) } /// List a parent's children in creation order, including closed children. @@ -265,29 +162,14 @@ pub fn children( parent: String, ) -> Result(List(identity.Member), String) { use _ <- result.try(storage.permit(db, module, "children")) - storage.read( - db, - members("WHERE parent=? ORDER BY created_at"), - [sqlight.text(parent)], - decoder(), - ) + storage.reading(db, rows(db).children(_, parent)) } /// Read whether a child or peer is closed. A missing session returns an error. pub fn closed(db: storage.Storage, session: String) -> Result(Bool, String) { case member(db, session) { Ok(Some(member)) -> Ok(member.closed) - Ok(None) -> - storage.snapshot(db, fn(connection) { - store.one( - connection, - "SELECT peer_closed_at IS NOT NULL FROM sessions WHERE id=?", - [sqlight.text(session)], - decode.field(0, decode.int, decode.success), - "session not found", - ) - |> result.map(fn(value) { value == 1 }) - }) + Ok(None) -> storage.snapshot(db, rows(db).peer_closed(_, session)) Error(error) -> Error(error) } } @@ -310,49 +192,29 @@ fn resolved( caller: String, to: String, ) -> Result(identity.Address, String) { + let family = rows(db) storage.snapshot(db, fn(connection) { - use me <- result.try( - rows(connection, members("WHERE session=?"), [sqlight.text(caller)]), - ) - let parent = case me { - [member] -> Some(member.parent) - _ -> None - } - use by_id <- result.try( - store.rows( - connection, - "SELECT s.id,COALESCE(f.name,NULLIF(s.name,''),s.title) FROM sessions s LEFT JOIN session_family f ON f.session=s.id WHERE s.id=?", - [sqlight.text(to)], - { - use id <- decode.field(0, decode.string) - use name <- decode.field(1, decode.string) - decode.success(Address(id, name)) - }, - ), - ) + use me <- result.try(family.member(connection, caller)) + let parent = option.map(me, fn(member) { member.parent }) + use by_id <- result.try(family.address(connection, to)) case to, parent, by_id { "parent", None, _ -> Error("this session has no parent; it is a root") - "parent", Some(id), _ -> Ok(Address(id, parent_name(connection, id))) - _, _, [address] -> Ok(address) - _, _, _ -> { - use children <- result.try( - rows(connection, members("WHERE parent=? AND name=?"), [ - sqlight.text(caller), - sqlight.text(to), - ]), - ) + "parent", Some(id), _ -> + Ok(Address(id, parent_name(family, connection, id))) + _, _, Some(address) -> Ok(address) + _, _, None -> { + use children <- result.try(family.named(connection, caller, to)) use siblings <- result.try(case parent { None -> Ok([]) Some(id) -> - rows( - connection, - members("WHERE parent=? AND name=? AND session<>?"), - [sqlight.text(id), sqlight.text(to), sqlight.text(caller)], + family.named(connection, id, to) + |> result.map( + list.filter(_, fn(member) { member.session != caller }), ) }) let up = case parent { Some(id) -> - case parent_name(connection, id) == to { + case parent_name(family, connection, id) == to { True -> [Address(id, to)] False -> [] } @@ -425,24 +287,11 @@ fn remote_stub( to: String, correspondents: Correspondents, ) -> Result(Option(identity.Stub), String) { - let stub = { - use member <- decode.field(0, decode.string) - use relation <- decode.field(1, decode.string) - use name <- decode.field(2, decode.string) - use peer <- decode.field(3, decode.string) - use host <- decode.field(4, decode.string) - decode.success(Stub(member, relation, name, peer, host)) - } - storage.read( - db, - "SELECT member,relation,name,peer,host FROM family_remote WHERE session=?1 AND (member=?2 OR (?2='parent' AND relation='parent') OR name=?2) LIMIT 1", - [sqlight.text(caller), sqlight.text(to)], - stub, - ) + storage.reading(db, rows(db).remote(_, caller, to)) |> result.try(fn(found) { case found { - [relative, ..] -> Ok(Some(relative)) - [] -> correspondents(caller, to) + Some(relative) -> Ok(Some(relative)) + None -> correspondents(caller, to) } }) } @@ -450,18 +299,19 @@ fn remote_stub( /// Return the family display name, or a root session's name or title. /// This is display metadata, not a stable identifier. pub fn name_of(db: storage.Storage, session: String) -> String { - storage.snapshot(db, fn(connection) { parent_name(connection, session) }) + let family = rows(db) + storage.snapshot(db, parent_name(family, _, session)) } /// A parent's name as its children say it: its own family name, or when it /// is a root the name it was given or its title. -fn parent_name(connection: sqlight.Connection, id: String) -> String { - store.rows( - connection, - "SELECT COALESCE(f.name,NULLIF(s.name,''),s.title) FROM sessions s LEFT JOIN session_family f ON f.session=s.id WHERE s.id=?", - [sqlight.text(id)], - decode.field(0, decode.string, decode.success), - ) - |> result.map(fn(found) { list.first(found) |> result.unwrap("parent") }) - |> result.unwrap("parent") +fn parent_name( + family: core_tables.Family, + connection: sqlight.Connection, + id: String, +) -> String { + case family.address(connection, id) { + Ok(Some(address)) -> address.name + _ -> "parent" + } } diff --git a/src/albedo/harness/sdk/session_info.gleam b/src/albedo/harness/sdk/session_info.gleam index dfc6617db..34f08029b 100644 --- a/src/albedo/harness/sdk/session_info.gleam +++ b/src/albedo/harness/sdk/session_info.gleam @@ -1,8 +1,29 @@ //// Session metadata and persisted stage values, shared without storage access. +//// The facts core's `sessions` table answers through `sdk/core_tables` are +//// typed here, below every module that reads them. import albedo_model as types import gleam/option.{type Option} +/// What a session is called: the name someone gave it (`""` for none), its +/// family name when it is a child, and its title. Each caller chooses its +/// own precedence among them. +pub type Names { + Names(id: String, given: String, family: Option(String), title: String) +} + +/// A session's names with its stored `cwd` and its `workspace`, the +/// workspace it asked to move to when it has one and its `cwd` otherwise. +pub type Summary { + Summary(names: Names, cwd: String, workspace: String) +} + +/// Whether a transfer is still importing a session, and whether it moved +/// to another daemon. +pub type Standing { + Standing(importing: Bool, moved: Bool) +} + pub type Info { Info( id: String, diff --git a/src/albedo/harness/sdk/sessions.gleam b/src/albedo/harness/sdk/sessions.gleam index 769d2dc02..809519f99 100644 --- a/src/albedo/harness/sdk/sessions.gleam +++ b/src/albedo/harness/sdk/sessions.gleam @@ -1,7 +1,9 @@ //// Facts the daemon's `sessions` table keeps about sessions an extension //// names: what a session is called, where it works, and whether a transfer //// is still importing it or moved it to another daemon. None of them reads -//// a session's history, which `sdk/history` owns. +//// a session's history, which `sdk/history` owns. Core reads the table +//// (`core_tables.Sessions`); this module checks the caller's capabilities +//// first. //// //// `names`, `standing`, `standing_in`, and `importing` are plain: they //// answer only about the sessions the caller passes in, as the family @@ -12,11 +14,9 @@ //// sessions the caller did not name, so they need `Sessions` //// (`capability.functions`). +import albedo/harness/sdk/core_tables import albedo/harness/sdk/session_info import albedo/harness/sdk/storage -import albedo/harness/sdk/store -import gleam/dynamic/decode -import gleam/json import gleam/list import gleam/option.{type Option, None, Some} import gleam/result @@ -29,24 +29,18 @@ const module = "albedo/harness/sdk/sessions" /// What a session is called: the name someone gave it (`""` for none), its /// family name when it is a child, and its title. Each caller chooses its /// own precedence among them. -pub type Names { - Names(id: String, given: String, family: Option(String), title: String) -} +pub type Names = + session_info.Names /// A session's names with its stored `cwd` and its `workspace`, the /// workspace it asked to move to when it has one and its `cwd` otherwise. -pub type Summary { - Summary(names: Names, cwd: String, workspace: String) -} +pub type Summary = + session_info.Summary /// Whether a transfer is still importing a session, and whether it moved /// to another daemon. -pub type Standing { - Standing(importing: Bool, moved: Bool) -} - -const summary_select = - "SELECT s.id,COALESCE(s.name,''),f.name,s.title,s.cwd,COALESCE(s.desired_workspace,s.cwd) FROM sessions s LEFT JOIN session_family f ON f.session=s.id" +pub type Standing = + session_info.Standing /// The name a session goes by among agents: its family name, else the name /// it was given, else its title, as `family.name_of` answers. @@ -58,23 +52,8 @@ pub fn label(names: Names) -> String { } } -fn names_decoder() -> decode.Decoder(Names) { - use id <- decode.field(0, decode.string) - use given <- decode.field(1, decode.string) - use family <- decode.field(2, decode.optional(decode.string)) - use title <- decode.field(3, decode.string) - decode.success(Names(id, given, family, title)) -} - -fn summary_decoder() -> decode.Decoder(Summary) { - use names <- decode.then(names_decoder()) - use cwd <- decode.field(4, decode.string) - use workspace <- decode.field(5, decode.string) - decode.success(Summary(names, cwd, workspace)) -} - -fn id_list(ids: List(String)) -> sqlight.Value { - sqlight.text(json.to_string(json.array(ids, json.string))) +fn rows(db: storage.Storage) -> core_tables.Sessions { + storage.core(db).sessions } /// The names of each of `ids` that has a row, in no particular order. An @@ -85,13 +64,7 @@ pub fn names( ) -> Result(List(Names), String) { case ids { [] -> Ok([]) - _ -> - storage.read( - db, - "SELECT s.id,COALESCE(s.name,''),f.name,s.title FROM sessions s LEFT JOIN session_family f ON f.session=s.id WHERE s.id IN (SELECT value FROM json_each(?))", - [id_list(ids)], - names_decoder(), - ) + _ -> storage.reading(db, rows(db).names(_, ids)) } } @@ -107,13 +80,7 @@ pub fn describe( ) case ids { [] -> Ok([]) - _ -> - storage.read( - db, - summary_select <> " WHERE s.id IN (SELECT value FROM json_each(?))", - [id_list(ids)], - summary_decoder(), - ) + _ -> storage.reading(db, rows(db).summaries(_, ids)) } } @@ -125,21 +92,14 @@ pub fn find( key: String, ) -> Result(Option(Summary), String) { use _ <- result.try(storage.permit(db, module, "find")) - storage.read( - db, - summary_select - <> " WHERE s.id=?1 OR f.name=?1 OR s.name=?1 OR s.title=?1 ORDER BY s.id=?1 DESC,s.activity_seq DESC,s.rowid DESC LIMIT 1", - [sqlight.text(key)], - summary_decoder(), - ) - |> result.map(fn(found) { list.first(found) |> option.from_result }) + storage.reading(db, rows(db).find(_, key)) } /// Every session, importing ones included, in storage order. It lists /// sessions, so it needs `Sessions`. pub fn all(db: storage.Storage) -> Result(List(Summary), String) { use _ <- result.try(storage.permit(db, module, "all")) - storage.read(db, summary_select, [], summary_decoder()) + storage.reading(db, rows(db).all) } /// Whether any session works in `workspace`, as its `cwd` or the workspace @@ -150,25 +110,7 @@ pub fn occupied( workspace: String, ) -> Result(Bool, String) { use _ <- result.try(storage.permit(db, module, "occupied")) - storage.read( - db, - "SELECT 1 FROM sessions WHERE cwd=?1 OR desired_workspace=?1 LIMIT 1", - [sqlight.text(workspace)], - decode.field(0, decode.int, decode.success), - ) - |> result.map(fn(found) { found != [] }) -} - -fn standing_sql() -> String { - "SELECT stage=" - <> session_info.stage_sql(session_info.Importing) - <> ",moved IS NOT NULL FROM sessions WHERE id=?" -} - -fn standing_decoder() -> decode.Decoder(Standing) { - use importing <- decode.field(0, sqlight.decode_bool()) - use moved <- decode.field(1, sqlight.decode_bool()) - decode.success(Standing(importing, moved)) + storage.reading(db, rows(db).occupied(_, workspace)) } /// The standing of `session`, or `None` when it has no row. @@ -176,20 +118,16 @@ pub fn standing( db: storage.Storage, session: String, ) -> Result(Option(Standing), String) { - storage.read(db, standing_sql(), [sqlight.text(session)], standing_decoder()) - |> result.map(fn(found) { list.first(found) |> option.from_result }) + storage.reading(db, rows(db).standing(_, session)) } /// `standing` on the caller's connection, inside its transaction. pub fn standing_in( - _db: storage.Storage, + db: storage.Storage, connection: sqlight.Connection, session: String, ) -> Result(Option(Standing), String) { - store.rows(connection, standing_sql(), [sqlight.text(session)], { - standing_decoder() - }) - |> result.map(fn(found) { list.first(found) |> option.from_result }) + rows(db).standing(connection, session) } /// Those of `ids` that a transfer is still importing. @@ -199,15 +137,6 @@ pub fn importing( ) -> Result(Set(String), String) { case ids { [] -> Ok(set.new()) - _ -> - storage.read( - db, - "SELECT id FROM sessions WHERE stage=" - <> session_info.stage_sql(session_info.Importing) - <> " AND id IN (SELECT value FROM json_each(?))", - [id_list(ids)], - decode.field(0, decode.string, decode.success), - ) - |> result.map(set.from_list) + _ -> storage.reading(db, rows(db).importing(_, ids)) } } diff --git a/test/daemon/bedrock_replay_preflight_test.gleam b/test/daemon/bedrock_replay_preflight_test.gleam index f481d9b64..9ce5b41c7 100644 --- a/test/daemon/bedrock_replay_preflight_test.gleam +++ b/test/daemon/bedrock_replay_preflight_test.gleam @@ -2,7 +2,7 @@ //// from producing native history. A fake runtime provider and explicit home //// exercise the real queue/configuration boundaries without auth or HTTP. -import albedo/harness/sdk/family +import albedo/daemon/family as core_family import albedo/harness/sdk/session_info import albedo/daemon/active_output @@ -23,7 +23,6 @@ import albedo/harness/extensions/bedrock/extension as bedrock import albedo/harness/loop import albedo/harness/runtime import albedo/harness/sdk/extension -import albedo/harness/sdk/storage import albedo/harness/sdk/store import albedo/harness/sdk/transcript import albedo_model as types @@ -236,7 +235,7 @@ fn fixture(home: String, check: fn(session_state.State(Nil)) -> Nil) -> Nil { runtime.start_with_extensions(home <> "/fixture.db", [fake_provider()]) let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let assert Ok(_) = mail.initialise(ledger) check(state(home, host)) runtime.stop(host) diff --git a/test/daemon/image_store_test.gleam b/test/daemon/image_store_test.gleam index 72fe7a67d..d2f21886d 100644 --- a/test/daemon/image_store_test.gleam +++ b/test/daemon/image_store_test.gleam @@ -4,7 +4,6 @@ import albedo/daemon/family import albedo/daemon/migrations/image_store import albedo/harness/runtime import albedo/harness/sdk/compaction -import albedo/harness/sdk/family as families import albedo/harness/sdk/image import albedo/harness/sdk/image_codec import albedo/harness/sdk/images @@ -118,7 +117,7 @@ pub fn commit_keeps_one_payload_and_rows_load_references_test() -> Nil { pub fn deleting_a_session_releases_only_unshared_payloads_test() -> Nil { let #(path, ledger) = ledger() - let assert Ok(_) = families.initialise(storage.host(ledger)) + let assert Ok(_) = family.initialise(ledger) let assert Ok(_) = conversation.create(ledger, session("a")) let assert Ok(_) = conversation.create(ledger, session("b")) let assert Ok(_) = diff --git a/test/daemon/mail_commit_test.gleam b/test/daemon/mail_commit_test.gleam index cdf1c1dce..f17c45307 100644 --- a/test/daemon/mail_commit_test.gleam +++ b/test/daemon/mail_commit_test.gleam @@ -3,11 +3,9 @@ import albedo/daemon/conversation import albedo/daemon/family import albedo/daemon/mail import albedo/harness/runtime -import albedo/harness/sdk/family as families import albedo/harness/sdk/letter as mail_value import albedo/harness/sdk/session_info import albedo/harness/sdk/settings -import albedo/harness/sdk/storage import albedo/harness/sdk/store import albedo_model as types import gleam/dynamic/decode @@ -25,7 +23,7 @@ pub fn close_before_mail_commit_rolls_back_turn_and_preserves_letter_test() -> N let assert Ok(host) = runtime.start(path) let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = families.initialise(storage.host(ledger)) + let assert Ok(_) = family.initialise(ledger) let assert Ok(_) = mail.initialise(ledger) let parent = session_info.Info( diff --git a/test/daemon/transcript_sources_test.gleam b/test/daemon/transcript_sources_test.gleam index e55d9e8ac..d0cfd4f88 100644 --- a/test/daemon/transcript_sources_test.gleam +++ b/test/daemon/transcript_sources_test.gleam @@ -1,7 +1,7 @@ //// Fit composition and legacy backfill faults require packed database fixtures //// and exact source bounds that the HTTP E2E API does not expose. -import albedo/harness/sdk/family +import albedo/daemon/family as core_family import albedo/harness/sdk/session_info import albedo/daemon/conversation @@ -140,7 +140,7 @@ pub fn ranged_images_equal_full_history_with_later_fits_test() -> Nil { let assert Ok(host) = runtime.start(path) let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let assert Ok(_) = conversation.create(ledger, session()) let data = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAAD" let assert Ok(original) = types.image("image/png", data, 2, 3, 24) diff --git a/test/harness/extension_capability_test.gleam b/test/harness/extension_capability_test.gleam index 8c7263dd5..ac2922cb8 100644 --- a/test/harness/extension_capability_test.gleam +++ b/test/harness/extension_capability_test.gleam @@ -7,6 +7,7 @@ import albedo/daemon/conversation import albedo/daemon/core_store +import albedo/daemon/family as core_family import albedo/daemon/mail import albedo/daemon/moves import albedo/daemon/transcript_text @@ -536,7 +537,7 @@ fn fixture(uses: List(capability.Capability)) -> extension.Extension { pub fn capability_functions_refuse_a_handle_whose_extension_does_not_declare_them_test() -> Nil { let assert Ok(ledger) = store.start(":memory:", "", core_store.tables()) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let silent = extension.storage_of(fixture([]), ledger) let declaring = extension.storage_of( @@ -599,7 +600,7 @@ fn session(ledger: store.Store, id: String) -> Nil { pub fn session_id_lookups_refuse_a_handle_without_sessions_test() -> Nil { let assert Ok(ledger) = store.start(":memory:", "", core_store.tables()) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) session(ledger, "p") session(ledger, "c") let assert Ok(_) = @@ -652,7 +653,7 @@ pub fn typed_core_reads_keep_the_capability_rules_test() -> Nil { let ledger = history_ledger() let assert Ok(_) = mail.initialise(ledger) let assert Ok(_) = moves.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let bound = fn(uses) { extension.session_storage_of(fixture(uses), ledger, "own") } @@ -714,7 +715,7 @@ pub fn typed_core_reads_keep_the_capability_rules_test() -> Nil { |> result.map(list.map(_, fn(names) { names.id })) |> should.equal(Ok(["other"])) sessions.standing(unbound, "other") - |> should.equal(Ok(Some(sessions.Standing(False, False)))) + |> should.equal(Ok(Some(session_info.Standing(False, False)))) sessions.importing(unbound, ["own", "other"]) |> should.equal(Ok(set.new())) store.close(ledger) } @@ -916,7 +917,7 @@ pub fn history_reads_of_another_session_need_history_test() -> Nil { fn mail_ledger() -> store.Store { let ledger = history_ledger() let assert Ok(_) = mail.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) list.each([#("own", "other"), #("other", "own")], fn(pair) { let #(id, peer) = pair let post = fn(kind, key) { @@ -1319,7 +1320,7 @@ pub fn every_state_operation_is_classified_test() -> Nil { /// their transcripts, so it needs `Sessions` and `History`. pub fn searching_every_session_needs_sessions_and_history_test() -> Nil { let ledger = history_ledger() - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let assert Ok(_) = transcript_text.install(ledger) let find = fn(handle) { agent_sessions.find(handle, "own", "needle", "", False, False, 0, 100) diff --git a/test/harness/extension_failure_test.gleam b/test/harness/extension_failure_test.gleam index 49261ba30..1998ec2ab 100644 --- a/test/harness/extension_failure_test.gleam +++ b/test/harness/extension_failure_test.gleam @@ -6,8 +6,8 @@ //// checked here against deliberately broken extensions. import albedo/daemon/core_store +import albedo/daemon/family as core_family import albedo/harness/extension/selection -import albedo/harness/sdk/family import albedo/harness/sdk/session_info import albedo/daemon/conversation @@ -49,7 +49,7 @@ fn host( ) let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let assert Ok(_) = conversation.create( ledger, diff --git a/test/harness/lcm_preparation_test.gleam b/test/harness/lcm_preparation_test.gleam index f1be221e5..916863d90 100644 --- a/test/harness/lcm_preparation_test.gleam +++ b/test/harness/lcm_preparation_test.gleam @@ -1,7 +1,7 @@ //// Payload decode counts are a performance invariant invisible to HTTP E2E. //// A long archived prefix must not be decoded by an ordinary preparation. -import albedo/harness/sdk/family +import albedo/daemon/family as core_family import albedo/harness/sdk/session_info import albedo/daemon/conversation @@ -36,7 +36,7 @@ pub fn below_trigger_and_stored_prior_do_not_decode_archived_payloads_test() -> ) let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let assert Ok(_) = conversation.create( ledger, diff --git a/test/harness/lcm_test.gleam b/test/harness/lcm_test.gleam index 342e3f486..daa5f50bd 100644 --- a/test/harness/lcm_test.gleam +++ b/test/harness/lcm_test.gleam @@ -1,7 +1,7 @@ //// Forks inside condensed nodes must retain only complete child summaries. //// Fixed graph ranges force fork boundaries that provider compaction cannot pin. -import albedo/harness/sdk/family +import albedo/daemon/family as core_family import albedo/harness/sdk/session_info import albedo/harness/sdk/storage import albedo/harness/sdk/store @@ -93,7 +93,7 @@ fn host_with( ) let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let assert Ok(_) = conversation.create( ledger, @@ -920,7 +920,7 @@ fn tight(capacity: Int, tail: Int) -> #(runtime.Runtime, runtime.Session) { ) let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let assert Ok(_) = conversation.create( ledger, diff --git a/test/harness/retrieval_performance_test.gleam b/test/harness/retrieval_performance_test.gleam index 5ca2e736f..5983c14d6 100644 --- a/test/harness/retrieval_performance_test.gleam +++ b/test/harness/retrieval_performance_test.gleam @@ -1,6 +1,7 @@ //// Trace actual BEAM reads and decodes: E2E cannot observe discarded matches, //// sparse keyset query counts, or how many source and node rows were decoded. +import albedo/daemon/family as core_family import albedo/harness/sdk/session_info import albedo/daemon/conversation @@ -8,7 +9,6 @@ import albedo/daemon/transcript_text import albedo/harness/extensions/agents/sessions import albedo/harness/extensions/lcm/graph import albedo/harness/runtime -import albedo/harness/sdk/family import albedo/harness/sdk/history import albedo/harness/sdk/search import albedo/harness/sdk/storage @@ -40,7 +40,7 @@ fn fixture() -> #(runtime.Runtime, store.Store) { let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) let assert Ok(_) = graph.initialise(lcm_storage(ledger)) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) let assert Ok(_) = transcript_text.install(ledger) let _ = list.each(["wanted", "other"], fn(session) { diff --git a/test/harness/session_fixture.gleam b/test/harness/session_fixture.gleam index be9b2c1e2..4db814687 100644 --- a/test/harness/session_fixture.gleam +++ b/test/harness/session_fixture.gleam @@ -1,18 +1,17 @@ //// Native session facts needed by a component runtime's composition capture. -import albedo/harness/sdk/family +import albedo/daemon/family as core_family import albedo/harness/sdk/session_info import albedo/daemon/conversation import albedo/harness/runtime -import albedo/harness/sdk/storage import albedo_model as types import gleam/option.{None} pub fn initialise(host: runtime.Runtime) -> Nil { let ledger = runtime.ledger(host) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = family.initialise(storage.host(ledger)) + let assert Ok(_) = core_family.initialise(ledger) Nil } diff --git a/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam b/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam index fa3dd03a0..1ab75a376 100644 --- a/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam +++ b/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam @@ -40,7 +40,6 @@ pub const host_readers = [ #("albedo/harness/sdk/history", "read_info"), #("albedo/harness/sdk/history", "snapshot_in"), #("albedo/harness/sdk/request_history", "last_turn_in"), - #("albedo/harness/sdk/family", "member_in"), #("albedo/harness/kernel/journal", "traces_in"), #("albedo/harness/kernel/journal", "session_hashes"), ] diff --git a/tools/lint/test/storage_rules_test.gleam b/tools/lint/test/storage_rules_test.gleam index 3e2eb5113..0a425d182 100644 --- a/tools/lint/test/storage_rules_test.gleam +++ b/tools/lint/test/storage_rules_test.gleam @@ -378,8 +378,8 @@ pub fn main() { root, extension_raw_store.rule, "alpha/raw.gleam", - "import albedo/harness/sdk/request_history.{last_turn_in}\nimport albedo/harness/sdk/family\npub fn go(db, ids) { #(last_turn_in, list.map(ids, family.member_in(db, _))) }", - 2, + "import albedo/harness/sdk/request_history.{last_turn_in}\npub fn go(db) { last_turn_in(db) }", + 1, ) check( root,