From cc1e9341a652ca5ef3fc9b8b6ab09ebdcc96aa49 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Mon, 16 Nov 2026 21:16:46 -0500 Subject: [PATCH] refactor(sdk): read request history through core's operations sdk/request_history keeps its session rule and calls core's Requests operations instead of running SQL on provider_requests. daemon/requests runs them, and selects its own full last turn through the same statement, so last_turn_in moves there and leaves the lint's host_readers list. last_turn still reads in one snapshot. LastTurn moves to sdk/core_tables, and request_history keeps it as an alias. --- robot-docs/extensions.md | 19 ++--- robot-docs/gleam-lint.md | 10 +-- robot-docs/provider-requests.md | 6 +- src/albedo/daemon/core_store.gleam | 2 + src/albedo/daemon/requests.gleam | 72 +++++++++++++++++-- src/albedo/harness/sdk/core_tables.gleam | 21 ++++++ src/albedo/harness/sdk/request_history.gleam | 70 ++++-------------- .../rules/extension_raw_store.gleam | 1 - tools/lint/test/storage_rules_test.gleam | 2 +- 9 files changed, 123 insertions(+), 80 deletions(-) diff --git a/robot-docs/extensions.md b/robot-docs/extensions.md index c00c7b826..f4905594f 100644 --- a/robot-docs/extensions.md +++ b/robot-docs/extensions.md @@ -730,6 +730,10 @@ area's record next to that area's schema, and - `Images`, the payload reads and inserts of `sdk/images` on the `images` table: [`daemon/images`](../src/albedo/daemon/images.gleam). Core's own writes, such as `images.externalize`, use the same insertion. +- `Requests`, the reads of `sdk/request_history` on `provider_requests`: + [`daemon/requests`](../src/albedo/daemon/requests.gleam), whose `last_turn` + selects the turn through the same statement. `request_history.LastTurn` names + `core_tables.LastTurn`. 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)`), @@ -1041,15 +1045,14 @@ 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 readers that take a -connection and read a session's stored data are core's alone. Most live in core, +connection and read a session's stored data are core's alone. They live in core, such as `session_rows.read_info` and `transcript_rows.snapshot`, which extension -code cannot import. The ones still in the SDK or the kernel, -`request_history.last_turn_in`, and `journal.traces_in` and `session_hashes`, -are refused in extension code by the `albedo_extension_raw_store` lint. 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. +code cannot import. The kernel's `journal.traces_in` and `session_hashes` are +refused in extension code by the `albedo_extension_raw_store` lint. 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. diff --git a/robot-docs/gleam-lint.md b/robot-docs/gleam-lint.md index 26aed2472..7435abc9a 100644 --- a/robot-docs/gleam-lint.md +++ b/robot-docs/gleam-lint.md @@ -183,10 +183,12 @@ reads modules under `src/albedo/harness/extensions`. It flags an import of `ledger`, which returns the shared store, and `core`, which returns the 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: `request_history.last_turn_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 +lists: the kernel's `journal.traces_in` and `session_hashes`. The SDK has none +left: its readers of core's tables take the handle, and core's own raw readers +live in `albedo/daemon`, which extension code cannot import. 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/robot-docs/provider-requests.md b/robot-docs/provider-requests.md index 881ca838c..1802d0f2a 100644 --- a/robot-docs/provider-requests.md +++ b/robot-docs/provider-requests.md @@ -37,8 +37,10 @@ do not prove a provider cache hit. [`page`](../src/albedo/daemon/requests.gleam) reads session rows with an ID cursor and time filter. `requests.last_turn` reads the latest completed turn row whose transcript sequence is attached, with the background calls that repeated -its prefix. It selects that turn through `request_history.last_turn_in`, which -also serves the narrower `request_history.last_turn` that extensions use. The +its prefix. It selects that turn through the same statement as the narrower +`request_history.last_turn` that extensions use, which reads it through core's +`Requests` operations +([core table operations](extensions.md#core-table-operations)). The [session HTTP handler](../src/albedo/daemon/http_sessions.gleam) exposes the request listing. The [usage-hours migration](../src/albedo/daemon/migrations/usage_hours.gleam) diff --git a/src/albedo/daemon/core_store.gleam b/src/albedo/daemon/core_store.gleam index 7b8676c2d..fa2da6123 100644 --- a/src/albedo/daemon/core_store.gleam +++ b/src/albedo/daemon/core_store.gleam @@ -5,6 +5,7 @@ import albedo/daemon/family import albedo/daemon/images import albedo/daemon/mail +import albedo/daemon/requests import albedo/daemon/session_rows import albedo/daemon/transcript_rows import albedo/harness/sdk/core_tables @@ -17,5 +18,6 @@ pub fn tables() -> core_tables.Tables { transcript: transcript_rows.operations(), mail: mail.operations(), images: images.operations(), + requests: requests.operations(), ) } diff --git a/src/albedo/daemon/requests.gleam b/src/albedo/daemon/requests.gleam index fed81fd95..51dcfab7d 100644 --- a/src/albedo/daemon/requests.gleam +++ b/src/albedo/daemon/requests.gleam @@ -14,6 +14,7 @@ import albedo_clock import albedo/daemon/stream_trace import albedo/harness/sdk/compaction +import albedo/harness/sdk/core_tables import albedo/harness/sdk/request_history.{Prefix} import albedo/harness/sdk/store import albedo_bytes/scalars @@ -520,6 +521,70 @@ pub fn page( }) } +/// The reads `core_store.tables` gathers for `sdk/request_history`, which +/// checks a caller's session rule before it calls them. +pub fn operations() -> core_tables.Requests { + core_tables.Requests(last_turn: last_turn_identity, outputs:) +} + +fn last_turn_identity( + db: sqlight.Connection, + session: String, +) -> Result(Option(core_tables.LastTurn), String) { + last_turn_in(db, session, "seq,head_hash,inputs", { + use seq <- decode.field(0, decode.optional(decode.int)) + use head_hash <- decode.field(1, decode.string) + use inputs <- decode.field(2, decode.int) + decode.success(core_tables.LastTurn(seq, head_hash, inputs)) + }) +} + +fn outputs( + db: sqlight.Connection, + session: String, + ids: List(Int), +) -> Result(List(#(Int, Int)), String) { + store.rows( + db, + "SELECT id,output_tokens FROM provider_requests WHERE session=? AND output_tokens IS NOT NULL AND id IN (SELECT value FROM json_each(?))", + [ + sqlight.text(session), + sqlight.text(json.to_string(json.array(ids, json.int))), + ], + { + use id <- decode.field(0, decode.int) + use tokens <- decode.field(1, decode.int) + decode.success(#(id, tokens)) + }, + ) +} + +/// The session's latest completed turn call whose response the transcript +/// holds, read on the caller's connection as `columns` decoded by `row`. +/// `last_turn` reads its full row through this selection, and +/// `sdk/request_history` its identity. +fn last_turn_in( + db: sqlight.Connection, + session: String, + columns: String, + row: decode.Decoder(a), +) -> Result(Option(a), String) { + store.rows( + db, + "SELECT " + <> columns + <> " FROM provider_requests WHERE session=? AND kind='turn' AND outcome='ok' AND seq IS NOT NULL ORDER BY id DESC LIMIT 1", + [sqlight.text(session)], + row, + ) + |> result.map(fn(rows) { + case rows { + [] -> None + [turn, ..] -> Some(turn) + } + }) +} + /// The session's latest completed turn call whose response the transcript /// holds, with the background calls since then that repeated its prefix /// (cache-warming pings), oldest first. @@ -528,12 +593,7 @@ pub fn last_turn( session: String, ) -> Result(Option(#(Row, List(Row))), String) { store.snapshot(database, fn(db) { - use turn <- result.try(request_history.last_turn_in( - db, - session, - columns, - row_decoder(), - )) + use turn <- result.try(last_turn_in(db, session, columns, row_decoder())) case turn { None -> Ok(None) Some(turn) -> { diff --git a/src/albedo/harness/sdk/core_tables.gleam b/src/albedo/harness/sdk/core_tables.gleam index 03c92f9d1..08809c195 100644 --- a/src/albedo/harness/sdk/core_tables.gleam +++ b/src/albedo/harness/sdk/core_tables.gleam @@ -37,6 +37,7 @@ pub type Tables { transcript: Transcript, mail: Mail, images: Images, + requests: Requests, ) } @@ -250,3 +251,23 @@ pub type Payload { Bytes(BitArray) Text(String) } + +/// Reads of core's `provider_requests` table, which records each model call. +pub type Requests { + Requests( + /// The session's latest completed turn call whose response the + /// transcript holds. + last_turn: fn(Connection, String) -> Result(Option(LastTurn), String), + /// The output tokens each listed call of the session reported, as + /// `#(id, tokens)`; a call that reported none is left out. + outputs: fn(Connection, String, List(Int)) -> + Result(List(#(Int, Int)), String), + ) +} + +/// A turn call's transcript sequence and its exact input identity: the hash +/// of its request head and how many projected inputs it carried. +/// `sdk/request_history` names it `request_history.LastTurn`. +pub type LastTurn { + LastTurn(seq: Option(Int), head_hash: String, inputs: Int) +} diff --git a/src/albedo/harness/sdk/request_history.gleam b/src/albedo/harness/sdk/request_history.gleam index 3a752ef73..c78649287 100644 --- a/src/albedo/harness/sdk/request_history.gleam +++ b/src/albedo/harness/sdk/request_history.gleam @@ -1,14 +1,13 @@ //// Request identities and the observation needed to bound a memory to the -//// completed model call. The daemon records provider request rows. +//// completed model call. The daemon records provider request rows and runs +//// every statement on them (`core_tables.Requests`); these reads check the +//// caller's session rule first. +import albedo/harness/sdk/core_tables import albedo/harness/sdk/storage -import albedo/harness/sdk/store import gleam/dict.{type Dict} -import gleam/dynamic/decode -import gleam/json -import gleam/option.{type Option, None, Some} +import gleam/option.{type Option} import gleam/result -import sqlight /// What makes cache analysis possible without storing requests: the request /// head (instructions and tools) as a hash, how many projected inputs the @@ -27,8 +26,11 @@ pub type Prefix { ) } -pub type LastTurn { - LastTurn(seq: Option(Int), head_hash: String, inputs: Int) +pub type LastTurn = + core_tables.LastTurn + +fn rows(ledger: storage.Storage) -> core_tables.Requests { + storage.core(ledger).requests } /// The latest successful turn attached to the transcript, with its exact input @@ -38,15 +40,7 @@ pub fn last_turn( session: String, ) -> Result(Option(LastTurn), String) { use _ <- result.try(permitted(ledger, "last_turn", session)) - storage.snapshot( - ledger, - last_turn_in(_, session, "seq,head_hash,inputs", { - use seq <- decode.field(0, decode.optional(decode.int)) - use head_hash <- decode.field(1, decode.string) - use inputs <- decode.field(2, decode.int) - decode.success(LastTurn(seq, head_hash, inputs)) - }), - ) + storage.snapshot(ledger, rows(ledger).last_turn(_, session)) } /// The output tokens the provider reported for each of `session`'s calls @@ -57,50 +51,10 @@ pub fn outputs( ids: List(Int), ) -> Result(Dict(Int, Int), String) { use _ <- result.try(permitted(ledger, "outputs", session)) - storage.read( - ledger, - "SELECT id,output_tokens FROM provider_requests WHERE session=? AND output_tokens IS NOT NULL AND id IN (SELECT value FROM json_each(?))", - [ - sqlight.text(session), - sqlight.text(json.to_string(json.array(ids, json.int))), - ], - { - use id <- decode.field(0, decode.int) - use tokens <- decode.field(1, decode.int) - decode.success(#(id, tokens)) - }, - ) + storage.reading(ledger, rows(ledger).outputs(_, session, ids)) |> result.map(dict.from_list) } -/// The session's latest completed turn call whose response the transcript -/// holds, read on the caller's connection as `columns` decoded by `row`. -/// The daemon reads its full request row through this same selection. -/// 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 last_turn_in( - db: sqlight.Connection, - session: String, - columns: String, - row: decode.Decoder(a), -) -> Result(Option(a), String) { - store.rows( - db, - "SELECT " - <> columns - <> " FROM provider_requests WHERE session=? AND kind='turn' AND outcome='ok' AND seq IS NOT NULL ORDER BY id DESC LIMIT 1", - [sqlight.text(session)], - row, - ) - |> result.map(fn(rows) { - case rows { - [] -> None - [turn, ..] -> Some(turn) - } - }) -} - /// `storage.permit_session` for this module's `function`, which reads /// `session`'s stored history (`capability.session_functions`). fn permitted( 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 72c6932f4..d8585e90e 100644 --- a/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam +++ b/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam @@ -37,7 +37,6 @@ const host_only = ["new", "host", "grant", "declare", "bind", "ledger", "core"] /// function. Extensions use the handle-taking reads instead /// (`capability.session_functions`, `capability.functions`). pub const host_readers = [ - #("albedo/harness/sdk/request_history", "last_turn_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 71a37dc1b..c3927d6bb 100644 --- a/tools/lint/test/storage_rules_test.gleam +++ b/tools/lint/test/storage_rules_test.gleam @@ -378,7 +378,7 @@ pub fn main() { root, extension_raw_store.rule, "alpha/raw.gleam", - "import albedo/harness/sdk/request_history.{last_turn_in}\npub fn go(db) { last_turn_in(db) }", + "import albedo/harness/kernel/journal.{session_hashes}\nimport albedo/harness/sdk/request_history\npub fn go(db, s) { #(session_hashes, request_history.last_turn(s, \"b\")) }", 1, ) check( -- knotmirror 0.0.1