First v1.1.1 commit. Adds the KV store the design notes commit to: `(app_id, collection, key)` identity with JSONB value and a per-app index. Trait lives in `picloud-shared` so the executor-core Rhai bridge (next commit), the Postgres impl, and tests all depend on the same surface without coupling crates. The `Services` bundle grows from empty to three fields: `kv`, `dead_letters` (NoopDeadLetterService stub — replaced by the Postgres impl in commit 8), and `events` (NoopEventEmitter until the outbox emitter lands with the dispatcher). Tests use `Services::default()` for an all-noop bundle. New capabilities `AppKvRead` / `AppKvWrite` join the Capability enum. They map onto the existing seven-value `Scope` (script:read / script:write) — the scope vocabulary stays locked per the `docs/versioning.md` commitment. Script-as-gate semantics in `KvServiceImpl`: capability check runs when `cx.principal.is_some()`, skipped when None (public HTTP). Cross-app isolation is enforced independently by deriving every row's `app_id` from `cx.app_id` rather than a script-passed argument. In-memory `KvRepo` impl + unit tests cover the round-trips, the cross-app isolation property, empty-collection rejection, script-as-gate behaviour for both anonymous and authed contexts, and cursor-style pagination. Postgres impl exists; integration testing waits for a real DB harness (see HANDBACK). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
141 lines
4.5 KiB
Rust
141 lines
4.5 KiB
Rust
//! `KvService` — the v1.1.1 key-value store contract.
|
|
//!
|
|
//! Lives in `picloud-shared` (not `executor-core`) so the Rhai bridge,
|
|
//! the manager-core Postgres impl, and any future in-memory test impl
|
|
//! can all depend on the same trait without dragging
|
|
//! `executor-core` into `manager-core`'s dep graph.
|
|
//!
|
|
//! Implementations MUST derive every storage `app_id` from `cx.app_id`
|
|
//! — never from a script-passed argument. That is the cross-app
|
|
//! isolation boundary; see `docs/sdk-shape.md`.
|
|
|
|
use async_trait::async_trait;
|
|
use thiserror::Error;
|
|
|
|
use crate::SdkCallCx;
|
|
|
|
/// `KvService` is collection-scoped. Scripts get a handle via
|
|
/// `kv::collection(name)` and call `get`/`set`/`has`/`delete`/`list`
|
|
/// on it. The trait surface accepts the collection by name so the
|
|
/// Postgres impl can avoid an extra round-trip to materialize the
|
|
/// collection (collections are namespaces, not first-class rows).
|
|
#[async_trait]
|
|
pub trait KvService: Send + Sync {
|
|
async fn get(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
key: &str,
|
|
) -> Result<Option<serde_json::Value>, KvError>;
|
|
|
|
async fn set(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
key: &str,
|
|
value: serde_json::Value,
|
|
) -> Result<(), KvError>;
|
|
|
|
async fn delete(&self, cx: &SdkCallCx, collection: &str, key: &str) -> Result<bool, KvError>;
|
|
|
|
async fn has(&self, cx: &SdkCallCx, collection: &str, key: &str) -> Result<bool, KvError>;
|
|
|
|
/// Cursor-style pagination. `cursor` is opaque to the caller;
|
|
/// implementations encode the resume key inside. `None` cursor
|
|
/// starts from the beginning. Implementations cap `limit` at a
|
|
/// reasonable ceiling internally (script can't request an unbounded
|
|
/// page).
|
|
async fn list(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
cursor: Option<&str>,
|
|
limit: u32,
|
|
) -> Result<KvListPage, KvError>;
|
|
}
|
|
|
|
/// One page of keys from `KvService::list`. `next_cursor` is `Some`
|
|
/// when more pages exist, `None` when exhausted. The cursor encoding
|
|
/// is implementation-defined (the Postgres impl base64-encodes the
|
|
/// last key).
|
|
#[derive(Debug, Clone)]
|
|
pub struct KvListPage {
|
|
pub keys: Vec<String>,
|
|
pub next_cursor: Option<String>,
|
|
}
|
|
|
|
/// Stub used by the test harness so executor-core integration tests
|
|
/// (which don't touch KV) can construct a `Services` bundle without
|
|
/// spinning up Postgres. Every call returns
|
|
/// `KvError::Backend("...")` so accidental KV use surfaces clearly.
|
|
#[derive(Debug, Default, Clone, Copy)]
|
|
pub struct NoopKvService;
|
|
|
|
#[async_trait]
|
|
impl KvService for NoopKvService {
|
|
async fn get(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_key: &str,
|
|
) -> Result<Option<serde_json::Value>, KvError> {
|
|
Err(KvError::Backend("kv is not wired in".into()))
|
|
}
|
|
|
|
async fn set(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_key: &str,
|
|
_value: serde_json::Value,
|
|
) -> Result<(), KvError> {
|
|
Err(KvError::Backend("kv is not wired in".into()))
|
|
}
|
|
|
|
async fn delete(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_key: &str,
|
|
) -> Result<bool, KvError> {
|
|
Err(KvError::Backend("kv is not wired in".into()))
|
|
}
|
|
|
|
async fn has(&self, _cx: &SdkCallCx, _collection: &str, _key: &str) -> Result<bool, KvError> {
|
|
Err(KvError::Backend("kv is not wired in".into()))
|
|
}
|
|
|
|
async fn list(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_cursor: Option<&str>,
|
|
_limit: u32,
|
|
) -> Result<KvListPage, KvError> {
|
|
Err(KvError::Backend("kv is not wired in".into()))
|
|
}
|
|
}
|
|
|
|
/// Failure modes surfaced to the Rhai bridge. The bridge converts each
|
|
/// to a Rhai runtime error string; the discriminants exist so internal
|
|
/// callers (admin endpoints, tests, GC) can react more precisely.
|
|
#[derive(Debug, Error)]
|
|
pub enum KvError {
|
|
/// Empty collection name; rejected at the SDK boundary per
|
|
/// `docs/sdk-shape.md`.
|
|
#[error("collection name must not be empty")]
|
|
InvalidCollection,
|
|
|
|
/// Caller principal lacked the required capability. Only raised
|
|
/// when `cx.principal.is_some()` — scripts running with
|
|
/// `principal: None` (public HTTP) operate under script-as-gate
|
|
/// semantics and skip the capability check.
|
|
#[error("forbidden")]
|
|
Forbidden,
|
|
|
|
/// Anything else — Postgres unavailable, serialization failure,
|
|
/// etc. The string is safe to surface to a script.
|
|
#[error("kv backend error: {0}")]
|
|
Backend(String),
|
|
}
|