Expand description
§turul-ai-catalog-storage
Storage for AI Catalog entries. A CatalogStore keeps entry-level
records and the Host Info record of each catalog, every record versioned by a content-derived
Etag and written under an optimistic Condition. assemble builds the served catalog
document from the stored records, and import loads a catalog document into them.
Part of turul-agent-card.
§Feature flags
| Feature | Enables | Default |
|---|---|---|
in-memory | InMemoryCatalogStore | yes |
sqlite | SqliteCatalogStore (SQLite via sqlx) | no |
postgres | PostgresCatalogStore (PostgreSQL via sqlx) | no |
dynamodb | DynamoDbCatalogStore (AWS DynamoDB) | no |
A write the DynamoDB backend loses to other writers’ transactions is retried at most 8 times, with
jittered exponential backoff, then fails with StoreError::Contended.
Every backend guarantees atomic conditional writes, read-your-writes, byte-identical reads, and
that no identifier holds both unversioned and versioned entries, and that no entry is replaced by a
put of the other mode. Each entry is stored with a Provenance: curated or a lease, and its
writer. The entry’s Etag covers it.
§Quick start
cargo add turul-ai-catalog-storageStore an entry, then assemble the catalog document it belongs to:
use turul_ai_catalog::identity::{ArtifactType, EntryIdentifier};
use turul_ai_catalog::model::{ArtifactSource, CatalogEntry};
use turul_ai_catalog_storage::{
CatalogId, CatalogStore, Condition, EntryRecord, InMemoryCatalogStore, Provenance, WriterId,
assemble,
};
let store = InMemoryCatalogStore::new();
let catalog = CatalogId::new("main")?;
let entry = CatalogEntry::new(
EntryIdentifier::new("urn:air:example.com:a2a:research"),
ArtifactType::A2A_AGENT_CARD,
ArtifactSource::Url("https://agents.example.com/.well-known/agent-card.json".into()),
);
let record = EntryRecord::new(entry)?;
let provenance = Provenance::Curated { writer: WriterId::new("operator")? };
store.put_entry(&catalog, &record, &provenance, Condition::Absent).await?;
let served = assemble(&store, &catalog, std::time::SystemTime::now()).await?;
assert!(!served.bytes.is_empty() && served.omitted.is_empty());§Leases and what assembly leaves out
assemble takes now; this crate never reads a clock. A lease is served while now is before
its expires_at. An expired lease, and an entry that fails validation as it stands now, are left
out of the document and listed in Assembled::omitted with the reason, so nothing is dropped
silently. Assembled::next_expiry is the earliest expiry among the leases served; a caller that
caches the document re-assembles at that instant.
A live lease is never missing from a served snapshot when its owner’s refresh commits before expiry:
refresh interval plus clock skew between the registrar’s host and the serving host must be less than
lease_ttl - lease_ttl / 4. Keep lease_ttl at least 4 times the refresh interval. The library does
not check this: the interval and the lease length are configured in different processes.
§Curated writer and upgrading a store
import writes every entry as curated by the WriterId it is given, and refuses an entry whose
key holds a lease or a curated record of another writer. The curated writer id is permanent
configuration: changing it fails at the first entry the catalog already holds. If it is lost,
run turul-ai-catalog store import --replace with the new id.
Records stored by an earlier release have no provenance and are not read as curated. SQL connect
fails on such a table; a DynamoDB assembly fails with StoreError::CorruptProvenance. No path reads
the earlier format: a store being served is upgraded by replacing or deleting its data, with this
release’s CLI:
- Same catalog id (DynamoDB; SQL
connectrefuses an earlier table), when the id holds no live lease: stop the previous release’s writers, runturul-ai-catalog store import --catalog-id LIVE --file CATALOG --writer WRITER --replace, then deploy the new function on the sameTURUL_AI_CATALOG_ID. - New catalog id:
turul-ai-catalog store importunder a newCatalogIdin the same table (SQL: a new database, SQLite: a new file), deploy the function withTURUL_AI_CATALOG_IDset to the new id, thenturul-ai-catalog store purge --catalog-id OLD --yeswithTURUL_AI_CATALOG_IDset to the new id, which the purge refuses to remove.
Never point the previous release at a new-format catalog id: it writes entries without provenance, which this release then reads as corrupt, and it bypasses the mode exclusion.
§Runtime registration
A Registrar lets writers the deployer has authenticated register entries as leases or curated
records, under a RegistrationPolicy: each WriterId has a WriterGrant of urn:air
publishers and non-urn:air identifier prefixes, and optionally must prove its publisher signature
through a PublisherVerifier. A writer without a grant, or a grant with neither publishers nor
prefixes, is refused. The registrar never reads url or publisher for authority, only
identifier. It holds its store and verifier as Arc<dyn CatalogStore> and
Arc<dyn PublisherVerifier>, so it can live in a service’s static state.
-
Modes are granted. A grant permits leases only;
WriterGrant::with_curatedalso permits curated entries, which never expire and are not verified again after their write. A curated request under a lease-only grant is refused withCuratedNotGrantedbefore the store is read. -
Co-granted writers share identifiers, not records. Ownership is per entry key. Writers granted the same publisher or prefix may each add versions of an identifier the other registered, and the first record fixes its version kind for both; neither can overwrite or remove the other’s record. Grant disjoint publishers and prefixes for one writer per identifier.
-
Role separation first. Authority holds only if no registering service’s role can write the table. Run the registrar under its own role behind the deployer’s authenticated channel; only the registrar’s role and the operator’s
importrole hold the writer actions; the serving role reads only. -
When a service registers. Once it is serving, never during init: a lease claims the artifact is reachable, and one left by an init that then fails stays served until it expires.
-
Refresh.
registerof the same key by the same writer inLeasemode refreshes. It writes only once less than three quarters oflease_ttlremain, so many instances refreshing one lease commit one write per quarter. Keeplease_ttlat least 4 times the refresh interval. -
Refresh failures. A refresh that fails, a
PublisherSignaturerefusal because a DID document or artifact could not be fetched included, is reported by the service and retried at its next interval. It is not fatal while the lease has time left. -
Removing a stale lease. Its owner calls
Registrar::unregister. Otherwise the operator runsturul-ai-catalog store remove(backed bylist_registrationsandremove_registration) or, with store credentials, deletes it by itsEntryKey, conditioned on the version it read so that a refresh in between makes the delete fail rather than remove a live lease:
use std::time::SystemTime;
use turul_ai_catalog::{identity::{EntryIdentifier, EntryKey}, timestamp::Timestamp};
use turul_ai_catalog_storage::{CatalogId, CatalogStore, Provenance, StoreError};
async fn remove_expired_lease(store: &dyn CatalogStore, catalog: &CatalogId, key: &EntryKey)
-> Result<bool, StoreError> {
let Some(stored) = store.get_entry(catalog, key).await? else { return Ok(false) };
let Provenance::Lease { expires_at, .. } = &stored.provenance else { return Ok(false) };
if *expires_at > Timestamp::from_system_time(SystemTime::now()) { return Ok(false) }
store.delete_entry(catalog, key, Some(&stored.etag)).await?; // PreconditionFailed if refreshed
Ok(true)
}Structs§
- Assembled
- The served document, what it left out and when it next changes by itself.
- Catalog
Id - A storage namespace naming one catalog document: 1 to 128 bytes of
[A-Za-z0-9._-]. - Dynamo
DbCatalog Store dynamodb - Records in one DynamoDB table with partition key
pk(S) and sort keysk(S). Beside each identifier’s entry items sits a guard item (sk="g:…") holding its version kind and entry count; a new entry and its guard change in one transaction. The table belongs to the deployer: the store neither creates nor verifies it. - Entry
Record - An entry that passed
validateand canonical serialisation. The only way to build one isEntryRecord::new. - Etag
- The version of one stored record: for the host, the lowercase hex SHA-256 of its record bytes;
for an entry,
Etag::of_entry. - Host
Record - Host Info that passed
validateand canonical serialisation. - InMemory
Catalog Store in-memory - The default backend: records in a process-local map, every write under one lock.
- Item
Counts - How many items of a catalog id a backend holds, by kind of backend key.
- Omission
- An entry record that assembly left out of the served document.
- Postgres
Catalog Store postgres - Records in PostgreSQL tables.
- Purge
Report - What
CatalogStore::purge_catalogremoved. - Record
Key - The backend key of a record within a catalog:
"h"for the host,"e:"and a fixed-length digest of the entry key for an entry,"g:"and a fixed-length digest of the identifier for the version-kind guard of an identifier. - Registered
- What a registration left in the store.
- Registrar
- Registers and removes entries of one catalog on behalf of writers the deployer has authenticated.
- Registration
- One stored entry as an operator sees it: its key, who wrote it, and whether it is served.
- Registration
Policy - The grants of every writer and the length of a lease.
- Scanned
Entry - One entry record as
CatalogStore::scan_entriesreports it: the key from its bytes, its stored version, and its provenance or why that does not read. - Sqlite
Catalog Store sqlite - Records in a SQLite database.
- Stored
- A record and its version.
- Stored
Entry - An entry record, who wrote it, and its version.
- Verifier
Refusal - The verifier’s reason for refusing an entry, as text.
- Writer
Grant - What one writer may register:
urn:airidentifiers under its publishers, other identifiers under its prefixes, and whether each registration must carry a publisher signature. - Writer
Id - The opaque, operator-assigned identity of whoever writes an entry: 1 to 128 bytes of
[A-Za-z0-9._-], compared exactly. The library trusts nothing about it beyond what the deployer authenticated, and never derives one from an address or an entry member.
Enums§
- Assemble
Error - Why
assemblefailed. - Condition
- The precondition of a write, checked atomically with the write.
- Expiry
- Whether a stored entry is served at a given instant, by the rule of
assemble. - Import
Error - Why
importrefused or failed to store a catalog document. - Import
Mode - How
importtreats what the catalog id already holds. - Mode
Kind - Which of the two entry modes a record was written in.
- Mode
Request - The mode a registration asks for.
- Omission
Reason - Why an
Omissionwas left out. - Policy
Error - Why a
RegistrationPolicyorWriterGrantwas refused at construction. - Provenance
- Who wrote an entry record and under which mode, stored beside the record bytes and covered by
the entry’s
Etag. - Provenance
Error - Why stored provenance bytes were refused.
- Record
Error - Why a record or catalog id was refused.
- Registration
Error - Why a registration or removal was refused.
- Remove
Error - Why
remove_registrationfailed. - Store
Error - Why a
CatalogStoreoperation failed.
Traits§
- Catalog
Store - Entry-level catalog records with
Etagversions. - Publisher
Verifier - Decides whether an entry carries proof that its
urn:airpublisher signed it. Storage defines the seam and never depends on the crypto that implements it.
Functions§
- assemble
- Builds the served catalog document of
catalogatnow, the one builder of it. - import
- Stores the host and every entry of
documentundercatalog, the one path from a catalog document to records. - list_
registrations - Lists every entry of
catalogwith its provenance and its expiry state atnow, ordered byEntryKey. A lease is live whilenowis before itsexpires_at. - remove_
registration - Removes the entry stored under
key, of either mode, and returns the provenance it had.