From bc9c10fa2d1c25bfac63cf3bf7d7b7cd12b04460 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Mon, 16 Nov 2026 08:59:43 -0500 Subject: [PATCH] refactor(packages): move byte and text helpers into albedo_bytes text_scalars, sdk/bytes, sdk/zlib, sdk/term, sdk/term_codec, sdk/digest and sdk/ids become the albedo_bytes workspace package: albedo_bytes, albedo_bytes/scalars, and albedo_bytes/zlib, term, term_codec, digest and ids. The scalar, digest and id unit tests move with them, and the package tests compact, utf8, newline and base64_url_decode. The extension wire-value checks stay in albedo. --- docs/extensions.md | 9 +- gleam.toml | 1 + manifest.toml | 2 + packages/albedo_bytes/.gitignore | 1 + packages/albedo_bytes/gleam.toml | 16 ++ packages/albedo_bytes/manifest.toml | 22 +++ .../albedo_bytes/src/albedo_bytes.gleam | 0 .../src/albedo_bytes}/digest.gleam | 4 +- .../albedo_bytes/src/albedo_bytes}/ids.gleam | 6 +- .../src/albedo_bytes/scalars.gleam | 0 .../albedo_bytes/src/albedo_bytes}/term.gleam | 0 .../src/albedo_bytes}/term_codec.gleam | 4 +- .../albedo_bytes/src/albedo_bytes}/zlib.gleam | 2 +- .../test/albedo_bytes/digest_ids_test.gleam | 107 ++++++++++ .../test/albedo_bytes/scalars_test.gleam | 28 +++ .../albedo_bytes/test/albedo_bytes_test.gleam | 49 +++++ priv/boot-modules.txt | 4 +- robot-docs/contributing.md | 9 +- robot-docs/extensions.md | 43 ++-- robot-docs/mcp.md | 10 +- robot-docs/proxy.md | 6 +- robot-docs/runtime.md | 2 +- robot-docs/workspace-packages.md | 14 +- src/albedo/cell_title.gleam | 6 +- src/albedo/daemon/active_output_store.gleam | 2 +- src/albedo/daemon/boot_code.gleam | 2 +- src/albedo/daemon/build_identity.gleam | 2 +- src/albedo/daemon/bus.gleam | 4 +- src/albedo/daemon/changes.gleam | 12 +- src/albedo/daemon/child_failure.gleam | 6 +- src/albedo/daemon/child_models.gleam | 8 +- src/albedo/daemon/context_snapshot.gleam | 6 +- src/albedo/daemon/conversation.gleam | 10 +- src/albedo/daemon/events.gleam | 6 +- src/albedo/daemon/folders.gleam | 10 +- src/albedo/daemon/http_api.gleam | 4 +- src/albedo/daemon/http_auth.gleam | 4 +- src/albedo/daemon/http_history.gleam | 4 +- src/albedo/daemon/http_transcript.gleam | 4 +- src/albedo/daemon/inbox.gleam | 4 +- src/albedo/daemon/inspection.gleam | 2 +- src/albedo/daemon/lifecycle.gleam | 2 +- src/albedo/daemon/mail.gleam | 6 +- .../migrations/session_context_blocks.gleam | 7 +- .../daemon/migrations/transcript_kinds.gleam | 12 +- src/albedo/daemon/refusal.gleam | 6 +- src/albedo/daemon/registry.gleam | 2 +- src/albedo/daemon/requests.gleam | 8 +- src/albedo/daemon/session_activity.gleam | 6 +- src/albedo/daemon/session_configuration.gleam | 4 +- src/albedo/daemon/session_deletion.gleam | 4 +- src/albedo/daemon/session_namespace.gleam | 4 +- src/albedo/daemon/session_naming.gleam | 23 +-- src/albedo/daemon/session_run.gleam | 2 +- src/albedo/daemon/session_salvage.gleam | 6 +- src/albedo/daemon/session_side_question.gleam | 2 +- src/albedo/daemon/storage_report.gleam | 7 +- src/albedo/daemon/tool_progress.gleam | 8 +- src/albedo/daemon/transfer_files.gleam | 4 +- src/albedo/harness/account_migration.gleam | 2 +- src/albedo/harness/capability_catalog.gleam | 4 +- src/albedo/harness/compaction.gleam | 12 +- src/albedo/harness/context_limits.gleam | 14 +- src/albedo/harness/extension_cache.gleam | 2 +- .../harness/extensions/agents/extension.gleam | 6 +- .../extensions/antigravity/catalog.gleam | 11 +- .../extensions/antigravity/counter.gleam | 2 +- .../extensions/antigravity/stream.gleam | 2 +- .../harness/extensions/antigravity/wire.gleam | 2 +- .../harness/extensions/artifacts/ledger.gleam | 6 +- .../extensions/artifacts/service.gleam | 10 +- .../extensions/bedrock/aws_profile.gleam | 7 +- .../harness/extensions/checklist/ledger.gleam | 4 +- .../harness/extensions/claude/accounts.gleam | 8 +- .../harness/extensions/claude/billing.gleam | 6 +- .../harness/extensions/claude/files.gleam | 4 +- .../harness/extensions/claude/wire.gleam | 6 +- .../harness/extensions/codex/accounts.gleam | 4 +- .../harness/extensions/codex/imagegen.gleam | 4 +- .../harness/extensions/codex/wire.gleam | 2 +- .../harness/extensions/discord/ingest.gleam | 10 +- .../harness/extensions/discord/ledger.gleam | 10 +- .../harness/extensions/discord/refs.gleam | 4 +- .../harness/extensions/exa/extension.gleam | 4 +- .../harness/extensions/git/extension.gleam | 4 +- .../extensions/github_copilot/catalog.gleam | 2 +- .../extensions/imagegen/extension.gleam | 2 +- .../harness/extensions/mcp/catalogue.gleam | 4 +- src/albedo/harness/extensions/mcp/oauth.gleam | 8 +- .../harness/extensions/mcp/servers.gleam | 6 +- .../harness/extensions/memory/README.md | 3 +- .../harness/extensions/memory/files.gleam | 4 +- .../harness/extensions/mistral/sign_in.gleam | 2 +- .../harness/extensions/mistral/wire.gleam | 4 +- .../harness/extensions/models/catalog.gleam | 2 +- .../extensions/models/catalog_cache.gleam | 2 +- .../harness/extensions/openai/extension.gleam | 2 +- .../extensions/opencode_zen/client.gleam | 8 +- .../extensions/paperclips/presentation.gleam | 4 +- .../extensions/perplexity/search.gleam | 4 +- .../persistent_agent/extension.gleam | 8 +- .../harness/extensions/proxy/chat.gleam | 6 +- .../harness/extensions/proxy/extension.gleam | 4 +- .../extensions/snapcompact/extension.gleam | 12 +- .../harness/extensions/webhooks/ledger.gleam | 8 +- .../harness/extensions/webhooks/service.gleam | 2 +- src/albedo/harness/file_fingerprint.gleam | 2 +- src/albedo/harness/instruction_files.gleam | 4 +- src/albedo/harness/kernel.gleam | 6 +- src/albedo/harness/kernel/cell_codec.gleam | 2 +- src/albedo/harness/kernel/driver.gleam | 14 +- src/albedo/harness/kernel/journal.gleam | 2 +- src/albedo/harness/kernel/native.gleam | 2 +- src/albedo/harness/kernel/python_tree.gleam | 6 +- src/albedo/harness/kernel/signal_helper.gleam | 2 +- src/albedo/harness/languages.gleam | 2 +- src/albedo/harness/loop.gleam | 6 +- src/albedo/harness/project_files.gleam | 6 +- src/albedo/harness/repetition.gleam | 4 +- src/albedo/harness/runtime/catalog.gleam | 4 +- src/albedo/harness/sdk/auth_accounts.gleam | 12 +- src/albedo/harness/sdk/credentials.gleam | 6 +- src/albedo/harness/sdk/family.gleam | 8 +- src/albedo/harness/sdk/http_api.gleam | 4 +- src/albedo/harness/sdk/image_codec.gleam | 4 +- src/albedo/harness/sdk/mail.gleam | 2 +- src/albedo/harness/sdk/oauth_flows.gleam | 4 +- src/albedo/harness/sdk/rotation.gleam | 6 +- src/albedo/harness/sdk/search.gleam | 10 +- src/albedo/harness/sdk/tool.gleam | 8 +- src/albedo/harness/sdk/transcript_codec.gleam | 2 +- src/albedo/harness/sdk/web_search.gleam | 4 +- src/albedo/harness/settings/composition.gleam | 2 +- src/albedo/harness/skills/files.gleam | 4 +- src/albedo/harness/skills/frontmatter.gleam | 7 +- src/albedo/harness/trust.gleam | 4 +- src/albedo/harness/worktree.gleam | 23 +-- src/albedo/openai_api/chat.gleam | 2 +- src/albedo/openai_api/fields.gleam | 2 +- src/albedo/openai_api/pool.gleam | 2 +- src/albedo/openai_api/sse.gleam | 2 +- ..._bedrock_replay_preflight_test_support.erl | 2 +- .../albedo_text_scalars_test_support.erl | 4 - test/daemon/session_name_scalars_test.gleam | 19 ++ test/daemon/text_scalars_test.gleam | 40 ---- test/e2e/areas.toml | 10 +- ...lbedo_antigravity_catalog_test_support.erl | 2 +- test/harness/albedo_runtime_test_support.erl | 4 +- test/harness/claude_wire_test.gleam | 2 +- test/harness/extension_digests_test.gleam | 89 +++++++++ test/harness/sdk_digest_ids_test.gleam | 183 ------------------ .../transcript_codec_golden_test.gleam | 4 +- 152 files changed, 704 insertions(+), 630 deletions(-) create mode 100644 packages/albedo_bytes/.gitignore create mode 100644 packages/albedo_bytes/gleam.toml create mode 100644 packages/albedo_bytes/manifest.toml rename src/albedo/harness/sdk/bytes.gleam => packages/albedo_bytes/src/albedo_bytes.gleam (100%) rename {src/albedo/harness/sdk => packages/albedo_bytes/src/albedo_bytes}/digest.gleam (89%) rename {src/albedo/harness/sdk => packages/albedo_bytes/src/albedo_bytes}/ids.gleam (89%) rename src/albedo/text_scalars.gleam => packages/albedo_bytes/src/albedo_bytes/scalars.gleam (100%) rename {src/albedo/harness/sdk => packages/albedo_bytes/src/albedo_bytes}/term.gleam (100%) rename {src/albedo/harness/sdk => packages/albedo_bytes/src/albedo_bytes}/term_codec.gleam (94%) rename {src/albedo/harness/sdk => packages/albedo_bytes/src/albedo_bytes}/zlib.gleam (98%) create mode 100644 packages/albedo_bytes/test/albedo_bytes/digest_ids_test.gleam create mode 100644 packages/albedo_bytes/test/albedo_bytes/scalars_test.gleam create mode 100644 packages/albedo_bytes/test/albedo_bytes_test.gleam delete mode 100644 test/daemon/albedo_text_scalars_test_support.erl create mode 100644 test/daemon/session_name_scalars_test.gleam delete mode 100644 test/daemon/text_scalars_test.gleam create mode 100644 test/harness/extension_digests_test.gleam delete mode 100644 test/harness/sdk_digest_ids_test.gleam diff --git a/docs/extensions.md b/docs/extensions.md index b5f92898c..e0d64989c 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -39,8 +39,8 @@ of each common contribution: - a cleaner that deletes its rows with their session. Besides the Gleam standard library and `sqlight`, it imports only the SDK, its -own modules, `albedo/transfer`, and `albedo/text_scalars`. It passes every lint -rule without an exception. Its files are: +own modules, `albedo/transfer`, and the `albedo_bytes` package. It passes every +lint rule without an exception. Its files are: | File | Owns | | ------------------------------------------------------------------------------- | -------------------------------------------------------- | @@ -140,8 +140,9 @@ carries every row, and the rows stay until the session is deleted. The checklist refuses a 51st open item and a text over 500 Unicode scalars. When an item is checked off, `ledger.check` deletes the session's oldest checked items past 50 in the same transaction, through the `(session, done, id)` index. Scalars are -measured with [`text_scalars`](../src/albedo/text_scalars.gleam). The -`albedo_text_scalars` lint refuses the grapheme functions of `gleam/string`, +measured with +[`albedo_bytes/scalars`](../packages/albedo_bytes/src/albedo_bytes/scalars.gleam). +The `albedo_text_scalars` lint refuses the grapheme functions of `gleam/string`, such as `length`, under `src/`. To add a column later, call `storage.add_columns(db, "table", [#(name, type)])` diff --git a/gleam.toml b/gleam.toml index bdf23c041..f776ba903 100644 --- a/gleam.toml +++ b/gleam.toml @@ -26,6 +26,7 @@ gleam_http = ">= 4.4.0 and < 5.0.0" yamerl = ">= 0.10.0 and < 1.0.0" gleam_crypto = ">= 1.0.0 and < 2.0.0" # First-party workspace packages; see robot-docs/workspace-packages.md. +albedo_bytes = { path = "packages/albedo_bytes" } albedo_clock = { path = "packages/albedo_clock" } albedo_protect = { path = "packages/albedo_protect" } aws_event_stream = { path = "packages/aws_event_stream" } diff --git a/manifest.toml b/manifest.toml index 843cf5e8f..d2b8e4440 100644 --- a/manifest.toml +++ b/manifest.toml @@ -7,6 +7,7 @@ # You should check this file into your source control repository. packages = [ + { name = "albedo_bytes", version = "0.1.0", build_tools = ["gleam"], requirements = ["albedo_protect", "gleam_crypto", "gleam_erlang", "gleam_stdlib"], source = "local", path = "packages/albedo_bytes" }, { name = "albedo_clock", version = "0.1.0", build_tools = ["gleam"], requirements = ["albedo_protect", "gleam_erlang", "gleam_stdlib"], source = "local", path = "packages/albedo_clock" }, { name = "albedo_protect", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], source = "local", path = "packages/albedo_protect" }, { name = "aws_event_stream", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], source = "local", path = "packages/aws_event_stream" }, @@ -30,6 +31,7 @@ packages = [ ] [requirements] +albedo_bytes = { path = "packages/albedo_bytes" } albedo_clock = { path = "packages/albedo_clock" } albedo_protect = { path = "packages/albedo_protect" } aws_event_stream = { path = "packages/aws_event_stream" } diff --git a/packages/albedo_bytes/.gitignore b/packages/albedo_bytes/.gitignore new file mode 100644 index 000000000..796b96d1c --- /dev/null +++ b/packages/albedo_bytes/.gitignore @@ -0,0 +1 @@ +/build diff --git a/packages/albedo_bytes/gleam.toml b/packages/albedo_bytes/gleam.toml new file mode 100644 index 000000000..ebea6a37d --- /dev/null +++ b/packages/albedo_bytes/gleam.toml @@ -0,0 +1,16 @@ +name = "albedo_bytes" +version = "0.1.0" +description = "Binary, text and term helpers: Unicode scalar slices, digests and ids, bounded zlib, and safe external term decoding" +# The albedo repository's licence; see robot-docs/workspace-packages.md. +licences = ["AGPL-3.0-only"] +gleam = ">= 1.19.0" +target = "erlang" + +[dependencies] +gleam_stdlib = ">= 1.0.0 and < 2.0.0" +gleam_erlang = ">= 1.3.0 and < 2.0.0" +gleam_crypto = ">= 1.0.0 and < 2.0.0" +albedo_protect = { path = "../albedo_protect" } + +[dev_dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/packages/albedo_bytes/manifest.toml b/packages/albedo_bytes/manifest.toml new file mode 100644 index 000000000..6c7be1eac --- /dev/null +++ b/packages/albedo_bytes/manifest.toml @@ -0,0 +1,22 @@ +# Do not manually edit this file, it is managed by Gleam. +# +# This file locks the dependency versions used, to make your build +# deterministic and to prevent unexpected versions from being included +# in your application. +# +# You should check this file into your source control repository. + +packages = [ + { name = "albedo_protect", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], source = "local", path = "../albedo_protect" }, + { name = "gleam_crypto", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "2DE9E4EF53CF6FEE049D4F765731F7178F7A11AEFAE00EEE63BF7536B354AD3F" }, + { name = "gleam_erlang", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "1124AD3AA21143E5AF0FC5CF3D9529F6DB8CA03E43A55711B60B6B7B3874375C" }, + { name = "gleam_stdlib", version = "1.0.5", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "CEE5B6C076A85B45F60C585F4316C63EC8B7127C119D5738C3958A9C4D50404E" }, + { name = "gleeunit", version = "1.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "EC31ABA74256AEA531EDF8169931D775BBB384FED0A8A1BDC4DD9354E3E21826" }, +] + +[requirements] +albedo_protect = { path = "../albedo_protect" } +gleam_crypto = { version = ">= 1.0.0 and < 2.0.0" } +gleam_erlang = { version = ">= 1.3.0 and < 2.0.0" } +gleam_stdlib = { version = ">= 1.0.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } diff --git a/src/albedo/harness/sdk/bytes.gleam b/packages/albedo_bytes/src/albedo_bytes.gleam similarity index 100% rename from src/albedo/harness/sdk/bytes.gleam rename to packages/albedo_bytes/src/albedo_bytes.gleam diff --git a/src/albedo/harness/sdk/digest.gleam b/packages/albedo_bytes/src/albedo_bytes/digest.gleam similarity index 89% rename from src/albedo/harness/sdk/digest.gleam rename to packages/albedo_bytes/src/albedo_bytes/digest.gleam index 7c822e7e7..4ee4d3ecb 100644 --- a/src/albedo/harness/sdk/digest.gleam +++ b/packages/albedo_bytes/src/albedo_bytes/digest.gleam @@ -1,7 +1,7 @@ -//// Hashes, encodings and a constant-time comparison for extensions. +//// Hashes, encodings and a constant-time comparison. //// //// Encoders produce lowercase hex, padded standard base64, and unpadded -//// URL-safe base64. Providers build ids and signatures in their own modules. +//// URL-safe base64. Callers build their own ids and signatures from these. import gleam/bit_array import gleam/crypto diff --git a/src/albedo/harness/sdk/ids.gleam b/packages/albedo_bytes/src/albedo_bytes/ids.gleam similarity index 89% rename from src/albedo/harness/sdk/ids.gleam rename to packages/albedo_bytes/src/albedo_bytes/ids.gleam index 9425774fa..99245c203 100644 --- a/src/albedo/harness/sdk/ids.gleam +++ b/packages/albedo_bytes/src/albedo_bytes/ids.gleam @@ -1,10 +1,10 @@ -//// Random and unique identifiers for extensions. +//// Random and unique identifiers. //// //// Random ids come from the operating system's strong random source. A -//// provider that needs a particular id shape builds it from these and from +//// caller that needs a particular id shape builds it from these and from //// `digest` in its own module. -import albedo/harness/sdk/digest +import albedo_bytes/digest import gleam/crypto /// `bytes` random bytes as lowercase hex, `2 * bytes` characters long. diff --git a/src/albedo/text_scalars.gleam b/packages/albedo_bytes/src/albedo_bytes/scalars.gleam similarity index 100% rename from src/albedo/text_scalars.gleam rename to packages/albedo_bytes/src/albedo_bytes/scalars.gleam diff --git a/src/albedo/harness/sdk/term.gleam b/packages/albedo_bytes/src/albedo_bytes/term.gleam similarity index 100% rename from src/albedo/harness/sdk/term.gleam rename to packages/albedo_bytes/src/albedo_bytes/term.gleam diff --git a/src/albedo/harness/sdk/term_codec.gleam b/packages/albedo_bytes/src/albedo_bytes/term_codec.gleam similarity index 94% rename from src/albedo/harness/sdk/term_codec.gleam rename to packages/albedo_bytes/src/albedo_bytes/term_codec.gleam index 749114b6d..731165754 100644 --- a/src/albedo/harness/sdk/term_codec.gleam +++ b/packages/albedo_bytes/src/albedo_bytes/term_codec.gleam @@ -1,5 +1,5 @@ -//// Erlang's external term format, for caches and fingerprints whose bytes -//// earlier daemons wrote or compare. Use JSON for new persisted data. +//// Erlang's external term format, for caches and fingerprints whose bytes an +//// earlier release wrote or compares. Use JSON for new persisted data. import albedo_protect import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/sdk/zlib.gleam b/packages/albedo_bytes/src/albedo_bytes/zlib.gleam similarity index 98% rename from src/albedo/harness/sdk/zlib.gleam rename to packages/albedo_bytes/src/albedo_bytes/zlib.gleam index c16743fbf..0e3cbb013 100644 --- a/src/albedo/harness/sdk/zlib.gleam +++ b/packages/albedo_bytes/src/albedo_bytes/zlib.gleam @@ -1,6 +1,6 @@ //// zlib streams for small payloads a caller carries through an untrusted //// round trip. Inflation is bounded, so a hostile stream cannot make the -//// daemon allocate more than the caller allows. +//// VM allocate more than the caller allows. import albedo_protect import gleam/bit_array diff --git a/packages/albedo_bytes/test/albedo_bytes/digest_ids_test.gleam b/packages/albedo_bytes/test/albedo_bytes/digest_ids_test.gleam new file mode 100644 index 000000000..1a0270e13 --- /dev/null +++ b/packages/albedo_bytes/test/albedo_bytes/digest_ids_test.gleam @@ -0,0 +1,107 @@ +//// Hashes, encodings and ids that callers store or send. A change of hex +//// case, padding or bit layout would break a stored row or an upstream cache +//// without failing anything else, so each value here is pinned to an expected +//// encoding. The seeds cover empty input, Unicode text, leading zero bytes, +//// and a random id's alphabet. + +import albedo_bytes/digest +import albedo_bytes/ids +import gleam/list +import gleam/string +import gleeunit/should + +const unicode = "héllo wörld ✓ 🚀" + +/// SHA-256 of this seed begins with a zero byte. +const zero_sha256 = "seed-93" + +const lower_hex = "0123456789abcdef" + +const url_alphabet = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" + +fn only(text: String, allowed: String) -> Bool { + text + |> string.to_graphemes + |> list.all(fn(char) { string.contains(allowed, char) }) +} + +fn sha256_hex(text: String) -> String { + digest.sha256(<>) |> digest.hex +} + +pub fn sha256_hex_matches_stored_digests_test() -> Nil { + sha256_hex("") + |> should.equal( + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + ) + sha256_hex(unicode) + |> should.equal( + "c1a19c07e61550d4eb0bec9e7259bbf521b29ef90cede8cc2b5170f4046a7373", + ) + sha256_hex(zero_sha256) + |> should.equal( + "00f05cfecece11c31532119ada1dc2c85a47c28553c20462834a7241852dae2e", + ) +} + +pub fn sha1_hex_keeps_leading_zeros_test() -> Nil { + digest.sha1(<<"":utf8>>) + |> digest.hex + |> should.equal("da39a3ee5e6b4b0d3255bfef95601890afd80709") + digest.sha1(<<"seed-244":utf8>>) + |> digest.hex + |> should.equal("00bc9f79aeb6346203a80a37d9bbcb3c9d593b86") +} + +pub fn base64_is_padded_standard_test() -> Nil { + digest.base64(<<>>) |> should.equal("") + digest.base64(<<"a":utf8>>) |> should.equal("YQ==") + digest.base64(<<"ab":utf8>>) |> should.equal("YWI=") + digest.base64(<>) + |> should.equal("aMOpbGxvIHfDtnJsZCDinJMg8J+agA==") + digest.base64(<<0, 0, 1, 250, 251, 252, 253, 254, 255>>) + |> should.equal("AAAB+vv8/f7/") +} + +pub fn base64_url_is_unpadded_and_url_safe_test() -> Nil { + let bytes = <<0, 0, 1, 250, 251, 252, 253, 254, 255, 1>> + let text = digest.base64_url(bytes) + text |> should.equal("AAAB-vv8_f7_AQ") +} + +pub fn hmac_sha256_matches_signed_webhooks_test() -> Nil { + digest.hmac_sha256(<<>>, <<>>) + |> digest.hex + |> should.equal( + "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", + ) + digest.hmac_sha256(<<"{\"event\":\"push\"}":utf8>>, <>) + |> digest.hex + |> should.equal( + "3a8c46851a448660228bb8d6ffaa4fd93498e9e932bfc7d32f1548b1bcf97dba", + ) +} + +pub fn seeded_uuid_keeps_the_version_and_variant_layout_test() -> Nil { + ids.seeded_uuid("") |> should.equal("e3b0c442-98fc-4c14-9afb-f4c8996fb924") + ids.seeded_uuid("session-1") + |> should.equal("84097828-fc31-48c8-9292-10df48901a85") + ids.seeded_uuid(unicode) + |> should.equal("c1a19c07-e615-40d4-ab0b-ec9e7259bbf5") + ids.seeded_uuid(zero_sha256) + |> should.equal("00f05cfe-cece-41c3-9532-119ada1dc2c8") +} + +pub fn random_ids_have_their_length_and_alphabet_test() -> Nil { + let hex = ids.random_hex(16) + string.length(hex) |> should.equal(32) + only(hex, lower_hex) |> should.be_true + let url = ids.random_url(32) + string.length(url) |> should.equal(43) + only(url, url_alphabet) |> should.be_true + ids.random_hex(16) |> should.not_equal(hex) + let first = ids.unique_int() + let second = ids.unique_int() + { first > 0 && second > 0 && first != second } |> should.be_true +} diff --git a/packages/albedo_bytes/test/albedo_bytes/scalars_test.gleam b/packages/albedo_bytes/test/albedo_bytes/scalars_test.gleam new file mode 100644 index 000000000..0a0e48724 --- /dev/null +++ b/packages/albedo_bytes/test/albedo_bytes/scalars_test.gleam @@ -0,0 +1,28 @@ +//// Scalar and grapheme limits differ on combining marks. A slice must also +//// release the large binary it came from, which only its referenced size +//// shows. + +import albedo_bytes/scalars +import gleam/string +import gleeunit/should + +@external(erlang, "binary", "referenced_byte_size") +fn referenced_size(text: String) -> Int + +pub fn scalar_slices_preserve_combining_marks_and_utf8_boundaries_test() -> Nil { + let text = "a\u{0301}😀z" + scalars.length(text) |> should.equal(4) + scalars.take(text, 2) |> should.equal("a\u{0301}") + scalars.drop(text, 2) |> should.equal("😀z") + scalars.tail(text, 2) |> should.equal("😀z") + scalars.take(text, -1) |> should.equal("") + scalars.drop(text, -1) |> should.equal(text) + scalars.tail(text, -1) |> should.equal("") + scalars.drop(text, 10) |> should.equal("") + scalars.take("", 2) |> should.equal("") + let large = string.repeat("😀", 100_000) + let prefix = scalars.take(large, 1) + let suffix = scalars.tail(large, 1) + referenced_size(prefix) |> should.equal(4) + referenced_size(suffix) |> should.equal(4) +} diff --git a/packages/albedo_bytes/test/albedo_bytes_test.gleam b/packages/albedo_bytes/test/albedo_bytes_test.gleam new file mode 100644 index 000000000..8682be874 --- /dev/null +++ b/packages/albedo_bytes/test/albedo_bytes_test.gleam @@ -0,0 +1,49 @@ +//// A protocol decoder relies on three byte operations: a kept slice must not +//// pin the frame it came from, invalid UTF-8 must be refused rather than +//// passed on as text, and a line ends at whichever of LF or CR comes first. + +import albedo_bytes +import gleam/bit_array +import gleam/crypto +import gleeunit + +pub fn main() -> Nil { + gleeunit.main() +} + +pub fn a_small_slice_of_a_large_binary_is_copied_test() -> Nil { + let frame = crypto.strong_random_bytes(200_000) + // The VM itself copies a slice under 64 bytes. + let assert Ok(slice) = bit_array.slice(frame, 100_000, 1000) + let assert 200_000 = referenced_size(slice) + let assert 1000 = referenced_size(albedo_bytes.compact(slice)) + let small = <<"kept":utf8>> + let assert True = albedo_bytes.compact(small) == small + Nil +} + +pub fn utf8_refuses_invalid_bytes_test() -> Nil { + let assert Ok("héllo") = albedo_bytes.utf8(<<"héllo":utf8>>) + let assert Error(Nil) = albedo_bytes.utf8(<<0xff, 0xfe>>) + let assert Error(Nil) = albedo_bytes.utf8(<<"ab":utf8, 0xc3>>) + Nil +} + +pub fn a_line_ends_at_the_first_lf_or_cr_test() -> Nil { + let assert 2 = albedo_bytes.newline(<<"ab\ncd":utf8>>) + let assert 2 = albedo_bytes.newline(<<"ab\r\ncd":utf8>>) + let assert 1 = albedo_bytes.newline(<<"a\rb\nc":utf8>>) + let assert -1 = albedo_bytes.newline(<<"abc":utf8>>) + Nil +} + +pub fn base64_url_decodes_with_or_without_padding_test() -> Nil { + let assert Ok(<<0, 0, 1, 250, 251, 252, 253, 254, 255, 1>>) = + albedo_bytes.base64_url_decode("AAAB-vv8_f7_AQ") + let assert Ok(<<"a":utf8>>) = albedo_bytes.base64_url_decode("YQ==") + let assert Error(Nil) = albedo_bytes.base64_url_decode("not base64!") + Nil +} + +@external(erlang, "binary", "referenced_byte_size") +fn referenced_size(bytes: BitArray) -> Int diff --git a/priv/boot-modules.txt b/priv/boot-modules.txt index 1432de63d..eaf9b45f7 100644 --- a/priv/boot-modules.txt +++ b/priv/boot-modules.txt @@ -113,7 +113,7 @@ albedo@openai_api@stream albedo@openai_api@stream_http albedo@openai_api@transport albedo@openai_api@types -albedo@text_scalars +albedo_bytes@scalars albedo@daemon@bus_queues albedo@harness@sdk@transcript_codec albedo@harness@sdk@image_codec @@ -131,4 +131,4 @@ gleam_crypto_ffi yamerl_constr yamerl_parser albedo@openai_api@request_body -albedo@harness@sdk@bytes +albedo_bytes diff --git a/robot-docs/contributing.md b/robot-docs/contributing.md index 015ce4d66..ea120941d 100644 --- a/robot-docs/contributing.md +++ b/robot-docs/contributing.md @@ -72,10 +72,11 @@ timestamps and deadlines. Use system time for durable timestamps and monotonic time for elapsed durations. Request an explicit native time unit. Keep a scaled test timer and the deadline it checks consistent. -Use [`text_scalars`](../src/albedo/text_scalars.gleam) for scalar-bounded -previews and names. Do not allocate a codepoint list for the entire input just -to truncate a prefix. Scalar limits and grapheme limits serve different -purposes. +Use +[`albedo_bytes/scalars`](../packages/albedo_bytes/src/albedo_bytes/scalars.gleam) +for scalar-bounded previews and names. Do not allocate a codepoint list for the +entire input just to truncate a prefix. Scalar limits and grapheme limits serve +different purposes. Take a token estimate from [`size`](../src/albedo/harness/size.gleam). The `albedo_token_estimates` [lint](gleam-lint.md#text-and-token-rules) refuses a diff --git a/robot-docs/extensions.md b/robot-docs/extensions.md index 5921291ad..4f4c9a558 100644 --- a/robot-docs/extensions.md +++ b/robot-docs/extensions.md @@ -108,11 +108,11 @@ Every other kernel module is private to core, including termination, and cell storage. Reach the kernel through Python bindings and host routes, not by driving cells. Shared helpers outside the SDK are [`albedo_clock`](../packages/albedo_clock/src/albedo_clock.gleam), -[`albedo/text_scalars`](../src/albedo/text_scalars.gleam), and -[`albedo/actor_call`](../src/albedo/actor_call.gleam). Transfer adapters import -[`albedo/transfer`](../src/albedo/transfer.gleam) for the `Adapter`, `Table`, -`Shift`, and `Activation` types. Use supplied host or session callbacks for -daemon effects that the SDK does not expose. Follow the +[`albedo_bytes/scalars`](../packages/albedo_bytes/src/albedo_bytes/scalars.gleam), +and [`albedo/actor_call`](../src/albedo/actor_call.gleam). Transfer adapters +import [`albedo/transfer`](../src/albedo/transfer.gleam) for the `Adapter`, +`Table`, `Shift`, and `Activation` types. Use supplied host or session callbacks +for daemon effects that the SDK does not expose. Follow the [shared contribution rules](contributing.md). The existing SDK modules are: @@ -205,10 +205,11 @@ The existing SDK modules are: The SDK also provides these helpers. -- [`sdk/digest`](../src/albedo/harness/sdk/digest.gleam): Hash, hex, base64, - HMAC, and constant-time comparison helpers. -- [`sdk/ids`](../src/albedo/harness/sdk/ids.gleam): Random and unique identifier - helpers, including a version-4-shaped UUID derived from a seed. +- [`albedo_bytes/digest`](../packages/albedo_bytes/src/albedo_bytes/digest.gleam): + Hash, hex, base64, HMAC, and constant-time comparison helpers. +- [`albedo_bytes/ids`](../packages/albedo_bytes/src/albedo_bytes/ids.gleam): + Random and unique identifier helpers, including a version-4-shaped UUID + derived from a seed. - [`sdk/env`](../src/albedo/harness/sdk/env.gleam): Environment variable lookup, returning an empty string when unset. Read it during resolution or startup callbacks, never while constructing cached declarations. @@ -243,19 +244,19 @@ The SDK also provides these helpers. Whole settings documents such as `creds.json`, read and committed as a group through the settings store. Hold `with_lock` across a read and the commit that depends on it. `mcp_secrets` reads the `mcp` section of `creds.json`. -- [`sdk/term_codec`](../src/albedo/harness/sdk/term_codec.gleam): The Erlang - external term format for caches and fingerprints that earlier daemons wrote. - `decode_safe` refuses bytes that would create unknown atoms or functions. - `tuple` and `binary` check decoded shapes exactly. Use JSON for new persisted - data. +- [`albedo_bytes/term_codec`](../packages/albedo_bytes/src/albedo_bytes/term_codec.gleam): + The Erlang external term format for caches and fingerprints that earlier + daemons wrote. `decode_safe` refuses bytes that would create unknown atoms or + functions. `tuple` and `binary` check decoded shapes exactly. Use JSON for new + persisted data. - [`sdk/log`](../src/albedo/harness/sdk/log.gleam): Warning and debug messages for the daemon's log. Never include secrets or tokens. - [`sdk/process_flag`](../src/albedo/harness/sdk/process_flag.gleam): Flags in the calling process's dictionary that last as long as that process. Use a key type your module owns. -- [`sdk/zlib`](../src/albedo/harness/sdk/zlib.gleam): zlib compression and - inflation bounded by a caller's byte limit. A damaged stream or checksum - mismatch is an error. +- [`albedo_bytes/zlib`](../packages/albedo_bytes/src/albedo_bytes/zlib.gleam): + zlib compression and inflation bounded by a caller's byte limit. A damaged + stream or checksum mismatch is an error. - [`sdk/priv`](../src/albedo/harness/sdk/priv.gleam): Paths and reads of files shipped in albedo's priv directory, as `pin` pinned it at boot. A failed read names the path and the file error. @@ -965,11 +966,11 @@ owner. Obtaining this value does not allocate a client. [`sdk/profiles`](../src/albedo/harness/sdk/profiles.gleam) owns provider-profile validation and decoding, including generic settings reads. It does not require a session context. Native persistence and settings locking retain their existing -owners. [`sdk/bytes`](../src/albedo/harness/sdk/bytes.gleam), +owners. [`albedo_bytes`](../packages/albedo_bytes/src/albedo_bytes.gleam), [`sdk/text`](../src/albedo/harness/sdk/text.gleam), and -[`sdk/term`](../src/albedo/harness/sdk/term.gleam) provide binary framing, -native text conversion, and legacy term-order operations. Keep those conversions -at foreign-data boundaries. +[`albedo_bytes/term`](../packages/albedo_bytes/src/albedo_bytes/term.gleam) +provide binary framing, native text conversion, and legacy term-order +operations. Keep those conversions at foreign-data boundaries. [`sdk/opencode_gateway`](../src/albedo/harness/sdk/opencode_gateway.gleam) shares gateway request encoding, Responses replay reduction, and charged, diff --git a/robot-docs/mcp.md b/robot-docs/mcp.md index 0c49483c9..65da1f8b4 100644 --- a/robot-docs/mcp.md +++ b/robot-docs/mcp.md @@ -95,11 +95,11 @@ discovery retains that tool without mirrored headers. `catalogue.encode` and `catalogue.saved` write and read the existing version 2 external-term cache through -[`sdk/term_codec`](../src/albedo/harness/sdk/term_codec.gleam). The cache holds -atom-keyed maps, not Gleam records. Keep that saved shape compatible with -existing catalogues. `saved` decodes safely, so it creates the cache's atoms -first; add any new key to `cache_atoms`. `servers.fingerprint` keys each saved -catalogue, so keep its term encoding stable as well. +[`albedo_bytes/term_codec`](../packages/albedo_bytes/src/albedo_bytes/term_codec.gleam). +The cache holds atom-keyed maps, not Gleam records. Keep that saved shape +compatible with existing catalogues. `saved` decodes safely, so it creates the +cache's atoms first; add any new key to `cache_atoms`. `servers.fingerprint` +keys each saved catalogue, so keep its term encoding stable as well. [`client.gleam`](../src/albedo/harness/extensions/mcp/client.gleam) owns each connection's process and its stdio port, which it opens, writes and reads through [`sdk/port_program`](../src/albedo/harness/sdk/port_program.gleam). The diff --git a/robot-docs/proxy.md b/robot-docs/proxy.md index 2ced6574b..f716a86b4 100644 --- a/robot-docs/proxy.md +++ b/robot-docs/proxy.md @@ -72,9 +72,9 @@ has no way to turn its thinking off. [`chat.carry`](../src/albedo/harness/extensions/proxy/chat.gleam) carries provider-specific turn state for replay. The extension replays carried turns verbatim only to the profile that produced them. `chat.pack` compresses state -with [`sdk/zlib`](../src/albedo/harness/sdk/zlib.gleam) into URL-safe base64. -`chat.unpack` bounds decompression to 4 MiB. Invalid or altered packed state -takes the portable replay path. +with [`albedo_bytes/zlib`](../packages/albedo_bytes/src/albedo_bytes/zlib.gleam) +into URL-safe base64. `chat.unpack` bounds decompression to 4 MiB. Invalid or +altered packed state takes the portable replay path. The synthetic conversation identity can also key provider-owned metadata. [Antigravity](provider-requests.md#antigravity-metadata) persists a local diff --git a/robot-docs/runtime.md b/robot-docs/runtime.md index 5dde52c11..f52b06bf4 100644 --- a/robot-docs/runtime.md +++ b/robot-docs/runtime.md @@ -205,7 +205,7 @@ restores the previous scale when it is done. Clock readings are never scaled. ## Unicode scalar limits Use -[`text_scalars.length`, `take`, `drop`, and `tail`](../src/albedo/text_scalars.gleam) +[`scalars.length`, `take`, `drop`, and `tail`](../packages/albedo_bytes/src/albedo_bytes/scalars.gleam) for scalar-bounded names and previews. These functions scan UTF-8 and copy retained slices. Do not build a list for the entire input merely to truncate it. The diff --git a/robot-docs/workspace-packages.md b/robot-docs/workspace-packages.md index d823e49da..0351eba7e 100644 --- a/robot-docs/workspace-packages.md +++ b/robot-docs/workspace-packages.md @@ -27,13 +27,13 @@ dependencies, so an `albedo/...` import fails to build. A package therefore cannot call albedo helpers such as [`clock`](../packages/albedo_clock/src/albedo_clock.gleam), -[`text_scalars`](../src/albedo/text_scalars.gleam), or the SDK's -[`bytes`](../src/albedo/harness/sdk/bytes.gleam). Take what the package needs as -a parameter, such as the signing time, or write the small helper inside the -package. Prefer Gleam. A direct `@external` to an OTP function such as -`erlang:crc32` or `binary:copy` is acceptable. Add an `.erl` file only when -Gleam cannot express the binding. Use only Hex packages that albedo already -depends on, and add none without the maintainers' agreement. +[`albedo_bytes/scalars`](../packages/albedo_bytes/src/albedo_bytes/scalars.gleam), +or the SDK's [`bytes`](../packages/albedo_bytes/src/albedo_bytes.gleam). Take +what the package needs as a parameter, such as the signing time, or write the +small helper inside the package. Prefer Gleam. A direct `@external` to an OTP +function such as `erlang:crc32` or `binary:copy` is acceptable. Add an `.erl` +file only when Gleam cannot express the binding. Use only Hex packages that +albedo already depends on, and add none without the maintainers' agreement. Keep the public API shaped for an outside caller. Return typed errors rather than strings, and keep albedo policy, such as Bedrock's signed content type, in diff --git a/src/albedo/cell_title.gleam b/src/albedo/cell_title.gleam index 876dd8309..65b86225d 100644 --- a/src/albedo/cell_title.gleam +++ b/src/albedo/cell_title.gleam @@ -1,6 +1,6 @@ //// Shared python cell title extraction used by the daemon progress scanner and the python tool. -import albedo/text_scalars +import albedo_bytes/scalars import gleam/option.{type Option, None, Some} import gleam/string @@ -16,7 +16,7 @@ pub fn clean_title(line: String) -> String { /// Recursively drops leading '#' characters. pub fn strip_hashes(text: String) -> String { case string.starts_with(text, "#") { - True -> strip_hashes(text_scalars.drop(text, 1)) + True -> strip_hashes(scalars.drop(text, 1)) False -> text } } @@ -50,5 +50,5 @@ fn first_non_blank(lines: List(String)) -> Option(String) { pub fn first_line(source: String) -> String { line_title(source) |> option.unwrap("") - |> text_scalars.take(120) + |> scalars.take(120) } diff --git a/src/albedo/daemon/active_output_store.gleam b/src/albedo/daemon/active_output_store.gleam index a084b99eb..3b9567e26 100644 --- a/src/albedo/daemon/active_output_store.gleam +++ b/src/albedo/daemon/active_output_store.gleam @@ -7,8 +7,8 @@ //// decoding. Spill writers share one home lock; drafts lock per session. import albedo/daemon/disk -import albedo/harness/sdk/digest import albedo/harness/sdk/http_api +import albedo_bytes/digest import albedo_clock import albedo_protect import gleam/bit_array diff --git a/src/albedo/daemon/boot_code.gleam b/src/albedo/daemon/boot_code.gleam index 367f9cc21..10a00fe6b 100644 --- a/src/albedo/daemon/boot_code.gleam +++ b/src/albedo/daemon/boot_code.gleam @@ -7,7 +7,7 @@ import albedo/harness/sdk/getenv import albedo/harness/sdk/port_program import albedo/harness/sdk/priv -import albedo/harness/sdk/term +import albedo_bytes/term import albedo_protect import gleam/dict import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/daemon/build_identity.gleam b/src/albedo/daemon/build_identity.gleam index a5ff9e490..a559b04d4 100644 --- a/src/albedo/daemon/build_identity.gleam +++ b/src/albedo/daemon/build_identity.gleam @@ -7,7 +7,7 @@ import albedo/harness/extension_cache import albedo/harness/extensions -import albedo/harness/sdk/digest +import albedo_bytes/digest import gleam/bit_array import gleam/crypto import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/daemon/bus.gleam b/src/albedo/daemon/bus.gleam index 76a92266e..a9fb2f2d1 100644 --- a/src/albedo/daemon/bus.gleam +++ b/src/albedo/daemon/bus.gleam @@ -4,7 +4,7 @@ import albedo/daemon/bus_queues import albedo/harness/sdk/letter import albedo/harness/sdk/session_identity as identity -import albedo/text_scalars +import albedo_bytes/scalars import gleam/erlang/process import gleam/json @@ -127,7 +127,7 @@ pub fn mailed( } fn scalar_prefix(value: String, count: Int) -> String { - text_scalars.take(value, count) + scalars.take(value, count) } /// One subscriber's bounded queue, removed when its stream process dies. diff --git a/src/albedo/daemon/changes.gleam b/src/albedo/daemon/changes.gleam index f3e03faa2..26b971ac8 100644 --- a/src/albedo/daemon/changes.gleam +++ b/src/albedo/daemon/changes.gleam @@ -20,7 +20,7 @@ import albedo/harness/sdk/port_program import albedo/harness/sdk/subprocess import albedo/harness/ssh import albedo/harness/vcs -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/bool @@ -565,7 +565,7 @@ fn status_records(records: List(String), status: GitStatus) -> GitStatus { } fn short(commit: String) -> String { - text_scalars.take(commit, 7) + scalars.take(commit, 7) } /// A path as git prints it in a header: C-quoted when it holds a quote, a @@ -695,7 +695,7 @@ fn colocated_changes( }) let #(against, commit) = case root_commit(facts.base) { True -> #(empty_tree, None) - False -> #(facts.base, Some(text_scalars.take(facts.base, 12))) + False -> #(facts.base, Some(scalars.take(facts.base, 12))) } // jj tracks every file it is not told to ignore, so a file git does not // know yet is one jj has not snapshotted: new in `@`. @@ -753,7 +753,7 @@ fn jj_changes( |> list.sort(by_path) let commit = case root_commit(facts.base) { True -> None - False -> Some(text_scalars.take(facts.base, 12)) + False -> Some(scalars.take(facts.base, 12)) } Ok(Changes( kind: "jj", @@ -774,7 +774,7 @@ fn jj_header(header: String) -> #(Status, Option(String), String) { let after = fn(prefix) { list.find_map(lines, fn(line) { case string.starts_with(line, prefix) { - True -> Ok(text_scalars.drop(line, text_scalars.length(prefix))) + True -> Ok(scalars.drop(line, scalars.length(prefix))) False -> Error(Nil) } }) @@ -790,7 +790,7 @@ fn jj_header(header: String) -> #(Status, Option(String), String) { after("diff --git a/") |> result.map(fn(both) { // `P b/P`: the same path twice. - text_scalars.take(both, { text_scalars.length(both) - 3 } / 2) + scalars.take(both, { scalars.length(both) - 3 } / 2) }) }) |> result.unwrap("") diff --git a/src/albedo/daemon/child_failure.gleam b/src/albedo/daemon/child_failure.gleam index f48202b86..e77b9db41 100644 --- a/src/albedo/daemon/child_failure.gleam +++ b/src/albedo/daemon/child_failure.gleam @@ -8,7 +8,7 @@ import albedo/harness/sdk/letter.{ RateLimited, TimedOut, } import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/int import gleam/list import gleam/option.{type Option, Some} @@ -137,8 +137,8 @@ fn clean(message: String) -> String { |> string.split(" ") |> list.filter(fn(word) { word != "" }) |> string.join(" ") - case text_scalars.length(line) > longest_detail { - True -> text_scalars.take(line, longest_detail - 3) <> "..." + case scalars.length(line) > longest_detail { + True -> scalars.take(line, longest_detail - 3) <> "..." False -> line } } diff --git a/src/albedo/daemon/child_models.gleam b/src/albedo/daemon/child_models.gleam index 051f97025..755289457 100644 --- a/src/albedo/daemon/child_models.gleam +++ b/src/albedo/daemon/child_models.gleam @@ -12,7 +12,7 @@ import albedo/daemon/session_provider import albedo/harness/runtime import albedo/harness/sdk/model_lists import albedo/harness/sdk/profiles as configuration -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/dict import gleam/erlang/process @@ -73,11 +73,7 @@ fn resolve_with( list.find_map(profiles, fn(profile) { let prefix = profile.name <> "/" case string.starts_with(requested, prefix) { - True -> - Ok(#( - profile, - text_scalars.drop(requested, text_scalars.length(prefix)), - )) + True -> Ok(#(profile, scalars.drop(requested, scalars.length(prefix)))) False -> Error(Nil) } }) diff --git a/src/albedo/daemon/context_snapshot.gleam b/src/albedo/daemon/context_snapshot.gleam index 9420aa2c7..15052f574 100644 --- a/src/albedo/daemon/context_snapshot.gleam +++ b/src/albedo/daemon/context_snapshot.gleam @@ -7,7 +7,7 @@ import albedo/harness/sdk/message_content as events import albedo/harness/size import albedo/openai_api/request as provider_request import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/crypto import gleam/int @@ -698,7 +698,7 @@ pub fn page_count(content: String) -> Int { } fn scalar_count(content: String) -> Int { - text_scalars.length(content) + scalars.length(content) } fn scalar_page_count(content: String, scalars_per_page: Int) -> Int { @@ -708,7 +708,7 @@ fn scalar_page_count(content: String, scalars_per_page: Int) -> Int { /// Page `index` of `content`, copied so a small page does not keep the whole /// rendered history alive. fn scalar_page(content: String, index: Int, scalars_per_page: Int) -> String { - text_scalars.slice(content, index * scalars_per_page, scalars_per_page) + scalars.slice(content, index * scalars_per_page, scalars_per_page) } fn kind_name(kind: SectionKind) -> String { diff --git a/src/albedo/daemon/conversation.gleam b/src/albedo/daemon/conversation.gleam index 1e8037a1a..6b1c633f2 100644 --- a/src/albedo/daemon/conversation.gleam +++ b/src/albedo/daemon/conversation.gleam @@ -31,7 +31,7 @@ import albedo/harness/sdk/transcript.{type FoldStep, Continue, Stop} import albedo/harness/sdk/transcript_codec.{type SourceRow} as codec import albedo/openai_api/cost import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bool import gleam/dynamic/decode import gleam/int @@ -633,7 +633,7 @@ fn index_preview_in( use _ <- result.try(case latest_user([#(input, kind)]) { None -> Ok(Nil) Some(text) -> { - let prefix = text_scalars.take(text, 256) + let prefix = scalars.take(text, 256) store.run( db, "UPDATE sessions SET preview_text=?,preview_truncated=?,preview_position=? WHERE id=? AND preview_position Result(Nil, String) { let text = option.unwrap(latest, "") - let prefix = text_scalars.take(text, 256) + let prefix = scalars.take(text, 256) store.run( db, "UPDATE sessions SET preview_text=?,preview_truncated=?,preview_position=? WHERE id=?", @@ -1391,8 +1391,8 @@ pub fn title(text: String) -> String { /// cut with an ellipsis. pub fn excerpt(text: String, limit: Int) -> String { let clean = session_configuration.clean_name(text) |> option.unwrap("") - case text_scalars.length(clean) > limit { - True -> text_scalars.take(clean, limit - 1) <> "…" + case scalars.length(clean) > limit { + True -> scalars.take(clean, limit - 1) <> "…" False -> clean } } diff --git a/src/albedo/daemon/events.gleam b/src/albedo/daemon/events.gleam index ece6432e7..193bec640 100644 --- a/src/albedo/daemon/events.gleam +++ b/src/albedo/daemon/events.gleam @@ -3,7 +3,7 @@ import albedo/harness/runtime/state as runtime_state import albedo/harness/size -import albedo/text_scalars +import albedo_bytes/scalars import albedo/daemon/http_history import albedo/daemon/mail @@ -511,11 +511,11 @@ pub fn activity(value: session_activity.Projection) -> json.Json { session_activity.Projection(..value, lines: case first.text { "" -> rest text -> { - let count = text_scalars.length(text) + let count = scalars.length(text) [ session_activity.Line( first.kind, - text_scalars.drop(text, int.max(1, count / 2)), + scalars.drop(text, int.max(1, count / 2)), ), ..rest ] diff --git a/src/albedo/daemon/folders.gleam b/src/albedo/daemon/folders.gleam index 7dd5f0f97..82cfa9ad1 100644 --- a/src/albedo/daemon/folders.gleam +++ b/src/albedo/daemon/folders.gleam @@ -13,7 +13,7 @@ import albedo/harness/sdk/file_status import albedo/harness/sdk/getenv import albedo/harness/ssh import albedo/harness/vcs -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/bool import gleam/dict.{type Dict} @@ -753,8 +753,8 @@ fn changed_at(changed: List(String), name: String) -> Int { fn subpath(root: String, dir: String) -> String { case dir == root, root { True, _ -> "" - False, "/" -> text_scalars.drop(dir, 1) - False, _ -> text_scalars.drop(dir, text_scalars.length(root) + 1) + False, "/" -> scalars.drop(dir, 1) + False, _ -> scalars.drop(dir, scalars.length(root) + 1) } } @@ -762,12 +762,12 @@ fn subpath(root: String, dir: String) -> String { /// (git's untracked directories) is dropped. fn inside(path: String, prefix: String) -> Result(String, Nil) { let path = case string.ends_with(path, "/") { - True -> text_scalars.take(path, text_scalars.length(path) - 1) + True -> scalars.take(path, scalars.length(path) - 1) False -> path } case prefix, string.starts_with(path, prefix <> "/") { "", _ -> Ok(path) - _, True -> Ok(text_scalars.drop(path, text_scalars.length(prefix) + 1)) + _, True -> Ok(scalars.drop(path, scalars.length(prefix) + 1)) _, False -> Error(Nil) } } diff --git a/src/albedo/daemon/http_api.gleam b/src/albedo/daemon/http_api.gleam index ba72a27d3..a28c5889b 100644 --- a/src/albedo/daemon/http_api.gleam +++ b/src/albedo/daemon/http_api.gleam @@ -12,7 +12,7 @@ import albedo/harness/context_limits import albedo/harness/location import albedo/harness/sdk/editor_mcp import albedo/harness/sdk/http_api as sdk_http_api -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/bool @@ -217,7 +217,7 @@ fn trailers( /// replaced, a single final newline is allowed. fn is_token(name: String) -> Bool { let name = case string.ends_with(name, "\n") { - True -> text_scalars.take(name, text_scalars.length(name) - 1) + True -> scalars.take(name, scalars.length(name) - 1) False -> name } name != "" && all_bytes(<>, is_token_byte) diff --git a/src/albedo/daemon/http_auth.gleam b/src/albedo/daemon/http_auth.gleam index fb7b3b14c..0fa47efe3 100644 --- a/src/albedo/daemon/http_auth.gleam +++ b/src/albedo/daemon/http_auth.gleam @@ -17,7 +17,7 @@ import albedo/harness/sdk/http_api as sdk_http_api import albedo/harness/sdk/oauth import albedo/harness/sdk/oauth_flows import albedo/harness/settings/composition -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/bytes_tree import gleam/dict.{type Dict} @@ -397,7 +397,7 @@ pub fn login( )) use _ <- result.try( case - text_scalars.length(supplied) > 0 + scalars.length(supplied) > 0 && bit_array.byte_size(bit_array.from_string(supplied)) <= 16_384 { True -> Ok(Nil) diff --git a/src/albedo/daemon/http_history.gleam b/src/albedo/daemon/http_history.gleam index 5aa36fbba..be2889b83 100644 --- a/src/albedo/daemon/http_history.gleam +++ b/src/albedo/daemon/http_history.gleam @@ -11,7 +11,7 @@ import albedo/harness/sdk/message_content as events import albedo/harness/sdk/transcript import albedo/harness/skills/catalog as skills import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic/decode import gleam/int import gleam/json @@ -631,7 +631,7 @@ pub fn checkpoint(entry: Entry) -> Option(json.Json) { json.object([ #("text", json.string(sdk_http_api.scalar_prefix(text, 256))), #("transcript_count", json.int(1)), - #("truncated", json.bool(text_scalars.length(text) > 256)), + #("truncated", json.bool(scalars.length(text) > 256)), ]), ), ]) diff --git a/src/albedo/daemon/http_transcript.gleam b/src/albedo/daemon/http_transcript.gleam index 842d9ca4e..fdea61f0c 100644 --- a/src/albedo/daemon/http_transcript.gleam +++ b/src/albedo/daemon/http_transcript.gleam @@ -13,7 +13,7 @@ import albedo/harness/sdk/http_api as sdk_http_api import albedo/harness/sdk/images import albedo/harness/sdk/store import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bytes_tree import gleam/dynamic/decode import gleam/http/request @@ -577,7 +577,7 @@ fn content_entry_ids( True -> case list.any(ids, fn(entry_id) { - entry_id == "" || text_scalars.length(entry_id) > 128 + entry_id == "" || scalars.length(entry_id) > 128 }) { True -> Error(invalid) diff --git a/src/albedo/daemon/inbox.gleam b/src/albedo/daemon/inbox.gleam index 7ed89b811..7f290554b 100644 --- a/src/albedo/daemon/inbox.gleam +++ b/src/albedo/daemon/inbox.gleam @@ -11,7 +11,7 @@ import albedo/harness/sdk/letter.{type Letter, Letter} as mail_value import albedo/harness/sdk/queue import albedo/harness/sdk/store import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bool import gleam/dict import gleam/list @@ -228,7 +228,7 @@ pub fn items( fn item(row: mail.Waiting) -> queue.Item { let submission = submission(row) - let text = text_scalars.take(submission.display, preview_scalars) + let text = scalars.take(submission.display, preview_scalars) queue.Item( id: row.letter.id, kind: mail.kind_name(row.letter.kind), diff --git a/src/albedo/daemon/inspection.gleam b/src/albedo/daemon/inspection.gleam index 36b2c6874..da3cab849 100644 --- a/src/albedo/daemon/inspection.gleam +++ b/src/albedo/daemon/inspection.gleam @@ -9,8 +9,8 @@ //// `$ALBEDO_HOME/inspect.log` every N seconds. import albedo/daemon/lifecycle -import albedo/harness/sdk/digest import albedo/harness/sdk/getenv +import albedo_bytes/digest import albedo_clock import gleam/bit_array import gleam/crypto diff --git a/src/albedo/daemon/lifecycle.gleam b/src/albedo/daemon/lifecycle.gleam index bae019604..a8daf473b 100644 --- a/src/albedo/daemon/lifecycle.gleam +++ b/src/albedo/daemon/lifecycle.gleam @@ -4,9 +4,9 @@ import albedo/daemon/boot_code import albedo/daemon/build_identity import albedo/daemon/standby_handover -import albedo/harness/sdk/digest import albedo/harness/sdk/getenv import albedo/harness/sdk/port_program +import albedo_bytes/digest import albedo_protect import gleam/bit_array import gleam/crypto diff --git a/src/albedo/daemon/mail.gleam b/src/albedo/daemon/mail.gleam index f7b766510..2f543d661 100644 --- a/src/albedo/daemon/mail.gleam +++ b/src/albedo/daemon/mail.gleam @@ -20,7 +20,6 @@ import albedo/daemon/migrations/mail_kinds import albedo/daemon/usage import albedo/harness/sdk/family import albedo/harness/sdk/http_api -import albedo/harness/sdk/ids import albedo/harness/sdk/input import albedo/harness/sdk/letter.{ Answer, Event, Input, Job, Letter, Message, Note, Receipt, Stopped, Webhook, @@ -30,7 +29,8 @@ import albedo/harness/sdk/session_identity as identity import albedo/harness/sdk/session_info import albedo/harness/sdk/storage import albedo/harness/sdk/store -import albedo/text_scalars +import albedo_bytes/ids +import albedo_bytes/scalars import gleam/bool import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode @@ -878,7 +878,7 @@ pub fn record_failure( store.write( db, "UPDATE mail SET attempts=attempts+1,last_error=? WHERE id=? AND " <> waits, - [sqlight.text(text_scalars.take(reason, 500)), sqlight.text(id)], + [sqlight.text(scalars.take(reason, 500)), sqlight.text(id)], ) } diff --git a/src/albedo/daemon/migrations/session_context_blocks.gleam b/src/albedo/daemon/migrations/session_context_blocks.gleam index 10b7289e4..ad466d30b 100644 --- a/src/albedo/daemon/migrations/session_context_blocks.gleam +++ b/src/albedo/daemon/migrations/session_context_blocks.gleam @@ -6,7 +6,7 @@ import albedo/harness/sdk/context_block.{type ContextBlock, ContextBlock} import albedo/harness/sdk/store import albedo/harness/sdk/transcript_codec import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic/decode import gleam/list import gleam/result @@ -72,10 +72,7 @@ fn block(text: String) -> ContextBlock { Ok(#(name, rest)) -> ContextBlock( name, - text_scalars.take( - rest, - text_scalars.length(rest) - text_scalars.length(closing), - ), + scalars.take(rest, scalars.length(rest) - scalars.length(closing)), ) Error(Nil) -> ContextBlock(context_block.append_system, text) } diff --git a/src/albedo/daemon/migrations/transcript_kinds.gleam b/src/albedo/daemon/migrations/transcript_kinds.gleam index 5702fdffa..8d6aa0e52 100644 --- a/src/albedo/daemon/migrations/transcript_kinds.gleam +++ b/src/albedo/daemon/migrations/transcript_kinds.gleam @@ -9,7 +9,7 @@ import albedo/harness/sdk/transcript.{type Kind} import albedo/harness/sdk/transcript_codec import albedo/harness/skills/catalog as skills import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic/decode import gleam/json import gleam/list @@ -122,19 +122,13 @@ fn note(text: String) -> Option(#(String, String)) { False -> None True -> { let inner = - text_scalars.take( - text, - text_scalars.length(text) - text_scalars.length(note_close), - ) + scalars.take(text, scalars.length(text) - scalars.length(note_close)) case string.split_once(inner, ">") { Ok(#(" Some(#("note", body)) Ok(#(" origin, body)) -> case string.ends_with(origin, "\"") { True -> - Some(#( - text_scalars.take(origin, text_scalars.length(origin) - 1), - body, - )) + Some(#(scalars.take(origin, scalars.length(origin) - 1), body)) False -> None } _ -> None diff --git a/src/albedo/daemon/refusal.gleam b/src/albedo/daemon/refusal.gleam index 74a606da7..248ad310d 100644 --- a/src/albedo/daemon/refusal.gleam +++ b/src/albedo/daemon/refusal.gleam @@ -6,7 +6,7 @@ import albedo/harness/location import albedo/harness/sdk/http_api as sdk_http_api import albedo/harness/sdk/profiles -import albedo/text_scalars +import albedo_bytes/scalars import gleam/int import gleam/result @@ -180,7 +180,7 @@ pub fn failure(refusal: Refusal) -> sdk_http_api.Failure { sdk_http_api.Failure( 409, "child_address_conflict", - text_scalars.take("a child named '" <> name <> "' already exists", 4096), + scalars.take("a child named '" <> name <> "' already exists", 4096), ) DepthLimit(deepest) -> sdk_http_api.Failure( @@ -455,7 +455,7 @@ pub fn failure(refusal: Refusal) -> sdk_http_api.Failure { sdk_http_api.Failure( 503, "request_unavailable", - text_scalars.take(detail, 4096), + scalars.take(detail, 4096), ) } } diff --git a/src/albedo/daemon/registry.gleam b/src/albedo/daemon/registry.gleam index bbc0bc011..9c12526eb 100644 --- a/src/albedo/daemon/registry.gleam +++ b/src/albedo/daemon/registry.gleam @@ -2,13 +2,13 @@ import albedo/daemon/lifecycle import albedo/harness/sdk/family as families -import albedo/harness/sdk/ids import albedo/harness/sdk/input import albedo/harness/sdk/letter as mail_value import albedo/harness/sdk/oauth_flows import albedo/harness/sdk/session_control as agents import albedo/harness/sdk/session_identity as identity import albedo/harness/sdk/session_info +import albedo_bytes/ids import albedo/actor_call import albedo/harness/sdk/storage diff --git a/src/albedo/daemon/requests.gleam b/src/albedo/daemon/requests.gleam index ab64b36ef..7efef0ed7 100644 --- a/src/albedo/daemon/requests.gleam +++ b/src/albedo/daemon/requests.gleam @@ -18,7 +18,7 @@ import albedo/harness/sdk/request_history.{Prefix} import albedo/harness/sdk/store import albedo/openai_api/cost import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/crypto import gleam/dict @@ -83,8 +83,8 @@ pub fn outcome(result: Result(a, types.Error)) -> Outcome { const detail_limit = 2000 fn bounded(text: String) -> String { - case text_scalars.length(text) > detail_limit { - True -> text_scalars.take(text, detail_limit) <> "…[truncated]" + case scalars.length(text) > detail_limit { + True -> scalars.take(text, detail_limit) <> "…[truncated]" False -> text } } @@ -212,7 +212,7 @@ fn short_hex(digest: BitArray) -> String { digest |> bit_array.base16_encode |> string.lowercase - |> text_scalars.take(12) + |> scalars.take(12) } fn input_identity(input: types.Input) -> String { diff --git a/src/albedo/daemon/session_activity.gleam b/src/albedo/daemon/session_activity.gleam index 7cf3ccf69..1d3a0610c 100644 --- a/src/albedo/daemon/session_activity.gleam +++ b/src/albedo/daemon/session_activity.gleam @@ -2,7 +2,7 @@ import albedo/harness/sdk/session_info -import albedo/text_scalars +import albedo_bytes/scalars import albedo/daemon/mail import albedo/daemon/turn @@ -158,7 +158,7 @@ pub fn output( _ -> Projection( ..append(projection, kind, text), - output_scalars: projection.output_scalars + text_scalars.length(text), + output_scalars: projection.output_scalars + scalars.length(text), output_utf8_bytes: projection.output_utf8_bytes + string.byte_size(text), streamed_answer: projection.streamed_answer || kind == AssistantLine, ) @@ -229,7 +229,7 @@ fn append_lines( } fn tail(text: String) -> String { - text_scalars.tail(text, 256) + scalars.tail(text, 256) } /// A new parent request invalidates progress belonging to the preceding work. diff --git a/src/albedo/daemon/session_configuration.gleam b/src/albedo/daemon/session_configuration.gleam index 356abed1d..8c71037dc 100644 --- a/src/albedo/daemon/session_configuration.gleam +++ b/src/albedo/daemon/session_configuration.gleam @@ -1,7 +1,7 @@ //// Durable editable session state. Callers validate runtime policy before //// committing a candidate on the shared store connection. -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import albedo/daemon/family @@ -125,7 +125,7 @@ pub fn clean_name(name: String) -> Option(String) { |> string.split(" ") |> list.filter(fn(part) { part != "" }) |> string.join(" ") - |> text_scalars.take(4096) + |> scalars.take(4096) |> string.trim case clean { "" -> None diff --git a/src/albedo/daemon/session_deletion.gleam b/src/albedo/daemon/session_deletion.gleam index 5c1c10343..de26044fa 100644 --- a/src/albedo/daemon/session_deletion.gleam +++ b/src/albedo/daemon/session_deletion.gleam @@ -1,6 +1,6 @@ //// Delete an observed family with bounded results and actual stop outcomes. -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import albedo/daemon/acp_attachments @@ -168,7 +168,7 @@ fn walk( dict.insert( progress.failures, member.id, - text_scalars.take(error, 256), + scalars.take(error, 256), ) False -> progress.failures }, diff --git a/src/albedo/daemon/session_namespace.gleam b/src/albedo/daemon/session_namespace.gleam index c672883e6..617a48927 100644 --- a/src/albedo/daemon/session_namespace.gleam +++ b/src/albedo/daemon/session_namespace.gleam @@ -17,7 +17,7 @@ import albedo/harness/sdk/input import albedo/harness/sdk/letter import albedo/harness/sdk/storage import albedo/harness/ssh -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bool import gleam/erlang/process import gleam/int @@ -715,7 +715,7 @@ type Running { } fn short(text: String) -> String { - text_scalars.take(string.trim(text), 100) + scalars.take(string.trim(text), 100) } /// Built from what the port owner observes and the cells journal, never from diff --git a/src/albedo/daemon/session_naming.gleam b/src/albedo/daemon/session_naming.gleam index 583ae9fd8..d67c38f87 100644 --- a/src/albedo/daemon/session_naming.gleam +++ b/src/albedo/daemon/session_naming.gleam @@ -1,7 +1,7 @@ //// Who named a session, the model's own choice, and bounded title inference. -import albedo/harness/sdk/ids import albedo/harness/sdk/session_info +import albedo_bytes/ids import albedo/daemon/bus import albedo/daemon/conversation @@ -24,7 +24,7 @@ import albedo/harness/sdk/storage import albedo/harness/sdk/store import albedo/openai_api import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import albedo_protect import gleam/dynamic/decode @@ -110,7 +110,7 @@ pub fn suggest( use name <- result.try(case config.clean_name(raw) { None -> Error("a name needs some words") Some(n) -> - case text_scalars.length(n) > longest { + case scalars.length(n) > longest { True -> Error( "keep the name under " @@ -221,12 +221,7 @@ pub fn maybe_start( Some(raw) -> case claim(state), string.trim(raw), state.title_job { Ok(True), text, None if text != "" -> - start_job( - state, - Some(order), - Some(text_scalars.take(text, 2000)), - on_done, - ) + start_job(state, Some(order), Some(scalars.take(text, 2000)), on_done) _, _, _ -> state } } @@ -438,7 +433,7 @@ fn generate( json.to_string( json.object([ #("task", json.string("session_title")), - #("message", json.string(text_scalars.take(text, 8000))), + #("message", json.string(scalars.take(text, 8000))), ]), ) let base = openai_api.request(info.model, [types.User(input)]) @@ -479,7 +474,7 @@ fn generate( "the title model returned no visible name", ) }) - |> result.map(text_scalars.take(_, longest)) + |> result.map(scalars.take(_, longest)) } fn context(ledger: store.Store, session: String) -> Result(String, String) { @@ -503,7 +498,7 @@ fn context(ledger: store.Store, session: String) -> Result(String, String) { } case string.trim(raw) { "" -> Error(Nil) - t -> Ok("user: " <> text_scalars.take(t, 2000)) + t -> Ok("user: " <> scalars.take(t, 2000)) } }) use _ <- result.try(case msgs { @@ -527,6 +522,6 @@ fn context(ledger: store.Store, session: String) -> Result(String, String) { }) |> list.take(4) |> list.reverse - |> list.map(fn(t) { "assistant: " <> text_scalars.take(t, 1000) }) - text_scalars.take(string.join(list.append(messages, replies), "\n\n"), 8000) + |> list.map(fn(t) { "assistant: " <> scalars.take(t, 1000) }) + scalars.take(string.join(list.append(messages, replies), "\n\n"), 8000) } diff --git a/src/albedo/daemon/session_run.gleam b/src/albedo/daemon/session_run.gleam index 4107cbef3..16acddf8c 100644 --- a/src/albedo/daemon/session_run.gleam +++ b/src/albedo/daemon/session_run.gleam @@ -16,11 +16,11 @@ import albedo/harness/repetition import albedo/harness/runtime import albedo/harness/sdk/accounts import albedo/harness/sdk/extension -import albedo/harness/sdk/ids import albedo/harness/sdk/store import albedo/harness/sdk/transcript import albedo/openai_api/types import albedo/process_helpers +import albedo_bytes/ids import gleam/erlang/process.{type Subject} import gleam/list import gleam/option.{type Option, None, Some} diff --git a/src/albedo/daemon/session_salvage.gleam b/src/albedo/daemon/session_salvage.gleam index 274d1c501..80526ae93 100644 --- a/src/albedo/daemon/session_salvage.gleam +++ b/src/albedo/daemon/session_salvage.gleam @@ -17,7 +17,7 @@ import albedo/harness/sdk/projection import albedo/harness/sdk/store import albedo/harness/sdk/transcript import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/int import gleam/io import gleam/json @@ -263,8 +263,8 @@ fn unwritten(tools: List(String)) -> String { } fn bounded(error: String) -> String { - case text_scalars.length(error) > 500 { - True -> text_scalars.take(error, 500) <> "…" + case scalars.length(error) > 500 { + True -> scalars.take(error, 500) <> "…" False -> error } } diff --git a/src/albedo/daemon/session_side_question.gleam b/src/albedo/daemon/session_side_question.gleam index 024517a71..dbd7d09ae 100644 --- a/src/albedo/daemon/session_side_question.gleam +++ b/src/albedo/daemon/session_side_question.gleam @@ -11,13 +11,13 @@ import albedo/daemon/usage import albedo/harness/loop import albedo/harness/runtime import albedo/harness/sdk/history -import albedo/harness/sdk/ids import albedo/harness/sdk/note import albedo/harness/sdk/profiles as configuration import albedo/harness/sdk/storage import albedo/harness/sdk/store import albedo/harness/sdk/transcript import albedo/openai_api/types +import albedo_bytes/ids import gleam/dynamic/decode import gleam/erlang/process import gleam/list diff --git a/src/albedo/daemon/storage_report.gleam b/src/albedo/daemon/storage_report.gleam index 8df3eca00..77abb67b7 100644 --- a/src/albedo/daemon/storage_report.gleam +++ b/src/albedo/daemon/storage_report.gleam @@ -4,7 +4,7 @@ import albedo/daemon/disk import albedo/harness/kernel/cell_storage import albedo/harness/sdk/store -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/bool import gleam/dict @@ -177,10 +177,7 @@ fn eligible(kind: StorageKind, name: String, known: set.Set(String)) -> Bool { case kind { Kernel -> string.ends_with(name, ".state") - && !set.contains( - known, - text_scalars.take(name, text_scalars.length(name) - 6), - ) + && !set.contains(known, scalars.take(name, scalars.length(name) - 6)) Backup -> string.starts_with(name, "albedo-before-image-store-") && string.ends_with(name, ".sqlite") diff --git a/src/albedo/daemon/tool_progress.gleam b/src/albedo/daemon/tool_progress.gleam index 4d8554853..687c40bcf 100644 --- a/src/albedo/daemon/tool_progress.gleam +++ b/src/albedo/daemon/tool_progress.gleam @@ -3,7 +3,7 @@ import albedo/cell_title import albedo/harness/kernel/tool as cell_tool import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dict.{type Dict} import gleam/int import gleam/list @@ -390,7 +390,7 @@ fn snapshot(call: Call) -> Snapshot { pub fn call_title(call: types.ToolCall) -> Option(String) { case call.name { name if name == cell_tool.name -> { - let prefix = text_scalars.take(call.arguments, 512) + let prefix = scalars.take(call.arguments, 512) let s = scan(scanner(), prefix) resolve_title(s) } @@ -442,7 +442,7 @@ fn finish_head_line(scanner: Scanner) -> Scanner { let cleaned = cell_title.clean_title(line) let title = case cleaned { "" -> None - _ -> Some(text_scalars.take(cleaned, 160)) + _ -> Some(scalars.take(cleaned, 160)) } Scanner( ..scanner, @@ -661,7 +661,7 @@ fn unicode_escape(scanner: Scanner, character: String) -> Scanner { _ -> "" } let digits = digits <> character - case text_scalars.length(digits) == 4 { + case scalars.length(digits) == 4 { False -> Scanner(..scanner, code_escape: Unicode(digits)) True -> case int.base_parse(digits, 16) { diff --git a/src/albedo/daemon/transfer_files.gleam b/src/albedo/daemon/transfer_files.gleam index e0f60aa05..3a35b4518 100644 --- a/src/albedo/daemon/transfer_files.gleam +++ b/src/albedo/daemon/transfer_files.gleam @@ -3,10 +3,10 @@ //// //// Errors name the path and the posix reason, as in `"/path: enoent"`. -import albedo/harness/sdk/digest import albedo/harness/sdk/file_status import albedo/harness/sdk/port_program -import albedo/harness/sdk/term +import albedo_bytes/digest +import albedo_bytes/term import gleam/bit_array import gleam/crypto import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/account_migration.gleam b/src/albedo/harness/account_migration.gleam index 96dd0e77a..bb89e721a 100644 --- a/src/albedo/harness/account_migration.gleam +++ b/src/albedo/harness/account_migration.gleam @@ -13,11 +13,11 @@ import albedo/harness/file_info import albedo/harness/sdk/credential_store.{type Object} -import albedo/harness/sdk/digest import albedo/harness/sdk/getenv import albedo/harness/sdk/key_accounts import albedo/harness/sdk/profiles import albedo/harness/sdk/settings_store +import albedo_bytes/digest import gleam/crypto import gleam/dict import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/capability_catalog.gleam b/src/albedo/harness/capability_catalog.gleam index 7d6a04cae..8dd9f90c3 100644 --- a/src/albedo/harness/capability_catalog.gleam +++ b/src/albedo/harness/capability_catalog.gleam @@ -2,12 +2,12 @@ import albedo/harness/capabilities import albedo/harness/instruction_files -import albedo/harness/sdk/digest import albedo/harness/sdk/extension import albedo/harness/sdk/storage import albedo/harness/sdk/store -import albedo/harness/sdk/term_codec import albedo/harness/skills/catalog as skills +import albedo_bytes/digest +import albedo_bytes/term_codec import gleam/bit_array import gleam/list import gleam/option.{type Option, None, Some} diff --git a/src/albedo/harness/compaction.gleam b/src/albedo/harness/compaction.gleam index 6fb512c6c..3378fdbc2 100644 --- a/src/albedo/harness/compaction.gleam +++ b/src/albedo/harness/compaction.gleam @@ -7,7 +7,7 @@ import albedo/harness/sdk/store import albedo/harness/size import albedo/openai_api/reasoning import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic/decode import gleam/int import gleam/json @@ -118,7 +118,7 @@ pub fn reply_text( _, types.Complete | _, types.ToolCalls -> Ok(Summary(text, False, written_by)) _, types.LengthLimit -> - case text_scalars.length(text) >= kept_cut_off_chars { + case scalars.length(text) >= kept_cut_off_chars { True -> Ok(Summary(text <> "\n\n" <> cut_off_mark, True, written_by)) False -> Error(Unfinished( @@ -554,8 +554,8 @@ fn quoted(text: String) -> String { |> list.map(string.trim) |> list.filter(fn(line) { line != "" }) |> string.join(" ") - case text_scalars.length(flat) > 160 { - True -> "\"" <> text_scalars.take(flat, 160) <> "…\"" + case scalars.length(flat) > 160 { + True -> "\"" <> scalars.take(flat, 160) <> "…\"" False -> "\"" <> flat <> "\"" } } @@ -1153,9 +1153,9 @@ fn summary_output(text: String) -> String { } fn summary_bounded(text: String, limit: Int) -> String { - case text_scalars.length(text) > limit { + case scalars.length(text) > limit { True -> - text_scalars.take(text, limit) + scalars.take(text, limit) <> "\n[remainder omitted from compaction summary input]" False -> text } diff --git a/src/albedo/harness/context_limits.gleam b/src/albedo/harness/context_limits.gleam index df473a291..9716b42a6 100644 --- a/src/albedo/harness/context_limits.gleam +++ b/src/albedo/harness/context_limits.gleam @@ -12,7 +12,7 @@ import albedo/harness/cache_ttl import albedo/harness/layered_table import albedo/harness/sdk/settings import albedo/harness/shipped -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode @@ -191,14 +191,8 @@ pub fn parse_tokens(raw: String) -> Result(Int, String) { string.ends_with(cleaned, "k"), string.ends_with(cleaned, "m") { - True, _ -> #( - text_scalars.take(cleaned, text_scalars.length(cleaned) - 1), - 1000, - ) - _, True -> #( - text_scalars.take(cleaned, text_scalars.length(cleaned) - 1), - 1_000_000, - ) + True, _ -> #(scalars.take(cleaned, scalars.length(cleaned) - 1), 1000) + _, True -> #(scalars.take(cleaned, scalars.length(cleaned) - 1), 1_000_000) _, _ -> #(cleaned, 1) } use tokens <- result.try(case string.split_once(number, ".") { @@ -216,7 +210,7 @@ pub fn parse_tokens(raw: String) -> Result(Int, String) { use frac <- result.try( int.parse(frac_part) |> result.replace_error(invalid_tokens(raw)), ) - let scale = ten_pow(text_scalars.length(frac_part)) + let scale = ten_pow(scalars.length(frac_part)) case whole >= 0 && frac >= 0 && scale <= multiplier { True -> Ok(whole * multiplier + frac * { multiplier / scale }) False -> Error(invalid_tokens(raw)) diff --git a/src/albedo/harness/extension_cache.gleam b/src/albedo/harness/extension_cache.gleam index dce006933..44169f315 100644 --- a/src/albedo/harness/extension_cache.gleam +++ b/src/albedo/harness/extension_cache.gleam @@ -3,7 +3,7 @@ //// refer to the sealed beams by module, index, and checksum, so the cache is //// only read beside the exact build seal it was written with. -import albedo/harness/sdk/term_codec +import albedo_bytes/term_codec import albedo_protect import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode diff --git a/src/albedo/harness/extensions/agents/extension.gleam b/src/albedo/harness/extensions/agents/extension.gleam index e1c683bac..aad2be52d 100644 --- a/src/albedo/harness/extensions/agents/extension.gleam +++ b/src/albedo/harness/extensions/agents/extension.gleam @@ -25,7 +25,7 @@ import albedo/harness/sdk/session_identity as identity import albedo/harness/sdk/sessions as session_facts import albedo/harness/sdk/storage import albedo/harness/worktree -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic import gleam/dynamic/decode import gleam/int @@ -524,7 +524,7 @@ fn dispatch( case string.trim(task), string.trim(model), - text_scalars.length(verifier) <= 4096 + scalars.length(verifier) <= 4096 { "", _, _ -> Error("task is empty; say what the child should do") _, "", _ -> @@ -748,7 +748,7 @@ fn dispatch( } "agents.progress" -> { use note <- result.try(service.text(args, "text")) - case string.trim(note), text_scalars.length(note) > 512 { + case string.trim(note), scalars.length(note) > 512 { "", _ -> Error("progress text is empty") _, True -> Error("progress is at most 512 characters; mail anything longer") diff --git a/src/albedo/harness/extensions/antigravity/catalog.gleam b/src/albedo/harness/extensions/antigravity/catalog.gleam index 667bc549a..940f8f3b7 100644 --- a/src/albedo/harness/extensions/antigravity/catalog.gleam +++ b/src/albedo/harness/extensions/antigravity/catalog.gleam @@ -11,7 +11,7 @@ import albedo/harness/sdk/file import albedo/harness/sdk/key_environment.{type KeyEnvironment} import albedo/harness/sdk/settings import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bool import gleam/dynamic/decode import gleam/int @@ -148,9 +148,9 @@ fn base_display_name(name: String) -> Option(String) { case string.ends_with(lower, suffix) { True -> { let stripped = - text_scalars.take( + scalars.take( trimmed, - text_scalars.length(trimmed) - text_scalars.length(suffix), + scalars.length(trimmed) - scalars.length(suffix), ) |> string.trim case stripped { @@ -178,10 +178,7 @@ fn split_id(id: String) -> #(String, Option(types.Effort)) { case string.ends_with(id, pair.0) { True -> Ok(#( - text_scalars.take( - id, - text_scalars.length(id) - text_scalars.length(pair.0), - ), + scalars.take(id, scalars.length(id) - scalars.length(pair.0)), Some(pair.1), )) False -> Error(Nil) diff --git a/src/albedo/harness/extensions/antigravity/counter.gleam b/src/albedo/harness/extensions/antigravity/counter.gleam index c7ac9439d..fffa30a79 100644 --- a/src/albedo/harness/extensions/antigravity/counter.gleam +++ b/src/albedo/harness/extensions/antigravity/counter.gleam @@ -1,5 +1,5 @@ -import albedo/harness/sdk/ids import albedo/harness/sdk/storage +import albedo_bytes/ids import gleam/dynamic/decode import gleam/result import sqlight diff --git a/src/albedo/harness/extensions/antigravity/stream.gleam b/src/albedo/harness/extensions/antigravity/stream.gleam index e53bf7a38..e588adf85 100644 --- a/src/albedo/harness/extensions/antigravity/stream.gleam +++ b/src/albedo/harness/extensions/antigravity/stream.gleam @@ -5,12 +5,12 @@ import albedo/gemini_api/refusal import albedo/harness/extensions/antigravity/catalog.{type Model} import albedo/harness/extensions/antigravity/family import albedo/harness/extensions/antigravity/wire -import albedo/harness/sdk/ids import albedo/openai_api/decoding import albedo/openai_api/fields import albedo/openai_api/replay import albedo/openai_api/stream as reducer import albedo/openai_api/types +import albedo_bytes/ids import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode import gleam/json.{type Json} diff --git a/src/albedo/harness/extensions/antigravity/wire.gleam b/src/albedo/harness/extensions/antigravity/wire.gleam index 240fb15e7..e02460531 100644 --- a/src/albedo/harness/extensions/antigravity/wire.gleam +++ b/src/albedo/harness/extensions/antigravity/wire.gleam @@ -5,13 +5,13 @@ import albedo/harness/extensions/antigravity/catalog.{type Model} import albedo/harness/extensions/antigravity/family import albedo/harness/extensions/antigravity/identity import albedo/harness/extensions/antigravity/schema -import albedo/harness/sdk/ids import albedo/openai_api import albedo/openai_api/replay import albedo/openai_api/types.{ type Error, type Input, type Request, Assistant, InvalidRequest, Replay, ToolOutput, User, UserImage, } +import albedo_bytes/ids import gleam/bool import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/extensions/artifacts/ledger.gleam b/src/albedo/harness/extensions/artifacts/ledger.gleam index a31b48100..3897924b7 100644 --- a/src/albedo/harness/extensions/artifacts/ledger.gleam +++ b/src/albedo/harness/extensions/artifacts/ledger.gleam @@ -6,7 +6,7 @@ import albedo/harness/sdk/sessions as session_facts import albedo/harness/sdk/storage -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/bool @@ -316,7 +316,7 @@ pub fn valid_path(path: String) -> Bool { pub fn valid_title(title: String) -> Result(String, Error) { let title = string.trim(title) - let length = text_scalars.length(title) + let length = scalars.length(title) case length { 0 -> Error(Invalid("an artifact needs a title")) _ if length > 200 -> @@ -401,7 +401,7 @@ fn check_files(files: List(File), entry: String) -> Result(Int, Error) { Ok(path) -> Error(Invalid( "\"" - <> text_scalars.take(path, 200) + <> scalars.take(path, 200) <> "\" is not a relative path inside the artifact (no leading /, no . or .. segments, not under .albedo/)", )) Error(_) -> diff --git a/src/albedo/harness/extensions/artifacts/service.gleam b/src/albedo/harness/extensions/artifacts/service.gleam index 74f4c2459..e4e0d803c 100644 --- a/src/albedo/harness/extensions/artifacts/service.gleam +++ b/src/albedo/harness/extensions/artifacts/service.gleam @@ -8,7 +8,7 @@ import albedo/harness/sdk/extension import albedo/harness/sdk/http_api as api import albedo/harness/sdk/memo import albedo/harness/sdk/priv -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/bool @@ -218,7 +218,7 @@ fn dispatch( |> result.replace_error(api.Failure( 404, "not_found", - "this artifact has no " <> text_scalars.take(path, 200), + "this artifact has no " <> scalars.take(path, 200), )), ) let etag = "\"" <> file.hash <> "\"" @@ -390,11 +390,11 @@ fn reported_decoder(version: Int) -> decode.Decoder(ledger.Failure) { decode.success(ledger.Failure( version:, at: 0, - message: text_scalars.take(message, 4000), - source: text_scalars.take(source, 2000), + message: scalars.take(message, 4000), + source: scalars.take(source, 2000), line: int.max(line, 0), column: int.max(column, 0), - stack: text_scalars.take(stack, 8000), + stack: scalars.take(stack, 8000), )) } } diff --git a/src/albedo/harness/extensions/bedrock/aws_profile.gleam b/src/albedo/harness/extensions/bedrock/aws_profile.gleam index 1342b064b..00dfa11a4 100644 --- a/src/albedo/harness/extensions/bedrock/aws_profile.gleam +++ b/src/albedo/harness/extensions/bedrock/aws_profile.gleam @@ -6,7 +6,7 @@ import albedo/harness/sdk/file import albedo/harness/sdk/network import albedo/harness/sdk/network_context import albedo/harness/sdk/subprocess -import albedo/text_scalars +import albedo_bytes/scalars import aws_sigv4 import gleam/bit_array import gleam/dict.{type Dict} @@ -253,10 +253,7 @@ fn find_section( fn is_header(line: String, header: String) -> Bool { string.starts_with(line, "[") && string.ends_with(line, "]") - && string.trim(text_scalars.take( - text_scalars.drop(line, 1), - text_scalars.length(line) - 2, - )) + && string.trim(scalars.take(scalars.drop(line, 1), scalars.length(line) - 2)) == header } diff --git a/src/albedo/harness/extensions/checklist/ledger.gleam b/src/albedo/harness/extensions/checklist/ledger.gleam index 85fc3780a..3182d2f43 100644 --- a/src/albedo/harness/extensions/checklist/ledger.gleam +++ b/src/albedo/harness/extensions/checklist/ledger.gleam @@ -4,7 +4,7 @@ //// go when it is deleted. import albedo/harness/sdk/storage -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bool import gleam/dynamic/decode import gleam/int @@ -73,7 +73,7 @@ pub fn add( text: String, ) -> Result(Item, Error) { let text = string.trim(text) - use _ <- result.try(case text, text_scalars.length(text) > max_text { + use _ <- result.try(case text, scalars.length(text) > max_text { "", _ -> Error(Invalid("an item needs text")) _, True -> Error(Invalid( diff --git a/src/albedo/harness/extensions/claude/accounts.gleam b/src/albedo/harness/extensions/claude/accounts.gleam index 6e3eb13ea..54e98c789 100644 --- a/src/albedo/harness/extensions/claude/accounts.gleam +++ b/src/albedo/harness/extensions/claude/accounts.gleam @@ -2,11 +2,11 @@ //// credentials; the token protocol and freshness policy belong here. import albedo/harness/sdk/credentials -import albedo/harness/sdk/digest import albedo/harness/sdk/http -import albedo/harness/sdk/ids import albedo/harness/sdk/usage_feed -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/ids +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/dynamic.{type Dynamic} @@ -234,7 +234,7 @@ fn post( <> { bit_array.to_string(body) |> result.unwrap("") - |> text_scalars.take(2048) + |> scalars.take(2048) }, ) Error(_) -> Error("Anthropic token request failed") diff --git a/src/albedo/harness/extensions/claude/billing.gleam b/src/albedo/harness/extensions/claude/billing.gleam index 20366ed23..f0308486d 100644 --- a/src/albedo/harness/extensions/claude/billing.gleam +++ b/src/albedo/harness/extensions/claude/billing.gleam @@ -2,9 +2,9 @@ //// The `cch` digits are an XXH64 of the whole request body, so they ride as //// an attestation marker the transport fills in while it writes the body. -import albedo/harness/sdk/digest import albedo/openai_api/attestation -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import gleam/bit_array import gleam/int import gleam/json.{type Json} @@ -70,7 +70,7 @@ fn header_prefix(version: String, first_user: String) -> String { let suffix = digest.sha256(<>) |> digest.hex - |> text_scalars.take(3) + |> scalars.take(3) "x-anthropic-billing-header: cc_version=" <> version <> "." diff --git a/src/albedo/harness/extensions/claude/files.gleam b/src/albedo/harness/extensions/claude/files.gleam index ce4a0e390..fc02d5631 100644 --- a/src/albedo/harness/extensions/claude/files.gleam +++ b/src/albedo/harness/extensions/claude/files.gleam @@ -3,13 +3,13 @@ //// them. Rejection generations fence stale completion. import albedo/harness/sdk/concurrent -import albedo/harness/sdk/digest import albedo/harness/sdk/file import albedo/harness/sdk/http -import albedo/harness/sdk/ids import albedo/harness/sdk/memo import albedo/harness/sdk/process_flag import albedo/openai_api/types +import albedo_bytes/digest +import albedo_bytes/ids import albedo_clock import albedo_protect import gleam/bit_array diff --git a/src/albedo/harness/extensions/claude/wire.gleam b/src/albedo/harness/extensions/claude/wire.gleam index 8c5af65d0..3d19c825c 100644 --- a/src/albedo/harness/extensions/claude/wire.gleam +++ b/src/albedo/harness/extensions/claude/wire.gleam @@ -17,7 +17,7 @@ import albedo/openai_api/replay import albedo/openai_api/stream import albedo/openai_api/transport import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/bool import gleam/crypto.{Sha256} @@ -805,9 +805,9 @@ pub fn claude_name(name: String) -> String { |> crypto.hash(Sha256, _) |> bit_array.base16_encode |> string.lowercase - |> text_scalars.take(16) + |> scalars.take(16) "mcp__albedo__" - <> text_scalars.take(text_scalars.drop(name, 4), 24) + <> scalars.take(scalars.drop(name, 4), 24) <> "_" <> digest } diff --git a/src/albedo/harness/extensions/codex/accounts.gleam b/src/albedo/harness/extensions/codex/accounts.gleam index a9832a3ff..526e7ba7b 100644 --- a/src/albedo/harness/extensions/codex/accounts.gleam +++ b/src/albedo/harness/extensions/codex/accounts.gleam @@ -8,7 +8,7 @@ import albedo/harness/sdk/rotation import albedo/harness/sdk/settings_store import albedo/harness/sdk/usage_feed import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/bool @@ -386,7 +386,7 @@ pub fn exchange( let detail = bit_array.to_string(body) |> result.unwrap("") - |> text_scalars.take(4096) + |> scalars.take(4096) |> string.trim Error( "codex token exchange failed (" diff --git a/src/albedo/harness/extensions/codex/imagegen.gleam b/src/albedo/harness/extensions/codex/imagegen.gleam index e67de93bc..3e3902b70 100644 --- a/src/albedo/harness/extensions/codex/imagegen.gleam +++ b/src/albedo/harness/extensions/codex/imagegen.gleam @@ -7,7 +7,7 @@ import albedo/harness/sdk/network import albedo/harness/sdk/network_context import albedo/harness/sdk/profiles as configuration import albedo/harness/sdk/settings -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bool import gleam/dynamic/decode import gleam/int @@ -153,7 +153,7 @@ fn generate( fn error_message(reply: String) -> String { case json.parse(reply, decode.at(["error", "message"], decode.string)) { - Ok(message) -> ": " <> text_scalars.take(message, 500) + Ok(message) -> ": " <> scalars.take(message, 500) Error(_) -> "" } } diff --git a/src/albedo/harness/extensions/codex/wire.gleam b/src/albedo/harness/extensions/codex/wire.gleam index ed407a4e1..8d8958e83 100644 --- a/src/albedo/harness/extensions/codex/wire.gleam +++ b/src/albedo/harness/extensions/codex/wire.gleam @@ -1,6 +1,5 @@ //// ChatGPT request shaping. The shared codec owns ordinary Responses encoding. -import albedo/harness/sdk/ids import albedo/openai_api import albedo/openai_api/cost import albedo/openai_api/refusal as openai_refusal @@ -8,6 +7,7 @@ import albedo/openai_api/request as shared import albedo/openai_api/stream import albedo/openai_api/transport import albedo/openai_api/types +import albedo_bytes/ids import albedo_clock import gleam/dynamic/decode import gleam/json diff --git a/src/albedo/harness/extensions/discord/ingest.gleam b/src/albedo/harness/extensions/discord/ingest.gleam index 2b280b801..b2d23dfa1 100644 --- a/src/albedo/harness/extensions/discord/ingest.gleam +++ b/src/albedo/harness/extensions/discord/ingest.gleam @@ -9,8 +9,8 @@ import albedo/harness/sdk/input import albedo/harness/sdk/letter as mail_value import albedo/harness/sdk/mail import albedo/harness/sdk/storage -import albedo/text_scalars import albedo/transfer +import albedo_bytes/scalars import albedo_clock import gleam/bool import gleam/dynamic/decode @@ -749,8 +749,8 @@ pub fn preview(text: String) -> Option(String) { case line { "" -> None _ -> - case text_scalars.length(line) > excerpt_limit { - True -> Some(text_scalars.take(line, excerpt_limit) <> "…") + case scalars.length(line) > excerpt_limit { + True -> Some(scalars.take(line, excerpt_limit) <> "…") False -> Some(line) } } @@ -798,8 +798,8 @@ pub fn body( let content = case message.content { "" -> "(no text)" text -> - case text_scalars.length(text) > content_limit { - True -> text_scalars.take(text, content_limit) <> "\n[truncated]" + case scalars.length(text) > content_limit { + True -> scalars.take(text, content_limit) <> "\n[truncated]" False -> text } } diff --git a/src/albedo/harness/extensions/discord/ledger.gleam b/src/albedo/harness/extensions/discord/ledger.gleam index abebfa185..a81b023e4 100644 --- a/src/albedo/harness/extensions/discord/ledger.gleam +++ b/src/albedo/harness/extensions/discord/ledger.gleam @@ -11,7 +11,7 @@ import albedo/harness/sdk/input import albedo/harness/sdk/mail import albedo/harness/sdk/sessions import albedo/harness/sdk/storage -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/dynamic/decode import gleam/json @@ -758,7 +758,7 @@ pub fn refuse( sqlight.text(bot), sqlight.text(user_id), sqlight.text(username), - sqlight.text(text_scalars.take(content, 200)), + sqlight.text(scalars.take(content, 200)), sqlight.int(albedo_clock.system_ms()), ], ), @@ -815,7 +815,7 @@ pub fn note_sent( /// Snowflakes compare as integers: by length, then by digits. pub fn newer(candidate: String, than: String) -> Bool { - case text_scalars.length(candidate) - text_scalars.length(than) { + case scalars.length(candidate) - scalars.length(than) { 0 -> string.compare(candidate, than) == order.Gt difference -> difference > 0 } @@ -864,7 +864,7 @@ pub fn visible( } fn validate_name(name: String) -> Result(Nil, Error) { - case string.trim(name) != "" && text_scalars.length(name) <= 64 { + case string.trim(name) != "" && scalars.length(name) <= 64 { True -> Ok(Nil) False -> Error(Invalid("name must be 1–64 characters")) } @@ -893,7 +893,7 @@ fn validate_owner(owner: String) -> Result(Nil, Error) { } fn validate_snowflake(value: String, label: String) -> Result(Nil, Error) { - let length = text_scalars.length(value) + let length = scalars.length(value) case length >= 17 && length <= 20 && ascii(value, "0123456789") { True -> Ok(Nil) False -> Error(Invalid(label <> " must be a Discord id of 17–20 digits")) diff --git a/src/albedo/harness/extensions/discord/refs.gleam b/src/albedo/harness/extensions/discord/refs.gleam index 221fc0759..b0bfcfb16 100644 --- a/src/albedo/harness/extensions/discord/refs.gleam +++ b/src/albedo/harness/extensions/discord/refs.gleam @@ -5,7 +5,7 @@ //// per bot, so they mean the same after a restart. import albedo/harness/sdk/storage -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic/decode import gleam/int import gleam/list @@ -236,7 +236,7 @@ fn without_hash(text: String) -> String { /// Discord ids are 17 to 20 digits; anything shorter is a number or a handle. fn snowflake(text: String) -> Bool { - let length = text_scalars.length(text) + let length = scalars.length(text) length >= 17 && length <= 20 && list.all(string.to_graphemes(text), fn(digit) { diff --git a/src/albedo/harness/extensions/exa/extension.gleam b/src/albedo/harness/extensions/exa/extension.gleam index 8b8bc3b24..afc134f44 100644 --- a/src/albedo/harness/extensions/exa/extension.gleam +++ b/src/albedo/harness/extensions/exa/extension.gleam @@ -7,7 +7,7 @@ import albedo/harness/sdk/extension import albedo/harness/sdk/http import albedo/harness/sdk/settings import albedo/harness/sdk/web_search -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/dynamic/decode import gleam/int @@ -97,7 +97,7 @@ fn search(query: web_search.Query) -> Result(web_search.Answer, String) { "Exa answered HTTP " <> int.to_string(status) <> ": " - <> text_scalars.take(string.trim(reply), 300), + <> scalars.take(string.trim(reply), 300), ) } } diff --git a/src/albedo/harness/extensions/git/extension.gleam b/src/albedo/harness/extensions/git/extension.gleam index d54b2b5b6..8f4cbf875 100644 --- a/src/albedo/harness/extensions/git/extension.gleam +++ b/src/albedo/harness/extensions/git/extension.gleam @@ -1,7 +1,7 @@ import albedo/harness/location import albedo/harness/sdk/extension import albedo/harness/worktree -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bool import gleam/list import gleam/option.{type Option, None, Some} @@ -147,7 +147,7 @@ fn entry( worktree.VcsEntry( name: name, path: Some(path), - revision: field(block, "HEAD") |> option.map(text_scalars.take(_, 8)), + revision: field(block, "HEAD") |> option.map(scalars.take(_, 8)), branch: field(block, "branch") |> option.map(string.replace(_, "refs/heads/", "")), ) diff --git a/src/albedo/harness/extensions/github_copilot/catalog.gleam b/src/albedo/harness/extensions/github_copilot/catalog.gleam index 227c515d9..1085b6fdd 100644 --- a/src/albedo/harness/extensions/github_copilot/catalog.gleam +++ b/src/albedo/harness/extensions/github_copilot/catalog.gleam @@ -7,12 +7,12 @@ import albedo/harness/extensions/github_copilot/accounts import albedo/harness/extensions/github_copilot/wire import albedo/harness/extensions/models/catalogue as models import albedo/harness/sdk/credentials -import albedo/harness/sdk/digest import albedo/harness/sdk/extension import albedo/harness/sdk/file import albedo/harness/sdk/http import albedo/harness/sdk/key_environment.{type KeyEnvironment} import albedo/harness/sdk/settings +import albedo_bytes/digest import gleam/bool import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/extensions/imagegen/extension.gleam b/src/albedo/harness/extensions/imagegen/extension.gleam index a8e27af42..1c70cb03d 100644 --- a/src/albedo/harness/extensions/imagegen/extension.gleam +++ b/src/albedo/harness/extensions/imagegen/extension.gleam @@ -3,7 +3,7 @@ import albedo/harness/extensions/imagegen/calls import albedo/harness/sdk/capability -import albedo/harness/sdk/digest +import albedo_bytes/digest import albedo/harness/sdk/extension import albedo/harness/sdk/history as conversation diff --git a/src/albedo/harness/extensions/mcp/catalogue.gleam b/src/albedo/harness/extensions/mcp/catalogue.gleam index c07cbc5d8..127a5318a 100644 --- a/src/albedo/harness/extensions/mcp/catalogue.gleam +++ b/src/albedo/harness/extensions/mcp/catalogue.gleam @@ -4,10 +4,10 @@ import albedo/harness/extensions/mcp/client import albedo/harness/extensions/mcp/param_headers -import albedo/harness/sdk/digest import albedo/harness/sdk/log -import albedo/harness/sdk/term_codec import albedo/openai_api/types +import albedo_bytes/digest +import albedo_bytes/term_codec import albedo_protect import gleam/bit_array import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/extensions/mcp/oauth.gleam b/src/albedo/harness/extensions/mcp/oauth.gleam index f93158051..1ee694dc9 100644 --- a/src/albedo/harness/extensions/mcp/oauth.gleam +++ b/src/albedo/harness/extensions/mcp/oauth.gleam @@ -17,10 +17,10 @@ import albedo/harness/sdk/file import albedo/harness/sdk/http import albedo/harness/sdk/oauth import albedo/harness/sdk/settings_documents -import albedo/harness/sdk/term import albedo/harness/sdk/text as patterns import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars +import albedo_bytes/term import albedo_clock import albedo_protect import gleam/bit_array @@ -1050,7 +1050,7 @@ fn account_label( }) }) case named, decode.run(previous, decode.string) { - [name, ..], _ -> dynamic.string(text_scalars.take(name, 256)) + [name, ..], _ -> dynamic.string(scalars.take(name, 256)) [], Ok(_) -> previous [], Error(_) -> case decode.run(issuer, decode.string) { @@ -1229,7 +1229,7 @@ fn described(what: String, status: Int, answer: Option(Dynamic)) -> String { option.to_result(text(answer, "error_description"), Nil) }) { - Ok(detail) -> ": " <> text_scalars.take(detail, 512) + Ok(detail) -> ": " <> scalars.take(detail, 512) Error(Nil) -> "" } what <> " was refused (" <> error_code <> detail <> ")" diff --git a/src/albedo/harness/extensions/mcp/servers.gleam b/src/albedo/harness/extensions/mcp/servers.gleam index f738153b3..af80ccd2b 100644 --- a/src/albedo/harness/extensions/mcp/servers.gleam +++ b/src/albedo/harness/extensions/mcp/servers.gleam @@ -5,15 +5,15 @@ import albedo/harness/extensions/mcp/client import albedo/harness/extensions/mcp/oauth -import albedo/harness/sdk/digest import albedo/harness/sdk/file import albedo/harness/sdk/file_status import albedo/harness/sdk/getenv import albedo/harness/sdk/priv import albedo/harness/sdk/settings import albedo/harness/sdk/subprocess -import albedo/harness/sdk/term -import albedo/harness/sdk/term_codec +import albedo_bytes/digest +import albedo_bytes/term +import albedo_bytes/term_codec import gleam/bit_array import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/extensions/memory/README.md b/src/albedo/harness/extensions/memory/README.md index cad71991d..cc08fe57b 100644 --- a/src/albedo/harness/extensions/memory/README.md +++ b/src/albedo/harness/extensions/memory/README.md @@ -88,7 +88,8 @@ The owner is [`files.gleam`](files.gleam). Its native [`albedo_memory.erl`](albedo_memory.erl) keeps the file and lock primitives that no SDK helper provides. `files.gleam` encodes import receipts and transfer digests in the external term format through -[`sdk/term_codec`](../../sdk/term_codec.gleam). Supporting scenarios live in the +[`albedo_bytes/term_codec`](../../../../../packages/albedo_bytes/src/albedo_bytes/term_codec.gleam). +Supporting scenarios live in the [snapshot tests](../../../../../test/harness/memory_snapshot_test.erl), [receipt compatibility tests](../../../../../test/harness/memory_receipt_test.gleam), and diff --git a/src/albedo/harness/extensions/memory/files.gleam b/src/albedo/harness/extensions/memory/files.gleam index f954ec1ca..c737288f5 100644 --- a/src/albedo/harness/extensions/memory/files.gleam +++ b/src/albedo/harness/extensions/memory/files.gleam @@ -5,9 +5,9 @@ //// serialized through a global lock. Reads span a workspace's link group. //// See README.md for snapshot transfer, receipts, and recovery. -import albedo/harness/sdk/digest import albedo/harness/sdk/settings -import albedo/harness/sdk/term_codec +import albedo_bytes/digest +import albedo_bytes/term_codec import gleam/bit_array import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode diff --git a/src/albedo/harness/extensions/mistral/sign_in.gleam b/src/albedo/harness/extensions/mistral/sign_in.gleam index 9b66d21dc..4ef123d29 100644 --- a/src/albedo/harness/extensions/mistral/sign_in.gleam +++ b/src/albedo/harness/extensions/mistral/sign_in.gleam @@ -6,11 +6,11 @@ //// This module owns requests, URL validation, polling policy, PKCE, and the //// stored credential. The SDK OAuth worker owns cancellation and input wakes. -import albedo/harness/sdk/digest import albedo/harness/sdk/http import albedo/harness/sdk/oauth import albedo/harness/sdk/rotation import albedo/openai_api/catalog as model_catalog +import albedo_bytes/digest import albedo_clock import gleam/bit_array import gleam/crypto diff --git a/src/albedo/harness/extensions/mistral/wire.gleam b/src/albedo/harness/extensions/mistral/wire.gleam index 60e8ae2d3..583a224bc 100644 --- a/src/albedo/harness/extensions/mistral/wire.gleam +++ b/src/albedo/harness/extensions/mistral/wire.gleam @@ -13,7 +13,7 @@ import albedo/openai_api/types.{ Assistant, ChatCompletions, InvalidRequest, Replay, ToolOutput, User, UserImage, } -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/crypto import gleam/dict @@ -435,7 +435,7 @@ fn tool_id(id: String) -> String { crypto.hash(crypto.Sha256, bit_array.from_string(id)) |> bit_array.base16_encode |> string.lowercase - |> text_scalars.take(9) + |> scalars.take(9) } } diff --git a/src/albedo/harness/extensions/models/catalog.gleam b/src/albedo/harness/extensions/models/catalog.gleam index 97970d328..c6d11cb61 100644 --- a/src/albedo/harness/extensions/models/catalog.gleam +++ b/src/albedo/harness/extensions/models/catalog.gleam @@ -1,8 +1,8 @@ //// Catalog interpretation and selection over the native revision-cached index. import albedo/harness/sdk/extension -import albedo/harness/sdk/term import albedo/openai_api/types +import albedo_bytes/term import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode diff --git a/src/albedo/harness/extensions/models/catalog_cache.gleam b/src/albedo/harness/extensions/models/catalog_cache.gleam index dba5bfc54..24afaeedf 100644 --- a/src/albedo/harness/extensions/models/catalog_cache.gleam +++ b/src/albedo/harness/extensions/models/catalog_cache.gleam @@ -11,7 +11,7 @@ import albedo/harness/sdk/credential_store import albedo/harness/sdk/file import albedo/harness/sdk/file_revision.{type Revision, Revision} import albedo/harness/sdk/pinned -import albedo/harness/sdk/term_codec +import albedo_bytes/term_codec import gleam/bit_array import gleam/dict.{type Dict} import gleam/dynamic diff --git a/src/albedo/harness/extensions/openai/extension.gleam b/src/albedo/harness/extensions/openai/extension.gleam index 892c16b4b..477b9acbc 100644 --- a/src/albedo/harness/extensions/openai/extension.gleam +++ b/src/albedo/harness/extensions/openai/extension.gleam @@ -3,7 +3,6 @@ import albedo/harness/extensions/models/catalogue as models import albedo/harness/extensions/openai/accounts import albedo/harness/extensions/openai/fast -import albedo/harness/sdk/digest import albedo/harness/sdk/extension import albedo/harness/sdk/file import albedo/harness/sdk/key_accounts @@ -22,6 +21,7 @@ import albedo/openai_api/request as wire import albedo/openai_api/stream as reducer import albedo/openai_api/transport import albedo/openai_api/types +import albedo_bytes/digest import gleam/bool import gleam/dict import gleam/dynamic/decode diff --git a/src/albedo/harness/extensions/opencode_zen/client.gleam b/src/albedo/harness/extensions/opencode_zen/client.gleam index ebf473f48..976b99913 100644 --- a/src/albedo/harness/extensions/opencode_zen/client.gleam +++ b/src/albedo/harness/extensions/opencode_zen/client.gleam @@ -6,8 +6,8 @@ //// its agent sends (the last two belong to the wire module). A paid model goes //// out with the profile's own key and none of that. -import albedo/harness/sdk/digest -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import gleam/string /// OpenCode's binary launches with @@ -52,7 +52,7 @@ pub fn free(model: String) -> Bool { /// back and refuses one outside its first sixty-four, so this id ends in /// `ffe` too. The clock half is not inspected, so a hash stands in for it. pub fn session(seed: String) -> String { - let clock = digest.sha1(<>) |> digest.hex |> text_scalars.take(9) + let clock = digest.sha1(<>) |> digest.hex |> scalars.take(9) let tail = digest.sha256(<>) |> base62(14, "") "ses_" <> clock <> "ffe" <> tail } @@ -73,7 +73,7 @@ fn base62(bytes: BitArray, count: Int, acc: String) -> String { base62( rest, count - 1, - acc <> text_scalars.drop(alphabet, byte % 62) |> text_scalars.take(1), + acc <> scalars.drop(alphabet, byte % 62) |> scalars.take(1), ) _, _ -> acc } diff --git a/src/albedo/harness/extensions/paperclips/presentation.gleam b/src/albedo/harness/extensions/paperclips/presentation.gleam index 3b8c0d630..6ab0f5f10 100644 --- a/src/albedo/harness/extensions/paperclips/presentation.gleam +++ b/src/albedo/harness/extensions/paperclips/presentation.gleam @@ -1,5 +1,5 @@ import albedo/harness/extensions/paperclips/ledger as paperclips -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dict import gleam/list import gleam/option.{type Option, None, Some} @@ -69,7 +69,7 @@ fn session_label( ) -> String { case session { Some(session) -> - dict.get(labels, session) |> result.unwrap(text_scalars.take(session, 8)) + dict.get(labels, session) |> result.unwrap(scalars.take(session, 8)) None -> "—" } } diff --git a/src/albedo/harness/extensions/perplexity/search.gleam b/src/albedo/harness/extensions/perplexity/search.gleam index 0eee05ee9..2adffc5c8 100644 --- a/src/albedo/harness/extensions/perplexity/search.gleam +++ b/src/albedo/harness/extensions/perplexity/search.gleam @@ -8,7 +8,7 @@ import albedo/harness/extensions/perplexity/accounts import albedo/harness/sdk/http import albedo/harness/sdk/settings import albedo/harness/sdk/web_search -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bit_array import gleam/dynamic/decode import gleam/int @@ -92,7 +92,7 @@ fn detail(body: String) -> String { json.parse(body, decode.at(["error", "message"], decode.string)) |> result.unwrap(body) |> string.trim - |> text_scalars.take(300) + |> scalars.take(300) } fn source_decoder() -> decode.Decoder(web_search.Source) { diff --git a/src/albedo/harness/extensions/persistent_agent/extension.gleam b/src/albedo/harness/extensions/persistent_agent/extension.gleam index 308642ae4..5af1a9533 100644 --- a/src/albedo/harness/extensions/persistent_agent/extension.gleam +++ b/src/albedo/harness/extensions/persistent_agent/extension.gleam @@ -32,7 +32,7 @@ import albedo/harness/sdk/transcript import albedo/harness/sdk/worker import albedo/harness/size import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/bool import gleam/dict.{type Dict} @@ -624,9 +624,9 @@ fn recent_memories(memories: List(String)) -> Option(String) { |> list.reverse |> list.fold(#([], 0), fn(acc, text) { let #(kept, used) = acc - case used + text_scalars.length(text) > recent_chars, kept { + case used + scalars.length(text) > recent_chars, kept { True, [_, ..] -> acc - _, _ -> #([text, ..kept], used + text_scalars.length(text)) + _, _ -> #([text, ..kept], used + scalars.length(text)) } }) case kept.0 { @@ -933,7 +933,7 @@ fn reformable( Ok(#(_, last)) -> last >= since Error(_) -> False } - recent || text_scalars.length(node.summary) < degenerate_chars + recent || scalars.length(node.summary) < degenerate_chars } // --------------------------------------------------------------------------- diff --git a/src/albedo/harness/extensions/proxy/chat.gleam b/src/albedo/harness/extensions/proxy/chat.gleam index 2bd69fcac..859477c90 100644 --- a/src/albedo/harness/extensions/proxy/chat.gleam +++ b/src/albedo/harness/extensions/proxy/chat.gleam @@ -2,15 +2,15 @@ //// requests become albedo's request types, and turns and stream events //// become completion objects and chunks. -import albedo/harness/sdk/bytes import albedo/harness/sdk/http_api as api import albedo/harness/sdk/image import albedo/harness/sdk/log import albedo/harness/sdk/message_content as events import albedo/harness/sdk/transcript -import albedo/harness/sdk/zlib import albedo/openai_api/replay import albedo/openai_api/types +import albedo_bytes +import albedo_bytes/zlib import gleam/bit_array import gleam/dict import gleam/dynamic/decode @@ -937,6 +937,6 @@ fn pack(json: String) -> String { /// The state `pack` carried. A client that truncated or rewrote the id fails /// the stream checksum and gets the portable path. fn unpack(state: String) -> Result(BitArray, Nil) { - use compressed <- result.try(bytes.base64_url_decode(state)) + use compressed <- result.try(albedo_bytes.base64_url_decode(state)) zlib.inflate(compressed, max_state_bytes) } diff --git a/src/albedo/harness/extensions/proxy/extension.gleam b/src/albedo/harness/extensions/proxy/extension.gleam index 01752bb56..43c825653 100644 --- a/src/albedo/harness/extensions/proxy/extension.gleam +++ b/src/albedo/harness/extensions/proxy/extension.gleam @@ -6,15 +6,15 @@ import albedo_clock import albedo/harness/extensions/proxy/chat import albedo/harness/sdk/capability -import albedo/harness/sdk/digest import albedo/harness/sdk/env import albedo/harness/sdk/extension import albedo/harness/sdk/http_api -import albedo/harness/sdk/ids import albedo/harness/sdk/profiles as configuration import albedo/harness/sdk/projection import albedo/openai_api/refusal import albedo/openai_api/types +import albedo_bytes/digest +import albedo_bytes/ids import gleam/bytes_tree import gleam/dynamic/decode import gleam/erlang/process diff --git a/src/albedo/harness/extensions/snapcompact/extension.gleam b/src/albedo/harness/extensions/snapcompact/extension.gleam index fb7f68b74..38b697069 100644 --- a/src/albedo/harness/extensions/snapcompact/extension.gleam +++ b/src/albedo/harness/extensions/snapcompact/extension.gleam @@ -11,7 +11,6 @@ import albedo/harness/compaction import albedo/harness/extensions/rolling/strategy as rolling import albedo/harness/extensions/snapcompact/transcript import albedo/harness/extensions/snapcompact/transfer -import albedo/harness/sdk/digest import albedo/harness/sdk/extension import albedo/harness/sdk/file import albedo/harness/sdk/file_status @@ -20,7 +19,8 @@ import albedo/harness/sdk/settings import albedo/harness/sdk/storage import albedo/harness/size import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import gleam/bit_array import gleam/dict.{type Dict} import gleam/dynamic/decode @@ -864,7 +864,7 @@ fn image_note(image: types.Image) -> String { let #(_, width, height, _) = types.image_meta(image) let hash = image_hash(image) "[image " - <> text_scalars.take(hash, 8) + <> scalars.take(hash, 8) <> " " <> int.to_string(width) <> "x" @@ -880,8 +880,8 @@ fn image_hash(image: types.Image) -> String { } fn cap(text: String, limit: Int) -> String { - case text_scalars.length(text) > limit { - True -> text_scalars.take(text, limit) <> " [+truncated]" + case scalars.length(text) > limit { + True -> scalars.take(text, limit) <> " [+truncated]" False -> text } } @@ -1036,7 +1036,7 @@ pub fn bound(shape: Shape, limit: Int, text: String) -> #(String, Int) { pages |> list.drop(list.length(head)) |> list.take(count - limit) - |> list.fold(0, fn(total, page) { total + text_scalars.length(page) }) + |> list.fold(0, fn(total, page) { total + scalars.length(page) }) #(string.concat(list.append(head, newest)), dropped) } } diff --git a/src/albedo/harness/extensions/webhooks/ledger.gleam b/src/albedo/harness/extensions/webhooks/ledger.gleam index f8055e75d..0b74a5521 100644 --- a/src/albedo/harness/extensions/webhooks/ledger.gleam +++ b/src/albedo/harness/extensions/webhooks/ledger.gleam @@ -2,15 +2,15 @@ import albedo/harness/sdk/letter as mail_value -import albedo/harness/sdk/digest -import albedo/harness/sdk/ids import albedo/harness/sdk/input import albedo/harness/sdk/mail import albedo/harness/sdk/sessions import albedo/harness/sdk/storage import albedo/harness/sdk/webhook_address as webhook_intake -import albedo/text_scalars import albedo/transfer +import albedo_bytes/digest +import albedo_bytes/ids +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/bool @@ -307,7 +307,7 @@ fn posted(delivery: Delivery) -> input.Input { let preview = delivery.body |> bit_array.to_string - |> result.map(fn(text) { text_scalars.take(text, 4000) }) + |> result.map(fn(text) { scalars.take(text, 4000) }) |> result.unwrap("[binary payload; read the delivery by id]") input.Input( delivery.session, diff --git a/src/albedo/harness/extensions/webhooks/service.gleam b/src/albedo/harness/extensions/webhooks/service.gleam index b405e65bd..2b39bc9b7 100644 --- a/src/albedo/harness/extensions/webhooks/service.gleam +++ b/src/albedo/harness/extensions/webhooks/service.gleam @@ -1,10 +1,10 @@ //// Bearer management and raw signed intake share one canonical mount. import albedo/harness/extensions/webhooks/ledger as hooks -import albedo/harness/sdk/digest import albedo/harness/sdk/extension import albedo/harness/sdk/http_api as api import albedo/harness/sdk/webhook_address as webhook_intake +import albedo_bytes/digest import gleam/bit_array import gleam/dynamic/decode import gleam/http.{type Method, Delete, Get, Patch, Post} diff --git a/src/albedo/harness/file_fingerprint.gleam b/src/albedo/harness/file_fingerprint.gleam index d73957fce..5bd34a634 100644 --- a/src/albedo/harness/file_fingerprint.gleam +++ b/src/albedo/harness/file_fingerprint.gleam @@ -4,8 +4,8 @@ //// not change its fingerprint. import albedo/harness/file_info -import albedo/harness/sdk/digest import albedo/harness/sdk/file +import albedo_bytes/digest import gleam/erlang/atom.{type Atom} pub type Fingerprint { diff --git a/src/albedo/harness/instruction_files.gleam b/src/albedo/harness/instruction_files.gleam index 2fd6052e3..e79b96518 100644 --- a/src/albedo/harness/instruction_files.gleam +++ b/src/albedo/harness/instruction_files.gleam @@ -4,13 +4,13 @@ import albedo/harness/capabilities import albedo/harness/file_fingerprint import albedo/harness/file_info import albedo/harness/project_files -import albedo/harness/sdk/digest import albedo/harness/sdk/env import albedo/harness/sdk/file import albedo/harness/sdk/settings import albedo/harness/sdk/storage -import albedo/harness/sdk/term_codec import albedo/harness/shipped +import albedo_bytes/digest +import albedo_bytes/term_codec import albedo_protect import gleam/bit_array import gleam/crypto diff --git a/src/albedo/harness/kernel.gleam b/src/albedo/harness/kernel.gleam index 774aeccdb..51d50d843 100644 --- a/src/albedo/harness/kernel.gleam +++ b/src/albedo/harness/kernel.gleam @@ -8,13 +8,13 @@ import albedo/harness/kernel/python_tree import albedo/harness/kernel/spare import albedo/harness/kernel/supervision.{type Remote, Remote} import albedo/harness/location -import albedo/harness/sdk/ids import albedo/harness/sdk/image import albedo/harness/sdk/settings import albedo/harness/sdk/store import albedo/harness/ssh import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/ids +import albedo_bytes/scalars import albedo_clock import gleam/bool import gleam/dynamic.{type Dynamic} @@ -814,7 +814,7 @@ fn boot_fresh( staged: Bool, ) -> Result(#(Kernel, link.Record), Error) { use runs <- result.try(runs) - let kernel = text_scalars.take(ids.random_hex(16), 16) + let kernel = scalars.take(ids.random_hex(16), 16) let record = link.Record( session: session, diff --git a/src/albedo/harness/kernel/cell_codec.gleam b/src/albedo/harness/kernel/cell_codec.gleam index ea204e000..ff890caa8 100644 --- a/src/albedo/harness/kernel/cell_codec.gleam +++ b/src/albedo/harness/kernel/cell_codec.gleam @@ -6,8 +6,8 @@ import albedo/harness/kernel as python import albedo/harness/kernel/cell_status import albedo/harness/sdk/image_codec -import albedo/harness/sdk/term_codec import albedo/openai_api/types +import albedo_bytes/term_codec import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode import gleam/erlang/atom diff --git a/src/albedo/harness/kernel/driver.gleam b/src/albedo/harness/kernel/driver.gleam index 6b76e98a4..4a27b5a65 100644 --- a/src/albedo/harness/kernel/driver.gleam +++ b/src/albedo/harness/kernel/driver.gleam @@ -44,9 +44,9 @@ import albedo/harness/kernel/supervision.{ } import albedo/harness/kernel/wedge import albedo/harness/kernel/wire -import albedo/harness/sdk/term_codec import albedo/process_helpers -import albedo/text_scalars +import albedo_bytes/scalars +import albedo_bytes/term_codec import albedo_clock import albedo_protect import gleam/bit_array @@ -992,7 +992,7 @@ fn job_summaries( Job( cell.id, None, - text_scalars.take(cell.command, 4096), + scalars.take(cell.command, 4096), False, "cell", None, @@ -1016,7 +1016,7 @@ fn job_summaries( Job( id, pid, - text_scalars.take(group.command, 4096), + scalars.take(group.command, 4096), group.service, "command", parent(group.cell), @@ -1028,7 +1028,7 @@ fn job_summaries( Job( job.id, None, - text_scalars.take(job.command, 4096), + scalars.take(job.command, 4096), job.service, "command", parent(job.cell), @@ -1578,7 +1578,7 @@ fn death_note(state: State) -> Option(String) { || string.contains(line, "received SIG") }) case list.last(fatal), list.last(lines) { - Ok(line), _ | Error(Nil), Ok(line) -> Some(text_scalars.take(line, 200)) + Ok(line), _ | Error(Nil), Ok(line) -> Some(scalars.take(line, 200)) Error(Nil), Error(Nil) -> None } } @@ -2858,7 +2858,7 @@ fn first_line(code: String) -> String { |> list.map(string.trim) |> list.find(fn(line) { line != "" && !string.starts_with(line, "#") }) { - Ok(line) -> text_scalars.take(line, 200) + Ok(line) -> scalars.take(line, 200) Error(Nil) -> "python cell" } } diff --git a/src/albedo/harness/kernel/journal.gleam b/src/albedo/harness/kernel/journal.gleam index e6e179940..6c947e5a4 100644 --- a/src/albedo/harness/kernel/journal.gleam +++ b/src/albedo/harness/kernel/journal.gleam @@ -4,11 +4,11 @@ import albedo/cell_title import albedo/harness/kernel as python import albedo/harness/kernel/cell_codec import albedo/harness/kernel/cell_status -import albedo/harness/sdk/ids import albedo/harness/sdk/images import albedo/harness/sdk/storage import albedo/harness/sdk/store import albedo/openai_api/types +import albedo_bytes/ids import gleam/bool import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode diff --git a/src/albedo/harness/kernel/native.gleam b/src/albedo/harness/kernel/native.gleam index 45a309916..70c8a6ccb 100644 --- a/src/albedo/harness/kernel/native.gleam +++ b/src/albedo/harness/kernel/native.gleam @@ -6,7 +6,7 @@ //// socket returns a value instead, so a dead peer never crashes its owner. import albedo/harness/process_name as python_name -import albedo/harness/sdk/term_codec +import albedo_bytes/term_codec import albedo_protect import gleam/bit_array import gleam/bytes_tree.{type BytesTree} diff --git a/src/albedo/harness/kernel/python_tree.gleam b/src/albedo/harness/kernel/python_tree.gleam index b44cf6399..33c7128e3 100644 --- a/src/albedo/harness/kernel/python_tree.gleam +++ b/src/albedo/harness/kernel/python_tree.gleam @@ -16,13 +16,13 @@ //// lock, so a boot never starts from a copy that pruning is removing. import albedo/harness/file_info -import albedo/harness/sdk/digest import albedo/harness/sdk/file import albedo/harness/sdk/log import albedo/harness/sdk/settings import albedo/harness/sdk/subprocess import albedo/harness/shipped -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/crypto @@ -347,7 +347,7 @@ fn prune_unclaimed(root: String, keep: String) -> Result(Nil, Saved) { let claimed = claimed() names |> list.filter(fn(name) { - text_scalars.length(name) == 64 + scalars.length(name) == 64 && name != keep && !list.contains(claimed, name) && is_dir(join(root, name)) diff --git a/src/albedo/harness/kernel/signal_helper.gleam b/src/albedo/harness/kernel/signal_helper.gleam index b10439d98..4d66fc058 100644 --- a/src/albedo/harness/kernel/signal_helper.gleam +++ b/src/albedo/harness/kernel/signal_helper.gleam @@ -8,7 +8,7 @@ //// helper has the request, so interpreter start has its own, longer budget. import albedo/harness/process_name as python_name -import albedo/harness/sdk/term_codec +import albedo_bytes/term_codec import albedo_protect import gleam/bit_array import gleam/dict.{type Dict} diff --git a/src/albedo/harness/languages.gleam b/src/albedo/harness/languages.gleam index cfbc125e4..9725e18af 100644 --- a/src/albedo/harness/languages.gleam +++ b/src/albedo/harness/languages.gleam @@ -3,8 +3,8 @@ //// `priv/linguist/languages.yml`, parsed with yamerl the first time a //// language is asked for and kept in a persistent term from then on. -import albedo/harness/sdk/term import albedo/harness/shipped +import albedo_bytes/term import albedo_protect import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/loop.gleam b/src/albedo/harness/loop.gleam index 21f8f79f2..82c75bb04 100644 --- a/src/albedo/harness/loop.gleam +++ b/src/albedo/harness/loop.gleam @@ -32,7 +32,7 @@ import albedo/harness/sdk/transcript import albedo/harness/size import albedo/openai_api/refusal import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/bool import gleam/dict @@ -1486,8 +1486,8 @@ fn display_text(items: List(types.Input)) -> String { /// Truncated views of oversized text: `limit` scalars, then why it ends. fn bounded(text: String, limit: Int, note: String) -> String { - case text_scalars.length(text) > limit { - True -> text_scalars.take(text, limit) <> note + case scalars.length(text) > limit { + True -> scalars.take(text, limit) <> note False -> text } } diff --git a/src/albedo/harness/project_files.gleam b/src/albedo/harness/project_files.gleam index d2f020d29..7e9619919 100644 --- a/src/albedo/harness/project_files.gleam +++ b/src/albedo/harness/project_files.gleam @@ -7,12 +7,12 @@ import albedo/harness/file_info import albedo/harness/location -import albedo/harness/sdk/digest import albedo/harness/sdk/file import albedo/harness/sdk/file_status import albedo/harness/sdk/settings import albedo/harness/ssh -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/dict @@ -114,7 +114,7 @@ fn mirror_name(workspace: String) -> String { bit_array.from_string(workspace) |> digest.sha256 |> digest.hex - |> text_scalars.take(24) + |> scalars.take(24) } /// The file whose presence marks a whole mirror and whose mtime says how diff --git a/src/albedo/harness/repetition.gleam b/src/albedo/harness/repetition.gleam index 49667dce1..c68413299 100644 --- a/src/albedo/harness/repetition.gleam +++ b/src/albedo/harness/repetition.gleam @@ -5,7 +5,7 @@ import albedo/harness/sdk/settings import albedo/harness/similarity -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic/decode import gleam/int import gleam/list @@ -100,7 +100,7 @@ fn signature(cells: List(Cell)) -> Option(Step) { let code = list.map(cells, fn(cell) { cell.code }) |> string.join("\n") - |> text_scalars.take(compared) + |> scalars.take(compared) Some(Step(calls, code)) } } diff --git a/src/albedo/harness/runtime/catalog.gleam b/src/albedo/harness/runtime/catalog.gleam index 17d80d9e0..d9efcfe2c 100644 --- a/src/albedo/harness/runtime/catalog.gleam +++ b/src/albedo/harness/runtime/catalog.gleam @@ -7,16 +7,16 @@ import albedo/harness/capability_catalog import albedo/harness/extension/selection import albedo/harness/instruction_files import albedo/harness/project_files -import albedo/harness/sdk/digest import albedo/harness/sdk/extension import albedo/harness/sdk/follow import albedo/harness/sdk/settings_store import albedo/harness/sdk/storage import albedo/harness/sdk/store -import albedo/harness/sdk/term_codec import albedo/harness/settings/composition import albedo/harness/skills/catalog as skills import albedo/harness/skills/files as skill_files +import albedo_bytes/digest +import albedo_bytes/term_codec import albedo_protect import gleam/bit_array import gleam/dict.{type Dict} diff --git a/src/albedo/harness/sdk/auth_accounts.gleam b/src/albedo/harness/sdk/auth_accounts.gleam index 019388a44..8b925402f 100644 --- a/src/albedo/harness/sdk/auth_accounts.gleam +++ b/src/albedo/harness/sdk/auth_accounts.gleam @@ -6,7 +6,6 @@ //// the wire bytes match the stored terms field for field. import albedo/harness/sdk/credential_store.{type Object} -import albedo/harness/sdk/digest import albedo/harness/sdk/http_api import albedo/harness/sdk/key_accounts import albedo/harness/sdk/key_environment.{type KeyEnvironment} @@ -14,7 +13,8 @@ import albedo/harness/sdk/oauth.{type Account, type Login} import albedo/harness/sdk/profiles import albedo/harness/sdk/rotation import albedo/harness/sdk/settings_store -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import albedo_protect import gleam/bit_array import gleam/crypto @@ -85,8 +85,8 @@ pub fn signed_in( "_albedo_account_meta", object([ #("provider", dyn(provider)), - #("label", dyn(text_scalars.take(described.label, 256))), - #("detail", dyn(text_scalars.take(described.detail, 4096))), + #("label", dyn(scalars.take(described.label, 256))), + #("detail", dyn(scalars.take(described.detail, 4096))), ]), ) let next = case list.any(values, matches) { @@ -474,8 +474,8 @@ fn saved_projection( "_albedo_account_meta", object([ #("provider", dyn(login.provider)), - #("label", dyn(text_scalars.take(described.label, 256))), - #("detail", dyn(text_scalars.take(described.detail, 4096))), + #("label", dyn(scalars.take(described.label, 256))), + #("detail", dyn(scalars.take(described.detail, 4096))), #("kind", dyn(kind(login))), ]), ) diff --git a/src/albedo/harness/sdk/credentials.gleam b/src/albedo/harness/sdk/credentials.gleam index 8f64902f0..aa152ae80 100644 --- a/src/albedo/harness/sdk/credentials.gleam +++ b/src/albedo/harness/sdk/credentials.gleam @@ -2,13 +2,13 @@ import albedo/harness/sdk/accounts import albedo/harness/sdk/credential_store.{type Object} -import albedo/harness/sdk/digest import albedo/harness/sdk/getenv import albedo/harness/sdk/key_accounts import albedo/harness/sdk/key_environment.{type KeyEnvironment} import albedo/harness/sdk/rotation import albedo/harness/sdk/settings_store -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import albedo_protect import gleam/dict import gleam/dynamic.{type Dynamic} @@ -26,7 +26,7 @@ import gleam/string pub fn fingerprint(value: String) -> String { digest.sha256(<>) |> digest.hex - |> text_scalars.take(16) + |> scalars.take(16) } /// A named profile's credential-overlay settings. Missing profiles decode as diff --git a/src/albedo/harness/sdk/family.gleam b/src/albedo/harness/sdk/family.gleam index f0fb8c956..c69342c44 100644 --- a/src/albedo/harness/sdk/family.gleam +++ b/src/albedo/harness/sdk/family.gleam @@ -10,7 +10,7 @@ import albedo/harness/sdk/session_identity.{Address, Here, Member, Stub, There} as identity import albedo/harness/sdk/storage import albedo/harness/sdk/store -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic/decode import gleam/list import gleam/option.{type Option, None, Some} @@ -115,7 +115,7 @@ pub fn valid_name(name: String) -> Result(Nil, NameFailure) { let allowed = string.to_graphemes("abcdefghijklmnopqrstuvwxyz0123456789-_") case name != "" - && text_scalars.length(name) <= 32 + && scalars.length(name) <= 32 && list.all(string.to_graphemes(name), list.contains(allowed, _)) { False -> Error(BadShape) @@ -185,7 +185,7 @@ pub fn set_role( ) -> Result(Nil, String) { use _ <- result.try(storage.permit(db, module, "set_role")) let value = string.trim(value) - case text_scalars.length(value) <= 512 { + case scalars.length(value) <= 512 { False -> Error("role is at most 512 characters") True -> storage.write(db, "UPDATE sessions SET role=? WHERE id=?", [ @@ -230,7 +230,7 @@ pub fn set_verifier( ) -> Result(Nil, String) { use _ <- result.try(storage.permit(db, module, "set_verifier")) let command = string.trim(command) - case text_scalars.length(command) <= 4096 { + case scalars.length(command) <= 4096 { False -> Error("verifier is at most 4096 characters") True -> storage.write(db, "UPDATE session_family SET verifier=? WHERE session=?", [ diff --git a/src/albedo/harness/sdk/http_api.gleam b/src/albedo/harness/sdk/http_api.gleam index 3b6209c65..91197bb98 100644 --- a/src/albedo/harness/sdk/http_api.gleam +++ b/src/albedo/harness/sdk/http_api.gleam @@ -4,7 +4,7 @@ //// these definitions so both follow the same wire conventions. import albedo/harness/ssh -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import albedo_protect import gleam/bit_array @@ -847,5 +847,5 @@ fn negotiate(req: request.Request(a), live: Bool) -> Result(Bool, Failure) { } pub fn scalar_prefix(text: String, limit: Int) -> String { - text_scalars.take(text, limit) + scalars.take(text, limit) } diff --git a/src/albedo/harness/sdk/image_codec.gleam b/src/albedo/harness/sdk/image_codec.gleam index c1fbce500..24ce9129a 100644 --- a/src/albedo/harness/sdk/image_codec.gleam +++ b/src/albedo/harness/sdk/image_codec.gleam @@ -12,10 +12,10 @@ //// are the ones earlier daemons wrote. Golden rows in //// `test/fixtures/transcript-codec-golden.txt` pin them. -import albedo/harness/sdk/digest import albedo/harness/sdk/image as image_payload -import albedo/harness/sdk/term_codec import albedo/openai_api/types +import albedo_bytes/digest +import albedo_bytes/term_codec import albedo_protect import gleam/bit_array import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/sdk/mail.gleam b/src/albedo/harness/sdk/mail.gleam index 4af25dba7..9e8edd868 100644 --- a/src/albedo/harness/sdk/mail.gleam +++ b/src/albedo/harness/sdk/mail.gleam @@ -16,9 +16,9 @@ //// its extension declares `Correspondence`. A host handle reaches every //// session's. -import albedo/harness/sdk/ids import albedo/harness/sdk/storage import albedo/harness/sdk/store +import albedo_bytes/ids import gleam/dynamic/decode import gleam/json import gleam/list diff --git a/src/albedo/harness/sdk/oauth_flows.gleam b/src/albedo/harness/sdk/oauth_flows.gleam index c1ffd520e..78299a432 100644 --- a/src/albedo/harness/sdk/oauth_flows.gleam +++ b/src/albedo/harness/sdk/oauth_flows.gleam @@ -11,14 +11,14 @@ import albedo/harness/sdk/auth_accounts import albedo/harness/sdk/credential_store -import albedo/harness/sdk/digest import albedo/harness/sdk/http_api -import albedo/harness/sdk/ids import albedo/harness/sdk/network import albedo/harness/sdk/network_context import albedo/harness/sdk/oauth.{type Account, type Keep, type Login} import albedo/harness/sdk/oauth_listener import albedo/harness/sdk/settings_store +import albedo_bytes/digest +import albedo_bytes/ids import albedo_clock import albedo_protect import gleam/bit_array diff --git a/src/albedo/harness/sdk/rotation.gleam b/src/albedo/harness/sdk/rotation.gleam index edf4b98ee..71f4775c8 100644 --- a/src/albedo/harness/sdk/rotation.gleam +++ b/src/albedo/harness/sdk/rotation.gleam @@ -9,10 +9,10 @@ //// holds the ordering and limit bookkeeping those usually share. import albedo/harness/sdk/accounts -import albedo/harness/sdk/digest import albedo/harness/sdk/extension import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import gleam/bit_array import gleam/erlang/process import gleam/list @@ -102,7 +102,7 @@ pub fn key_bytes_label(key: BitArray) -> String { key |> digest.sha256 |> bit_array.base16_encode - |> text_scalars.take(8) + |> scalars.take(8) |> string.lowercase } diff --git a/src/albedo/harness/sdk/search.gleam b/src/albedo/harness/sdk/search.gleam index f8085ef28..f163bd1e0 100644 --- a/src/albedo/harness/sdk/search.gleam +++ b/src/albedo/harness/sdk/search.gleam @@ -10,7 +10,7 @@ import albedo/harness/compaction import albedo/harness/sdk/history import albedo/harness/sdk/storage import albedo/harness/sdk/tool -import albedo/text_scalars +import albedo_bytes/scalars import gleam/bool import gleam/dynamic/decode import gleam/int @@ -30,7 +30,7 @@ pub type Match { /// `pattern` trimmed, checked, and lowercased for matching. pub fn needle(pattern: String) -> Result(String, String) { let pattern = string.trim(pattern) - case pattern != "" && text_scalars.length(pattern) <= 200 { + case pattern != "" && scalars.length(pattern) <= 200 { True -> Ok(string.lowercase(pattern)) False -> Error("search pattern must be 1..200 characters") } @@ -355,7 +355,7 @@ fn probe(needle: String) -> String { False -> #(longest, "") True -> { let current = current <> grapheme - case text_scalars.length(current) > text_scalars.length(longest) { + case scalars.length(current) > scalars.length(longest) { True -> #(current, current) False -> #(longest, current) } @@ -395,11 +395,11 @@ pub fn kind(text: String) -> String { /// contains `needle`, case-insensitively. pub fn preview(text: String, needle: String) -> String { let at = case string.split_once(string.lowercase(text), needle) { - Ok(#(before, _)) -> text_scalars.length(before) + Ok(#(before, _)) -> scalars.length(before) Error(_) -> 0 } case at > 120 { - True -> "…" <> tool.excerpt(text_scalars.drop(text, at - 100), 400) + True -> "…" <> tool.excerpt(scalars.drop(text, at - 100), 400) False -> tool.excerpt(text, 400) } } diff --git a/src/albedo/harness/sdk/tool.gleam b/src/albedo/harness/sdk/tool.gleam index 2da97d70e..c8c0b2fb3 100644 --- a/src/albedo/harness/sdk/tool.gleam +++ b/src/albedo/harness/sdk/tool.gleam @@ -10,7 +10,7 @@ import albedo/harness/sdk/message_content import albedo/harness/sdk/storage import albedo/harness/sdk/transcript import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import albedo_clock import gleam/dict import gleam/dynamic/decode @@ -155,13 +155,13 @@ fn image_description(image: types.Image) -> String { <> "x" <> int.to_string(height) <> " #" - <> text_scalars.take(types.image_identity(image), 8) + <> scalars.take(types.image_identity(image), 8) } /// `value` cut to `maximum` scalars with an ellipsis. pub fn excerpt(value: String, maximum: Int) -> String { - case text_scalars.length(value) > maximum { - True -> text_scalars.take(value, maximum) <> "…" + case scalars.length(value) > maximum { + True -> scalars.take(value, maximum) <> "…" False -> value } } diff --git a/src/albedo/harness/sdk/transcript_codec.gleam b/src/albedo/harness/sdk/transcript_codec.gleam index 79e6a2830..e3e9d470a 100644 --- a/src/albedo/harness/sdk/transcript_codec.gleam +++ b/src/albedo/harness/sdk/transcript_codec.gleam @@ -3,9 +3,9 @@ //// attaches lazy image payload readers; it does not fetch stored images. import albedo/harness/sdk/image_codec -import albedo/harness/sdk/term_codec import albedo/harness/sdk/transcript import albedo/openai_api/types +import albedo_bytes/term_codec import albedo_protect import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode diff --git a/src/albedo/harness/sdk/web_search.gleam b/src/albedo/harness/sdk/web_search.gleam index d30b08b4b..82bfccc78 100644 --- a/src/albedo/harness/sdk/web_search.gleam +++ b/src/albedo/harness/sdk/web_search.gleam @@ -7,7 +7,7 @@ import albedo/harness/sdk/storage import albedo/openai_api import albedo/openai_api/stream import albedo/openai_api/types -import albedo/text_scalars +import albedo_bytes/scalars import gleam/dynamic/decode import gleam/erlang/process import gleam/int @@ -103,7 +103,7 @@ pub fn failure(error: types.Error) -> String { } fn short(detail: String) -> String { - detail |> string.trim |> text_scalars.take(300) + detail |> string.trim |> scalars.take(300) } /// The state `hear` folds `exchange`'s streamed payloads into, from diff --git a/src/albedo/harness/settings/composition.gleam b/src/albedo/harness/settings/composition.gleam index c847ed101..89af1ab68 100644 --- a/src/albedo/harness/settings/composition.gleam +++ b/src/albedo/harness/settings/composition.gleam @@ -1,9 +1,9 @@ //// Read-only settings inputs for composition discovery and admission checks. //// Both reads hold the per-home settings lock. -import albedo/harness/sdk/digest import albedo/harness/sdk/settings_store import albedo/harness/settings/projection +import albedo_bytes/digest import gleam/dict import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode diff --git a/src/albedo/harness/skills/files.gleam b/src/albedo/harness/skills/files.gleam index 52dadc662..bb7b6b2de 100644 --- a/src/albedo/harness/skills/files.gleam +++ b/src/albedo/harness/skills/files.gleam @@ -8,10 +8,10 @@ import albedo/harness/file_fingerprint import albedo/harness/file_info -import albedo/harness/sdk/digest import albedo/harness/sdk/file -import albedo/harness/sdk/term_codec import albedo/harness/skills/frontmatter +import albedo_bytes/digest +import albedo_bytes/term_codec import albedo_protect import gleam/bit_array import gleam/dict.{type Dict} diff --git a/src/albedo/harness/skills/frontmatter.gleam b/src/albedo/harness/skills/frontmatter.gleam index 2a16f3409..04657a94e 100644 --- a/src/albedo/harness/skills/frontmatter.gleam +++ b/src/albedo/harness/skills/frontmatter.gleam @@ -2,7 +2,7 @@ //// the name and description rules. import albedo/harness/sdk/file -import albedo/text_scalars +import albedo_bytes/scalars import albedo_protect import gleam/bit_array import gleam/dynamic.{type Dynamic} @@ -230,10 +230,7 @@ fn validate( None -> True Some(value) -> valid_name(value) } - case - name_valid, - description != "" && text_scalars.length(description) <= 1024 - { + case name_valid, description != "" && scalars.length(description) <= 1024 { True, True -> Ok(#(name, description)) False, _ -> Error("frontmatter name is invalid") _, False -> Error("frontmatter description is invalid") diff --git a/src/albedo/harness/trust.gleam b/src/albedo/harness/trust.gleam index 86e4d4968..29421e2be 100644 --- a/src/albedo/harness/trust.gleam +++ b/src/albedo/harness/trust.gleam @@ -3,9 +3,9 @@ //// verifier and leave it in the store OTP reads (macOS revokes during trust //// evaluation), so OTP keeps trusting it until it is listed here. -import albedo/harness/sdk/digest import albedo/harness/sdk/settings -import albedo/harness/sdk/term_codec +import albedo_bytes/digest +import albedo_bytes/term_codec import albedo_protect import gleam/bit_array import gleam/dynamic.{type Dynamic} diff --git a/src/albedo/harness/worktree.gleam b/src/albedo/harness/worktree.gleam index eec56b0a6..1603f4c31 100644 --- a/src/albedo/harness/worktree.gleam +++ b/src/albedo/harness/worktree.gleam @@ -1,13 +1,13 @@ import albedo/daemon/bus import albedo/harness/local_command import albedo/harness/location -import albedo/harness/sdk/digest import albedo/harness/sdk/file import albedo/harness/sdk/follow import albedo/harness/sdk/settings import albedo/harness/sdk/storage import albedo/harness/sdk/store -import albedo/text_scalars +import albedo_bytes/digest +import albedo_bytes/scalars import albedo_clock import gleam/bit_array import gleam/bool @@ -118,7 +118,7 @@ const allowed_chars = pub fn valid_name(name: String) -> Result(String, String) { let trimmed = string.trim(name) let allowed = string.to_graphemes(allowed_chars) - let length = text_scalars.length(trimmed) + let length = scalars.length(trimmed) case trimmed != "" && length <= 48 @@ -142,7 +142,7 @@ pub fn repo_slug(repo_root: String) -> String { |> list.last |> result.unwrap("repo") |> sanitize_slug - let hash = text_scalars.take(sha256_hex(normalized), 8) + let hash = scalars.take(sha256_hex(normalized), 8) base <> "-" <> hash } @@ -158,7 +158,7 @@ pub fn sanitize_slug(text: String) -> String { }) |> string.concat |> trim_dashes - |> text_scalars.take(24) + |> scalars.take(24) |> trim_dashes case cleaned { "" -> "repo" @@ -280,7 +280,7 @@ pub fn create_for_agent( Error(_) -> base_name <> "-" - <> text_scalars.take( + <> scalars.take( sha256_hex(base_name <> ":" <> int.to_string(albedo_clock.system_ms())), 6, ) @@ -674,7 +674,7 @@ fn choose_name( _ -> { Ok( "wt-" - <> text_scalars.take( + <> scalars.take( sha256_hex( repo_root <> ":" <> int.to_string(albedo_clock.system_ms()), ), @@ -796,10 +796,10 @@ pub fn relative_subdir(checkout_root: String, dir: String) -> String { True -> "" False -> case root { - "/" -> text_scalars.drop(target, 1) + "/" -> scalars.drop(target, 1) _ -> case string.starts_with(target, root <> "/") { - True -> text_scalars.drop(target, text_scalars.length(root) + 1) + True -> scalars.drop(target, scalars.length(root) + 1) False -> "" } } @@ -836,10 +836,7 @@ pub fn present(value: Option(String)) -> Option(String) { pub fn strip_suffix(text: String, suffix: String) -> Option(String) { case string.ends_with(text, suffix) { True -> - Some(text_scalars.take( - text, - text_scalars.length(text) - text_scalars.length(suffix), - )) + Some(scalars.take(text, scalars.length(text) - scalars.length(suffix))) False -> None } } diff --git a/src/albedo/openai_api/chat.gleam b/src/albedo/openai_api/chat.gleam index 67e64ca51..ea582e1d1 100644 --- a/src/albedo/openai_api/chat.gleam +++ b/src/albedo/openai_api/chat.gleam @@ -1,10 +1,10 @@ -import albedo/harness/sdk/term import albedo/openai_api/cost import albedo/openai_api/fields import albedo/openai_api/reasoning import albedo/openai_api/refusal import albedo/openai_api/replay import albedo/openai_api/types +import albedo_bytes/term import gleam/dict.{type Dict} import gleam/dynamic import gleam/dynamic/decode diff --git a/src/albedo/openai_api/fields.gleam b/src/albedo/openai_api/fields.gleam index 2bbce9905..dc2d68ade 100644 --- a/src/albedo/openai_api/fields.gleam +++ b/src/albedo/openai_api/fields.gleam @@ -6,7 +6,7 @@ //// The parser's objects are Erlang maps with string keys, so a read is one //// map lookup and one runtime type test. Its strings are already valid UTF-8. -import albedo/harness/sdk/term +import albedo_bytes/term import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode diff --git a/src/albedo/openai_api/pool.gleam b/src/albedo/openai_api/pool.gleam index b02efdbbe..16d19d3fe 100644 --- a/src/albedo/openai_api/pool.gleam +++ b/src/albedo/openai_api/pool.gleam @@ -8,10 +8,10 @@ //// dropped. import albedo/harness/sdk/network.{type Route} -import albedo/harness/sdk/term import albedo/openai_api/connect.{type Transport} import albedo/openai_api/framing.{type Framing} import albedo/openai_api/socket.{type Socket} +import albedo_bytes/term import albedo_clock import gleam/bit_array import gleam/dict.{type Dict} diff --git a/src/albedo/openai_api/sse.gleam b/src/albedo/openai_api/sse.gleam index 7f26b3cb0..bc056a363 100644 --- a/src/albedo/openai_api/sse.gleam +++ b/src/albedo/openai_api/sse.gleam @@ -1,6 +1,6 @@ //// Incremental framing. Only newly received bytes are searched for line endings. -import albedo/harness/sdk/bytes as byte_ops +import albedo_bytes as byte_ops import gleam/bit_array import gleam/int import gleam/list diff --git a/test/daemon/albedo_bedrock_replay_preflight_test_support.erl b/test/daemon/albedo_bedrock_replay_preflight_test_support.erl index 3ae3dccba..56c2aec3c 100644 --- a/test/daemon/albedo_bedrock_replay_preflight_test_support.erl +++ b/test/daemon/albedo_bedrock_replay_preflight_test_support.erl @@ -7,7 +7,7 @@ with_home(Run) -> Home = unicode:characters_to_binary( filename:join( os:getenv("TMPDIR", "/tmp"), - "albedo-replay-preflight-" ++ binary_to_list('albedo@harness@sdk@ids':random_hex(16)) + "albedo-replay-preflight-" ++ binary_to_list(albedo_bytes@ids:random_hex(16)) ) ), ok = file:make_dir(Home), diff --git a/test/daemon/albedo_text_scalars_test_support.erl b/test/daemon/albedo_text_scalars_test_support.erl deleted file mode 100644 index df16bde74..000000000 --- a/test/daemon/albedo_text_scalars_test_support.erl +++ /dev/null @@ -1,4 +0,0 @@ --module(albedo_text_scalars_test_support). --export([referenced_size/1]). - -referenced_size(Text) -> binary:referenced_byte_size(Text). diff --git a/test/daemon/session_name_scalars_test.gleam b/test/daemon/session_name_scalars_test.gleam new file mode 100644 index 000000000..d731f1c62 --- /dev/null +++ b/test/daemon/session_name_scalars_test.gleam @@ -0,0 +1,19 @@ +//// Session names are cut by Unicode scalars, so a run of combining marks is +//// kept whole up to the limit rather than dropped as one grapheme. + +import albedo/daemon/session_configuration +import albedo_bytes/scalars +import gleam/option.{Some} +import gleam/string +import gleeunit/should + +pub fn names_mask_controls_and_limit_scalars_without_splitting_marks_test() -> Nil { + session_configuration.clean_name( + " a\u{0000}\u{0085}\u{00AD}\u{200B}\u{200E}\u{2028}\u{2060}\u{FEFF} b ", + ) + |> should.equal(Some("a b")) + let text = "x" <> string.repeat("\u{0301}", 5000) + let assert Some(clean) = session_configuration.clean_name(text) + scalars.length(clean) |> should.equal(4096) + clean |> should.equal("x" <> string.repeat("\u{0301}", 4095)) +} diff --git a/test/daemon/text_scalars_test.gleam b/test/daemon/text_scalars_test.gleam deleted file mode 100644 index 326996a84..000000000 --- a/test/daemon/text_scalars_test.gleam +++ /dev/null @@ -1,40 +0,0 @@ -//// Scalar and grapheme limits differ on combining marks. E2E cannot inspect -//// retained binary size or exhaust exact slicing boundaries deterministically. - -import albedo/daemon/session_configuration -import albedo/text_scalars -import gleam/option.{Some} -import gleam/string -import gleeunit/should - -@external(erlang, "albedo_text_scalars_test_support", "referenced_size") -fn referenced_size(text: String) -> Int - -pub fn scalar_slices_preserve_combining_marks_and_utf8_boundaries_test() -> Nil { - let text = "a\u{0301}😀z" - text_scalars.length(text) |> should.equal(4) - text_scalars.take(text, 2) |> should.equal("a\u{0301}") - text_scalars.drop(text, 2) |> should.equal("😀z") - text_scalars.tail(text, 2) |> should.equal("😀z") - text_scalars.take(text, -1) |> should.equal("") - text_scalars.drop(text, -1) |> should.equal(text) - text_scalars.tail(text, -1) |> should.equal("") - text_scalars.drop(text, 10) |> should.equal("") - text_scalars.take("", 2) |> should.equal("") - let large = string.repeat("😀", 100_000) - let prefix = text_scalars.take(large, 1) - let suffix = text_scalars.tail(large, 1) - referenced_size(prefix) |> should.equal(4) - referenced_size(suffix) |> should.equal(4) -} - -pub fn names_mask_controls_and_limit_scalars_without_splitting_marks_test() -> Nil { - session_configuration.clean_name( - " a\u{0000}\u{0085}\u{00AD}\u{200B}\u{200E}\u{2028}\u{2060}\u{FEFF} b ", - ) - |> should.equal(Some("a b")) - let text = "x" <> string.repeat("\u{0301}", 5000) - let assert Some(clean) = session_configuration.clean_name(text) - text_scalars.length(clean) |> should.equal(4096) - clean |> should.equal("x" <> string.repeat("\u{0301}", 4095)) -} diff --git a/test/e2e/areas.toml b/test/e2e/areas.toml index 2a1a170fb..7244e7b02 100644 --- a/test/e2e/areas.toml +++ b/test/e2e/areas.toml @@ -306,7 +306,7 @@ ] "src/albedo/harness/sdk/once.gleam" = ["model_catalogs_test.py"] "src/albedo/harness/extensions/github_copilot/**" = ["github_copilot_test.py"] -"src/albedo/harness/sdk/digest.gleam" = [ +"packages/albedo_bytes/src/albedo_bytes/digest.gleam" = [ "webhooks_test.py", "proxy_test.py", "antigravity_test.py", @@ -322,7 +322,7 @@ "model_catalogs_test.py", "mistral_test.py", ] -"src/albedo/harness/sdk/ids.gleam" = [ +"packages/albedo_bytes/src/albedo_bytes/ids.gleam" = [ "webhooks_test.py", "proxy_test.py", "antigravity_test.py", @@ -810,9 +810,11 @@ "provider_protocols_test.py", "opencode_go_login_test.py", ] -"src/albedo/harness/sdk/term.gleam" = ["model_catalogs_test.py"] +"packages/albedo_bytes/src/albedo_bytes/term.gleam" = ["model_catalogs_test.py"] "src/albedo/harness/sdk/text.gleam" = ["antigravity_test.py"] -"src/albedo/harness/sdk/bytes.gleam" = ["bedrock_effort_catalog_test.py"] +"packages/albedo_bytes/src/albedo_bytes.gleam" = [ + "bedrock_effort_catalog_test.py", +] # Workspace packages Bedrock signs and decodes its stream through. "packages/aws_sigv4/**" = ["bedrock_effort_catalog_test.py"] "packages/aws_event_stream/**" = ["bedrock_effort_catalog_test.py"] diff --git a/test/harness/albedo_antigravity_catalog_test_support.erl b/test/harness/albedo_antigravity_catalog_test_support.erl index 8dd60cbeb..2c851c4ae 100644 --- a/test/harness/albedo_antigravity_catalog_test_support.erl +++ b/test/harness/albedo_antigravity_catalog_test_support.erl @@ -5,7 +5,7 @@ home_with_catalog(Json) -> Home = filename:join( os:getenv("TMPDIR", "/tmp"), - "albedo-antigravity-catalog-" ++ binary_to_list('albedo@harness@sdk@ids':random_hex(16)) + "albedo-antigravity-catalog-" ++ binary_to_list(albedo_bytes@ids:random_hex(16)) ), ok = file:make_dir(Home), ok = file:write_file(filename:join(Home, "antigravity.json"), Json), diff --git a/test/harness/albedo_runtime_test_support.erl b/test/harness/albedo_runtime_test_support.erl index b66479d59..813d4cfe6 100644 --- a/test/harness/albedo_runtime_test_support.erl +++ b/test/harness/albedo_runtime_test_support.erl @@ -13,7 +13,7 @@ temporary_workspace() -> Path = filename:join( os:getenv("TMPDIR", "/tmp"), - "runtime-workspace-" ++ binary_to_list('albedo@harness@sdk@ids':random_hex(16)) + "runtime-workspace-" ++ binary_to_list(albedo_bytes@ids:random_hex(16)) ), ok = file:make_dir(Path), unicode:characters_to_binary(Path). @@ -24,7 +24,7 @@ temporary_database() -> unicode:characters_to_binary( filename:join( os:getenv("TMPDIR", "/tmp"), - "albedo-" ++ binary_to_list('albedo@harness@sdk@ids':random_hex(16)) ++ ".sqlite" + "albedo-" ++ binary_to_list(albedo_bytes@ids:random_hex(16)) ++ ".sqlite" ) ). cleanup(Path) -> diff --git a/test/harness/claude_wire_test.gleam b/test/harness/claude_wire_test.gleam index 1b748d7fc..081a61185 100644 --- a/test/harness/claude_wire_test.gleam +++ b/test/harness/claude_wire_test.gleam @@ -3,11 +3,11 @@ import albedo/harness/extensions/claude/fast import albedo/harness/extensions/claude/wire -import albedo/harness/sdk/digest import albedo/harness/sdk/file import albedo/harness/sdk/token_count import albedo/openai_api import albedo/openai_api/types +import albedo_bytes/digest import gleam/dict import gleam/dynamic import gleam/dynamic/decode diff --git a/test/harness/extension_digests_test.gleam b/test/harness/extension_digests_test.gleam new file mode 100644 index 000000000..18ff5914f --- /dev/null +++ b/test/harness/extension_digests_test.gleam @@ -0,0 +1,89 @@ +//// Hashes and ids that extensions store or send. +//// +//// Webhook signatures, proxy conversation names, Antigravity session numbers, +//// and stored credential fields all leave the daemon as text. A change of hex +//// case or bit layout would break a stored row or an upstream cache without +//// failing a scenario, so each value here is pinned to an expected encoding. +//// The seeds cover empty input, Unicode text, leading zero bytes, and signed +//// 64-bit overflow. albedo_bytes tests the digests and ids they build on. + +import albedo/harness/extensions/antigravity/wire as antigravity_wire +import albedo/harness/extensions/proxy/extension as proxy +import albedo/harness/extensions/webhooks/ledger as webhooks +import albedo/harness/sdk/credentials +import gleam/dynamic +import gleam/dynamic/decode +import gleam/string +import gleeunit/should + +const unicode = "héllo wörld ✓ 🚀" + +/// SHA-256 of this seed begins with a zero byte. +const zero_sha256 = "seed-93" + +const body = <<"{\"event\":\"push\"}":utf8>> + +const mac = "fa9ac3de029f5e967d289328ce5f550d49367ea440a4277db76c94951c370cfc" + +pub fn webhook_signature_accepts_the_hmac_after_the_prefix_test() -> Nil { + webhooks.verify(body, "sha256=" <> mac, "s3cr3t", "sha256=") + |> should.be_true + // Hex decoding has always accepted either case. + webhooks.verify(body, "sha256=" <> string.uppercase(mac), "s3cr3t", "sha256=") + |> should.be_true + webhooks.verify(body, mac, "s3cr3t", "") |> should.be_true +} + +pub fn webhook_signature_rejects_malformed_or_wrong_headers_test() -> Nil { + let check = fn(signature, secret) { + webhooks.verify(body, signature, secret, "sha256=") + } + check("sha1=" <> mac, "s3cr3t") |> should.be_false + check(mac, "s3cr3t") |> should.be_false + check("sha256=" <> string.slice(mac, 0, 62), "s3cr3t") |> should.be_false + check("sha256=" <> mac <> "00", "s3cr3t") |> should.be_false + check("sha256=" <> string.slice(mac, 0, 63) <> "g", "s3cr3t") + |> should.be_false + check("sha256=" <> mac, "other") |> should.be_false + check("", "s3cr3t") |> should.be_false +} + +pub fn proxy_conversation_names_twelve_digest_bytes_test() -> Nil { + proxy.conversation("") |> should.equal("proxy-e3b0c44298fc1c149afbf4c8") + proxy.conversation(unicode) |> should.equal("proxy-c1a19c07e61550d4eb0bec9e") + proxy.conversation(zero_sha256) + |> should.equal("proxy-00f05cfecece11c31532119a") +} + +pub fn antigravity_session_number_matches_signed_fnv1a64_test() -> Nil { + antigravity_wire.session_number("hello world") + |> should.equal("8618312879776256743") + antigravity_wire.session_number("seed-0") + |> should.equal("-3293997480711204455") + antigravity_wire.session_number(zero_sha256) + |> should.equal("-9217408853894395433") + antigravity_wire.session_number("") |> should.equal("-3750763034362895579") + antigravity_wire.session_number(unicode) + |> should.equal("-8847811134071694416") +} + +pub fn credential_text_reads_only_string_fields_test() -> Nil { + let credential = + dynamic.properties([ + #(dynamic.string("apiKey"), dynamic.string("sk-1")), + #(dynamic.string("label"), dynamic.int(5)), + ]) + credentials.text(credential, "apiKey") |> should.equal("sk-1") + credentials.text(credential, "label") |> should.equal("") + credentials.text(credential, "missing") |> should.equal("") + credentials.text(dynamic.string("not a map"), "apiKey") |> should.equal("") + let lenient = { + use key <- decode.optional_field("apiKey", "", credentials.lenient_text()) + use label <- decode.optional_field("label", "", credentials.lenient_text()) + decode.success(#(key, label)) + } + decode.run(credential, lenient) |> should.equal(Ok(#("sk-1", ""))) +} + +@external(erlang, "albedo_sdk_c_test_support", "webhook_headers") +pub fn non_binary_webhook_headers_are_false_test() -> Nil diff --git a/test/harness/sdk_digest_ids_test.gleam b/test/harness/sdk_digest_ids_test.gleam deleted file mode 100644 index dede02508..000000000 --- a/test/harness/sdk_digest_ids_test.gleam +++ /dev/null @@ -1,183 +0,0 @@ -//// Hashes, encodings and ids that extensions store or send. -//// -//// Webhook signatures and digests, proxy conversation names, Antigravity -//// session numbers and UUIDs, and stored credential fields all leave the -//// daemon as text. A change of hex case, padding or bit layout would break a -//// stored row or an upstream cache without failing a scenario, so each value -//// here is pinned to an expected encoding. The seeds cover empty input, Unicode -//// text, leading zero bytes, and signed 64-bit overflow. - -import albedo/harness/extensions/antigravity/wire as antigravity_wire -import albedo/harness/extensions/proxy/extension as proxy -import albedo/harness/extensions/webhooks/ledger as webhooks -import albedo/harness/sdk/credentials -import albedo/harness/sdk/digest -import albedo/harness/sdk/ids -import gleam/dynamic -import gleam/dynamic/decode -import gleam/list -import gleam/string -import gleeunit/should - -const unicode = "héllo wörld ✓ 🚀" - -/// SHA-256 of this seed begins with a zero byte. -const zero_sha256 = "seed-93" - -const lower_hex = "0123456789abcdef" - -const url_alphabet = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" - -fn only(text: String, allowed: String) -> Bool { - text - |> string.to_graphemes - |> list.all(fn(char) { string.contains(allowed, char) }) -} - -fn sha256_hex(text: String) -> String { - digest.sha256(<>) |> digest.hex -} - -pub fn sha256_hex_matches_stored_digests_test() -> Nil { - sha256_hex("") - |> should.equal( - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - ) - sha256_hex(unicode) - |> should.equal( - "c1a19c07e61550d4eb0bec9e7259bbf521b29ef90cede8cc2b5170f4046a7373", - ) - sha256_hex(zero_sha256) - |> should.equal( - "00f05cfecece11c31532119ada1dc2c85a47c28553c20462834a7241852dae2e", - ) -} - -pub fn sha1_hex_keeps_leading_zeros_test() -> Nil { - digest.sha1(<<"":utf8>>) - |> digest.hex - |> should.equal("da39a3ee5e6b4b0d3255bfef95601890afd80709") - digest.sha1(<<"seed-244":utf8>>) - |> digest.hex - |> should.equal("00bc9f79aeb6346203a80a37d9bbcb3c9d593b86") -} - -pub fn base64_is_padded_standard_test() -> Nil { - digest.base64(<<>>) |> should.equal("") - digest.base64(<<"a":utf8>>) |> should.equal("YQ==") - digest.base64(<<"ab":utf8>>) |> should.equal("YWI=") - digest.base64(<>) - |> should.equal("aMOpbGxvIHfDtnJsZCDinJMg8J+agA==") - digest.base64(<<0, 0, 1, 250, 251, 252, 253, 254, 255>>) - |> should.equal("AAAB+vv8/f7/") -} - -pub fn base64_url_is_unpadded_and_url_safe_test() -> Nil { - let bytes = <<0, 0, 1, 250, 251, 252, 253, 254, 255, 1>> - let text = digest.base64_url(bytes) - text |> should.equal("AAAB-vv8_f7_AQ") -} - -pub fn hmac_sha256_matches_signed_webhooks_test() -> Nil { - digest.hmac_sha256(<<>>, <<>>) - |> digest.hex - |> should.equal( - "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", - ) - digest.hmac_sha256(<<"{\"event\":\"push\"}":utf8>>, <>) - |> digest.hex - |> should.equal( - "3a8c46851a448660228bb8d6ffaa4fd93498e9e932bfc7d32f1548b1bcf97dba", - ) -} - -const body = <<"{\"event\":\"push\"}":utf8>> - -const mac = "fa9ac3de029f5e967d289328ce5f550d49367ea440a4277db76c94951c370cfc" - -pub fn webhook_signature_accepts_the_hmac_after_the_prefix_test() -> Nil { - webhooks.verify(body, "sha256=" <> mac, "s3cr3t", "sha256=") - |> should.be_true - // Hex decoding has always accepted either case. - webhooks.verify(body, "sha256=" <> string.uppercase(mac), "s3cr3t", "sha256=") - |> should.be_true - webhooks.verify(body, mac, "s3cr3t", "") |> should.be_true -} - -pub fn webhook_signature_rejects_malformed_or_wrong_headers_test() -> Nil { - let check = fn(signature, secret) { - webhooks.verify(body, signature, secret, "sha256=") - } - check("sha1=" <> mac, "s3cr3t") |> should.be_false - check(mac, "s3cr3t") |> should.be_false - check("sha256=" <> string.slice(mac, 0, 62), "s3cr3t") |> should.be_false - check("sha256=" <> mac <> "00", "s3cr3t") |> should.be_false - check("sha256=" <> string.slice(mac, 0, 63) <> "g", "s3cr3t") - |> should.be_false - check("sha256=" <> mac, "other") |> should.be_false - check("", "s3cr3t") |> should.be_false -} - -pub fn proxy_conversation_names_twelve_digest_bytes_test() -> Nil { - proxy.conversation("") |> should.equal("proxy-e3b0c44298fc1c149afbf4c8") - proxy.conversation(unicode) |> should.equal("proxy-c1a19c07e61550d4eb0bec9e") - proxy.conversation(zero_sha256) - |> should.equal("proxy-00f05cfecece11c31532119a") -} - -pub fn seeded_uuid_keeps_the_version_and_variant_layout_test() -> Nil { - ids.seeded_uuid("") |> should.equal("e3b0c442-98fc-4c14-9afb-f4c8996fb924") - ids.seeded_uuid("session-1") - |> should.equal("84097828-fc31-48c8-9292-10df48901a85") - ids.seeded_uuid(unicode) - |> should.equal("c1a19c07-e615-40d4-ab0b-ec9e7259bbf5") - ids.seeded_uuid(zero_sha256) - |> should.equal("00f05cfe-cece-41c3-9532-119ada1dc2c8") -} - -pub fn antigravity_session_number_matches_signed_fnv1a64_test() -> Nil { - antigravity_wire.session_number("hello world") - |> should.equal("8618312879776256743") - antigravity_wire.session_number("seed-0") - |> should.equal("-3293997480711204455") - antigravity_wire.session_number(zero_sha256) - |> should.equal("-9217408853894395433") - antigravity_wire.session_number("") |> should.equal("-3750763034362895579") - antigravity_wire.session_number(unicode) - |> should.equal("-8847811134071694416") -} - -pub fn random_ids_have_their_length_and_alphabet_test() -> Nil { - let hex = ids.random_hex(16) - string.length(hex) |> should.equal(32) - only(hex, lower_hex) |> should.be_true - let url = ids.random_url(32) - string.length(url) |> should.equal(43) - only(url, url_alphabet) |> should.be_true - ids.random_hex(16) |> should.not_equal(hex) - let first = ids.unique_int() - let second = ids.unique_int() - { first > 0 && second > 0 && first != second } |> should.be_true -} - -pub fn credential_text_reads_only_string_fields_test() -> Nil { - let credential = - dynamic.properties([ - #(dynamic.string("apiKey"), dynamic.string("sk-1")), - #(dynamic.string("label"), dynamic.int(5)), - ]) - credentials.text(credential, "apiKey") |> should.equal("sk-1") - credentials.text(credential, "label") |> should.equal("") - credentials.text(credential, "missing") |> should.equal("") - credentials.text(dynamic.string("not a map"), "apiKey") |> should.equal("") - let lenient = { - use key <- decode.optional_field("apiKey", "", credentials.lenient_text()) - use label <- decode.optional_field("label", "", credentials.lenient_text()) - decode.success(#(key, label)) - } - decode.run(credential, lenient) |> should.equal(Ok(#("sk-1", ""))) -} - -@external(erlang, "albedo_sdk_c_test_support", "webhook_headers") -pub fn non_binary_webhook_headers_are_false_test() -> Nil diff --git a/test/harness/transcript_codec_golden_test.gleam b/test/harness/transcript_codec_golden_test.gleam index 0ab4e390d..c027058ae 100644 --- a/test/harness/transcript_codec_golden_test.gleam +++ b/test/harness/transcript_codec_golden_test.gleam @@ -5,12 +5,12 @@ //// hashes it references. Regenerate it only from a codec that is known to //// match the rows earlier daemons wrote. -import albedo/harness/sdk/digest import albedo/harness/sdk/image_codec -import albedo/harness/sdk/term_codec import albedo/harness/sdk/transcript import albedo/harness/sdk/transcript_codec import albedo/openai_api/types +import albedo_bytes/digest +import albedo_bytes/term_codec import gleam/bit_array import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} -- knotmirror 0.0.1