diff --git a/Cargo.lock b/Cargo.lock index efc7a32..5f26839 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,6 +31,18 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" + [[package]] name = "arraydeque" version = "0.5.1" @@ -176,6 +188,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.13.1" @@ -193,6 +211,9 @@ name = "bumpalo" version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +dependencies = [ + "allocator-api2", +] [[package]] name = "bytecount" @@ -222,6 +243,187 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "cranelift-assembler-x64" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b242b4c3675139f52f0b55624fb92571551a344305c5998f55ad20fa527bc55" +dependencies = [ + "cranelift-assembler-x64-meta", +] + +[[package]] +name = "cranelift-assembler-x64-meta" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "499715f19799219f32641b14f2a162f91e50bc1b61c2d2184c2be971716f5c56" +dependencies = [ + "cranelift-srcgen", +] + +[[package]] +name = "cranelift-bforest" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebca2ea7c62c56feb88a5b23ec380460fe6d7c18134520f6ddf4bfa35cbea68" +dependencies = [ + "cranelift-entity", +] + +[[package]] +name = "cranelift-bitset" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe11f154b62d7421d909503a746e89995393b1b71926e6f12b08a2076396d7fb" +dependencies = [ + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-codegen" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f2d0da3d51979dc0183fac3076a535477eab794716b063143ecb16632408664" +dependencies = [ + "bumpalo", + "cranelift-assembler-x64", + "cranelift-bforest", + "cranelift-bitset", + "cranelift-codegen-meta", + "cranelift-codegen-shared", + "cranelift-control", + "cranelift-entity", + "cranelift-isle", + "gimli", + "hashbrown 0.15.5", + "libm", + "log", + "regalloc2", + "rustc-hash", + "serde", + "smallvec", + "target-lexicon", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-codegen-meta" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "483b2c94a1b7f6fba0714387ba34ca56d114b2214a80be018acbb2ed40e09a1e" +dependencies = [ + "cranelift-assembler-x64-meta", + "cranelift-codegen-shared", + "cranelift-srcgen", + "heck", +] + +[[package]] +name = "cranelift-codegen-shared" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4aae718c336a52d90d4ebe9a2d8c3cf0906a4bee78f0e6867e777eebbe554fe" + +[[package]] +name = "cranelift-control" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a18e94519070dc56cddb71906a08cea6a28a1d7c58ed501b88f273fa6b45fa07" +dependencies = [ + "arbitrary", +] + +[[package]] +name = "cranelift-entity" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0ab4e0eff1045ff2f5ddd8195bf3c97d7b5ef9b780cb044e0cce76e4d352057" +dependencies = [ + "cranelift-bitset", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-frontend" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7645a236e1ec49e660f09ec9fa979a1c5d0b612c419db7610573d4d58a03b7c" +dependencies = [ + "cranelift-codegen", + "log", + "smallvec", + "target-lexicon", +] + +[[package]] +name = "cranelift-isle" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57e0b4a1a0ea01cc19084ff01aaeb640dfe22905d47d83037a419b81ba587ed0" + +[[package]] +name = "cranelift-jit" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c68c397bbf844f50828b66c315844331404d89e93592515d56837cb9b2b97cf3" +dependencies = [ + "anyhow", + "cranelift-codegen", + "cranelift-control", + "cranelift-entity", + "cranelift-module", + "cranelift-native", + "libc", + "log", + "region", + "target-lexicon", + "wasmtime-internal-jit-icache-coherence", + "windows-sys 0.61.2", +] + +[[package]] +name = "cranelift-module" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a58a82190e7ecd36b8d55880eadff59f045d20a491dacfaecc11aac24453092" +dependencies = [ + "anyhow", + "cranelift-codegen", + "cranelift-control", +] + +[[package]] +name = "cranelift-native" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bdec40b396eb630ecfb0e7a81766d7287f464a7631b9eb5862f7711f1020012" +dependencies = [ + "cranelift-codegen", + "libc", + "target-lexicon", +] + +[[package]] +name = "cranelift-srcgen" +version = "0.129.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a1a001a9dc4557d9e2be324bc932621c0aa9bf33b74dfefa2338f0bf8913329" + +[[package]] +name = "crossbeam-channel" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + [[package]] name = "data-encoding" version = "2.11.1" @@ -324,6 +526,18 @@ dependencies = [ "serde", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "foldhash" version = "0.2.0" @@ -430,33 +644,45 @@ dependencies = [ "r-efi 6.0.0", ] +[[package]] +name = "gimli" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf7f043f89559805f8c7cacc432749b2fa0d0a0a9ee46ce47164ed5ba7f126c" +dependencies = [ + "fnv", + "hashbrown 0.16.1", + "indexmap", + "stable_deref_trait", +] + [[package]] name = "hashbrown" -version = "0.14.5" +version = "0.15.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" dependencies = [ - "ahash", + "foldhash 0.1.5", ] [[package]] name = "hashbrown" -version = "0.17.1" +version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" dependencies = [ - "allocator-api2", - "equivalent", - "foldhash", + "foldhash 0.2.0", ] [[package]] -name = "hashlink" -version = "0.9.1" +name = "hashbrown" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" dependencies = [ - "hashbrown 0.14.5", + "allocator-api2", + "equivalent", + "foldhash 0.2.0", ] [[package]] @@ -540,6 +766,22 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-rustls" +version = "0.27.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfa8e654703247911e29c23fbeaa261834bd9bb74efba2f9acddc37bfb127f53" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + [[package]] name = "hyper-util" version = "0.1.20" @@ -754,11 +996,17 @@ version = "0.2.189" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + [[package]] name = "libsqlite3-sys" -version = "0.30.1" +version = "0.38.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" +checksum = "f1d20bef17f513b9b3004532233187769cd072d790971f4e4da0e346eb6401e8" dependencies = [ "cc", "pkg-config", @@ -786,6 +1034,15 @@ version = "0.4.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad" +[[package]] +name = "mach2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d640282b302c0bb0a2a8e0233ead9035e3bed871f0b7e81fe4a1ec829765db44" +dependencies = [ + "libc", +] + [[package]] name = "matchit" version = "0.8.4" @@ -974,7 +1231,7 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pd-host-function" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript.git?rev=b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390#b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390" +source = "git+https://github.com/rustscript-lang/rustscript.git?rev=2f433edab3f1907e8cb060e901357d7e2af016ce#2f433edab3f1907e8cb060e901357d7e2af016ce" dependencies = [ "pd-host-schema", "proc-macro2", @@ -985,7 +1242,7 @@ dependencies = [ [[package]] name = "pd-host-schema" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript.git?rev=b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390#b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390" +source = "git+https://github.com/rustscript-lang/rustscript.git?rev=2f433edab3f1907e8cb060e901357d7e2af016ce#2f433edab3f1907e8cb060e901357d7e2af016ce" dependencies = [ "proc-macro2", "syn 2.0.119", @@ -994,13 +1251,18 @@ dependencies = [ [[package]] name = "pd-vm" version = "0.1.0" -source = "git+https://github.com/rustscript-lang/rustscript.git?rev=b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390#b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390" +source = "git+https://github.com/rustscript-lang/rustscript.git?rev=2f433edab3f1907e8cb060e901357d7e2af016ce#2f433edab3f1907e8cb060e901357d7e2af016ce" dependencies = [ "base64 0.22.1", + "cranelift-codegen", + "cranelift-frontend", + "cranelift-jit", + "cranelift-module", + "cranelift-native", "futures-channel", - "futures-util", "http-body-util", "hyper", + "hyper-rustls", "hyper-util", "libc", "paste", @@ -1009,15 +1271,14 @@ dependencies = [ "regex", "rt-format", "rusqlite", - "rustls", "self_cell", "serde", "serde_json", "syn 2.0.119", "tokio", - "tokio-rustls", + "tokio-rusqlite", + "tower-service", "url", - "webpki-roots", "windows-sys 0.59.0", ] @@ -1114,7 +1375,7 @@ version = "0.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" dependencies = [ - "bitflags", + "bitflags 2.13.1", ] [[package]] @@ -1154,6 +1415,20 @@ dependencies = [ "serde_json", ] +[[package]] +name = "regalloc2" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08effbc1fa53aaebff69521a5c05640523fab037b34a4a2c109506bc938246fa" +dependencies = [ + "allocator-api2", + "bumpalo", + "hashbrown 0.15.5", + "log", + "rustc-hash", + "smallvec", +] + [[package]] name = "regex" version = "1.13.1" @@ -1183,6 +1458,18 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +[[package]] +name = "region" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6b6ebd13bc009aef9cd476c1310d49ac354d36e240cf1bd753290f3dc7199a7" +dependencies = [ + "bitflags 1.3.2", + "libc", + "mach2", + "windows-sys 0.52.0", +] + [[package]] name = "ring" version = "0.17.14" @@ -1197,6 +1484,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rsqlite-vfs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c" +dependencies = [ + "hashbrown 0.16.1", + "thiserror", +] + [[package]] name = "rt-format" version = "0.3.1" @@ -1209,18 +1506,25 @@ dependencies = [ [[package]] name = "rusqlite" -version = "0.32.1" +version = "0.40.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7753b721174eb8ff87a9a0e799e2d7bc3749323e773db92e0984debb00019d6e" +checksum = "23f2a97da3e3873c73cb2a2e71b35c40ff95e0b1eefa8d72d8499a6928c3b5b3" dependencies = [ - "bitflags", + "bitflags 2.13.1", "fallible-iterator", "fallible-streaming-iterator", - "hashlink 0.9.1", + "hashlink", "libsqlite3-sys", "smallvec", + "sqlite-wasm-rs", ] +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + [[package]] name = "rusticata-macros" version = "4.1.0" @@ -1322,6 +1626,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ab42ca02749e120097e328d91d415325bdf43b1c72c4c8badf37375fe40a813" +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + [[package]] name = "serde" version = "1.0.229" @@ -1401,12 +1711,6 @@ dependencies = [ "unsafe-libyaml", ] -[[package]] -name = "semver" -version = "1.0.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" - [[package]] name = "shlex" version = "2.0.1" @@ -1445,6 +1749,18 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "sqlite-wasm-rs" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75" +dependencies = [ + "cc", + "js-sys", + "rsqlite-vfs", + "wasm-bindgen", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -1517,6 +1833,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "target-lexicon" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c447be3a2abfa84b2d91680c56600bd6b2873866de27b36ec911be705e291da" + [[package]] name = "thiserror" version = "2.0.21" @@ -1605,6 +1927,17 @@ dependencies = [ "syn 3.0.3", ] +[[package]] +name = "tokio-rusqlite" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bce5670317c76f8505e7e5a8d655330a435fc35d3deaf756de8406db64a94e8c" +dependencies = [ + "crossbeam-channel", + "rusqlite", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.26.4" @@ -1838,6 +2171,27 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "wasmtime-internal-core" +version = "42.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be7c68311d6220c20cefdf334e0c8021e16a050383c67edc5be42e5661ddf265" +dependencies = [ + "libm", +] + +[[package]] +name = "wasmtime-internal-jit-icache-coherence" +version = "42.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52c0779e711777b915d017b3f54049e658057a77df99e0e7958406b3c5d7d07" +dependencies = [ + "cfg-if", + "libc", + "wasmtime-internal-core", + "windows-sys 0.61.2", +] + [[package]] name = "webpki-roots" version = "1.0.9" @@ -1981,7 +2335,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c6edb26322e610d4f04b7cd34478317685d24d0999437e551fb97c5441151041" dependencies = [ "arraydeque", - "hashlink 0.12.1", + "hashlink", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index a0c25e6..b56e62a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,8 @@ hyper = { version = "1", features = ["client", "http1"] } hyper-util = { version = "0.1", features = ["client-legacy", "http1", "tokio"] } parking_lot = "0.12" rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] } -rustscript-vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript.git", rev = "b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390", default-features = false, features = ["runtime", "http-client", "sqlite"] } -pd-host-function = { git = "https://github.com/rustscript-lang/rustscript.git", rev = "b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390" } +rustscript-vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript.git", rev = "2f433edab3f1907e8cb060e901357d7e2af016ce", default-features = false, features = ["runtime", "http-client", "sqlite", "cranelift-jit"] } +pd-host-function = { git = "https://github.com/rustscript-lang/rustscript.git", rev = "2f433edab3f1907e8cb060e901357d7e2af016ce" } serde = { version = "1", features = ["derive"] } serde_json = "1" serde_yaml = "0.9" @@ -60,31 +60,16 @@ workspace-io-fault = [] opt-level = 3 [[test]] -name = "auth_store_tests" -path = "tests/auth_store_tests.rs" +name = "auth_config_fixture_tests" +path = "tests/auth_config_fixture_tests.rs" required-features = ["config-fixture"] [[test]] -name = "auth_store_rss_tests" -path = "tests/auth_store_rss_tests.rs" +name = "provider_loop_tests" +path = "tests/provider_loop_tests.rs" required-features = ["config-fixture"] [[test]] -name = "config_auth_rss_tests" -path = "tests/config_auth_rss_tests.rs" -required-features = ["config-fixture"] - -[[test]] -name = "oauth_flow_tests" -path = "tests/oauth_flow_tests.rs" -required-features = ["config-fixture"] - -[[test]] -name = "oauth_rss_entry_tests" -path = "tests/oauth_rss_entry_tests.rs" -required-features = ["config-fixture"] - -[[test]] -name = "provider_auth_rss_entry_tests" -path = "tests/provider_auth_rss_entry_tests.rs" +name = "provider_config_fixture_tests" +path = "tests/provider_config_fixture_tests.rs" required-features = ["config-fixture"] diff --git a/docs/configuration.md b/docs/configuration.md index ac60ea4..8265ed5 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -8,7 +8,7 @@ passes through to the `pd-vm` core. Reserved-but-unimplemented configuration is called out explicitly in [Reserved configuration](#reserved-configuration) and must not be treated as real. -A drift guard (`tests/docs_consistency_tests.rs`) compares this document +A drift guard (`tests/cases/docs_consistency_tests.rs`) compares this document against `src/` in both directions: a variable that the binaries read but this document misses, or a variable this document advertises that no binary reads, fails the test suite. @@ -32,7 +32,7 @@ Persistent `config.yaml` / `auth.yaml` reach RSS through a host-native module (`auth_host_module`, registered on `agent_host_catalog` and exercised by the provider path) and the Stage A fixture host register that bridge. The fixture host additionally exposes `config::check_policy` and binds `HostHome` -before RSS runs, so it is the surface used by `tests/config_auth_rss_tests.rs`; +before RSS runs, so it is the surface used by `tests/cases/config_auth_rss_tests.rs`; in both cases RSS cannot supply or override the filesystem path. The generic loader keeps bounded YAML, HTTPS-or-loopback URL syntax, secret-key rejection, and credential-ID reference integrity. Provider-name authority mapping, diff --git a/docs/deployment.md b/docs/deployment.md index 9cbfc65..b685575 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -8,7 +8,7 @@ treated as available. ## 1. Delivery constraint: the core dependency is not released `rustscript-agent` depends on the RustScript VM through a **pinned Git -dependency**: `rustscript-vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript.git", rev = "b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390", ... }` in `Cargo.toml`. +dependency**: `rustscript-vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript.git", rev = "2f433edab3f1907e8cb060e901357d7e2af016ce", ... }` in `Cargo.toml`. Consequences, stated plainly: - A build fetches the exact pinned core revision from the canonical HTTPS @@ -18,7 +18,7 @@ Consequences, stated plainly: - `cargo build/test --locked` resolves the core source from `Cargo.lock`, which records the canonical git source for both `pd-vm` and `pd-host-function`: - `git+https://github.com/rustscript-lang/rustscript.git?rev=b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390#b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390`. + `git+https://github.com/rustscript-lang/rustscript.git?rev=2f433edab3f1907e8cb060e901357d7e2af016ce#2f433edab3f1907e8cb060e901357d7e2af016ce`. A build therefore needs network access to github.com (or a pre-warmed Cargo git cache); it never reads the state of a local core checkout. - **There is no crates.io release of this crate, and none can be made until @@ -46,7 +46,7 @@ before deploying: ```bash # The lockfile records the exact git source for pd-vm and pd-host-function: grep -A3 'name = "pd-vm"' Cargo.lock -# source = "git+https://github.com/rustscript-lang/rustscript.git?rev=b5066dd…#b5066dd…" +# source = "git+https://github.com/rustscript-lang/rustscript.git?rev=fc8b0c8…#fc8b0c8…" # The resolved dependency graph shows the same source: cargo tree --locked -p rustscript-vm ``` diff --git a/rss/agent/guardian.rss b/rss/agent/guardian.rss index 0e3d829..0551762 100644 --- a/rss/agent/guardian.rss +++ b/rss/agent/guardian.rss @@ -1,5 +1,62 @@ use agent; use json; +use super::agent::provider_body; + +// The assessment is a fixed shape: the review policy below only ever reads +// these four keys, so the model's reply is decoded into them instead of a +// dynamic object. +struct GuardianAssessment { + outcome: Option, + risk_level: Option, + user_authorization: Option, + rationale: Option +} + +fn present_text(value: Option) -> bool { + match value { + Some(inner) => true, + None => false, + } +} + +fn unwrap_text(value: Option) -> string { + match value { + Some(inner) => inner, + None => "", + } +} + +/// Only keys the model actually sent are carried, so the fixed policy below +/// keeps testing presence exactly as it did against a dynamic object. +fn assessment_fields(assessment: GuardianAssessment) -> map { + let mut fields: map = {}; + if present_text(assessment.outcome) { + fields["outcome"] = unwrap_text(assessment.outcome); + } + if present_text(assessment.risk_level) { + fields["risk_level"] = unwrap_text(assessment.risk_level); + } + if present_text(assessment.user_authorization) { + fields["user_authorization"] = unwrap_text(assessment.user_authorization); + } + if present_text(assessment.rationale) { + fields["rationale"] = unwrap_text(assessment.rationale); + } + fields +} + + +/// Canonical JSON text for a guest value. The host owns serialization: this pin +/// rejects the typed `json::encode::` form, so an open payload only ever +/// leaves the guest as text. +fn json_text_of(value: map) -> string { + let encoded = agent::json_text(value); + let mut text: string = ""; + if encoded.ok { + text = encoded.text; + } + text +} fn map_string(value: map, key: string, fallback: string) -> string { let mut result: string = fallback; @@ -77,9 +134,15 @@ fn assessment_payload(text: string) -> map { } index += 1; } - let mut parsed: map = { ok: false, value: {} }; + let mut parsed: map = { ok: false, code: "malformed_payload", message: "assessment is not a JSON object", value: {} }; if first >= 0 && last > first { - parsed = agent::parse_json_object(text[first:(last + 1)]); + let candidate = agent::parse_json_object(text[first:(last + 1)]); + if candidate.ok { + let assessment: GuardianAssessment = json::decode::(candidate.value); + parsed = { ok: true, code: "", message: "", value: assessment_fields(assessment) }; + } else { + parsed = { ok: false, code: candidate.code, message: candidate.message, value: {} }; + } } parsed } @@ -309,8 +372,57 @@ fn preview_has_sensitive_marker(text: string) -> bool { found } +fn present_bool(value: Option) -> bool { + match value { + Some(inner) => true, + None => false, + } +} + +fn bool_value(value: Option, fallback: bool) -> bool { + match value { + Some(inner) => inner, + None => fallback, + } +} + +fn present_int(value: Option) -> bool { + match value { + Some(inner) => true, + None => false, + } +} + +fn int_value(value: Option, fallback: int) -> int { + match value { + Some(inner) => inner, + None => fallback, + } +} + +fn present_argv(value: Option>) -> bool { + match value { + Some(inner) => true, + None => false, + } +} + +fn unwrap_argv(value: Option>) -> array { + match value { + Some(inner) => inner, + None => [], + } +} + /// Build a bounded, owner-facing description from the validated exact action. /// Never include file/process content or output; unsupported/unreviewable forms fail closed. +/// +/// The arguments come from the provider's own JSON text (`arguments_json`), +/// which the host parses into the closed `AgentPreviewArguments` projection. +/// That is what keeps a text-only tool call previewable: dispatch no longer +/// fills an `arguments` map, so reading a map here would fail every write and +/// execute approval closed with `approval_preview_unavailable`. A parse refusal +/// carries its own code instead of being flattened into "no arguments". fn starts_with(text: string, prefix: string) -> bool { prefix.length <= text.length && text[0:(prefix.length)] == prefix } @@ -584,37 +696,27 @@ fn terminal_shell_eval_opaque(argv: array) -> bool { fn normalize_absolute_path(raw: string) -> map { let mut result: map = { ok: false, code: "not_absolute", path: "" }; if starts_with(raw, "/") { - let mut components: array = []; + let segments: array = string_split_literal(raw, "/"); + let mut components: array = []; let mut count: int = 0; - let mut current: string = ""; let mut index: int = 0; let mut escaping: bool = false; - while index <= raw.length { - let at_end: bool = index == raw.length; - let mut separator: bool = false; - if at_end == false { - if raw[index:(index + 1)] == "/" { - separator = true; + let length: int = segments.length; + while index < length { + let current: string = segments[index].copy(); + if current == ".." { + if count == 0 { + escaping = true; + } else { + count = count - 1; } - } - if at_end || separator { - if current == ".." { - if count == 0 { - escaping = true; - } else { - count = count - 1; - } - } else if current != "" && current != "." { - if count < components.length { - components[count] = current; - } else { - components[components.length] = current; - } - count = count + 1; + } else if current != "" && current != "." { + if count < components.length { + components[count] = current; + } else { + components[components.length] = current; } - current = ""; - } else { - current = current + raw[index:(index + 1)]; + count = count + 1; } index += 1; } @@ -1121,7 +1223,18 @@ fn sandbox_facts(context: map, action: map) -> map { let root: map = normalize_absolute_path(raw_root); if map_bool(root, "ok", false) { let root_path: string = map_string(root, "path", ""); - let arguments: map = map_map(action, "arguments"); + let mut arguments: map = map_map(action, "arguments"); + let arguments_text: string = map_string(action, "arguments_json", ""); + if arguments_text != "" { + let projected: AgentPreviewArguments = agent::preview_arguments(arguments_text); + arguments = {}; + if projected.ok { + if present_text(projected.path.copy()) { arguments.path = unwrap_text(projected.path.copy()); } + if present_argv(projected.argv.copy()) { arguments.argv = unwrap_argv(projected.argv.copy()); } + if present_text(projected.cwd.copy()) { arguments.cwd = unwrap_text(projected.cwd.copy()); } + if present_text(projected.action.copy()) { arguments.action = unwrap_text(projected.action.copy()); } + } + } let tool_name: string = map_string(action, "tool_name", ""); let mut target_kind: string = "other"; let mut resolved: map = { ok: false, code: "target_outside_workspace", path: "", boundary: "outside" }; @@ -1250,55 +1363,50 @@ fn sandbox_facts_json(facts: map) -> string { opaque: map_bool(facts, "opaque", false), credentials: map_bool(facts, "credentials", false) }; - json::encode(details) + json_text_of(details) } pub fn action_preview(action: map) -> map { let tool_name: string = map_string(action, "tool_name", ""); - let arguments: map = map_map(action, "arguments"); + let arguments: AgentPreviewArguments = agent::preview_arguments(map_string(action, "arguments_json", "")); let mut preview: string = ""; - let mut owner_text: string = ""; let mut valid: bool = false; + let mut owner_text: string = ""; let mut preview_reason: string = "unavailable"; - if tool_name == "write_file" && arguments.has("path") && type(arguments["path"].copy()) == "string" && arguments.has("content") && type(arguments["content"].copy()) == "string" { - let path: string = arguments["path"].copy(); - let content: string = arguments["content"].copy(); - if visible_text(path) && agent::utf8_byte_len(path) <= 512 && !preview_has_sensitive_marker(path) { - let details: map = { operation: "replace file", path: path, content_bytes: agent::utf8_byte_len(content), file_content: "withheld" }; - preview = "write_file " + json::encode(details); - valid = true; - } - } else if tool_name == "patch" && arguments.has("path") && type(arguments["path"].copy()) == "string" && arguments.has("old_string") && type(arguments["old_string"].copy()) == "string" && arguments.has("new_string") && type(arguments["new_string"].copy()) == "string" { - let path: string = arguments["path"].copy(); - let old_text: string = arguments["old_string"].copy(); - let new_text: string = arguments["new_string"].copy(); - let mut replace_all: bool = false; - let mut option_valid: bool = true; - if arguments.has("replace_all") { - if type(arguments["replace_all"].copy()) == "bool" { - replace_all = arguments["replace_all"].copy(); - } else { - option_valid = false; + let mut refused: string = ""; + if arguments.ok == false { + // Explicit refusal: malformed text, a non-object body, or a known field + // with the wrong type. Never a silent empty projection. + refused = arguments.code.copy(); + } else { + if tool_name == "write_file" && present_text(arguments.path.copy()) && present_text(arguments.content.copy()) { + let path: string = unwrap_text(arguments.path.copy()); + let content: string = unwrap_text(arguments.content.copy()); + if visible_text(path) && agent::utf8_byte_len(path) <= 512 && !preview_has_sensitive_marker(path) { + let details: map = { operation: "replace file", path: path, content_bytes: agent::utf8_byte_len(content), file_content: "withheld" }; + preview = "write_file " + json_text_of(details); + valid = true; } - } - if option_valid && visible_text(path) && agent::utf8_byte_len(path) <= 512 && !preview_has_sensitive_marker(path) { - let details: map = { operation: "replace text", path: path, old_bytes: agent::utf8_byte_len(old_text), new_bytes: agent::utf8_byte_len(new_text), replace_all: replace_all, patch_content: "withheld" }; - preview = "patch " + json::encode(details); - valid = true; - } - } else if tool_name == "terminal" && arguments.has("argv") && type(arguments["argv"].copy()) == "array" { - let argv: array = arguments["argv"].copy(); - let mut safe_argv: array = []; - let mut owner_argv: array = []; - let mut program: string = ""; - let mut argv_bytes: int = 0; - let mut argv_valid: bool = argv.length > 0 && argv.length <= 16; - let mut index: int = 0; - while argv_valid && index < argv.length { - if type(argv[index].copy()) != "string" { - argv_valid = false; - } else { + } else if tool_name == "patch" && present_text(arguments.path.copy()) && present_text(arguments.old_string.copy()) && present_text(arguments.new_string.copy()) { + let path: string = unwrap_text(arguments.path.copy()); + let old_text: string = unwrap_text(arguments.old_string.copy()); + let new_text: string = unwrap_text(arguments.new_string.copy()); + let replace_all: bool = bool_value(arguments.replace_all.copy(), false); + if visible_text(path) && agent::utf8_byte_len(path) <= 512 && !preview_has_sensitive_marker(path) { + let details: map = { operation: "replace text", path: path, old_bytes: agent::utf8_byte_len(old_text), new_bytes: agent::utf8_byte_len(new_text), replace_all: replace_all, patch_content: "withheld" }; + preview = "patch " + json_text_of(details); + valid = true; + } + } else if tool_name == "terminal" && present_argv(arguments.argv.copy()) { + let argv: array = unwrap_argv(arguments.argv.copy()); + let mut safe_argv: array = []; + let mut owner_argv: array = []; + let mut program: string = ""; + let mut argv_bytes: int = 0; + let mut argv_valid: bool = argv.length > 0 && argv.length <= 16; + let mut index: int = 0; + while argv_valid && index < argv.length { let argument: string = argv[index].copy(); let argument_bytes: int = agent::utf8_byte_len(argument); argv_bytes += argument_bytes; @@ -1314,136 +1422,86 @@ pub fn action_preview(action: map) -> map { } } else if argument == "-c" || argument == "--command" || argument == "-e" || argument == "--eval" { argv_valid = false; - preview_reason = "shell_eval"; - } else if preview_has_sensitive_marker(argument) { - safe_argument = ""; - owner_argv[owner_argv.length] = ""; } else { safe_argument = ""; - owner_argv[owner_argv.length] = argument; - } - if index == 0 { - owner_argv[owner_argv.length] = argument; + if preview_has_sensitive_marker(argument) { + owner_argv[owner_argv.length] = ""; + } else { + owner_argv[owner_argv.length] = argument; + } } + if index == 0 { owner_argv[owner_argv.length] = argument; } safe_argv[safe_argv.length] = safe_argument; } + index += 1; } - index += 1; - } - if argv_valid && terminal_shell_eval_opaque(argv) { - argv_valid = false; - preview_reason = "shell_eval"; - } - let mut stdin_bytes: int = 0; - let mut terminal_valid: bool = true; - if arguments.has("stdin") { - if type(arguments["stdin"].copy()) == "string" { - let stdin: string = arguments["stdin"].copy(); - stdin_bytes = agent::utf8_byte_len(stdin); + if argv_valid && terminal_shell_eval_opaque(argv) { + argv_valid = false; + preview_reason = "shell_eval"; + } + let mut stdin_bytes: int = 0; + if present_text(arguments.stdin.copy()) { + stdin_bytes = agent::utf8_byte_len(unwrap_text(arguments.stdin.copy())); if stdin_bytes > 0 { - terminal_valid = false; + argv_valid = false; } - } else { - terminal_valid = false; } - } - let mut cwd: string = ""; - if arguments.has("cwd") { - if type(arguments["cwd"].copy()) == "string" { - cwd = arguments["cwd"].copy(); + let mut cwd: string = ""; + if present_text(arguments.cwd.copy()) { + cwd = unwrap_text(arguments.cwd.copy()); if agent::utf8_byte_len(cwd) > 512 || preview_has_sensitive_marker(cwd) { - terminal_valid = false; + argv_valid = false; } - } else { - terminal_valid = false; - } - } - let mut timeout_ms: int = 0; - if arguments.has("timeout_ms") { - if type(arguments["timeout_ms"].copy()) == "int" { - timeout_ms = arguments["timeout_ms"].copy(); - } else { - terminal_valid = false; } - } - let mut max_output_bytes: int = 0; - if arguments.has("max_output_bytes") { - if type(arguments["max_output_bytes"].copy()) == "int" { - max_output_bytes = arguments["max_output_bytes"].copy(); - } else { - terminal_valid = false; + let timeout_ms: int = int_value(arguments.timeout_ms.copy(), 0); + let max_output_bytes: int = int_value(arguments.max_output_bytes.copy(), 0); + let background: bool = bool_value(arguments.background.copy(), false); + if argv_valid && program != "" { + let details: map = { program: program, argv: safe_argv, cwd: cwd, argv_bytes: argv_bytes, stdin_bytes: stdin_bytes, timeout_ms: timeout_ms, max_output_bytes: max_output_bytes, background: background, output: "not included" }; + preview = "terminal " + json_text_of(details); + let owner_details: map = { program: program, argv: owner_argv, cwd: cwd, argv_bytes: argv_bytes, stdin_bytes: stdin_bytes, timeout_ms: timeout_ms, max_output_bytes: max_output_bytes, background: background, output: "not included" }; + owner_text = "terminal " + json_text_of(owner_details); + valid = true; } - } - let mut background: bool = false; - if arguments.has("background") { - if type(arguments["background"].copy()) == "bool" { - background = arguments["background"].copy(); - } else { - terminal_valid = false; + } else if tool_name == "process" && present_text(arguments.action.copy()) && present_text(arguments.process_id.copy()) { + let action_name: string = unwrap_text(arguments.action.copy()); + let process_id: string = unwrap_text(arguments.process_id.copy()); + let mut data_bytes: int = 0; + let mut process_valid: bool = action_name == "poll" || action_name == "wait" || action_name == "log" || action_name == "write" || action_name == "close" || action_name == "kill"; + if present_text(arguments.data.copy()) { + data_bytes = agent::utf8_byte_len(unwrap_text(arguments.data.copy())); } - } - if argv_valid && terminal_valid && program != "" { - let details: map = { program: program, argv: safe_argv, cwd: cwd, argv_bytes: argv_bytes, stdin_bytes: stdin_bytes, timeout_ms: timeout_ms, max_output_bytes: max_output_bytes, background: background, output: "not included" }; - preview = "terminal " + json::encode(details); - let owner_details: map = { program: program, argv: owner_argv, cwd: cwd, argv_bytes: argv_bytes, stdin_bytes: stdin_bytes, timeout_ms: timeout_ms, max_output_bytes: max_output_bytes, background: background, output: "not included" }; - owner_text = "terminal " + json::encode(owner_details); - valid = true; - } - } else if tool_name == "process" && arguments.has("action") && type(arguments["action"].copy()) == "string" && arguments.has("process_id") && type(arguments["process_id"].copy()) == "string" { - let action_name: string = arguments["action"].copy(); - let process_id: string = arguments["process_id"].copy(); - let mut data_bytes: int = 0; - let mut process_valid: bool = action_name == "poll" || action_name == "wait" || action_name == "log" || action_name == "write" || action_name == "close" || action_name == "kill"; - if arguments.has("data") { - if type(arguments["data"].copy()) == "string" { - let data: string = arguments["data"].copy(); - data_bytes = agent::utf8_byte_len(data); - } else { + if process_id == "" || agent::utf8_byte_len(process_id) > 128 || preview_has_sensitive_marker(process_id) { process_valid = false; } - } - if process_id == "" || agent::utf8_byte_len(process_id) > 128 || preview_has_sensitive_marker(process_id) { - process_valid = false; - } - let mut options: map = {}; - if arguments.has("timeout_ms") { - if type(arguments["timeout_ms"].copy()) == "int" { - options.timeout_ms = arguments["timeout_ms"].copy(); - } else { - process_valid = false; + let mut options: map = {}; + if present_int(arguments.timeout_ms.copy()) { + options.timeout_ms = int_value(arguments.timeout_ms.copy(), 0); } - } - if arguments.has("offset") { - if type(arguments["offset"].copy()) == "int" { - options.offset = arguments["offset"].copy(); - } else { - process_valid = false; + if present_int(arguments.offset.copy()) { + options.offset = int_value(arguments.offset.copy(), 0); } - } - if arguments.has("limit") { - if type(arguments["limit"].copy()) == "int" { - options.limit = arguments["limit"].copy(); - } else { - process_valid = false; + if present_int(arguments.limit.copy()) { + options.limit = int_value(arguments.limit.copy(), 0); + } + if process_valid { + let details: map = { operation: action_name, process_id: process_id, data_bytes: data_bytes, options: options, process_data: "withheld", process_output: "not included" }; + preview = "process " + json_text_of(details); + valid = true; } } - if process_valid { - let details: map = { operation: action_name, process_id: process_id, data_bytes: data_bytes, options: options, process_data: "withheld", process_output: "not included" }; - preview = "process " + json::encode(details); - valid = true; + if valid && agent::utf8_byte_len(preview) > 2048 { + valid = false; + preview = ""; + owner_text = ""; } } - if valid && agent::utf8_byte_len(preview) > 2048 { - valid = false; - preview = ""; - owner_text = ""; - } - if valid && agent::utf8_byte_len(owner_text) > 2048 { - owner_text = ""; - } - { ok: valid, text: preview, reason: preview_reason, owner_text: owner_text } + if valid && agent::utf8_byte_len(owner_text) > 2048 { owner_text = ""; } + { ok: valid, text: preview, code: refused, reason: preview_reason, owner_text: owner_text } } + + fn reviewer_request(context: map, action: map, facts: map, user_input: string, system_prompt: string, coding_prompt: string) -> map { let provider_options: map = map_map(context, "provider_options"); let guardian: map = map_map(provider_options, "guardian"); @@ -1540,25 +1598,25 @@ pub fn review(context: map, action: map) -> map { } } while attempt <= 3 && done == false { - let before: map = agent::control_check(); - if map_bool(before, "ok", false) == false { - let error: map = map_map(before, "error"); + let before = agent::control_check(); + if before.ok == false { + let error: map = match before.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } }; result = empty_result(false, map_string(error, "code", "cancelled"), "Guardian review was interrupted; no action was executed"); done = true; } else { let mut request: map = reviewer_request(context, action, facts, user_input, system_prompt, coding_prompt); let step_key: string = map_string(action, "_provider_step_key", ""); if step_key != "" { - request["_provider_step_key"] = step_key + ":" + json::encode(attempt); + request["_provider_step_key"] = step_key + ":" + json::encode::(attempt); } - let envelope: map = agent::provider_call(request); - let after: map = agent::control_check(); - if map_bool(after, "ok", false) == false { - let error: map = map_map(after, "error"); + let envelope = agent::provider_call(json_text_of(request)); + let after = agent::control_check(); + if after.ok == false { + let error: map = match after.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } }; result = empty_result(false, map_string(error, "code", "cancelled"), "Guardian review was interrupted; no action was executed"); done = true; - } else if map_bool(envelope, "ok", false) == false { - let error: map = map_map(envelope, "error"); + } else if envelope.ok == false { + let error: map = provider_body::failure(envelope.error); let retry: map = fail_or_retry(is_retryable(error), attempt, "guardian_provider_failed"); if map_bool(retry, "retry", false) && attempt < 3 { attempt += 1; @@ -1567,7 +1625,7 @@ pub fn review(context: map, action: map) -> map { done = true; } } else { - let response: map = map_map(envelope, "response"); + let response: map = provider_body::response(envelope.response); let calls: array = map_array(response, "tool_calls"); let text: string = map_string(response, "text", ""); let parsed: map = assessment_payload(text); diff --git a/rss/agent/main.rss b/rss/agent/main.rss index f746928..0006f23 100644 --- a/rss/agent/main.rss +++ b/rss/agent/main.rss @@ -13,6 +13,8 @@ use agent_runtime; use json; use super::tools::dispatch as tools; use super::agent::guardian as guardian; +use super::agent::provider_body; +use super::llm::tool_descriptor; // --------------------------------------------------------------------------- // Typed context accessors (same-module; defensive dynamic navigation) @@ -264,9 +266,40 @@ fn text_part(text: string) -> map { { type: "text", text: text } } -fn encode_arguments_json(arguments: map) -> string { - let encoded: string = json::encode(arguments); - encoded +/// Canonical JSON text for a guest value. +/// +/// The host owns serialization (`agent::json_text`): this pin rejects the typed +/// `json::encode::` form, and an open payload may only leave the guest as +/// text. Failure is reported as an empty string, never as `{}`. +fn json_text_of(value: map) -> string { + let encoded = agent::json_text(value); + let mut text: string = ""; + if encoded.ok { + text = encoded.text; + } + text +} + +/// The provider's own `arguments_json` text, verbatim. +/// +/// A call that only carries an `arguments` map is serialized by the host. A +/// call with neither is left empty so the tool boundary fails closed instead of +/// silently sending `{}` in place of the provider's arguments. +fn call_arguments_json(call: map) -> string { + let mut text: string = ""; + if call.has("arguments_json") { + if type(call.arguments_json.copy()) == "string" { + text = ctx_string(call, "arguments_json", ""); + } + } + if text == "" { + if call.has("arguments") { + if type(call.arguments.copy()) == "map" { + text = json_text_of(ctx_map(call, "arguments")); + } + } + } + text } fn tool_call_part(call: map) -> map { @@ -274,7 +307,7 @@ fn tool_call_part(call: map) -> map { type: "tool_call", tool_call_id: ctx_string(call, "id", ""), name: ctx_string(call, "name", ""), - arguments_json: encode_arguments_json(ctx_map(call, "arguments")) + arguments_json: call_arguments_json(call) } } @@ -338,28 +371,12 @@ fn seed_messages(context: map, messages: array) -> array { seeded } -// Native `RunContext.tool_schemas` entries are validated registry -// `ToolDescriptor` maps (`{name, description, toolset, risk_class, schema}`). -// The canonical LlmRequest contract still carries the schema as a JSON string -// in `schema_json`, which the Chat/Gemini/Anthropic codecs splice into their -// wire bodies; convert only at this boundary so native and canonical shapes -// stay distinct everywhere else. -fn native_tool_to_canonical(descriptor: map) -> map { - let schema: map = descriptor["schema"]; - let schema_json: string = json::encode(schema); - { - name: ctx_string(descriptor, "name", ""), - description: ctx_string(descriptor, "description", ""), - schema_json: schema_json - } -} - fn native_tools_to_canonical(descriptors: array) -> array { let mut tools: array = []; let mut index = 0; while index < descriptors.length { let descriptor: map = array_map(descriptors, index); - tools[tools.length] = native_tool_to_canonical(descriptor); + tools[tools.length] = tool_descriptor::from_native(descriptor); index += 1; } tools @@ -454,8 +471,13 @@ fn persist_approval_request(context: map, metadata: map, action: map) -> map { let owner_preview: string = ctx_string(preview_result, "text", ""); let reviewer: string = ctx_string(approval_settings(context), "reviewer", "guardian"); let mut expected_scope: string = "single_call"; - if reviewer == "auto" { - expected_scope = "guardian_pending"; + if reviewer == "auto" { expected_scope = "guardian_pending"; } + // A refused projection (malformed text, a wrongly typed field) reports its + // own code: "no preview" is not one failure, and the owner-facing approval + // reason must not disguise an unparseable action as an unsupported one. + let mut code: string = "approval_preview_unavailable"; + if ctx_bool(preview_result, "ok", false) == false { + code = ctx_string(preview_result, "code", "approval_preview_unavailable"); } let mut request: map = { id: approval_id, @@ -472,36 +494,40 @@ fn persist_approval_request(context: map, metadata: map, action: map) -> map { }; request.owner_preview = owner_preview; let owner_argv_preview: string = ctx_string(preview_result, "owner_text", ""); - if owner_argv_preview != "" && agent::utf8_byte_len(owner_argv_preview) <= 2048 { - request.owner_argv_preview = owner_argv_preview; - } - let mut stored: map = { - ok: false, - result: { rows: [] }, - error: { code: "approval_preview_unavailable" } - }; + if owner_argv_preview != "" && agent::utf8_byte_len(owner_argv_preview) <= 2048 { request.owner_argv_preview = owner_argv_preview; } + let mut created: int = 0; + let mut stored_ok: bool = false; + let mut stored_rows: int = 0; if ctx_bool(preview_result, "ok", false) && owner_preview != "" && agent::utf8_byte_len(owner_preview) <= 2048 { - stored = agent::approval_storage("request", request); - } - let data: map = ctx_map(stored, "result"); - let rows: array = ctx_array(data, "rows"); - let error: map = ctx_map(stored, "error"); - let code: string = ctx_string(error, "code", "approval_storage_unavailable"); - let created: int = ctx_int(data, "created", 0); - let mut valid: bool = ctx_bool(stored, "ok", false) && rows.length == 1; - if valid { - let row: array = rows[0]; - let mut scope_ok: bool = false; - if row.length >= 12 { - let scope: string = row[8].copy(); - scope_ok = scope == expected_scope; - if reviewer == "auto" && created == 0 && (scope == "guardian_pending" || scope == "human_pending") { - scope_ok = true; + // The durable request is open JSON: it crosses as text and the host + // parses it, keeping the stored row byte-for-byte as before. + let stored = agent::approval_storage("request", json_text_of(request)); + let stored_row_list: array = stored.rows.copy(); + stored_ok = stored.ok; + stored_rows = stored_row_list.length; + created = stored.created; + code = stored.code; + if stored_ok && stored_rows == 1 { + let row: AgentApprovalRow = stored_row_list[0].copy(); + let scope_ok: bool = row.decision_scope.copy() == expected_scope || (reviewer == "auto" && created == 0 && (row.decision_scope.copy() == "guardian_pending" || row.decision_scope.copy() == "human_pending")); + if row.id != approval_id + || row.run_id != run_id + || row.session_id != session_id + || row.tool_call_id != call_id + || row.tool_name != ctx_string(action, "tool_name", "") + || row.arguments_json != "{}" + || row.risk_class != ctx_string(action, "risk_class", "") + || row.state != "pending" + || !scope_ok + || row.one_time != 1 + { + stored_ok = false; } } - if row.length < 12 || row[0] != approval_id || row[1] != run_id || row[2] != session_id || row[3] != call_id || row[4] != ctx_string(action, "tool_name", "") || row[5] != "{}" || row[6] != ctx_string(action, "risk_class", "") || row[7] != "pending" || scope_ok == false || row[9] != 1 { - valid = false; - } + } + let mut valid: bool = stored_ok && stored_rows == 1; + if code == "" { + code = "approval_storage_unavailable"; } { ok: valid, approval_id: approval_id, code: code, reason: ctx_string(preview_result, "reason", ""), created: created } } @@ -509,16 +535,15 @@ fn persist_approval_request(context: map, metadata: map, action: map) -> map { fn resolve_approval(context: map, approval_id: string, state: string, reason: string) -> bool { let run_id: string = ctx_string(context, "run_id", ""); let resolved_at_ms: int = ctx_int(ctx_map(context, "metadata"), "created_at_ms", 0); - let result: map = agent::approval_storage("resolve", { + let result = agent::approval_storage("resolve", json_text_of({ id: approval_id, run_id: run_id, state: state, resolver: "guardian", decision_reason: reason, resolved_at_ms: resolved_at_ms - }); - let data: map = ctx_map(result, "result"); - ctx_bool(result, "ok", false) && ctx_int(data, "rows_affected", 0) == 1 + })); + result.ok && result.rows_affected == 1 } fn wait_for_user_approval(context: map, approval_id: string) -> map { @@ -527,25 +552,26 @@ fn wait_for_user_approval(context: map, approval_id: string) -> map { let mut done: bool = false; let mut result: map = { ok: false, code: "approval_wait_failed", message: "user approval could not be confirmed" }; while done == false { - let control: map = agent::control_check(); + let checked = agent::control_check(); + let control: map = { ok: checked.ok, error: match checked.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } }; if ctx_bool(control, "ok", false) == false { let error: map = ctx_map(control, "error"); result = { ok: false, code: ctx_string(error, "code", "cancelled"), message: "user approval wait was interrupted" }; done = true; } else { - let stored: map = agent::approval_storage("get", { run_id: run_id, approval_id: approval_id }); - let data: map = ctx_map(stored, "result"); - let rows: array = ctx_array(data, "rows"); - if ctx_bool(stored, "ok", false) == false || rows.length != 1 { + let stored = agent::approval_storage("get", json_text_of({ run_id: run_id, approval_id: approval_id })); + let stored_row_list: array = stored.rows.copy(); + let rows: int = stored_row_list.length; + if stored.ok == false || rows != 1 { result = { ok: false, code: "approval_storage_unavailable", message: "pending user approval could not be read" }; done = true; } else { - let row: array = rows[0]; - if row.length < 8 || row[0] != approval_id || row[1] != run_id || row[2] != session_id { + let row: AgentApprovalRow = stored_row_list[0].copy(); + if row.id != approval_id || row.run_id != run_id || row.session_id != session_id { result = { ok: false, code: "approval_owner_mismatch", message: "approval owner changed; no action was executed" }; done = true; } else { - let state: string = row[7].copy(); + let state: string = row.state.copy(); if state == "approved" { result = { ok: true, code: "user_approved", message: "user approved this exact action" }; done = true; @@ -571,17 +597,17 @@ fn wait_for_user_approval(context: map, approval_id: string) -> map { fn approval_handoff(context: map, approval_id: string, tool_call_id: string, tool_name: string) -> map { let run_id: string = ctx_string(context, "run_id", ""); let session_id: string = ctx_string(context, "session_id", ""); - let stored: map = agent::approval_storage("handoff", { + let stored: map = agent::approval_storage("handoff", json_text_of({ id: approval_id, run_id: run_id, session_id: session_id, tool_call_id: tool_call_id, tool_name: tool_name, now_ms: 0 - }); + })); let mut result: map = { ok: false, code: "approval_handoff_rejected", message: "approval handoff failed; no action was executed" }; if ctx_bool(stored, "ok", false) { - let readback: map = agent::approval_storage("get", { run_id: run_id, approval_id: approval_id }); + let readback: map = agent::approval_storage("get", json_text_of({ run_id: run_id, approval_id: approval_id })); let data: map = ctx_map(readback, "result"); let rows: array = ctx_array(data, "rows"); if ctx_bool(readback, "ok", false) && rows.length == 1 { @@ -605,7 +631,7 @@ fn decide_approval(context: map, action: map, approval_id: string, created: int) result = wait_for_user_approval(context, approval_id); } else if reviewer == "auto" { if created != 1 { - let existing: map = agent::approval_storage("get", { run_id: ctx_string(context, "run_id", ""), approval_id: approval_id }); + let existing: map = agent::approval_storage("get", json_text_of({ run_id: ctx_string(context, "run_id", ""), approval_id: approval_id })); let data: map = ctx_map(existing, "result"); let rows: array = ctx_array(data, "rows"); let mut waiting: bool = false; @@ -690,26 +716,59 @@ fn decide_approval(context: map, action: map, approval_id: string, created: int) result } +/// The managed publication as the plain map the durable protocol already +/// understands. The host publishes it as one logical `Option` slot. +fn publication_fields(publication: ManagedPublicationRef) -> map { + let mut carried: map = {}; + carried.format = publication.format.copy(); + carried.effect_id = publication.effect_id.copy(); + carried.workspace_id = publication.workspace_id.copy(); + carried.branch_id = publication.branch_id.copy(); + carried.epoch = publication.epoch; + carried.base_head = publication.base_head.copy(); + carried.base_head_version = publication.base_head_version; + carried.observed_head = publication.observed_head.copy(); + carried.observed_head_version = publication.observed_head_version; + carried.status = publication.status.copy(); + carried +} + +fn publication_of(value: Option) -> map { + match value { + Some(publication) => publication_fields(publication), + None => {} + } +} + fn execute_pinned_dispatch(context: map, dispatch_input: map, facts: map) -> map { let action: map = ctx_map(facts, "action"); let mut call: map = ctx_map(dispatch_input, "call"); if ctx_bool(facts, "ok", false) { call.arguments = ctx_map(action, "arguments"); } - let arguments: map = ctx_map(call, "arguments"); - let encoded_arguments: string = json::encode(arguments); + // The canonical arguments are the provider's own JSON text: the pinned + // invocation compares that text's parsed object with the durable parent, so + // re-encoding an empty map here would break both the digest and the + // admission check. Only a call that carries no text at all is serialized + // from its map by the host, and a call with neither stays empty. + let mut canonical_arguments: string = ""; + if ctx_bool(facts, "ok", false) { + canonical_arguments = ctx_string(action, "arguments_json", ""); + } + if canonical_arguments == "" { + canonical_arguments = call_arguments_json(call); + } // Descriptor/approval resolution stays in this original harness. The // child needs the normalized call and bounded limits, not a second copy // of all six public schemas. let normalized_input: map = { call: call, config: ctx_map(dispatch_input, "config") }; - let canonical_input: string = json::encode(normalized_input); + let canonical_input: string = json_text_of(normalized_input); let tool_metadata: map = ctx_map(facts, "metadata"); - let canonical_arguments: string = encoded_arguments; - let digest: string = encoded_arguments; + let digest: string = canonical_arguments.copy(); let tool_name: string = ctx_string(tool_metadata, "name", ctx_string(call, "name", "")); let call_id: string = ctx_string(tool_metadata, "call_id", ctx_string(call, "id", "")); let metadata: map = ctx_map(context, "metadata"); - let invoked: map = agent_runtime::invoke_pinned_tool({ + let invoked: PinnedToolInvokeResult = agent_runtime::invoke_pinned_tool({ tool_name: tool_name, call_id: call_id, canonical_arguments: canonical_arguments, @@ -721,23 +780,20 @@ fn execute_pinned_dispatch(context: map, dispatch_input: map, facts: map) -> map summary: ctx_string(tool_metadata, "summary", tool_name), untrusted_graph_id: "" }); - let canonical_result: string = ctx_string(invoked, "canonical_result", ""); + let canonical_result: string = invoked.canonical_result.copy(); let mut result: map = tools::approval_failure( dispatch_input, - ctx_string(invoked, "code", "pinned_tool_failed"), - ctx_string(invoked, "message", "pinned tool failed closed; no action was executed") + invoked.code.copy(), + invoked.message.copy() ); if canonical_result.length > 0 { - let decoded: map = json::decode(canonical_result); - if decoded.has("ok") { - result = tools::result_envelope(call, decoded); - if invoked.has("managed_publication") { - let publication: map = ctx_map(invoked,"managed_publication"); - if ctx_string(publication,"format","") == "agent.managed-effect/v1" { - result.managed_publication = publication; - } else { - result = tools::approval_failure(dispatch_input,"managed_outcome_invalid","Native managed publication format is invalid"); - } + result = tools::result_envelope(call, invoked.copy()); + let publication: map = publication_of(invoked.managed_publication); + if publication.length > 0 { + if ctx_string(publication, "format", "") == "agent.managed-effect/v1" { + result.managed_publication = publication; + } else { + result = tools::approval_failure(dispatch_input, "managed_outcome_invalid", "Native managed publication format is invalid"); } } } @@ -764,7 +820,7 @@ fn dispatch_reviewed(context: map, call: map) -> map { result = tools::dispatch(dispatch_input); } else { let tool_metadata: map = ctx_map(facts, "metadata"); - let replay: map = agent_runtime::tool_replay(tool_metadata); + let replay: map = agent_runtime::tool_replay(json_text_of(tool_metadata)); if ctx_bool(replay, "ok", false) == false { result = tools::approval_failure( dispatch_input, @@ -800,7 +856,7 @@ fn dispatch_reviewed(context: map, call: map) -> map { ); } else if mode == "allow" { if ctx_bool(facts, "review_required", false) { - let attested: map = agent_runtime::approval_attest(tool_metadata); + let attested: map = agent_runtime::approval_attest(json_text_of(tool_metadata)); if ctx_bool(attested, "ok", false) { result = execute_pinned_dispatch(context, dispatch_input, facts); } else { @@ -844,7 +900,7 @@ fn dispatch_reviewed(context: map, call: map) -> map { ctx_string(decision, "message", "Approval failed closed; no action was executed") ); } else { - let attested: map = agent_runtime::approval_attest(tool_metadata); + let attested: map = agent_runtime::approval_attest(json_text_of(tool_metadata)); if ctx_bool(attested, "ok", false) == false { result = tools::approval_failure( dispatch_input, @@ -892,14 +948,16 @@ fn dispatch_serial(context: map, calls: array, max_tool_calls: int, tool_calls_u ); stopped = true; } else { - let control: map = agent::control_check(); + let checked = agent::control_check(); + let control: map = { ok: checked.ok, error: match checked.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } }; if ctx_bool(control, "ok", false) == false { failed = control_failed(control, 0, 0, used); stopped = true; } else { let call: map = array_map(calls, i); let dispatched: map = dispatch_reviewed(context, call); - let after: map = agent::control_check(); + let checked_after = agent::control_check(); + let after: map = { ok: checked_after.ok, error: match checked_after.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } }; messages[messages.length] = tool_result_message(ctx_map(dispatched, "content_block")); used += 1; if ctx_bool(dispatched, "ok", false) == false { @@ -965,17 +1023,31 @@ fn run_serial_loop(context: map) -> map { let mut running = true; let mut decision: map = failed_code("internal", "loop did not produce a decision", 0, 0, 0); while running { - let mut refreshed: map = agent::refresh_context({ + // The native refresh owns the durable transcript. Carry its canonical + // array as text until the provider adapter builds the final request. + let refreshed = agent::refresh_context(json_text_of({ run_id: ctx_string(context, "run_id", ""), session_id: ctx_string(context, "session_id", ""), messages: messages - }); - if ctx_bool(refreshed, "storage_available", true) == false { - refreshed = { ok: true, messages: messages }; - } - let mut control: map = agent::control_check(); - if ctx_bool(refreshed, "ok", false) == false { - control = { ok: false, error: ctx_map(refreshed, "error") }; + })); + let checked_turn = agent::control_check(); + let mut control: map = { ok: checked_turn.ok, error: match checked_turn.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } }; + if refreshed.ok == false { + let mut refresh_code: string = refreshed.code.copy(); + if refresh_code == "" { + refresh_code = "context_refresh_failed"; + } + let mut refresh_message: string = refreshed.message.copy(); + if refresh_message == "" { + refresh_message = "context refresh failed"; + } + control = { + ok: false, + error: { + code: refresh_code, + message: refresh_message + } + }; } if ctx_bool(control, "ok", false) == false { decision = control_failed(control, turn, retry_count, tool_calls_used); @@ -984,18 +1056,22 @@ fn run_serial_loop(context: map) -> map { decision = failed_code("max_turns", "max_turns exceeded", turn, retry_count, tool_calls_used); running = false; } else { - messages = ctx_array(refreshed, "messages"); let mut events: array = []; events[events.length] = { type: "model.started", turn: turn, model: model }; let mut request: map = build_llm_request(context, messages); - request["_provider_step_key"] = "business:" + json::encode(turn); - let provider_result: map = agent::provider_call(request); - let after: map = agent::control_check(); + if refreshed.storage_available { + request["messages"] = []; + request["_refreshed_messages_json"] = refreshed.messages_json; + } + request["_provider_step_key"] = "business:" + json::encode::(turn); + let provider_result = agent::provider_call(json_text_of(request)); + let checked_after = agent::control_check(); + let after: map = { ok: checked_after.ok, error: match checked_after.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } }; if ctx_bool(after, "ok", false) == false { decision = control_failed(after, turn, retry_count, tool_calls_used); running = false; - } else if ctx_bool(provider_result, "ok", false) == false { - let error: map = ctx_map(provider_result, "error"); + } else if provider_result.ok == false { + let error: map = provider_body::failure(provider_result.error); if error_is_retryable(error) { if retry_count >= max_retries { decision = run_failed_decision( @@ -1017,7 +1093,8 @@ fn run_serial_loop(context: map) -> map { } else { let delay: int = backoff_delay_ms(base_delay, retry_count, cap_delay); agent::sleep_ms(delay); - let after_sleep: map = agent::control_check(); + let checked_sleep = agent::control_check(); + let after_sleep: map = { ok: checked_sleep.ok, error: match checked_sleep.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } }; if ctx_bool(after_sleep, "ok", false) == false { decision = control_failed(after_sleep, turn, retry_count, tool_calls_used); running = false; @@ -1031,7 +1108,7 @@ fn run_serial_loop(context: map) -> map { running = false; } } else { - let response: map = ctx_map(provider_result, "response"); + let response: map = provider_body::response(provider_result.response); if response_is_malformed(response) { decision = failed_code("malformed_payload", "provider response is malformed", turn, retry_count, tool_calls_used); running = false; diff --git a/rss/agent/provider_body.rss b/rss/agent/provider_body.rss new file mode 100644 index 0000000..66766b0 --- /dev/null +++ b/rss/agent/provider_body.rss @@ -0,0 +1,138 @@ +// Provider bodies cross the host boundary as serialized JSON text, so each +// consumer decodes the text into a declared shape and normalizes it back to the +// plain map the rest of the agent already understands. +// +// Only the fields the agent reads are declared. The decoder ignores every other +// provider field, so provider-specific additions never break the boundary, and +// a tool call's open `arguments` object is carried as `arguments_json` text by +// the host adapter. +use json; + +struct ProviderFailure { + status: Option, + type: Option, + code: Option, + message: Option, + param: Option, + request_id: Option, + retryable: Option +} + +struct ProviderToolCall { + id: Option, + name: Option, + arguments_json: Option +} + +struct ProviderResponse { + text: Option, + tool_calls: Option> +} + +fn text_present(value: Option) -> bool { + match value { + Some(inner) => true, + None => false, + } +} + +fn text_value(value: Option) -> string { + match value { + Some(inner) => inner, + None => "", + } +} + +fn int_present(value: Option) -> bool { + match value { + Some(inner) => true, + None => false, + } +} + +fn bool_present(value: Option) -> bool { + match value { + Some(inner) => true, + None => false, + } +} + +fn int_value(value: Option, fallback: int) -> int { + match value { + Some(inner) => inner, + None => fallback, + } +} + +fn bool_value(value: Option, fallback: bool) -> bool { + match value { + Some(inner) => inner, + None => fallback, + } +} + +/// The provider error object as the retry policy already reads it. Every field +/// the host publishes is carried through — including `type`, `param` and +/// `request_id`, which the retry classifier and the terminal decision use. +/// Absent keys stay absent; `status`/`retryable` keep their documented +/// defaults at the call site, never here. +pub fn failure(body: string) -> map { + let mut result: map = {}; + if body != "" { + let decoded: ProviderFailure = json::decode::(body); + if text_present(decoded.type.copy()) { + result["type"] = text_value(decoded.type.copy()); + } + if text_present(decoded.code.copy()) { + result["code"] = text_value(decoded.code.copy()); + } + if text_present(decoded.message.copy()) { + result["message"] = text_value(decoded.message.copy()); + } + if text_present(decoded.param.copy()) { + result["param"] = text_value(decoded.param.copy()); + } + if text_present(decoded.request_id.copy()) { + result["request_id"] = text_value(decoded.request_id.copy()); + } + if int_present(decoded.status.copy()) { + result["status"] = int_value(decoded.status.copy(), 0); + } + if bool_present(decoded.retryable.copy()) { + result["retryable"] = bool_value(decoded.retryable.copy(), false); + } + } + result +} + +/// `{text, tool_calls}` where every tool call is `{id, name, arguments_json}`. +pub fn response(body: string) -> map { + let mut result: map = { text: "", tool_calls: [] }; + if body != "" { + let decoded: ProviderResponse = json::decode::(body); + result.text = text_value(decoded.text.copy()); + let mut calls: array = []; + let list: array = match decoded.tool_calls { + Some(items) => items, + None => [], + }; + let mut index: int = 0; + while index < list.length { + let call: ProviderToolCall = list[index].copy(); + let mut entry: map = {}; + if text_present(call.id.copy()) { + entry["id"] = text_value(call.id.copy()); + } + if text_present(call.name.copy()) { + entry["name"] = text_value(call.name.copy()); + } + if text_present(call.arguments_json.copy()) { + entry["arguments_json"] = text_value(call.arguments_json.copy()); + } + calls[calls.length] = entry; + index += 1; + } + result.tool_calls = calls; + } + result +} diff --git a/rss/agent/provider_runtime.rss b/rss/agent/provider_runtime.rss index 93e0705..79b345a 100644 --- a/rss/agent/provider_runtime.rss +++ b/rss/agent/provider_runtime.rss @@ -30,43 +30,27 @@ struct ProviderRefreshResult { ok: bool, code: string, outcome: string, - metadata: AuthMetadata?, - failure: AuthBridgeFailure? + metadata: Option, + failure: Option } struct PreflightReady { - home: resource?, + home: Option>, policy: resource, has_refresh_token: bool, - refresh_path: string? -} - -struct BufferedSlotPresence { - slot: resource? -} - -struct BufferedSlotBinding { - slot: resource -} - -struct HostHomePresence { - home: resource? -} - -struct PolicySlotPresence { - policy: resource? + refresh_path: Option } struct PolicyPresence { - home: resource?, - policy: resource?, - error: AuthBridgeFailure? + home: Option>, + policy: Option>, + error: Option } struct PreflightResult { ok: bool, - error: AuthBridgeFailure?, - value: PreflightReady?, + error: Option, + value: Option, credential_kind: string, did_refresh: bool } @@ -76,11 +60,80 @@ struct ProviderLookup { provider: ProviderEntry } +/// Everything the 401 retry needs after the first bridge call: the credential +/// facts and the retained policy slots, carried as logical `Option` leaves. +struct RetryPlan { + credential_kind: string, + did_refresh: bool, + has_refresh_token: bool, + refresh_path: Option, + retry_home: Option>, + retry_policy: Option>, + intent: OpaqueProviderIntent +} + +// --------------------------------------------------------------------------- +// Logical Option carriers +// +// The host publishes an absent leaf as a tagged `None` carrier. Each helper +// below consumes exactly one carrier with an exhaustive match; the payload is +// bound at the site that needs it, never behind a null comparison. +// --------------------------------------------------------------------------- + +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn int_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn oauth_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn envelope_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_failure() -> Option { + None +} + +fn no_text() -> Option { + None +} + +fn no_home() -> Option> { + None +} + fn max_int() -> int { 9223372036854775807 } fn empty_metadata() -> AuthMetadata { + let account_id: Option = None; + let last_refresh_at_ms: Option = None; { credential_id: "", provider: "", @@ -89,35 +142,79 @@ fn empty_metadata() -> AuthMetadata { token_type: "", expires_at_ms: 0, scopes: [], - account_id: null, + account_id: account_id, generation: 0, status: "", - last_refresh_at_ms: null, + last_refresh_at_ms: last_refresh_at_ms, has_refresh_token: false } } fn empty_metadata_envelope() -> AuthMetadataEnvelope { - {ok: false, error: null, metadata: empty_metadata(), outcome: ""} + {ok: false, error: no_text(), metadata: empty_metadata(), outcome: ""} } fn empty_provider() -> ProviderEntry { - {name: "", protocol: "", base_url: "", auth: null, oauth: null} + let auth: Option = None; + let oauth: Option = None; + {name: "", protocol: "", base_url: "", auth: auth, oauth: oauth} +} + +/// The absent provider OAuth block. Only materialized where the caller has +/// already decided that the block is missing and is about to refuse the +/// credential; it never stands in for a real configuration. +fn no_oauth_config() -> OAuthConfig { + let flow: Option = None; + let issuer: Option = None; + let client_id: Option = None; + let device_user_code_path: Option = None; + let device_poll_path: Option = None; + let authorization_path: Option = None; + let token_endpoint: Option = None; + let redirect_uri: Option = None; + let scopes: Option> = None; + let authorization_parameters: Option> = None; + { + flow: flow, + issuer: issuer, + client_id: client_id, + device_user_code_path: device_user_code_path, + device_poll_path: device_poll_path, + authorization_path: authorization_path, + token_endpoint: token_endpoint, + redirect_uri: redirect_uri, + refresh_skew_seconds: 0, + scopes: scopes, + authorization_parameters: authorization_parameters + } +} + +fn empty_policy() -> PolicyPresence { + let home: Option> = None; + let policy: Option> = None; + let error: Option = None; + {home: home, policy: policy, error: error} +} + +fn failed_policy(error: AuthBridgeFailure) -> PolicyPresence { + let home: Option> = None; + let policy: Option> = None; + {home: home, policy: policy, error: Some(error)} } fn bridge_failure(code: string) -> AuthBridgeFailure { - {code: code, cancelled: false, deadline_exceeded: false, revoked: false, transport_phase: null, transport_category: null} + {code: code, cancelled: false, deadline_exceeded: false, revoked: false, transport_phase: None, transport_category: None} } fn failure_from_code(code: string) -> AuthBridgeFailure { if code.copy() == "cancelled" => { - {code: code, cancelled: true, deadline_exceeded: false, revoked: false, transport_phase: null, transport_category: null} + {code: code, cancelled: true, deadline_exceeded: false, revoked: false, transport_phase: None, transport_category: None} } else if code.copy() == "deadline_elapsed" => { - {code: code, cancelled: false, deadline_exceeded: true, revoked: false, transport_phase: null, transport_category: null} + {code: code, cancelled: false, deadline_exceeded: true, revoked: false, transport_phase: None, transport_category: None} } else if code.copy() == "deadline_exceeded" => { - {code: code, cancelled: false, deadline_exceeded: true, revoked: false, transport_phase: null, transport_category: null} + {code: code, cancelled: false, deadline_exceeded: true, revoked: false, transport_phase: None, transport_category: None} } else if code.copy() == "revoked" => { - {code: code, cancelled: false, deadline_exceeded: false, revoked: true, transport_phase: null, transport_category: null} + {code: code, cancelled: false, deadline_exceeded: false, revoked: true, transport_phase: None, transport_category: None} } else => { bridge_failure(code.copy()) } @@ -141,27 +238,40 @@ fn safe_relative_path(value: string) -> bool { find_text(value, "://") < 0 && find_text(value, "?") < 0 && find_text(value, "#") < 0 } -fn token_path(endpoint: string) -> string? { - let mut selected_path: string? = null; +/// The token path of an `https://host/path` endpoint, or `None` when the +/// endpoint is unusable. The chosen path is wrapped in `Some` at the point where +/// it is selected, so the carrier never needs a null. +fn token_path(endpoint: string) -> Option { + let mut selected_path: Option = None; if find_text(endpoint, "https://") == 0 && find_text(endpoint, "?") < 0 && find_text(endpoint, "#") < 0 { let authority_and_path: string = endpoint[8:]; let slash: int = find_text(authority_and_path, "/"); if slash < 0 { if authority_and_path != "" && find_text(authority_and_path, "@") < 0 { - selected_path = "/"; + selected_path = Some("/"); } } else { let authority: string = authority_and_path.copy()[0:slash]; let path: string = authority_and_path[slash:]; if authority != "" && find_text(authority, "@") < 0 && safe_relative_path(path) { - selected_path = path; + selected_path = Some(path); } } } selected_path } +/// Whether the provider entry's optional `auth` credential id names the +/// credential being looked up. The `Some` arm binds the id; a `None` auth block +/// never matches. +fn provider_auth_matches(auth_id: Option, credential_id: string) -> bool { + match auth_id { + Some(value) => value.copy() == credential_id.copy(), + None => false + } +} + fn lookup_provider(public_config: BoundedPublicConfig, credential_id: string) -> ProviderLookup { let providers = public_config.providers.copy(); let mut found: bool = false; @@ -169,40 +279,43 @@ fn lookup_provider(public_config: BoundedPublicConfig, credential_id: string) -> let mut index: int = 0; while index < providers.length && !found { let candidate: ProviderEntry = providers[index].copy(); - let auth_holder: string? = candidate.auth.copy(); - if auth_holder != null { - let auth_id: string = auth_holder; - if auth_id == credential_id { - found = true; - selected = candidate; - } + if provider_auth_matches(candidate.auth.copy(), credential_id) { + found = true; + selected = candidate; } index += 1; } {found: found, provider: selected} } -fn preflight_failed(error: AuthBridgeFailure?) -> PreflightResult { - {ok: false, error: error, value: null, credential_kind: "", did_refresh: false} +fn preflight_refused(failure: AuthBridgeFailure) -> PreflightResult { + preflight_failed(Some(failure)) +} + +fn preflight_failed(error: Option) -> PreflightResult { + let value: Option = None; + {ok: false, error: error, value: value, credential_kind: "", did_refresh: false} } fn preflight_ready( - home: resource?, + home: Option>, binding: OpaquePolicyBinding, credential_kind: string, has_refresh_token: bool, - refresh_path: string?, + refresh_path: Option, refreshed: bool ) -> PreflightResult { + let error: Option = None; + let value: PreflightReady = { + home: home, + policy: binding.policy, + has_refresh_token: has_refresh_token, + refresh_path: refresh_path + }; { ok: true, - error: null, - value: { - home: home, - policy: binding.policy, - has_refresh_token: has_refresh_token, - refresh_path: refresh_path - }, + error: error, + value: Some(value), credential_kind: credential_kind, did_refresh: refreshed } @@ -222,67 +335,92 @@ fn refresh_provider( } fn reauth_required_result() -> TransportResultEnvelope { + let value: Option = None; + let error: AuthBridgeFailure = {code: "reauth_required", cancelled: false, deadline_exceeded: false, revoked: false}; + {ok: false, error: Some(error), value: value} +} + +/// The absent-response frame: every logical `Option` leaf is a tagged `None` +/// carrier, never a physical null. +fn empty_transport_response() -> TransportResponseEnvelope { + let retry_after_ms: Option = None; + let access_slot: Option> = None; + let refresh_slot: Option> = None; + let device_handle: Option> = None; { ok: false, - error: {code: "reauth_required", cancelled: false, deadline_exceeded: false, revoked: false, transport_phase: null, transport_category: null}, - value: null + error: no_text(), + status: 0, + public_headers: [], + body_without_secret_fields: "", + retry_after_ms: retry_after_ms, + access_slot: access_slot, + refresh_slot: refresh_slot, + device_handle: device_handle } } -fn refresh_failure_result(code: string, failure_value: AuthBridgeFailure?) -> TransportResultEnvelope { +fn no_value_result_or(failure: Option) -> TransportResultEnvelope { + let value: Option = None; + {ok: false, error: failure, value: value} +} + +fn no_value_result(error: AuthBridgeFailure) -> TransportResultEnvelope { + no_value_result_or(Some(error)) +} + +fn with_response( + ok: bool, + failure: Option, + response: TransportResponseEnvelope +) -> TransportResultEnvelope { + {ok: ok, error: failure, value: Some(response)} +} + +fn bridge_failure_result(code: string, failure: AuthBridgeFailure) -> TransportResultEnvelope { + let error: AuthBridgeFailure = { + code: code.copy(), + cancelled: failure.cancelled.copy(), + deadline_exceeded: failure.deadline_exceeded.copy(), + revoked: failure.revoked.copy(), + transport_phase: failure.transport_phase.copy(), + transport_category: failure.transport_category.copy() + }; + no_value_result(error) +} + +fn refresh_failure_without_detail(code: string, canonical_code: string) -> TransportResultEnvelope { + if canonical_code.copy() == "reauth_required" => { + reauth_required_result() + } else => { + no_value_result(failure_from_code(code.copy())) + } +} + +fn refresh_failure_result(code: string, failure_value: Option) -> TransportResultEnvelope { let mut canonical_code: string = code.copy(); if code.copy() == "reauth_required" || code.copy() == "refresh_unavailable" { canonical_code = "reauth_required"; } - if failure_value != null => { - let failure: AuthBridgeFailure = failure_value; - { - ok: false, - error: { - code: canonical_code.copy(), - cancelled: failure.cancelled.copy(), - deadline_exceeded: failure.deadline_exceeded.copy(), - revoked: failure.revoked.copy(), - transport_phase: null, - transport_category: null - }, - value: null - } - } else if canonical_code.copy() == "reauth_required" => { - reauth_required_result() - } else => { - {ok: false, error: failure_from_code(code.copy()), value: null} + match failure_value { + Some(failure) => bridge_failure_result(canonical_code, failure), + None => refresh_failure_without_detail(code, canonical_code) } } -fn close_response_slots(response: TransportResponseEnvelope) -> null { - let has_access: bool = response.has("access_slot"); - let has_refresh: bool = response.has("refresh_slot"); - let access: BufferedSlotPresence = if has_access => { - {slot: response.access_slot} - } else => { - {slot: null} - }; - let refresh: BufferedSlotPresence = if has_refresh => { - {slot: response.refresh_slot} - } else => { - {slot: null} - }; - if access.has("slot") { - let slot: resource? = access.slot; - if slot != null { - let binding: BufferedSlotBinding = {slot: slot}; - auth::close_slot(binding.slot); - } +/// Closes one published slot. The carrier is consumed here: the `Some` arm hands +/// the live handle to the host close, the `None` arm has nothing to close. +fn close_slot_option(slot: Option>) -> () { + match slot { + Some(value) => auth::close_slot(value), + None => () } - if refresh.has("slot") { - let slot: resource? = refresh.slot; - if slot != null { - let binding: BufferedSlotBinding = {slot: slot}; - auth::close_slot(binding.slot); - } - } - null +} + +fn close_response_slots(response: TransportResponseEnvelope) -> () { + close_slot_option(response.access_slot); + close_slot_option(response.refresh_slot); + () } fn skew_ms_guarded(skew_seconds: int) -> int { @@ -301,62 +439,66 @@ fn preflight(binding: OpaqueHostHomeBinding, credential_id: string) -> Preflight let snapshot: ConfigSnapshotEnvelope = config::load_snapshot(&binding.home); let snapshot_has_error: bool = snapshot.has("error"); let snapshot_ok: bool = snapshot.ok; - let snapshot_error: string? = if snapshot_has_error => { snapshot.error } else => { null }; - if !snapshot_ok || snapshot_error != null => { + let snapshot_error: Option = if snapshot_has_error => { snapshot.error } else => { None }; + if !snapshot_ok || text_present(snapshot_error) => { let code: string = snapshot_error.unwrap_or("auth_unavailable"); - preflight_failed(failure_from_code(code)) + preflight_refused(failure_from_code(code)) } else => { let policy_binding: OpaquePolicyBinding = {policy: snapshot.policy_handle}; let public_config: BoundedPublicConfig = snapshot.public_config; let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result(credential_id.copy(), &policy_binding.policy); - if loaded.error.copy() != null => { - preflight_failed(loaded.error.copy()) + let loaded_failure: Option = loaded.error.copy(); + if failure_present(loaded_failure) => { + preflight_failed(loaded_failure) } else if !loaded.ok => { - preflight_failed(bridge_failure("invalid_credential_metadata")) + preflight_refused(bridge_failure("invalid_credential_metadata")) } else => { - let maybe_envelope: AuthMetadataEnvelope? = loaded.value.copy(); - if maybe_envelope == null => { - preflight_failed(bridge_failure("invalid_credential_metadata")) + let maybe_envelope: Option = loaded.value.copy(); + if !envelope_present(maybe_envelope) => { + preflight_refused(bridge_failure("invalid_credential_metadata")) } else => { let envelope: AuthMetadataEnvelope = maybe_envelope.unwrap_or(empty_metadata_envelope()); - if !envelope.ok.copy() || envelope.error.copy() != null || envelope.outcome.copy() != "loaded" => { - preflight_failed(bridge_failure("invalid_credential_metadata")) + let envelope_failure: Option = envelope.error.copy(); + if !envelope.ok.copy() || text_present(envelope_failure) || envelope.outcome.copy() != "loaded" => { + preflight_refused(bridge_failure("invalid_credential_metadata")) } else => { let metadata: AuthMetadata = envelope.metadata.copy(); if metadata.credential_id.copy() != credential_id.copy() || metadata.generation.copy() < 0 || metadata.provider.copy() == "" || metadata.status.copy() != "active" => { - preflight_failed(bridge_failure("invalid_credential_metadata")) + preflight_refused(bridge_failure("invalid_credential_metadata")) } else if metadata.kind.copy() != "oauth" => { - preflight_ready(binding.home, policy_binding, metadata.kind.copy(), false, null, false) + preflight_ready(Some(binding.home), policy_binding, metadata.kind.copy(), false, no_text(), false) } else => { let selected: ProviderLookup = lookup_provider(public_config, credential_id.copy()); if !selected.found.copy() => { - preflight_failed(bridge_failure("credential_not_admitted")) + preflight_refused(bridge_failure("credential_not_admitted")) } else => { let provider: ProviderEntry = selected.provider.copy(); if provider.name.copy() != metadata.provider.copy() => { - preflight_failed(bridge_failure("credential_not_admitted")) + preflight_refused(bridge_failure("credential_not_admitted")) } else => { let now_field: OAuthNowResultEnvelope = oauth::now_ms_result(&policy_binding.policy); - if now_field.error.copy() != null => { - preflight_failed(now_field.error.copy()) - } else if !now_field.ok.copy() || now_field.value.copy() == null => { - preflight_failed(bridge_failure("clock_unavailable")) + let now_failure: Option = now_field.error.copy(); + let now_value: Option = now_field.value.copy(); + if failure_present(now_failure) => { + preflight_failed(now_failure) + } else if !now_field.ok.copy() || !int_present(now_value) => { + preflight_refused(bridge_failure("clock_unavailable")) } else => { - let now_ms: int = now_field.value.copy(); + let now_ms: int = now_value.unwrap_or(-1); if now_ms < 0 || now_ms > 9000000000000000 => { - preflight_failed(bridge_failure("clock_unavailable")) - } else if provider.oauth.copy() == null => { + preflight_refused(bridge_failure("clock_unavailable")) + } else if !oauth_present(provider.oauth.copy()) => { if metadata.expires_at_ms.copy() > now_ms => { - preflight_ready(binding.home, policy_binding, metadata.kind.copy(), metadata.has_refresh_token.copy(), null, false) + preflight_ready(Some(binding.home), policy_binding, metadata.kind.copy(), metadata.has_refresh_token.copy(), no_text(), false) } else => { - preflight_failed(bridge_failure("refresh_unavailable")) + preflight_refused(bridge_failure("refresh_unavailable")) } } else => { - let oauth_config: OAuthConfig = provider.oauth.copy(); + let oauth_config: OAuthConfig = provider.oauth.copy().unwrap_or(no_oauth_config()); let skew_ms: int = skew_ms_guarded(oauth_config.refresh_skew_seconds); - let endpoint_holder: string? = oauth_config.token_endpoint; + let endpoint_holder: Option = oauth_config.token_endpoint; let expires_at: int = metadata.expires_at_ms.copy(); let mut needs_refresh: bool = false; if expires_at <= now_ms { @@ -368,25 +510,25 @@ fn preflight(binding: OpaqueHostHomeBinding, credential_id: string) -> Preflight } } if !needs_refresh => { - let refresh_path: string? = if endpoint_holder == null => { - null + let refresh_path: Option = if !text_present(endpoint_holder) => { + no_text() } else => { token_path(endpoint_holder.unwrap_or("")) }; preflight_ready( - binding.home, + Some(binding.home), policy_binding, metadata.kind.copy(), metadata.has_refresh_token.copy(), refresh_path, false ) - } else if !metadata.has_refresh_token.copy() || endpoint_holder == null => { - preflight_failed(bridge_failure("refresh_unavailable")) + } else if !metadata.has_refresh_token.copy() || !text_present(endpoint_holder) => { + preflight_refused(bridge_failure("refresh_unavailable")) } else => { - let path_holder: string? = token_path(endpoint_holder.unwrap_or("")); - if path_holder == null => { - preflight_failed(bridge_failure("refresh_unavailable")) + let path_holder: Option = token_path(endpoint_holder.unwrap_or("")); + if !text_present(path_holder) => { + preflight_refused(bridge_failure("refresh_unavailable")) } else => { let refreshed: ProviderRefreshResult = refresh_provider( @@ -396,17 +538,17 @@ fn preflight(binding: OpaqueHostHomeBinding, credential_id: string) -> Preflight ); if !refreshed.ok.copy() => { let has_failure: bool = refreshed.has("failure"); - let failure: AuthBridgeFailure? = if has_failure => { refreshed.failure.copy() } else => { null }; - if failure != null => { + let failure: Option = if has_failure => { refreshed.failure.copy() } else => { no_failure() }; + if failure_present(failure) => { preflight_failed(failure) } else => { - preflight_failed(failure_from_code(refreshed.code.copy())) + preflight_refused(failure_from_code(refreshed.code.copy())) } } else if refreshed.outcome.copy() != "committed" && refreshed.outcome.copy() != "adopted" => { - preflight_failed(bridge_failure("refresh_failed")) + preflight_refused(bridge_failure("refresh_failed")) } else => { preflight_ready( - null, + no_home(), policy_binding, metadata.kind.copy(), metadata.has_refresh_token.copy(), @@ -431,11 +573,80 @@ fn retain_retry_policy(binding: OpaqueHostHomeBinding) -> PolicyPresence { let snapshot: ConfigSnapshotEnvelope = config::load_snapshot(&binding.home); let has_error: bool = snapshot.has("error"); let ok: bool = snapshot.ok; - let error: string? = if has_error => { snapshot.error } else => { null }; - if !ok || error != null => { - {home: null, policy: null, error: failure_from_code(error.unwrap_or("auth_unavailable"))} + let error: Option = if has_error => { snapshot.error } else => { None }; + if !ok || text_present(error) => { + failed_policy(failure_from_code(error.unwrap_or("auth_unavailable"))) } else => { - {home: binding.home, policy: snapshot.policy_handle, error: null} + let home: Option> = Some(binding.home); + let policy: Option> = Some(snapshot.policy_handle); + {home: home, policy: policy, error: no_failure()} + } +} + +/// Retains the retry policy only when the preflight carried a home handle; the +/// carrier is consumed by a match, so the `Some` arm binds the live resource. +fn retain_with_home(home: Option>) -> PolicyPresence { + match home { + Some(value) => retain_retry_policy({home: value}), + None => empty_policy() + } +} + +/// Runs the forced refresh and then the retried call, both of which only exist +/// when the retained home and policy handles are present. +fn forced_refresh_with_slots( + home: Option>, + policy: Option>, + path: string, + intent: OpaqueProviderIntent +) -> TransportResultEnvelope { + match home { + Some(home_value) => match policy { + Some(policy_value) => forced_refresh_retry( + {home: home_value}, + {policy: policy_value}, + path, + intent + ), + None => reauth_required_result() + }, + None => reauth_required_result() + } +} + +fn retried_response( + retry_ok: bool, + retry_failure: Option, + response: TransportResponseEnvelope +) -> TransportResultEnvelope { + // The envelope owns live slot handles, so it is taken apart exactly once: + // each field is moved out here and the frame is rebuilt on the carry path. + let status: int = response.status; + let response_ok: bool = response.ok; + let response_error: Option = response.error; + let headers = response.public_headers; + let body: string = response.body_without_secret_fields; + let retry_after: Option = response.retry_after_ms; + let access_slot: Option> = response.access_slot; + let refresh_slot: Option> = response.refresh_slot; + let device_handle: Option> = response.device_handle; + if status == 401 => { + close_slot_option(access_slot); + close_slot_option(refresh_slot); + reauth_required_result() + } else => { + let carried: TransportResponseEnvelope = { + ok: response_ok, + error: response_error, + status: status, + public_headers: headers, + body_without_secret_fields: body, + retry_after_ms: retry_after, + access_slot: access_slot, + refresh_slot: refresh_slot, + device_handle: device_handle + }; + with_response(retry_ok, retry_failure, carried) } } @@ -448,37 +659,143 @@ fn forced_refresh_retry( let refreshed: ProviderRefreshResult = refresh_provider(binding, intent.credential_id.copy(), path.copy()); let has_failure: bool = refreshed.has("failure"); - let failure: AuthBridgeFailure? = if has_failure => { refreshed.failure.copy() } else => { null }; + let failure: Option = if has_failure => { refreshed.failure.copy() } else => { no_failure() }; let outcome: string = refreshed.outcome.copy(); if !refreshed.ok.copy() => { refresh_failure_result(refreshed.code.copy(), failure) } else if outcome.copy() != "committed" && outcome.copy() != "adopted" => { - refresh_failure_result("save_failed", null) + refresh_failure_result("save_failed", no_failure()) } else => { - let mut retried: TransportResultEnvelope = bridge_call(policy_binding, intent.copy()); + let retried: TransportResultEnvelope = bridge_call(policy_binding, intent.copy()); let retry_has_value: bool = retried.has("value"); - // Native wrap_value guarantees ok => error is null. Leave that - // nullable field untouched: moving/reassigning it changes the guest - // map shape consumed by the codec. Restore only inspected scalars. + // Native wrap_value guarantees ok => error is absent, so the inspected + // scalar and carrier are the whole envelope: the response payload is the + // only field that is moved out of it. let retry_ok: bool = retried.ok; - retried.ok = retry_ok; - if !retry_ok || !retry_has_value => { - retried + let retry_failure: Option = retried.error; + let retry_value: Option = if !retry_ok || !retry_has_value => { + None } else => { - let mut response: TransportResponseEnvelope = retried.value; - let status: int = response.status; - response.status = status; - if status == 401 => { - close_response_slots(response); - reauth_required_result() - } else => { - retried.value = response; - retried - } + retried.value + }; + match retry_value { + Some(response) => retried_response(retry_ok, retry_failure, response), + None => with_response(retry_ok, retry_failure, empty_transport_response()) } } } +fn first_refusal(plan: RetryPlan) -> TransportResultEnvelope { + if plan.did_refresh || !plan.has_refresh_token => { + reauth_required_result() + } else if !text_present(plan.refresh_path) => { + reauth_required_result() + } else => { + // Slot carriers are taken out of the plan once, so the helper call moves + // each handle exactly one time. + let path: string = plan.refresh_path.unwrap_or(""); + let retry_home: Option> = plan.retry_home; + let retry_policy: Option> = plan.retry_policy; + let retry_intent: OpaqueProviderIntent = plan.intent; + forced_refresh_with_slots(retry_home, retry_policy, path, retry_intent) + } +} + +fn first_response( + first_ok: bool, + first_failure: Option, + response: TransportResponseEnvelope, + plan: RetryPlan +) -> TransportResultEnvelope { + // Taken apart exactly once: the slot handles are move-only, so the envelope + // cannot be handed to two branches whole. + let status: int = response.status; + let response_ok: bool = response.ok; + let response_error: Option = response.error; + let headers = response.public_headers; + let body: string = response.body_without_secret_fields; + let retry_after: Option = response.retry_after_ms; + let access_slot: Option> = response.access_slot; + let refresh_slot: Option> = response.refresh_slot; + let device_handle: Option> = response.device_handle; + // `credential_kind` is read without `.copy()`: the plan carries live slot + // handles, so copying one of its fields would copy the whole plan. + if status != 401 || plan.credential_kind != "oauth" => { + let carried: TransportResponseEnvelope = { + ok: response_ok, + error: response_error, + status: status, + public_headers: headers, + body_without_secret_fields: body, + retry_after_ms: retry_after, + access_slot: access_slot, + refresh_slot: refresh_slot, + device_handle: device_handle + }; + with_response(first_ok, first_failure, carried) + } else => { + close_slot_option(access_slot); + close_slot_option(refresh_slot); + first_refusal(plan) + } +} + +fn send_first( + policy: resource, + intent: OpaqueProviderIntent, + plan: RetryPlan +) -> TransportResultEnvelope { + let first: TransportResultEnvelope = bridge_call({policy: policy}, intent.copy()); + let first_has_value: bool = first.has("value"); + let first_ok: bool = first.ok; + let first_failure: Option = first.error; + let first_value: Option = if !first_ok || !first_has_value => { + None + } else => { + first.value + }; + match first_value { + Some(response) => first_response(first_ok, first_failure, response, plan), + None => with_response(first_ok, first_failure, empty_transport_response()) + } +} + +fn call_ready( + ready: PreflightReady, + credential_kind: string, + did_refresh: bool, + intent: OpaqueProviderIntent +) -> TransportResultEnvelope { + let has_home: bool = ready.has("home"); + let has_refresh_path: bool = ready.has("refresh_path"); + let has_refresh_token: bool = ready.has_refresh_token; + let ready_home: Option> = if has_home => { ready.home } else => { no_home() }; + let refresh_path: Option = if has_refresh_path => { ready.refresh_path } else => { no_text() }; + let retained: PolicyPresence = if credential_kind.copy() == "oauth" && !did_refresh && + has_refresh_token && text_present(refresh_path) => { + retain_with_home(ready_home) + } else => { + empty_policy() + }; + let retained_failure: Option = retained.error; + let retry_home: Option> = retained.home; + let retry_policy: Option> = retained.policy; + if failure_present(retained_failure) => { + no_value_result_or(retained_failure) + } else => { + let plan: RetryPlan = { + credential_kind: credential_kind, + did_refresh: did_refresh, + has_refresh_token: has_refresh_token, + refresh_path: refresh_path, + retry_home: retry_home, + retry_policy: retry_policy, + intent: intent.copy() + }; + send_first(ready.policy, intent, plan) + } +} + pub fn call(binding: OpaqueHostHomeBinding, intent: OpaqueProviderIntent) -> TransportResultEnvelope { let prepared: PreflightResult = preflight(binding, intent.credential_id.copy()); let has_value: bool = prepared.has("value"); @@ -486,88 +803,36 @@ pub fn call(binding: OpaqueHostHomeBinding, intent: OpaqueProviderIntent) -> Tra let credential_kind: string = prepared.credential_kind; let did_refresh: bool = prepared.did_refresh; let ok: bool = prepared.ok; - let failure: AuthBridgeFailure? = if has_error => { prepared.error } else => { null }; - if !ok || failure != null || !has_value => { - {ok: false, error: failure, value: null} + let failure: Option = if has_error => { prepared.error } else => { no_failure() }; + let ready: Option = if !ok || failure_present(failure) || !has_value => { + None } else => { - let ready: PreflightReady = prepared.value; - let has_home: bool = ready.has("home"); - let has_refresh_path: bool = ready.has("refresh_path"); - let has_refresh_token: bool = ready.has_refresh_token; - let home_holder: HostHomePresence = if has_home => { - {home: ready.home} - } else => { - {home: null} - }; - let refresh_path: string? = if has_refresh_path => { ready.refresh_path } else => { null }; - let retained: PolicyPresence = if credential_kind.copy() == "oauth" && !did_refresh && - has_refresh_token && refresh_path != null && home_holder.has("home") => { - retain_retry_policy({home: home_holder?.home}) - } else => { - {home: null, policy: null, error: null} - }; - let retained_has_home: bool = retained.has("home"); - let retained_has_policy: bool = retained.has("policy"); - let retained_has_error: bool = retained.has("error"); - let retained_failure: AuthBridgeFailure? = if retained_has_error => { retained.error } else => { null }; - let retry_home: HostHomePresence = if retained_has_home => { - {home: retained.home} - } else => { - {home: null} - }; - let retry_policy: PolicySlotPresence = if retained_has_policy => { - {policy: retained.policy} - } else => { - {policy: null} - }; - if retained_failure != null => { - {ok: false, error: retained_failure, value: null} - } else => { - let first_binding: OpaquePolicyBinding = {policy: ready.policy}; - let mut first: TransportResultEnvelope = bridge_call(first_binding, intent.copy()); - let first_has_value: bool = first.has("value"); - let first_ok: bool = first.ok; - first.ok = first_ok; - if !first_ok || !first_has_value => { - first - } else => { - let mut response: TransportResponseEnvelope = first.value; - let status: int = response.status; - response.status = status; - if status != 401 || credential_kind.copy() != "oauth" => { - first.value = response; - first - } else => { - close_response_slots(response); - if did_refresh || !has_refresh_token => { - reauth_required_result() - } else if refresh_path == null || !retained_has_home || !retained_has_policy => { - reauth_required_result() - } else => { - let path: string = refresh_path.unwrap_or(""); - forced_refresh_retry( - {home: retry_home?.home}, - {policy: retry_policy?.policy}, - path, - intent.copy() - ) - } - } - } - } + prepared.value + }; + match ready { + Some(value) => call_ready(value, credential_kind, did_refresh, intent), + None => no_value_result_or(failure) } } +fn stream_start_failure(failure: Option) -> OAuthStreamStartResultEnvelope { + let value: Option = None; + {ok: false, error: failure, value: value} +} + pub fn start_stream(binding: OpaqueHostHomeBinding, intent: OpaqueProviderIntent, markers: array) -> OAuthStreamStartResultEnvelope { let prepared: PreflightResult = preflight(binding, intent.credential_id.copy()); let has_value: bool = prepared.has("value"); let has_error: bool = prepared.has("error"); let ok: bool = prepared.ok; - let failure: AuthBridgeFailure? = if has_error => { prepared.error } else => { null }; - if !ok || failure != null || !has_value => { - {ok: false, error: failure, value: null} + let failure: Option = if has_error => { prepared.error } else => { no_failure() }; + let ready: Option = if !ok || failure_present(failure) || !has_value => { + None } else => { - let ready: PreflightReady = prepared.value; - bridge_start_stream({policy: ready.policy}, intent, markers) + prepared.value + }; + match ready { + Some(value) => bridge_start_stream({policy: value.policy}, intent, markers), + None => stream_start_failure(failure) } } diff --git a/rss/agent/workspace.rss b/rss/agent/workspace.rss index 1da05ba..7682fec 100644 --- a/rss/agent/workspace.rss +++ b/rss/agent/workspace.rss @@ -10,11 +10,11 @@ use workspace; struct WorkspaceOpenOutcome { ok: bool, - directory: resource?, - canonical_path: string?, - effective_selector: string?, - policy_generation: int?, - error: map? + directory: Option>, + canonical_path: Option, + effective_selector: Option, + policy_generation: Option, + error: Option } fn char_at(value: string, index: int) -> string { @@ -142,17 +142,6 @@ fn named_lookup(workspaces: map, key: string) -> map { resolved } -fn configured_default(workspaces: map) -> map { - let mut resolved: map = {found: false, path: ""}; - if workspaces.has("default") { - if type(workspaces["default"]) == "string" { - let value: string = workspaces["default"]; - resolved = {found: true, path: value}; - } - } - resolved -} - fn failure(code: string, message: string) -> map { { ok: false, @@ -162,7 +151,7 @@ fn failure(code: string, message: string) -> map { effective_selector: "", selected_name: "", generation: 0, - directory: null + directory: () } } @@ -171,7 +160,7 @@ fn failure(code: string, message: string) -> map { // `selector` is the raw external token. `session_selector` is the stored // session binding (possibly empty). RSS owns default/named/session priority // and the user-visible rejection meaning; nothing here grants authority. -fn resolve(workspaces: map, selector: string, session_selector: string, selector_present: bool) -> map { +fn resolve(workspaces: WorkspaceConfig, selector: string, session_selector: string, selector_present: bool, managed_workspace_root: string) -> map { let mut resolved: map = {ok: false, path: "", name: "", effective_selector: "", code: "", message: ""}; let mut effective: string = selector; if selector.length > 4096 { @@ -183,10 +172,16 @@ fn resolve(workspaces: map, selector: string, session_selector: string, selector effective = session_selector; } if effective.length == 0 { - let fallback: map = configured_default(workspaces); - if map_bool(fallback, "found", false) { - let path: string = map_string(fallback, "path", ""); + let configured: Option = workspaces.default; + let present: bool = match configured.copy() { + Some(_) => true, + None => false, + }; + if present { + let path: string = configured.unwrap_or(""); resolved = {ok: true, path: path.copy(), name: "", effective_selector: path.copy(), code: "", message: ""}; + } else if managed_workspace_root.length > 0 { + resolved = {ok: true, path: managed_workspace_root.copy(), name: "", effective_selector: managed_workspace_root.copy(), code: "", message: ""}; } else { resolved = {ok: false, path: "", name: "", effective_selector: "", code: "workspace_unavailable", message: "no workspace is configured or selected"}; } @@ -237,8 +232,9 @@ fn open_with_policy(context: map, allow_switch: bool) -> map { outcome = failure("workspace_unavailable", "the trusted configuration is unavailable"); } else { let generation: int = snapshot.policy_generation; - let workspaces: map = snapshot.public_config.workspaces; - let resolved: map = resolve(workspaces, selector, session_selector, selector_present); + let workspaces: WorkspaceConfig = snapshot.public_config.workspaces; + let managed_workspace_root: string = map_string(context, "managed_workspace_root", ""); + let resolved: map = resolve(workspaces, selector, session_selector, selector_present, managed_workspace_root); if map_bool(resolved, "ok", false) == false { outcome = failure(map_string(resolved, "code", "workspace_unavailable"), map_string(resolved, "message", "the workspace selection was rejected")); outcome["selected_name"] = map_string(resolved, "name", ""); @@ -249,13 +245,12 @@ fn open_with_policy(context: map, allow_switch: bool) -> map { }; let opened: WorkspaceOpenResult = workspace::open_result(selection, &snapshot.policy_handle); let opened_ok: bool = opened.ok; - let opened_path: string? = opened.canonical_path; - let opened_directory: resource? = opened.directory; - let opened_canonical_path: string = map_string( - {canonical_path: opened_path}, - "canonical_path", - "" - ); + let opened_path: Option = opened.canonical_path; + let opened_directory: Option> = opened.directory; + let opened_canonical_path: string = match opened_path { + Some(path) => path.copy(), + None => "" + }; if opened_ok == false || opened_canonical_path.length == 0 { let mut rejected: map = failure("workspace_denied", "the selected workspace is not available"); rejected["selected_name"] = map_string(resolved, "name", ""); diff --git a/rss/auth/callback_contract.rss b/rss/auth/callback_contract.rss index 633cd59..b4e98b0 100644 --- a/rss/auth/callback_contract.rss +++ b/rss/auth/callback_contract.rss @@ -15,15 +15,15 @@ pub fn wait(callback_handle: resource) -> CallbackEnvelope { oauth::callback_wait(&callback_handle) } -pub fn close_callback(callback_handle: resource) -> null { +pub fn close_callback(callback_handle: resource) -> () { oauth::close_callback(callback_handle) } -pub fn close_verifier(verifier_handle: resource) -> null { +pub fn close_verifier(verifier_handle: resource) -> () { oauth::close_verifier(verifier_handle) } -pub fn close_code(code_handle: resource) -> null { +pub fn close_code(code_handle: resource) -> () { oauth::close_code(code_handle) } diff --git a/rss/auth/capture_contract.rss b/rss/auth/capture_contract.rss index 71b4fe4..6747f44 100644 --- a/rss/auth/capture_contract.rss +++ b/rss/auth/capture_contract.rss @@ -15,7 +15,7 @@ pub fn start_device(policy: resource) -> TransportResponseEnvelope public_headers: [{name: "content-type", value: "application/json"}], public_body: "{}" }; - let usage = {kind: "none"}; + let usage: TransportCredentialUse = {kind: "none"}; oauth::transport(&request, &usage) } @@ -34,19 +34,19 @@ pub fn exchange_selected( verifier: resource, payload: TransportPayloadIntent ) -> TransportResponseEnvelope { - let usage = {kind: "code_verifier", code_handle: code, verifier_handle: verifier}; + let usage: TransportCredentialUse = {kind: "code_verifier", code_handle: Some(code), verifier_handle: Some(verifier)}; oauth::transport_payload(&request, &usage, payload) } -pub fn close_device(device: resource) -> null { +pub fn close_device(device: resource) -> () { oauth::close_device(device) } -pub fn close_code(code: resource) -> null { +pub fn close_code(code: resource) -> () { oauth::close_code(code) } -pub fn close_verifier(verifier: resource) -> null { +pub fn close_verifier(verifier: resource) -> () { oauth::close_verifier(verifier) } @@ -54,7 +54,7 @@ pub fn issue_access(policy: resource) -> resource { auth::access_handle("fixture-codex", 4, &policy).handle } -pub fn close_access(access: resource) -> null { +pub fn close_access(access: resource) -> () { auth::close_access(access) } @@ -62,7 +62,7 @@ pub fn issue_refresh(policy: resource) -> resource { auth::refresh_handle("fixture-codex", 4, &policy).handle } -pub fn close_refresh(refresh: resource) -> null { +pub fn close_refresh(refresh: resource) -> () { auth::close_refresh(refresh) } @@ -72,18 +72,21 @@ pub fn save_slots( refresh: resource, expected: int ) -> AuthMetadataEnvelope { + let scopes: array = []; + let metadata: CredentialMetadata = { + provider: "openai-codex", kind: "oauth", source: "synthetic-capture-test", + token_type: "Bearer", expires_at_ms: 1900000000000, scopes: scopes, + account_id: Some("synthetic-capture-account"), generation: expected, + status: "active", last_refresh_at_ms: Some(1790000000000), has_refresh_token: true + }; + let refresh_slot: Option> = Some(refresh); auth::save_if_generation({ credential_id: "fixture-codex", expected_generation: expected, policy_handle: policy, - metadata: { - provider: "openai-codex", kind: "oauth", source: "synthetic-capture-test", - token_type: "Bearer", expires_at_ms: 1900000000000, scopes: [], - account_id: "synthetic-capture-account", generation: expected, - status: "active", last_refresh_at_ms: 1790000000000, has_refresh_token: true - }, + metadata: metadata, access_slot: access, refresh_action: "replace", - refresh_slot: refresh + refresh_slot: refresh_slot }) } diff --git a/rss/auth/code_exchange_contract.rss b/rss/auth/code_exchange_contract.rss index 9677374..9ce15ee 100644 --- a/rss/auth/code_exchange_contract.rss +++ b/rss/auth/code_exchange_contract.rss @@ -20,7 +20,14 @@ pub fn exchange( code: resource, verifier: resource ) -> TransportResponseEnvelope { - let usage = {kind: "code_verifier", code_handle: code, verifier_handle: verifier}; + let usage: TransportCredentialUse = { + kind: "code_verifier", + access_handle: None, + refresh_handle: None, + code_handle: Some(code), + verifier_handle: Some(verifier), + device_handle: None + }; oauth::transport(&request, &usage) } @@ -28,18 +35,18 @@ pub fn save(request: SaveCredentialRequest) -> AuthMetadataEnvelope { auth::save_if_generation(&request) } -pub fn close_callback(callback: resource) -> null { +pub fn close_callback(callback: resource) -> () { oauth::close_callback(callback) } -pub fn close_verifier(verifier: resource) -> null { +pub fn close_verifier(verifier: resource) -> () { oauth::close_verifier(verifier) } -pub fn close_code(code: resource) -> null { +pub fn close_code(code: resource) -> () { oauth::close_code(code) } -pub fn close_slot(slot: resource) -> null { +pub fn close_slot(slot: resource) -> () { auth::close_slot(slot) } diff --git a/rss/auth/codex_device.rss b/rss/auth/codex_device.rss index 4d51b9b..3c2c195 100644 --- a/rss/auth/codex_device.rss +++ b/rss/auth/codex_device.rss @@ -21,7 +21,7 @@ struct CodexDeviceLoginResult { ok: bool, code: string, message: string, - metadata: AuthMetadataEnvelope? + metadata: Option } @@ -79,13 +79,13 @@ struct DeviceStart { struct DeviceStartState { policy_handle: resource, - device_handle: resource?, + device_handle: Option>, start: DeviceStart } struct DevicePollState { policy_handle: resource, - device_handle: resource?, + device_handle: Option>, user_code: string, interval_ms: int } @@ -95,8 +95,8 @@ struct DevicePoll { last_ms: int, ok: bool, failure: CodexDeviceLoginResult, - code_handle: resource?, - verifier_handle: resource? + code_handle: Option>, + verifier_handle: Option> } struct DeviceStartJson { @@ -126,8 +126,147 @@ fn min_interval_ms() -> int { 1000 } fn max_interval_ms() -> int { 60000 } fn max_int() -> int { 9223372036854775807 } +// --------------------------------------------------------------------------- +// Logical Option carriers +// --------------------------------------------------------------------------- + +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn int_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn oauth_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn envelope_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn field_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_failure() -> Option { + None +} + +fn host_failure_bridge() -> AuthBridgeFailure { + {code: "host_failure", cancelled: false, deadline_exceeded: false, revoked: false} +} + +fn no_text() -> Option { + None +} + +fn no_int() -> Option { + None +} + +fn no_envelope() -> Option { + None +} + +fn no_oauth() -> Option { + None +} + +fn no_scopes() -> Option> { + None +} + +fn no_query_entries() -> Option> { + None +} + +/// The absent provider OAuth block, materialized only where the caller has +/// already refused the credential for having none. +fn empty_oauth_config() -> OAuthConfig { + { + flow: no_text(), + issuer: no_text(), + client_id: no_text(), + device_user_code_path: no_text(), + device_poll_path: no_text(), + authorization_path: no_text(), + token_endpoint: no_text(), + redirect_uri: no_text(), + refresh_skew_seconds: 0, + scopes: no_scopes(), + authorization_parameters: no_query_entries() + } +} + +fn no_code() -> Option> { + None +} + +fn no_verifier() -> Option> { + None +} + +fn no_device() -> Option> { + None +} + +fn no_slot() -> Option> { + None +} + +fn no_access() -> Option> { + None +} + +fn no_refresh() -> Option> { + None +} + +fn empty_field() -> PublicJsonFieldEnvelope { + { + kind: "", + string_value: no_text(), + int_value: no_int(), + float_value: no_float(), + bool_value: no_bool(), + length: no_int() + } +} + +fn no_float() -> Option { + None +} + +fn no_bool() -> Option { + None +} + fn failed(code: string, message: string) -> CodexDeviceLoginResult { - {ok: false, code: code, message: message, metadata: null} + {ok: false, code: code, message: message, metadata: no_envelope()} } fn empty_instruction() -> CodexDeviceInstruction { @@ -135,43 +274,52 @@ fn empty_instruction() -> CodexDeviceInstruction { } fn empty_transport_response() -> TransportResponseEnvelope { - {ok: false, error: null, status: 0, public_headers: [], body_without_secret_fields: "", retry_after_ms: null, access_slot: null, refresh_slot: null, device_handle: null} + let access_slot: Option> = None; + let refresh_slot: Option> = None; + let device_handle: Option> = None; + {ok: false, error: no_text(), status: 0, public_headers: [], body_without_secret_fields: "", retry_after_ms: no_int(), access_slot: access_slot, refresh_slot: refresh_slot, device_handle: device_handle} } fn empty_transport_capture() -> TransportCaptureEnvelope { - {ok: false, error: null, response: empty_transport_response(), code_handle: null, verifier_handle: null} + {ok: false, error: no_text(), response: empty_transport_response(), code_handle: no_code(), verifier_handle: no_verifier()} } fn empty_auth_metadata() -> AuthMetadataEnvelope { + let account_id: Option = None; + let last_refresh_at_ms: Option = None; { ok: false, - error: null, - metadata: {credential_id: "", provider: "", kind: "", source: "", token_type: "", expires_at_ms: 0, scopes: [], account_id: null, generation: 0, status: "", last_refresh_at_ms: null, has_refresh_token: false}, + error: no_text(), + metadata: {credential_id: "", provider: "", kind: "", source: "", token_type: "", expires_at_ms: 0, scopes: [], account_id: account_id, generation: 0, status: "", last_refresh_at_ms: last_refresh_at_ms, has_refresh_token: false}, outcome: "" } } fn succeeded(metadata: AuthMetadataEnvelope) -> CodexDeviceLoginResult { - {ok: true, code: "ok", message: "Codex device login completed.", metadata: metadata} + {ok: true, code: "ok", message: "Codex device login completed.", metadata: Some(metadata)} } -fn static_failure(error_code: AuthBridgeFailure?, stage_code: string, stage_message: string) -> CodexDeviceLoginResult { +fn static_failure(error_code: Option, stage_code: string, stage_message: string) -> CodexDeviceLoginResult { + match error_code { + Some(detail) => mapped_static_failure(detail, stage_code, stage_message), + None => failed(stage_code, stage_message) + } +} + +fn mapped_static_failure(detail: AuthBridgeFailure, stage_code: string, stage_message: string) -> CodexDeviceLoginResult { let mut code: string = stage_code; let mut message: string = stage_message; - if error_code != null { - let detail: AuthBridgeFailure = error_code; - if detail.code.copy() == "policy_inactive" { - code = "cancelled_or_inactive"; - message = "The login was cancelled or is no longer active."; - } - if detail.code.copy() == "wait_deadline_exceeded" { - code = "deadline_or_inactive"; - message = "The login wait ended at an inactive deadline."; - } - if detail.code.copy() == "clock_unavailable" { - code = "clock_unavailable"; - message = "The login clock is unavailable."; - } + if detail.code.copy() == "policy_inactive" { + code = "cancelled_or_inactive"; + message = "The login was cancelled or is no longer active."; + } + if detail.code.copy() == "wait_deadline_exceeded" { + code = "deadline_or_inactive"; + message = "The login wait ended at an inactive deadline."; + } + if detail.code.copy() == "clock_unavailable" { + code = "clock_unavailable"; + message = "The login clock is unavailable."; } failed(code, message) } @@ -316,15 +464,15 @@ fn trim_trailing_slashes(value: string) -> string { value[0:end] } -fn configured(value: string?, fallback: string) -> string { - let mut selected: string = fallback; - if value != null { - let actual: string = value; - if actual != "" { - selected = actual; - } +fn configured(value: Option, fallback: string) -> string { + match value { + Some(actual) => if actual != "" => { + actual + } else => { + fallback + }, + None => fallback } - selected } fn invalid_endpoints(message: string) -> DeviceEndpointChoice { @@ -345,16 +493,16 @@ fn invalid_endpoints(message: string) -> DeviceEndpointChoice { fn endpoint_choice(provider: ProviderEntry, credential_id: string, expected_generation: int) -> DeviceEndpointChoice { if expected_generation < 0 || credential_id == "" => { invalid_endpoints("The selected credential request is invalid.") - } else if provider.auth == null => { + } else if !text_present(provider.auth) => { invalid_endpoints("The selected provider does not match the credential.") - } else if provider.oauth == null => { + } else if !oauth_present(provider.oauth) => { invalid_endpoints("The selected provider has no device-login configuration.") } else => { - let selected_id: string = provider.auth; - let config: OAuthConfig = provider.oauth; + let selected_id: string = provider.auth.unwrap_or(""); + let config: OAuthConfig = provider.oauth.unwrap_or(empty_oauth_config()); if selected_id != credential_id => { invalid_endpoints("The selected provider does not match the credential.") - } else if config.flow != "codex-device" => { + } else if config.flow.unwrap_or("") != "codex-device" => { invalid_endpoints("The selected provider does not support Codex device login.") } else => { let issuer: string = configured(config.issuer, default_issuer()); @@ -420,14 +568,14 @@ fn clock_check_value(read: OAuthNowResultEnvelope, deadline_ms: int, previous_ms now_ms: previous_ms, failure: static_failure(read.error, "clock_unavailable", "The login clock is unavailable.") } - } else if read.value.copy() == null => { + } else if !int_present(read.value.copy()) => { { ok: false, now_ms: previous_ms, failure: failed("clock_unavailable", "The login clock is unavailable.") } } else => { - let current_ms: int = read.value.copy(); + let current_ms: int = read.value.copy().unwrap_or(-1); if current_ms < 0 || current_ms < previous_ms => { { ok: false, @@ -447,14 +595,14 @@ fn clock_check_value(read: OAuthNowResultEnvelope, deadline_ms: int, previous_ms } fn interval_from_field(field: PublicJsonFieldResultEnvelope) -> DeviceInterval { - if !field.ok || field.value.copy() == null => { + if !field.ok || !field_present(field.value.copy()) => { {ok: false, milliseconds: 0} } else => { - let parsed: PublicJsonFieldEnvelope = field.value.copy(); + let parsed: PublicJsonFieldEnvelope = field.value.copy().unwrap_or(empty_field()); if parsed.kind.copy() == "missing" => { {ok: true, milliseconds: default_interval_ms()} - } else if parsed.kind.copy() == "int" && parsed.int_value.copy() != null => { - let seconds: int = parsed.int_value.copy(); + } else if parsed.kind.copy() == "int" && int_present(parsed.int_value.copy()) => { + let seconds: int = parsed.int_value.copy().unwrap_or(0); if seconds < 0 => { {ok: false, milliseconds: 0} } else if seconds == 0 => { @@ -464,8 +612,8 @@ fn interval_from_field(field: PublicJsonFieldResultEnvelope) -> DeviceInterval { } else => { {ok: true, milliseconds: seconds * 1000} } - } else if parsed.kind.copy() == "string" && parsed.string_value.copy() != null => { - let text: string = parsed.string_value.copy(); + } else if parsed.kind.copy() == "string" && text_present(parsed.string_value.copy()) => { + let text: string = parsed.string_value.copy().unwrap_or(""); parse_interval_string(text) } else => { {ok: false, milliseconds: 0} @@ -530,21 +678,21 @@ fn valid_user_code(value: string) -> bool { fn user_code_from_body(primary: PublicJsonFieldResultEnvelope, legacy: PublicJsonFieldResultEnvelope) -> DeviceStart { let mut selected: string = ""; let mut valid: bool = false; - if primary.ok && primary.value.copy() != null && legacy.ok && legacy.value.copy() != null { - let primary_value: PublicJsonFieldEnvelope = primary.value.copy(); - let legacy_value: PublicJsonFieldEnvelope = legacy.value.copy(); - if primary_value.kind.copy() == "string" && primary_value.string_value.copy() != null { - selected = primary_value.string_value.copy(); + if primary.ok && field_present(primary.value.copy()) && legacy.ok && field_present(legacy.value.copy()) { + let primary_value: PublicJsonFieldEnvelope = primary.value.copy().unwrap_or(empty_field()); + let legacy_value: PublicJsonFieldEnvelope = legacy.value.copy().unwrap_or(empty_field()); + if primary_value.kind.copy() == "string" && text_present(primary_value.string_value.copy()) { + selected = primary_value.string_value.copy().unwrap_or(""); valid = true; if legacy_value.kind.copy() != "missing" { - if legacy_value.kind.copy() != "string" || legacy_value.string_value.copy() == null { + if legacy_value.kind.copy() != "string" || !text_present(legacy_value.string_value.copy()) { valid = false; - } else if legacy_value.string_value.copy() != selected.copy() { + } else if legacy_value.string_value.copy().unwrap_or("") != selected.copy() { valid = false; } } - } else if primary_value.kind.copy() == "missing" && legacy_value.kind.copy() == "string" && legacy_value.string_value.copy() != null { - selected = legacy_value.string_value.copy(); + } else if primary_value.kind.copy() == "missing" && legacy_value.kind.copy() == "string" && text_present(legacy_value.string_value.copy()) { + selected = legacy_value.string_value.copy().unwrap_or(""); valid = true; } } @@ -560,14 +708,14 @@ fn interval_from_body(field: PublicJsonFieldResultEnvelope) -> DeviceInterval { } fn start_expiry(field: PublicJsonFieldResultEnvelope) -> int { - if !field.ok || field.value.copy() == null => { + if !field.ok || !field_present(field.value.copy()) => { 0 } else => { - let parsed: PublicJsonFieldEnvelope = field.value.copy(); + let parsed: PublicJsonFieldEnvelope = field.value.copy().unwrap_or(empty_field()); if parsed.kind.copy() == "missing" => { 900 - } else if parsed.kind.copy() == "int" && parsed.int_value.copy() != null => { - let seconds: int = parsed.int_value.copy(); + } else if parsed.kind.copy() == "int" && int_present(parsed.int_value.copy()) => { + let seconds: int = parsed.int_value.copy().unwrap_or(0); if seconds > 0 => { if seconds > 900 => { 900 } else => { seconds } } else => { 0 } @@ -575,27 +723,32 @@ fn start_expiry(field: PublicJsonFieldResultEnvelope) -> int { } } -struct DeviceHandlePresence { - handle: resource? +/// One device-session carrier plus its presence. The handle is move-only, so the +/// carrier is taken apart once and handed back inside this shell. +struct DevicePeek { + present: bool, + handle: Option> +} + +fn peek_device(value: Option>) -> DevicePeek { + match value { + Some(handle) => {present: true, handle: Some(handle)}, + None => {present: false, handle: no_device()} + } } struct DeviceStartResponseParts { status: int, ok: bool, body: string, - device: DeviceHandlePresence + device: Option> } fn device_start_response_parts(response: TransportResponseEnvelope) -> DeviceStartResponseParts { - let has_device: bool = response.has("device_handle"); - let device: DeviceHandlePresence = if has_device => { - {handle: response.device_handle} - } else => { - {handle: null} - }; let response_status: int = response.status; let response_ok: bool = response.ok; let response_body: string = response.body_without_secret_fields; + let device: Option> = response.device_handle; {status: response_status, ok: response_ok, body: response_body, device: device} } @@ -610,16 +763,20 @@ fn device_start(policy: resource, credential_id: string, endpoints: public_headers: [{name: "content-type", value: "application/json"}], public_body: public_body }; - let usage: TransportCredentialUse = {kind: "none", access_handle: null, refresh_handle: null, code_handle: null, verifier_handle: null, device_handle: null}; + let usage: TransportCredentialUse = {kind: "none", access_handle: no_access(), refresh_handle: no_refresh(), code_handle: no_code(), verifier_handle: no_verifier(), device_handle: no_device()}; let response: TransportResultEnvelope = oauth::transport_result(&request, &usage); - let response_value: TransportResponseEnvelope? = response.value; - let value: TransportResponseEnvelope = response_value.unwrap_or(empty_transport_response()); - let parts: DeviceStartResponseParts = device_start_response_parts(value); + let response_value: Option = response.value; + // The response frame owns live slot handles, so it is consumed by the match + // rather than through an eager `unwrap_or` fallback. + let parts: DeviceStartResponseParts = match response_value { + Some(value) => device_start_response_parts(value), + None => device_start_response_parts(empty_transport_response()) + }; let response_status: int = parts.status; let value_ok: bool = parts.ok; let start_response_body: string = parts.body; - let device_holder: DeviceHandlePresence = parts.device; - let has_device_handle: bool = device_holder.has("handle"); + let device_handle_peek: DevicePeek = peek_device(parts.device); + let has_device_handle: bool = device_handle_peek.present; let start: DeviceStart = if !response.ok => { {ok: false, failure: static_failure(response.error, "start_transport_failed", "Device authorization could not be started."), user_code: "", interval_ms: 0, expires_in_seconds: 0, verification_url: "", instruction: empty_instruction()} } else => { @@ -648,118 +805,103 @@ fn device_start(policy: resource, credential_id: string, endpoints: } } }; - let response_device_handle: resource? = device_holder?.handle; + let response_device_handle: Option> = device_handle_peek.handle; {policy_handle: request.policy_handle, device_handle: response_device_handle, start: start} } -struct SecretSlotPresence { - handle: resource? +struct SlotPeek { + present: bool, + handle: Option> } -struct CodeHandlePresence { - handle: resource? +fn peek_slot(value: Option>) -> SlotPeek { + match value { + Some(handle) => {present: true, handle: Some(handle)}, + None => {present: false, handle: no_slot()} + } } -struct VerifierHandlePresence { - handle: resource? +struct CodePeek { + present: bool, + handle: Option> +} + +fn peek_code(value: Option>) -> CodePeek { + match value { + Some(handle) => {present: true, handle: Some(handle)}, + None => {present: false, handle: no_code()} + } +} + +struct VerifierPeek { + present: bool, + handle: Option> +} + +fn peek_verifier(value: Option>) -> VerifierPeek { + match value { + Some(handle) => {present: true, handle: Some(handle)}, + None => {present: false, handle: no_verifier()} + } } struct DeviceCaptureParts { ok: bool, response: TransportResponseEnvelope, - code: CodeHandlePresence, - verifier: VerifierHandlePresence + code: Option>, + verifier: Option> } fn device_capture_parts(envelope: TransportCaptureEnvelope) -> DeviceCaptureParts { - let has_code: bool = envelope.has("code_handle"); - let has_verifier: bool = envelope.has("verifier_handle"); let capture_ok: bool = envelope.ok; let response: TransportResponseEnvelope = envelope.response; - if has_code && has_verifier => { - {ok: capture_ok, response: response, code: {handle: envelope.code_handle}, verifier: {handle: envelope.verifier_handle}} - } else if has_code => { - {ok: capture_ok, response: response, code: {handle: envelope.code_handle}, verifier: {handle: null}} - } else if has_verifier => { - {ok: capture_ok, response: response, code: {handle: null}, verifier: {handle: envelope.verifier_handle}} - } else => { - {ok: capture_ok, response: response, code: {handle: null}, verifier: {handle: null}} - } + let code: Option> = envelope.code_handle; + let verifier: Option> = envelope.verifier_handle; + {ok: capture_ok, response: response, code: code, verifier: verifier} } struct ExchangeResponseParts { status: int, ok: bool, body: string, - access: SecretSlotPresence, - refresh: SecretSlotPresence + access: Option>, + refresh: Option> } struct PollResponseParts { status: int, has_retry_after: bool, retry_after_ms: int, - access: SecretSlotPresence, - refresh: SecretSlotPresence, - device: DeviceHandlePresence + access: Option>, + refresh: Option>, + device: Option> } fn exchange_response_parts(response: TransportResponseEnvelope) -> ExchangeResponseParts { - let has_access: bool = response.has("access_slot"); - let has_refresh: bool = response.has("refresh_slot"); let response_status: int = response.status; let response_ok: bool = response.ok; let response_body: string = response.body_without_secret_fields; - if has_access && has_refresh => { - {status: response_status, ok: response_ok, body: response_body, access: {handle: response.access_slot}, refresh: {handle: response.refresh_slot}} - } else if has_access => { - {status: response_status, ok: response_ok, body: response_body, access: {handle: response.access_slot}, refresh: {handle: null}} - } else if has_refresh => { - {status: response_status, ok: response_ok, body: response_body, access: {handle: null}, refresh: {handle: response.refresh_slot}} - } else => { - {status: response_status, ok: response_ok, body: response_body, access: {handle: null}, refresh: {handle: null}} - } + let access: Option> = response.access_slot; + let refresh: Option> = response.refresh_slot; + {status: response_status, ok: response_ok, body: response_body, access: access, refresh: refresh} } fn poll_response_parts(response: TransportResponseEnvelope) -> PollResponseParts { - let has_access: bool = response.has("access_slot"); - let has_refresh: bool = response.has("refresh_slot"); - let has_device: bool = response.has("device_handle"); - let retry_after_field_present: bool = response.has("retry_after_ms"); let response_status: int = response.status; - let mut has_retry_after: bool = false; - let mut retry_after: int = 0; - if retry_after_field_present { - let retry_after_value: int? = response.retry_after_ms; - if retry_after_value != null { - has_retry_after = true; - retry_after = retry_after_value.copy(); - } - } - if has_access && has_refresh && has_device => { - {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: {handle: response.access_slot}, refresh: {handle: response.refresh_slot}, device: {handle: response.device_handle}} - } else if has_access && has_refresh => { - {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: {handle: response.access_slot}, refresh: {handle: response.refresh_slot}, device: {handle: null}} - } else if has_access && has_device => { - {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: {handle: response.access_slot}, refresh: {handle: null}, device: {handle: response.device_handle}} - } else if has_refresh && has_device => { - {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: {handle: null}, refresh: {handle: response.refresh_slot}, device: {handle: response.device_handle}} - } else if has_access => { - {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: {handle: response.access_slot}, refresh: {handle: null}, device: {handle: null}} - } else if has_refresh => { - {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: {handle: null}, refresh: {handle: response.refresh_slot}, device: {handle: null}} - } else if has_device => { - {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: {handle: null}, refresh: {handle: null}, device: {handle: response.device_handle}} - } else => { - {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: {handle: null}, refresh: {handle: null}, device: {handle: null}} - } + let retry_after_value: Option = response.retry_after_ms; + let has_retry_after: bool = int_present(retry_after_value); + let retry_after: int = retry_after_value.unwrap_or(0); + let access: Option> = response.access_slot; + let refresh: Option> = response.refresh_slot; + let device: Option> = response.device_handle; + {status: response_status, has_retry_after: has_retry_after, retry_after_ms: retry_after, access: access, refresh: refresh, device: device} } fn poll_until_complete(state: DevicePollState, credential_id: string, endpoints: DeviceEndpointChoice, deadline_ms: int, last_ms: int) -> DevicePoll { let mut complete: bool = false; let mut keep_polling: bool = true; - let mut code_handle: resource? = null; - let mut verifier_handle: resource? = null; + let mut code_handle: Option> = no_code(); + let mut verifier_handle: Option> = no_verifier(); let mut failure: CodexDeviceLoginResult = failed("poll_failed", "Codex device authorization did not complete."); let base_interval_ms: int = state.interval_ms; let mut retry_delay_ms: int = base_interval_ms; @@ -776,7 +918,7 @@ fn poll_until_complete(state: DevicePollState, credential_id: string, endpoints: public_headers: [{name: "content-type", value: "application/json"}], public_body: public_body }; - let usage: TransportCredentialUse = {kind: "device_session", access_handle: null, refresh_handle: null, code_handle: null, verifier_handle: null, device_handle: state.device_handle}; + let usage: TransportCredentialUse = {kind: "device_session", access_handle: no_access(), refresh_handle: no_refresh(), code_handle: no_code(), verifier_handle: no_verifier(), device_handle: state.device_handle}; while keep_polling { if !first_poll { let before_wait: DeviceClockCheck = clock_check_value(oauth::now_ms_result(&request.policy_handle), deadline_ms, current_ms); @@ -818,30 +960,32 @@ fn poll_until_complete(state: DevicePollState, credential_id: string, endpoints: } } if keep_polling { - let payload: TransportPayloadIntent = {encoding: "json_object", secret_fields: "device", device_field: "device_auth_id", code_field: null}; + let payload: TransportPayloadIntent = {encoding: "json_object", secret_fields: "device", device_field: "device_auth_id", code_field: ""}; let capture: TransportCaptureIntent = {code_field: "authorization_code", redirect_uri: endpoints.redirect_uri}; let polled: TransportCaptureResultEnvelope = oauth::transport_capture_result(&request, &usage, payload, capture); - let polled_value: TransportCaptureEnvelope? = polled.value; - let envelope: TransportCaptureEnvelope = polled_value.unwrap_or(empty_transport_capture()); - let parts: DeviceCaptureParts = device_capture_parts(envelope); + let polled_value: Option = polled.value; + let parts: DeviceCaptureParts = match polled_value { + Some(envelope) => device_capture_parts(envelope), + None => device_capture_parts(empty_transport_capture()) + }; let envelope_ok: bool = parts.ok; let response: TransportResponseEnvelope = parts.response; let response_parts: PollResponseParts = poll_response_parts(response); - let code_holder: CodeHandlePresence = parts.code; - let verifier_holder: VerifierHandlePresence = parts.verifier; - let has_code: bool = code_holder.has("handle"); - let has_verifier: bool = verifier_holder.has("handle"); - let captured_code: resource? = code_holder?.handle; - let captured_verifier: resource? = verifier_holder?.handle; + let code_peek: CodePeek = peek_code(parts.code); + let verifier_peek: VerifierPeek = peek_verifier(parts.verifier); + let has_code: bool = code_peek.present; + let has_verifier: bool = verifier_peek.present; + let captured_code: Option> = code_peek.handle; + let captured_verifier: Option> = verifier_peek.handle; let response_status: int = response_parts.status; let retry_after_present: bool = response_parts.has_retry_after; let retry_after_value: int = response_parts.retry_after_ms; - let response_access_holder: SecretSlotPresence = response_parts.access; - let response_refresh_holder: SecretSlotPresence = response_parts.refresh; - let response_device_holder: DeviceHandlePresence = response_parts.device; - let has_response_access: bool = response_access_holder.has("handle"); - let has_response_refresh: bool = response_refresh_holder.has("handle"); - let has_response_device: bool = response_device_holder.has("handle"); + let response_access_peek: SlotPeek = peek_slot(response_parts.access); + let response_refresh_peek: SlotPeek = peek_slot(response_parts.refresh); + let response_device_peek: DevicePeek = peek_device(response_parts.device); + let has_response_access: bool = response_access_peek.present; + let has_response_refresh: bool = response_refresh_peek.present; + let has_response_device: bool = response_device_peek.present; let after: DeviceClockCheck = clock_check_value(oauth::now_ms_result(&request.policy_handle), deadline_ms, current_ms); current_ms = after.now_ms.copy(); if !after.ok { @@ -875,7 +1019,7 @@ fn poll_until_complete(state: DevicePollState, credential_id: string, endpoints: next_poll_delay_ms = requested; keep_polling = true; } else if response_status.copy() >= 200 && response_status.copy() < 300 { - if !has_code || !has_verifier || captured_code == null || captured_verifier == null || + if !has_code || !has_verifier || has_response_access || has_response_refresh || has_response_device { failure = failed("invalid_poll_response", "The device authorization response is invalid."); keep_polling = false; @@ -895,7 +1039,7 @@ fn poll_until_complete(state: DevicePollState, credential_id: string, endpoints: if complete => { {policy_handle: request.policy_handle, last_ms: current_ms, ok: true, failure: failed("", ""), code_handle: code_handle, verifier_handle: verifier_handle} } else => { - {policy_handle: request.policy_handle, last_ms: current_ms, ok: false, failure: failure, code_handle: null, verifier_handle: null} + {policy_handle: request.policy_handle, last_ms: current_ms, ok: false, failure: failure, code_handle: no_code(), verifier_handle: no_verifier()} } } @@ -912,7 +1056,100 @@ fn safe_form_client_id(value: string) -> bool { valid } -fn exchange_and_save(details: DeviceLoginDetails, endpoints: DeviceEndpointChoice, policy: resource, last_ms: int, code_handle: resource?, verifier_handle: resource?, deadline_ms: int) -> DeviceLoginOutcome { +fn exchange_present( + details: DeviceLoginDetails, + policy: resource, + token_response_body: string, + now_ms: int, + has_refresh: bool, + access_slot: resource, + refresh_slot: Option>, + deadline_ms: int +) -> CodexDeviceLoginResult { + let expiry_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy, token_response_body.copy(), "/expires_in"); + let token_type_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy, token_response_body.copy(), "/token_type"); + let mut expires_in: int = 0; + let mut token_type: string = "Bearer"; + let expiry_option: Option = expiry_field.value.copy(); + let token_type_option: Option = token_type_field.value.copy(); + let mut valid: bool = expiry_field.ok && field_present(expiry_option) && token_type_field.ok && field_present(token_type_option); + if valid { + let expiry: PublicJsonFieldEnvelope = expiry_option.unwrap_or(empty_field()); + if expiry.kind == "int" && int_present(expiry.int_value.copy()) { + expires_in = expiry.int_value.copy().unwrap_or(0); + if expires_in <= 0 || expires_in > max_int() / 1000 { + valid = false; + } + } else { + valid = false; + } + let token_type_value: PublicJsonFieldEnvelope = token_type_option.unwrap_or(empty_field()); + if token_type_value.kind.copy() == "missing" { + token_type = "Bearer"; + } else if token_type_value.kind == "string" && text_present(token_type_value.string_value.copy()) { + token_type = token_type_value.string_value.copy().unwrap_or(""); + if token_type != "Bearer" || token_type.length > 64 { + valid = false; + } + } else { + valid = false; + } + } + let mut outcome: CodexDeviceLoginResult = failed("invalid_token_response", "The token response is invalid."); + if valid { + let lifetime_ms: int = expires_in * 1000; + if now_ms > max_int() - lifetime_ms { + valid = false; + } else { + let expires_at_ms: int = now_ms + lifetime_ms; + let mut refresh_action: string = "clear"; + if has_refresh { refresh_action = "replace"; } + let metadata: CredentialMetadata = { + provider: details.provider.name, + kind: "oauth", + source: "codex-device", + token_type: token_type, + expires_at_ms: expires_at_ms, + scopes: [], + account_id: no_text(), + generation: details.expected_generation, + status: "active", + last_refresh_at_ms: no_int(), + has_refresh_token: has_refresh + }; + let save_request: SaveCredentialRequest = { + credential_id: details.credential_id, + expected_generation: details.expected_generation, + policy_handle: policy, + metadata: metadata, + access_slot: access_slot, + refresh_action: refresh_action, + refresh_slot: refresh_slot + }; + let save_check: DeviceClockCheck = clock_check_value(oauth::now_ms_result(&save_request.policy_handle), deadline_ms, now_ms); + if !save_check.ok { + outcome = save_check.failure; + } else { + let saved: AuthSaveResultEnvelope = auth::save_if_generation_result(&save_request); + let saved_metadata: Option = saved.value; + let has_saved_metadata: bool = envelope_present(saved_metadata); + let saved_value: AuthMetadataEnvelope = saved_metadata.unwrap_or(empty_auth_metadata()); + if !saved.ok { + outcome = static_failure(saved.error, "save_failed", "The credential could not be saved."); + } else if !has_saved_metadata { + outcome = failed("save_failed", "The credential could not be saved."); + } else if !saved_value.ok.copy() { + outcome = failed("save_failed", "The credential could not be saved."); + } else { + outcome = succeeded(saved_value); + } + } + } + } + outcome +} + +fn exchange_and_save(details: DeviceLoginDetails, endpoints: DeviceEndpointChoice, policy: resource, last_ms: int, code_handle: Option>, verifier_handle: Option>, deadline_ms: int) -> DeviceLoginOutcome { let before: DeviceClockCheck = clock_check_value(oauth::now_ms_result(&policy), deadline_ms, last_ms); if !before.ok => { {policy_handle: policy, result: before.failure} @@ -929,21 +1166,23 @@ fn exchange_and_save(details: DeviceLoginDetails, endpoints: DeviceEndpointChoic public_headers: [{name: "content-type", value: "application/x-www-form-urlencoded"}], public_body: public_body }; - let usage: TransportCredentialUse = {kind: "code_verifier", access_handle: null, refresh_handle: null, code_handle: code_handle, verifier_handle: verifier_handle, device_handle: null}; - let payload: TransportPayloadIntent = {encoding: "form", secret_fields: "authorization_code", device_field: null, code_field: "code"}; + let usage: TransportCredentialUse = {kind: "code_verifier", access_handle: no_access(), refresh_handle: no_refresh(), code_handle: code_handle, verifier_handle: verifier_handle, device_handle: no_device()}; + let payload: TransportPayloadIntent = {encoding: "form", secret_fields: "authorization_code", device_field: "", code_field: "code"}; let exchanged: TransportResultEnvelope = oauth::transport_payload_result(&transport_request, &usage, payload); - let exchanged_value: TransportResponseEnvelope? = exchanged.value; - let response: TransportResponseEnvelope = exchanged_value.unwrap_or(empty_transport_response()); - let parts: ExchangeResponseParts = exchange_response_parts(response); + let exchanged_value: Option = exchanged.value; + let parts: ExchangeResponseParts = match exchanged_value { + Some(response) => exchange_response_parts(response), + None => exchange_response_parts(empty_transport_response()) + }; let response_status: int = parts.status; let response_ok: bool = parts.ok; let token_response_body: string = parts.body; - let access_holder: SecretSlotPresence = parts.access; - let refresh_holder: SecretSlotPresence = parts.refresh; - let has_access: bool = access_holder.has("handle"); - let has_refresh: bool = refresh_holder.has("handle"); - let response_access_slot: resource? = access_holder?.handle; - let response_refresh_slot: resource? = refresh_holder?.handle; + let access_peek: SlotPeek = peek_slot(parts.access); + let refresh_peek: SlotPeek = peek_slot(parts.refresh); + let has_access: bool = access_peek.present; + let has_refresh: bool = refresh_peek.present; + let response_access_slot: Option> = access_peek.handle; + let response_refresh_slot: Option> = refresh_peek.handle; let after: DeviceClockCheck = clock_check_value(oauth::now_ms_result(&transport_request.policy_handle), deadline_ms, before.now_ms); let mut result_policy: resource = transport_request.policy_handle; let mut exchange_result: CodexDeviceLoginResult = failed("exchange_transport_failed", "Codex device token exchange failed."); @@ -958,115 +1197,57 @@ fn exchange_and_save(details: DeviceLoginDetails, endpoints: DeviceEndpointChoic } else if !has_access { exchange_result = failed("invalid_token_response", "The token response is invalid."); } else { - let access_slot: resource? = response_access_slot; - let refresh_slot: resource? = response_refresh_slot; - let expiry_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&result_policy, token_response_body.copy(), "/expires_in"); - let token_type_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&result_policy, token_response_body.copy(), "/token_type"); - let mut expires_in: int = 0; - let mut token_type: string = "Bearer"; - let mut valid: bool = expiry_field.ok && expiry_field.value.copy() != null && token_type_field.ok && token_type_field.value.copy() != null; - if valid { - let expiry: PublicJsonFieldEnvelope = expiry_field.value; - if expiry.kind == "int" && expiry.int_value.copy() != null { - expires_in = expiry.int_value; - if expires_in <= 0 || expires_in > max_int() / 1000 { - valid = false; - } - } else { - valid = false; - } - let token_type_value: PublicJsonFieldEnvelope = token_type_field.value; - if token_type_value.kind.copy() == "missing" { - token_type = "Bearer"; - } else if token_type_value.kind == "string" && token_type_value.string_value.copy() != null { - token_type = token_type_value.string_value; - if token_type != "Bearer" || token_type.length > 64 { - valid = false; - } - } else { - valid = false; - } - } - let mut outcome: CodexDeviceLoginResult = failed("invalid_token_response", "The token response is invalid."); - if valid { - let lifetime_ms: int = expires_in * 1000; - if after.now_ms.copy() > max_int() - lifetime_ms { - valid = false; - } else { - let expires_at_ms: int = after.now_ms.copy() + lifetime_ms; - let mut refresh_action: string = "clear"; - if has_refresh.copy() { refresh_action = "replace"; } - let metadata: CredentialMetadata = { - provider: details.provider.name, - kind: "oauth", - source: "codex-device", - token_type: token_type, - expires_at_ms: expires_at_ms, - scopes: [], - account_id: null, - generation: details.expected_generation, - status: "active", - last_refresh_at_ms: null, - has_refresh_token: has_refresh - }; - let save_request: SaveCredentialRequest = { - credential_id: details.credential_id, - expected_generation: details.expected_generation, - policy_handle: result_policy, - metadata: metadata, - access_slot: access_slot, - refresh_action: refresh_action, - refresh_slot: refresh_slot - }; - let save_check: DeviceClockCheck = clock_check_value(oauth::now_ms_result(&save_request.policy_handle), deadline_ms, after.now_ms.copy()); - if !save_check.ok { - outcome = save_check.failure; - } else { - let saved: AuthSaveResultEnvelope = auth::save_if_generation_result(&save_request); - let saved_metadata: AuthMetadataEnvelope? = saved.value; - let has_saved_metadata: bool = saved_metadata != null; - let saved_value: AuthMetadataEnvelope = saved_metadata.unwrap_or(empty_auth_metadata()); - if !saved.ok { - outcome = static_failure(saved.error, "save_failed", "The credential could not be saved."); - } else if !has_saved_metadata { - outcome = failed("save_failed", "The credential could not be saved."); - } else if !saved_value.ok.copy() { - outcome = failed("save_failed", "The credential could not be saved."); - } else { - outcome = succeeded(saved_value); - } - } - result_policy = save_request.policy_handle; - } - } - exchange_result = outcome; + // The access slot is the payload of this exchange: the carrier is + // consumed here and the live handle is handed to the save request. + exchange_result = match response_access_slot { + Some(access_slot) => exchange_present( + details, + result_policy, + token_response_body, + after.now_ms, + has_refresh, + access_slot, + response_refresh_slot, + deadline_ms + ), + None => failed("invalid_token_response", "The token response is invalid.") + }; } {policy_handle: result_policy, result: exchange_result} } } +fn bool_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + fn instruction_acknowledged(result: DeviceInstructionResultEnvelope) -> bool { + let acknowledged_value: Option = result.value.copy(); if result.has("ok") && result.ok.copy() && - (!result.has("error") || result.error.copy() == null) && - result.has("value") && result.value.copy() != null => { - let acknowledged: bool = result.value.copy(); - acknowledged + !failure_present(result.error.copy()) && + bool_present(acknowledged_value) => { + acknowledged_value.unwrap_or(false) } else => { false } } fn instruction_failure(result: DeviceInstructionResultEnvelope) -> CodexDeviceLoginResult { - if result.has("ok") && !result.ok.copy() && result.has("value") && - result.value.copy() == null && result.has("error") && result.error.copy() != null => { - let failure: AuthBridgeFailure = result.error.copy(); + let failed_value: Option = result.value.copy(); + let failure_carrier: Option = result.error.copy(); + if result.has("ok") && !result.ok.copy() && + !bool_present(failed_value) && failure_present(failure_carrier) => { + let failure: AuthBridgeFailure = failure_carrier.unwrap_or(host_failure_bridge()); let code: string = failure.code.copy(); if code.copy() == "ui_unavailable" || code.copy() == "ui_sink_full" || code.copy() == "ui_sink_closed" || code.copy() == "ui_instruction_invalid" => { failed(code.copy(), "The device login instruction could not be displayed.") } else if code.copy() == "policy_inactive" || code.copy() == "wait_deadline_exceeded" || code.copy() == "clock_unavailable" => { - static_failure(result.error.copy(), "instruction_failed", "The device login instruction could not be delivered.") + static_failure(failure_carrier, "instruction_failed", "The device login instruction could not be delivered.") } else => { failed("host_failure", "The device login instruction could not be delivered.") } @@ -1096,10 +1277,10 @@ fn login_inner(request: CodexDeviceLoginRequest, on_instruction: fn(CodexDeviceI let initial: OAuthNowResultEnvelope = oauth::now_ms_result(&policy); if !initial.ok => { {policy_handle: policy, result: static_failure(initial.error, "clock_unavailable", "The login clock is unavailable.")} - } else if initial.value.copy() == null => { + } else if !int_present(initial.value.copy()) => { {policy_handle: policy, result: failed("clock_unavailable", "The login clock is unavailable.")} } else => { - let now_ms: int = initial.value.copy(); + let now_ms: int = initial.value.copy().unwrap_or(-1); if now_ms < 0 || now_ms > max_int() - flow_deadline_ms() => { {policy_handle: policy, result: failed("clock_unavailable", "The login clock is unavailable.")} } else => { @@ -1163,13 +1344,13 @@ pub fn login_with_instruction_result( fn legacy_instruction_ack( instruction: CodexDeviceInstruction, - on_instruction: fn(CodexDeviceInstruction) -> null + on_instruction: fn(CodexDeviceInstruction) -> () ) -> DeviceInstructionResultEnvelope { on_instruction(instruction); - {ok: true, value: true, error: null} + {ok: true, value: Some(true), error: no_failure()} } -pub fn login(request: CodexDeviceLoginRequest, on_instruction: fn(CodexDeviceInstruction) -> null) -> CodexDeviceLoginResult { +pub fn login(request: CodexDeviceLoginRequest, on_instruction: fn(CodexDeviceInstruction) -> ()) -> CodexDeviceLoginResult { let adapter: fn(CodexDeviceInstruction) -> DeviceInstructionResultEnvelope = |instruction| legacy_instruction_ack(instruction, on_instruction.copy()); login_with_instruction_result(request, adapter) diff --git a/rss/auth/commands.rss b/rss/auth/commands.rss index 81ef6c5..84f7e28 100644 --- a/rss/auth/commands.rss +++ b/rss/auth/commands.rss @@ -6,24 +6,24 @@ use self::login::{run as login_run}; struct AuthCommandInput { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } struct AuthEntryResult { ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option } struct CliAuthRow { provider: string, - credential_id: string?, + credential_id: Option, status: string, - source: string?, + source: Option, generation: int } @@ -42,7 +42,7 @@ struct CliLogoutRow { struct ProviderStatus { ok: bool, row: string, - credential_id: string?, + credential_id: Option, category: string, inactive: bool } @@ -52,21 +52,84 @@ struct EffectiveSelection { provider: string, model: string, source: string, - error: string? + error: Option } fn entry( ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option ) -> AuthEntryResult { {ok: ok, operation: operation, credential_id: credential_id, generation: generation, status: status, error: error} } -fn report_row(provider: string, credential_id: string?, status: string, source: string?, generation: int) -> string { +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn envelope_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn mutation_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_text() -> Option { + None +} + +fn host_failure_bridge() -> AuthBridgeFailure { + {code: "host_failure", cancelled: false, deadline_exceeded: false, revoked: false} +} + +fn empty_metadata_envelope() -> AuthMetadataEnvelope { + {ok: false, error: no_text(), metadata: empty_metadata(), outcome: ""} +} + +fn empty_metadata() -> AuthMetadata { + let account_id: Option = None; + let last_refresh_at_ms: Option = None; + { + credential_id: "", + provider: "", + kind: "", + source: "", + token_type: "", + expires_at_ms: 0, + scopes: [], + account_id: account_id, + generation: 0, + status: "", + last_refresh_at_ms: last_refresh_at_ms, + has_refresh_token: false + } +} + +fn empty_mutation() -> RedactedMutationResult { + {ok: false, error: no_text(), deleted: false} +} + +fn report_row(provider: string, credential_id: Option, status: string, source: Option, generation: int) -> string { let row: CliAuthRow = { provider: provider, credential_id: credential_id, @@ -81,37 +144,39 @@ fn unconfigured_status(provider: ProviderEntry) -> ProviderStatus { let name: string = provider.name.copy(); { ok: true, - row: report_row(name.copy(), null, "not_configured", null, 0), - credential_id: null, + row: report_row(name.copy(), no_text(), "not_configured", no_text(), 0), + credential_id: no_text(), category: "", inactive: false } } fn metadata_value_valid(envelope: AuthMetadataEnvelope) -> bool { - envelope.ok.copy() && envelope.error.copy() == null && envelope.outcome.copy() == "loaded" + envelope.ok.copy() && !text_present(envelope.error.copy()) && envelope.outcome.copy() == "loaded" } fn provider_status(provider: ProviderEntry, loaded: AuthMetadataResultEnvelope) -> ProviderStatus { let name: string = provider.name.copy(); - let credential_id: string = provider.auth.copy(); + let credential_id: Option = provider.auth.copy(); + let loaded_failure: Option = loaded.error.copy(); + let loaded_value: Option = loaded.value.copy(); if loaded.ok => { - if loaded.error.copy() == null && loaded.value.copy() != null => { - let envelope: AuthMetadataEnvelope = loaded.value.copy(); + if !failure_present(loaded_failure) && envelope_present(loaded_value) => { + let envelope: AuthMetadataEnvelope = loaded_value.unwrap_or(empty_metadata_envelope()); if metadata_value_valid(envelope.copy()) => { let metadata: AuthMetadata = envelope.metadata.copy(); { ok: true, - row: report_row(name.copy(), metadata.credential_id.copy(), metadata.status.copy(), metadata.source.copy(), metadata.generation.copy()), - credential_id: metadata.credential_id.copy(), + row: report_row(name.copy(), Some(metadata.credential_id.copy()), metadata.status.copy(), Some(metadata.source.copy()), metadata.generation.copy()), + credential_id: Some(metadata.credential_id.copy()), category: "", inactive: metadata.status.copy() == "disabled" || metadata.status.copy() == "reauth_required" } } else => { { ok: false, - row: report_row(name.copy(), credential_id.copy(), "host_failure", null, 0), - credential_id: credential_id.copy(), + row: report_row(name.copy(), credential_id, "host_failure", no_text(), 0), + credential_id: credential_id, category: "host_failure", inactive: false } @@ -119,29 +184,33 @@ fn provider_status(provider: ProviderEntry, loaded: AuthMetadataResultEnvelope) } else => { { ok: false, - row: report_row(name.copy(), credential_id.copy(), "host_failure", null, 0), - credential_id: credential_id.copy(), + row: report_row(name.copy(), credential_id, "host_failure", no_text(), 0), + credential_id: credential_id, category: "host_failure", inactive: false } } } else => { - if loaded.error.copy() != null && loaded.value.copy() == null => { - let failure: AuthBridgeFailure = loaded.error.copy(); - let code: string = failure.code.copy(); + if failure_present(loaded_failure) && !envelope_present(loaded_value) => { + // The native failure carrier is consumed once: the code is the only + // fact this projection needs. + let code: string = match loaded_failure { + Some(failure) => failure.code.copy(), + None => "host_failure" + }; if code.copy() == "credential_not_found" => { { ok: true, - row: report_row(name.copy(), credential_id.copy(), "missing", null, 0), - credential_id: credential_id.copy(), + row: report_row(name.copy(), credential_id, "missing", no_text(), 0), + credential_id: credential_id, category: "", inactive: true } } else => { { ok: false, - row: report_row(name.copy(), credential_id.copy(), code.copy(), null, 0), - credential_id: credential_id.copy(), + row: report_row(name.copy(), credential_id, code.copy(), no_text(), 0), + credential_id: credential_id, category: code.copy(), inactive: false } @@ -149,8 +218,8 @@ fn provider_status(provider: ProviderEntry, loaded: AuthMetadataResultEnvelope) } else => { { ok: false, - row: report_row(name.copy(), credential_id.copy(), "host_failure", null, 0), - credential_id: credential_id.copy(), + row: report_row(name.copy(), credential_id, "host_failure", no_text(), 0), + credential_id: credential_id, category: "host_failure", inactive: false } @@ -178,24 +247,24 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt if operation.copy() == "auth.login" => { login_run(home, command) } else if operation.copy() == "auth.status" => { - if command.credential_id.copy() != null => { - entry(false, operation.copy(), null, 0, "invalid_command", "invalid_command") + if text_present(command.credential_id.copy()) => { + entry(false, operation.copy(), no_text(), 0, "invalid_command", Some("invalid_command")) } else => { let snapshot = config::load_snapshot(&home); let generation: int = snapshot.policy_generation; let policy: resource = snapshot.policy_handle; let public_config = snapshot.public_config; let providers = public_config.providers.copy(); - if command.provider.copy() == null => { + if !text_present(command.provider.copy()) => { let mut index: int = 0; let mut report: string = ""; let mut failure: string = ""; while index < providers.length { let provider: ProviderEntry = providers[index].copy(); - let state: ProviderStatus = if provider.auth.copy() == null => { + let state: ProviderStatus = if !text_present(provider.auth.copy()) => { unconfigured_status(provider.copy()) } else => { - let credential_id: string = provider.auth.copy(); + let credential_id: string = provider.auth.copy().unwrap_or(""); let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result(credential_id.copy(), &policy); provider_status(provider.copy(), loaded) }; @@ -206,38 +275,38 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt index += 1; } if failure.copy() == "" => { - entry(true, operation.copy(), null, generation.copy(), report, null) + entry(true, operation.copy(), no_text(), generation.copy(), report, no_text()) } else => { - entry(false, operation.copy(), null, generation.copy(), report, failure.copy()) + entry(false, operation.copy(), no_text(), generation.copy(), report, Some(failure.copy())) } } else => { let selected: EffectiveSelection = selection::effective_selection(public_config.copy(), command.provider.copy()); if !selected.ok.copy() => { - let code: string = selected.error.copy(); - entry(false, operation.copy(), null, generation.copy(), code.copy(), code.copy()) + let code: string = selected.error.copy().unwrap_or(""); + entry(false, operation.copy(), no_text(), generation.copy(), code.copy(), Some(code.copy())) } else => { let requested: string = selected.provider.copy(); let mut index: int = 0; let mut found: bool = false; - let mut result: AuthEntryResult = entry(false, operation.copy(), null, generation.copy(), "provider_not_found", "provider_not_found"); + let mut result: AuthEntryResult = entry(false, operation.copy(), no_text(), generation.copy(), "provider_not_found", Some("provider_not_found")); while index < providers.length && !found { let provider: ProviderEntry = providers[index].copy(); if provider.name.copy() == requested.copy() { found = true; - let state: ProviderStatus = if provider.auth.copy() == null => { + let state: ProviderStatus = if !text_present(provider.auth.copy()) => { unconfigured_status(provider.copy()) } else => { - let credential_id: string = provider.auth.copy(); + let credential_id: string = provider.auth.copy().unwrap_or(""); let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result(credential_id.copy(), &policy); provider_status(provider.copy(), loaded) }; - if provider.auth.copy() == null { - result = entry(true, operation.copy(), null, generation.copy(), state.row.copy(), null); + if !text_present(provider.auth.copy()) { + result = entry(true, operation.copy(), no_text(), generation.copy(), state.row.copy(), no_text()); } else { if state.ok { - result = entry(true, operation.copy(), state.credential_id.copy(), generation.copy(), state.row.copy(), null); + result = entry(true, operation.copy(), state.credential_id, generation.copy(), state.row.copy(), no_text()); } else { - result = entry(false, operation.copy(), state.credential_id.copy(), generation.copy(), state.row.copy(), state.category.copy()); + result = entry(false, operation.copy(), state.credential_id, generation.copy(), state.row.copy(), Some(state.category.copy())); } } } @@ -248,14 +317,14 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt } } } else if operation.copy() == "config.check" => { - if command.provider.copy() != null || command.credential_id.copy() != null => { - entry(false, operation.copy(), null, 0, "invalid_command", "invalid_command") + if text_present(command.provider.copy()) || text_present(command.credential_id.copy()) => { + entry(false, operation.copy(), no_text(), 0, "invalid_command", Some("invalid_command")) } else => { let snapshot = config::load_snapshot(&home); let generation: int = snapshot.policy_generation; let policy: resource = snapshot.policy_handle; let public_config: BoundedPublicConfig = snapshot.public_config; - let selection: EffectiveSelection = selection::effective_selection(public_config.copy(), null); + let selection: EffectiveSelection = selection::effective_selection(public_config.copy(), no_text()); let providers = public_config.providers; let mut report: string = ""; let mut index: int = 0; @@ -263,10 +332,10 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt let mut credential_failure: bool = false; while index < providers.length { let provider: ProviderEntry = providers[index].copy(); - let state: ProviderStatus = if provider.auth.copy() == null => { + let state: ProviderStatus = if !text_present(provider.auth.copy()) => { unconfigured_status(provider.copy()) } else => { - let credential_id: string = provider.auth.copy(); + let credential_id: string = provider.auth.copy().unwrap_or(""); let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result(credential_id.copy(), &policy); provider_status(provider.copy(), loaded) }; @@ -283,25 +352,25 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt report = report + selection_row(selection.provider.copy(), selection.model.copy(), selection.source.copy()); } if native_failure.copy() != "" => { - entry(false, operation.copy(), null, generation.copy(), report, native_failure.copy()) + entry(false, operation.copy(), no_text(), generation.copy(), report, Some(native_failure.copy())) } else if !selection.ok.copy() => { - let code: string = selection.error.copy(); - entry(false, operation.copy(), null, generation.copy(), report, code.copy()) + let code: string = selection.error.copy().unwrap_or(""); + entry(false, operation.copy(), no_text(), generation.copy(), report, Some(code.copy())) } else if credential_failure => { - entry(false, operation.copy(), null, generation.copy(), report, "credential_check_failed") + entry(false, operation.copy(), no_text(), generation.copy(), report, Some("credential_check_failed")) } else => { - entry(true, operation.copy(), null, generation.copy(), report, null) + entry(true, operation.copy(), no_text(), generation.copy(), report, no_text()) } } } else if operation.copy() == "auth.logout" => { - if command.provider.copy() != null => { - entry(false, operation.copy(), null, 0, "invalid_command", "invalid_command") - } else if command.credential_id.copy() == null => { - entry(false, operation.copy(), null, 0, "credential_id_required", "credential_id_required") + if text_present(command.provider.copy()) => { + entry(false, operation.copy(), no_text(), 0, "invalid_command", Some("invalid_command")) + } else if !text_present(command.credential_id.copy()) => { + entry(false, operation.copy(), no_text(), 0, "credential_id_required", Some("credential_id_required")) } else => { - let credential_id: string = command.credential_id.copy(); + let credential_id: string = command.credential_id.copy().unwrap_or(""); if credential_id.copy() == "" => { - entry(false, operation.copy(), null, 0, "credential_id_required", "credential_id_required") + entry(false, operation.copy(), no_text(), 0, "credential_id_required", Some("credential_id_required")) } else => { let snapshot = config::load_snapshot(&home); let generation: int = snapshot.policy_generation; @@ -318,55 +387,55 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt let policy: resource = snapshot.policy_handle; let preflight: AuthMetadataResultEnvelope = auth::load_metadata_result(credential_id.copy(), &policy); if preflight.ok.copy() => { - if preflight.error.copy() == null && preflight.value.copy() != null => { - let metadata: AuthMetadataEnvelope = preflight.value.copy(); + if !failure_present(preflight.error.copy()) && envelope_present(preflight.value.copy()) => { + let metadata: AuthMetadataEnvelope = preflight.value.copy().unwrap_or(empty_metadata_envelope()); if metadata_value_valid(metadata.copy()) => { let deleted: AuthDeleteResultEnvelope = auth::delete_result(credential_id.copy(), &policy); if deleted.ok.copy() => { - if deleted.error.copy() == null && deleted.value.copy() != null => { - let mutation: RedactedMutationResult = deleted.value.copy(); - if mutation.ok.copy() && mutation.error.copy() == null && mutation.deleted.copy() => { - entry(true, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "deleted"), null) + if !failure_present(deleted.error.copy()) && mutation_present(deleted.value.copy()) => { + let mutation: RedactedMutationResult = deleted.value.copy().unwrap_or(empty_mutation()); + if mutation.ok.copy() && !text_present(mutation.error.copy()) && mutation.deleted.copy() => { + entry(true, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "deleted"), no_text()) } else => { - entry(false, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "host_failure"), "host_failure") + entry(false, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "host_failure"), Some("host_failure")) } } else => { - entry(false, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "host_failure"), "host_failure") + entry(false, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "host_failure"), Some("host_failure")) } } else => { - if deleted.error.copy() != null && deleted.value.copy() == null => { - let bridge_failure: AuthBridgeFailure = deleted.error.copy(); + if failure_present(deleted.error.copy()) && !mutation_present(deleted.value.copy()) => { + let bridge_failure: AuthBridgeFailure = deleted.error.copy().unwrap_or(host_failure_bridge()); let logout_code: string = bridge_failure.code.copy(); - entry(false, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), logout_code.copy()), logout_code.copy()) + entry(false, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), logout_code.copy()), Some(logout_code.copy())) } else => { - entry(false, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "host_failure"), "host_failure") + entry(false, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "host_failure"), Some("host_failure")) } } } else => { - entry(false, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "host_failure"), "host_failure") + entry(false, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "host_failure"), Some("host_failure")) } } else => { - entry(false, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "host_failure"), "host_failure") + entry(false, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "host_failure"), Some("host_failure")) } } else => { - if preflight.error.copy() != null && preflight.value.copy() == null => { - let bridge_failure: AuthBridgeFailure = preflight.error.copy(); + if failure_present(preflight.error.copy()) && !envelope_present(preflight.value.copy()) => { + let bridge_failure: AuthBridgeFailure = preflight.error.copy().unwrap_or(host_failure_bridge()); let preflight_code: string = bridge_failure.code.copy(); if preflight_code.copy() == "credential_not_found" => { - entry(true, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "already_absent"), null) + entry(true, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "already_absent"), no_text()) } else => { - entry(false, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), preflight_code.copy()), preflight_code.copy()) + entry(false, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), preflight_code.copy()), Some(preflight_code.copy())) } } else => { - entry(false, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "host_failure"), "host_failure") + entry(false, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "host_failure"), Some("host_failure")) } } } else => { - entry(true, operation.copy(), credential_id.copy(), generation.copy(), logout_report(credential_id.copy(), "already_absent"), null) + entry(true, operation.copy(), Some(credential_id.copy()), generation.copy(), logout_report(credential_id.copy(), "already_absent"), no_text()) } } } } else => { - entry(false, operation.copy(), command.credential_id.copy(), 0, "unsupported_operation", "unsupported_operation") + entry(false, operation.copy(), command.credential_id.copy(), 0, "unsupported_operation", Some("unsupported_operation")) } } diff --git a/rss/auth/config_entry.rss b/rss/auth/config_entry.rss index 6b944dd..3e8b6eb 100644 --- a/rss/auth/config_entry.rss +++ b/rss/auth/config_entry.rss @@ -108,7 +108,8 @@ fn with_live_handle(context: map, intent: map) -> map { let snapshot: map = config::load_snapshot(context["host_home"]); let ok: bool = map_bool(snapshot, "ok", false); if ok => { - wrap_probe(config::check_policy(snapshot["policy_handle"].copy(), intent)) + let probed: map = config::check_policy(snapshot["policy_handle"].copy(), intent); + wrap_probe(probed) } else => { snapshot_or_error(snapshot) } @@ -122,7 +123,8 @@ fn probe_forged(context: map) -> map { class: "OpaquePolicyHandle", id: "forged" }; - wrap_probe(config::check_policy(forged, { op: "inspect" })) + let probed: map = config::check_policy(forged, { op: "inspect" }); + wrap_probe(probed) } else => { snapshot_or_error(snapshot) } @@ -132,7 +134,8 @@ fn probe_copy(context: map) -> map { let snapshot: map = config::load_snapshot(context["host_home"]); let ok: bool = map_bool(snapshot, "ok", false); if ok => { - wrap_probe(config::check_policy(snapshot["policy_handle"].copy(), { op: "inspect" })) + let probed: map = config::check_policy(snapshot["policy_handle"].copy(), { op: "inspect" }); + wrap_probe(probed) } else => { snapshot_or_error(snapshot) } @@ -142,12 +145,15 @@ fn probe_stringify(context: map) -> map { let snapshot: map = config::load_snapshot(context["host_home"]); let ok: bool = map_bool(snapshot, "ok", false); if ok => { - let handle_type: string = type(snapshot["policy_handle"].copy()); - let from_text: map = wrap_probe(config::check_policy(handle_type, { op: "inspect" })); + let carrier_type: string = type(snapshot["policy_handle"].copy()); + let handle_type: string = map_string(snapshot, "policy_handle_class", ""); + let probed: map = config::check_policy(carrier_type, { op: "inspect" }); + let from_text: map = wrap_probe(probed); { ok: true, handle_type: handle_type, - rendered: handle_type, + carrier_type: carrier_type, + rendered: carrier_type, reconstructed_ok: map_bool(from_text, "ok", false), reconstructed_code: map_string(map_map(from_text, "error"), "code", "") } @@ -162,8 +168,9 @@ fn probe_serialize(context: map) -> map { if ok => { { ok: true, - handle_type: type(snapshot["policy_handle"].copy()), - echoed: snapshot["policy_handle"].copy() + handle_type: map_string(snapshot, "policy_handle_class", ""), + carrier_type: type(snapshot["policy_handle"].copy()), + echoed: "" } } else => { snapshot_or_error(snapshot) @@ -175,10 +182,11 @@ fn probe_stale(context: map) -> map { let ok: bool = map_bool(snapshot, "ok", false); if ok => { let generation: int = map_int(snapshot, "policy_generation", 0); - wrap_probe(config::check_policy(snapshot["policy_handle"].copy(), { + let probed: map = config::check_policy(snapshot["policy_handle"].copy(), { op: "inspect", policy_generation: generation + 1 - })) + }); + wrap_probe(probed) } else => { snapshot_or_error(snapshot) } @@ -191,7 +199,8 @@ fn probe_expire(context: map) -> map { let expired: map = config::check_policy(snapshot["policy_handle"].copy(), { op: "expire" }); let expire_ok: bool = map_bool(expired, "ok", false); if expire_ok => { - wrap_probe(config::check_policy(snapshot["policy_handle"].copy(), { op: "inspect" })) + let probed: map = config::check_policy(snapshot["policy_handle"].copy(), { op: "inspect" }); + wrap_probe(probed) } else => { wrap_probe(expired) } @@ -203,9 +212,11 @@ fn probe_expire(context: map) -> map { pub fn run(context: map) -> map { let kind: string = map_string(context, "kind", "load"); if kind == "load" => { - sanitize_load(config::load_snapshot(context["host_home"])) + let snapshot: map = config::load_snapshot(context["host_home"]); + sanitize_load(snapshot) } else if kind == "supply_path" => { - sanitize_load(config::load_snapshot("/tmp/rss-supplied-home")) + let snapshot: map = config::load_snapshot("/tmp/rss-supplied-home"); + sanitize_load(snapshot) } else if kind == "forge_handle" => { probe_forged(context) } else if kind == "copy_handle" => { diff --git a/rss/auth/device_contract.rss b/rss/auth/device_contract.rss index 13a0ee2..0b49daa 100644 --- a/rss/auth/device_contract.rss +++ b/rss/auth/device_contract.rss @@ -7,7 +7,7 @@ pub fn snapshot(home: resource) -> resource { } pub fn start(policy: resource, id: string, path: string, public_body: string) -> TransportResponseEnvelope { - let request = { + let request: TransportPublicRequest = { policy_handle: policy, credential_id: id, method: "POST", @@ -15,12 +15,12 @@ pub fn start(policy: resource, id: string, path: string, public_bod public_headers: [], public_body: public_body }; - let usage = {kind: "none"}; + let usage: TransportCredentialUse = {kind: "none"}; oauth::transport(&request, &usage) } pub fn poll(policy: resource, id: string, path: string, public_body: string, device: resource) -> TransportResponseEnvelope { - let request = { + let request: TransportPublicRequest = { policy_handle: policy, credential_id: id, method: "POST", @@ -28,10 +28,10 @@ pub fn poll(policy: resource, id: string, path: string, public_body public_headers: [], public_body: public_body }; - let usage = {kind: "device_session", device_handle: device}; + let usage: TransportCredentialUse = {kind: "device_session", device_handle: Some(device)}; oauth::transport(&request, &usage) } -pub fn close(device: resource) -> null { +pub fn close(device: resource) -> () { oauth::close_device(device) } diff --git a/rss/auth/enrollment_contract.rss b/rss/auth/enrollment_contract.rss index c348c09..33a497e 100644 --- a/rss/auth/enrollment_contract.rss +++ b/rss/auth/enrollment_contract.rss @@ -46,22 +46,22 @@ pub fn access_handle( auth::access_handle(credential_id, expected, &policy) } -pub fn close_device(device: resource) -> null { +pub fn close_device(device: resource) -> () { oauth::close_device(device) } -pub fn close_code(code: resource) -> null { +pub fn close_code(code: resource) -> () { oauth::close_code(code) } -pub fn close_verifier(verifier: resource) -> null { +pub fn close_verifier(verifier: resource) -> () { oauth::close_verifier(verifier) } -pub fn close_slot(slot: resource) -> null { +pub fn close_slot(slot: resource) -> () { auth::close_slot(slot) } -pub fn close_access(access: resource) -> null { +pub fn close_access(access: resource) -> () { auth::close_access(access) } diff --git a/rss/auth/fallible_contract.rss b/rss/auth/fallible_contract.rss index f653200..497b014 100644 --- a/rss/auth/fallible_contract.rss +++ b/rss/auth/fallible_contract.rss @@ -43,7 +43,7 @@ pub fn begin_manual(policy: resource) -> PkceBeginEnvelope { oauth::pkce_begin(&policy, intent) } -pub fn close_access(access: resource) -> null { +pub fn close_access(access: resource) -> () { auth::close_access(access) } @@ -63,7 +63,7 @@ pub fn public_json_field_result( oauth::public_json_field_result(&policy, json_text, pointer) } -pub fn cleanup(policy: resource) -> null { +pub fn cleanup(policy: resource) -> () { auth::cleanup_transients(&policy) } @@ -76,7 +76,7 @@ pub fn start_device_result(policy: resource) -> TransportResultEnve public_headers: [{name: "content-type", value: "application/json"}], public_body: "{\"client_id\":\"public-client-id\"}" }; - let usage = {kind: "none"}; + let usage: TransportCredentialUse = {kind: "none"}; oauth::transport_result(&request, &usage) } @@ -101,11 +101,16 @@ pub fn save_result(request: SaveCredentialRequest) -> AuthSaveResultEnvelope { auth::save_if_generation_result(&request) } +struct TransportCleanupProbe { + transport: TransportResultEnvelope, + cleanup_completed: bool +} + pub fn transport_result_then_cleanup( request: TransportPublicRequest, credential_use: TransportCredentialUse, policy: resource -) -> map { +) -> TransportCleanupProbe { let transport = oauth::transport_result(&request, &credential_use); auth::cleanup_transients(&policy); {transport: transport, cleanup_completed: true} diff --git a/rss/auth/login.rss b/rss/auth/login.rss index 50da378..83ee9c5 100644 --- a/rss/auth/login.rss +++ b/rss/auth/login.rss @@ -8,17 +8,17 @@ use self::types; struct AuthCommandInput { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } struct AuthEntryResult { ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option } struct CodexDeviceLoginRequest { @@ -38,7 +38,7 @@ struct CodexDeviceLoginResult { ok: bool, code: string, message: string, - metadata: AuthMetadataEnvelope? + metadata: Option } struct ProviderSelection { @@ -64,35 +64,83 @@ struct LoginReportRow { fn entry( ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option ) -> AuthEntryResult { {ok: ok, operation: operation, credential_id: credential_id, generation: generation, status: status, error: error} } -fn failed(operation: string, code: string, credential_id: string?, generation: int) -> AuthEntryResult { - entry(false, operation, credential_id, generation, code.copy(), code.copy()) +fn failed(operation: string, code: string, credential_id: Option, generation: int) -> AuthEntryResult { + entry(false, operation, credential_id, generation, code.copy(), Some(code.copy())) +} + +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn envelope_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn oauth_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_text() -> Option { + None +} + +fn no_oauth() -> Option { + None +} + +fn no_scopes() -> Option> { + None +} + +fn no_query_entries() -> Option> { + None +} + +fn host_failure_bridge() -> AuthBridgeFailure { + {code: "host_failure", cancelled: false, deadline_exceeded: false, revoked: false} } fn empty_provider() -> ProviderEntry { - {name: "", protocol: "", base_url: "", auth: null, oauth: null} + {name: "", protocol: "", base_url: "", auth: no_text(), oauth: no_oauth()} } fn empty_oauth_config() -> OAuthConfig { { - flow: null, - issuer: null, - client_id: null, - device_user_code_path: null, - device_poll_path: null, - authorization_path: null, - token_endpoint: null, - redirect_uri: null, + flow: no_text(), + issuer: no_text(), + client_id: no_text(), + device_user_code_path: no_text(), + device_poll_path: no_text(), + authorization_path: no_text(), + token_endpoint: no_text(), + redirect_uri: no_text(), refresh_skew_seconds: 0, - scopes: null, - authorization_parameters: null + scopes: no_scopes(), + authorization_parameters: no_query_entries() } } @@ -117,9 +165,11 @@ fn metadata_selection( credential_id: string, provider: string ) -> MetadataSelection { - if loaded.ok.copy() && loaded.error.copy() == null && loaded.value.copy() != null => { - let envelope: AuthMetadataEnvelope = loaded.value.copy(); - if envelope.ok.copy() && envelope.error.copy() == null && envelope.outcome.copy() == "loaded" => { + let loaded_failure: Option = loaded.error.copy(); + let loaded_value: Option = loaded.value.copy(); + if loaded.ok.copy() && !failure_present(loaded_failure) && envelope_present(loaded_value) => { + let envelope: AuthMetadataEnvelope = loaded_value.unwrap_or(empty_auth_metadata()); + if envelope.ok.copy() && !text_present(envelope.error.copy()) && envelope.outcome.copy() == "loaded" => { let metadata: AuthMetadata = envelope.metadata.copy(); if metadata.credential_id.copy() != credential_id.copy() || metadata.generation.copy() < 0 => { {ok: false, expected_generation: 0, has_refresh_token: false, error: "host_failure"} @@ -131,8 +181,8 @@ fn metadata_selection( } else => { {ok: false, expected_generation: 0, has_refresh_token: false, error: "host_failure"} } - } else if !loaded.ok.copy() && loaded.error.copy() != null && loaded.value.copy() == null => { - let failure: AuthBridgeFailure = loaded.error.copy(); + } else if !loaded.ok.copy() && failure_present(loaded_failure) && !envelope_present(loaded_value) => { + let failure: AuthBridgeFailure = loaded_failure.unwrap_or(host_failure_bridge()); let code: string = failure.code.copy(); if code.copy() == "credential_not_found" => { {ok: true, expected_generation: 0, has_refresh_token: false, error: ""} @@ -164,10 +214,12 @@ fn report_row(provider: string, credential_id: string, status: string, generatio } fn empty_auth_metadata() -> AuthMetadataEnvelope { + let account_id: Option = None; + let last_refresh_at_ms: Option = None; { ok: false, - error: null, - metadata: {credential_id: "", provider: "", kind: "", source: "", token_type: "", expires_at_ms: 0, scopes: [], account_id: null, generation: 0, status: "", last_refresh_at_ms: null, has_refresh_token: false}, + error: no_text(), + metadata: {credential_id: "", provider: "", kind: "", source: "", token_type: "", expires_at_ms: 0, scopes: [], account_id: account_id, generation: 0, status: "", last_refresh_at_ms: last_refresh_at_ms, has_refresh_token: false}, outcome: "" } } @@ -181,24 +233,24 @@ fn workflow_result( ) -> AuthEntryResult { if !result.ok.copy() => { if result.code.copy() != "" => { - failed(operation.copy(), result.code.copy(), credential_id.copy(), expected_generation.copy()) + failed(operation.copy(), result.code.copy(), Some(credential_id.copy()), expected_generation.copy()) } else => { - failed(operation.copy(), "host_failure", credential_id.copy(), expected_generation.copy()) + failed(operation.copy(), "host_failure", Some(credential_id.copy()), expected_generation.copy()) } } else if result.code.copy() == "ok" => { - let optional_metadata: AuthMetadataEnvelope? = result.metadata; + let optional_metadata: Option = result.metadata; let envelope: AuthMetadataEnvelope = optional_metadata.unwrap_or(empty_auth_metadata()); - if envelope.ok.copy() && envelope.error.copy() == null && + if envelope.ok.copy() && !text_present(envelope.error.copy()) && (envelope.outcome.copy() == "committed" || envelope.outcome.copy() == "adopted") => { let metadata: AuthMetadata = envelope.metadata.copy(); if metadata.credential_id.copy() != credential_id.copy() || metadata.provider.copy() != provider.copy() || metadata.generation.copy() < 0 => { - failed(operation.copy(), "host_failure", credential_id.copy(), expected_generation.copy()) + failed(operation.copy(), "host_failure", Some(credential_id.copy()), expected_generation.copy()) } else => { entry( true, operation.copy(), - metadata.credential_id.copy(), + Some(metadata.credential_id.copy()), metadata.generation.copy(), report_row( metadata.provider.copy(), @@ -207,58 +259,58 @@ fn workflow_result( metadata.generation.copy(), metadata.source.copy() ), - null + no_text() ) } } else => { - failed(operation.copy(), "host_failure", credential_id.copy(), expected_generation.copy()) + failed(operation.copy(), "host_failure", Some(credential_id.copy()), expected_generation.copy()) } } else => { - failed(operation.copy(), "host_failure", credential_id.copy(), expected_generation.copy()) + failed(operation.copy(), "host_failure", Some(credential_id.copy()), expected_generation.copy()) } } pub fn run(home: resource, command: AuthCommandInput) -> AuthEntryResult { let operation: string = command.operation.copy(); - let requested_credential: string? = command.credential_id.copy(); - let requested_provider_holder: string? = command.provider.copy(); + let requested_credential: Option = command.credential_id.copy(); + let requested_provider_holder: Option = command.provider.copy(); let requested_provider: string = requested_provider_holder.unwrap_or(""); if operation.copy() != "auth.login" => { - failed(operation.copy(), "unsupported_operation", null, 0) - } else if requested_credential != null => { - failed(operation.copy(), "invalid_command", null, 0) + failed(operation.copy(), "unsupported_operation", no_text(), 0) + } else if text_present(requested_credential) => { + failed(operation.copy(), "invalid_command", no_text(), 0) } else if requested_provider == "" => { - failed(operation.copy(), "provider_required", null, 0) + failed(operation.copy(), "provider_required", no_text(), 0) } else => { let snapshot = config::load_snapshot(&home); let snapshot_generation: int = snapshot.policy_generation; - if !snapshot.ok || snapshot.error != null || snapshot_generation < 0 => { - failed(operation.copy(), "host_failure", null, 0) + if !snapshot.ok || text_present(snapshot.error) || snapshot_generation < 0 => { + failed(operation.copy(), "host_failure", no_text(), 0) } else => { let policy: resource = snapshot.policy_handle; let public_config: BoundedPublicConfig = snapshot.public_config; let selected: ProviderSelection = provider_selection(public_config, requested_provider.copy()); if !selected.found.copy() => { - failed(operation.copy(), "provider_not_found", null, snapshot_generation.copy()) + failed(operation.copy(), "provider_not_found", no_text(), snapshot_generation.copy()) } else => { let provider: ProviderEntry = selected.provider.copy(); - let credential_holder: string? = provider.auth.copy(); + let credential_holder: Option = provider.auth.copy(); let credential_id: string = credential_holder.unwrap_or(""); - let oauth_holder: OAuthConfig? = provider.oauth.copy(); + let oauth_holder: Option = provider.oauth.copy(); let oauth_config: OAuthConfig = oauth_holder.unwrap_or(empty_oauth_config()); - let flow_holder: string? = oauth_config.flow; + let flow_holder: Option = oauth_config.flow; let flow: string = flow_holder.unwrap_or(""); if credential_id == "" => { - failed(operation.copy(), "credential_id_required", null, snapshot_generation.copy()) - } else if oauth_holder == null => { - failed(operation.copy(), "login_flow_unsupported", credential_id.copy(), snapshot_generation.copy()) + failed(operation.copy(), "credential_id_required", no_text(), snapshot_generation.copy()) + } else if !oauth_present(oauth_holder) => { + failed(operation.copy(), "login_flow_unsupported", Some(credential_id.copy()), snapshot_generation.copy()) } else if flow != "codex-device" && flow != "authorization_code_pkce" => { - failed(operation.copy(), "login_flow_unsupported", credential_id.copy(), snapshot_generation.copy()) + failed(operation.copy(), "login_flow_unsupported", Some(credential_id.copy()), snapshot_generation.copy()) } else => { let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result(credential_id.copy(), &policy); let metadata: MetadataSelection = metadata_selection(loaded, credential_id.copy(), provider.name.copy()); if !metadata.ok.copy() => { - failed(operation.copy(), metadata.error.copy(), credential_id.copy(), metadata.expected_generation.copy()) + failed(operation.copy(), metadata.error.copy(), Some(credential_id.copy()), metadata.expected_generation.copy()) } else => { let expected_generation: int = metadata.expected_generation.copy(); if flow == "codex-device" => { @@ -289,28 +341,25 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt metadata.has_refresh_token.copy() ); if !result.ok.copy() => { - failed(operation.copy(), result.code.copy(), credential_id.copy(), expected_generation.copy()) - } else if result.metadata.copy() == null => { - failed(operation.copy(), "host_failure", credential_id.copy(), expected_generation.copy()) + failed(operation.copy(), result.code.copy(), Some(credential_id.copy()), expected_generation.copy()) + } else if !envelope_present(result.metadata.copy()) => { + failed(operation.copy(), "host_failure", Some(credential_id.copy()), expected_generation.copy()) } else => { - let envelope: AuthMetadataEnvelope = result.metadata; - let envelope_error_present: bool = envelope.has("error"); - let envelope_has_error: bool = if envelope_error_present => { - envelope.error.copy() != null - } else => {false}; + let envelope: AuthMetadataEnvelope = result.metadata.unwrap_or(empty_auth_metadata()); + let envelope_has_error: bool = text_present(envelope.error.copy()); if !envelope.ok.copy() || envelope_has_error || (envelope.outcome.copy() != "committed" && envelope.outcome.copy() != "adopted") => { - failed(operation.copy(), "host_failure", credential_id.copy(), expected_generation.copy()) + failed(operation.copy(), "host_failure", Some(credential_id.copy()), expected_generation.copy()) } else => { let saved: AuthMetadata = envelope.metadata.copy(); if saved.credential_id.copy() != credential_id.copy() || saved.provider.copy() != requested_provider.copy() || saved.generation.copy() < 0 => { - failed(operation.copy(), "host_failure", credential_id.copy(), expected_generation.copy()) + failed(operation.copy(), "host_failure", Some(credential_id.copy()), expected_generation.copy()) } else => { entry( true, operation.copy(), - saved.credential_id.copy(), + Some(saved.credential_id.copy()), saved.generation.copy(), report_row( saved.provider.copy(), @@ -319,7 +368,7 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt saved.generation.copy(), saved.source.copy() ), - null + no_text() ) } } diff --git a/rss/auth/oauth_flow.rss b/rss/auth/oauth_flow.rss index 79d7004..a94b96d 100644 --- a/rss/auth/oauth_flow.rss +++ b/rss/auth/oauth_flow.rss @@ -383,6 +383,7 @@ fn denied_header(context: map) -> map { } fn local_refresh_validate(context: map) -> map { + // The fixture adapter accepts a request map, including the host policy. let issued: map = auth::refresh_handle({ credential_id: map_string(context, "credential_id", "primary"), expected_generation: map_int(context, "expected_generation", 0), @@ -437,6 +438,7 @@ fn local_access_validate(context: map) -> map { } fn reserved_form_refresh(context: map) -> map { + // The fixture adapter accepts a request map, including the host policy. let issued: map = auth::refresh_handle({ credential_id: map_string(context, "credential_id", "primary"), expected_generation: map_int(context, "expected_generation", 0), @@ -737,6 +739,7 @@ fn serialized_code(context: map) -> map { } fn refresh_save(context: map) -> map { + // The fixture adapter accepts a request map, including the host policy. let issued: map = auth::refresh_handle({ credential_id: map_string(context, "credential_id", "primary"), expected_generation: map_int(context, "expected_generation", 0), diff --git a/rss/auth/payload_contract.rss b/rss/auth/payload_contract.rss index f20b471..2890753 100644 --- a/rss/auth/payload_contract.rss +++ b/rss/auth/payload_contract.rss @@ -15,7 +15,7 @@ pub fn start_device(policy: resource) -> TransportResponseEnvelope public_headers: [{name: "content-type", value: "application/json"}], public_body: "{}" }; - let usage = {kind: "none"}; + let usage: TransportCredentialUse = {kind: "none"}; oauth::transport_payload( &request, &usage, @@ -48,7 +48,7 @@ pub fn selected_code( verifier: resource, payload: TransportPayloadIntent ) -> TransportResponseEnvelope { - let usage = {kind: "code_verifier", code_handle: code, verifier_handle: verifier}; + let usage: TransportCredentialUse = {kind: "code_verifier", code_handle: Some(code), verifier_handle: Some(verifier)}; oauth::transport_payload(&request, &usage, payload) } @@ -56,6 +56,6 @@ pub fn access_handle(policy: resource) -> AccessHandleEnvelope { auth::access_handle("fixture-codex", 4, &policy) } -pub fn close_device(device: resource) -> null { +pub fn close_device(device: resource) -> () { oauth::close_device(device) } diff --git a/rss/auth/pkce_instruction_session.rss b/rss/auth/pkce_instruction_session.rss index 18eacba..9816ac9 100644 --- a/rss/auth/pkce_instruction_session.rss +++ b/rss/auth/pkce_instruction_session.rss @@ -14,7 +14,7 @@ struct PkceStartResult { code: string, selected_mode: string, fallback_used: bool, - value: PkceBeginEnvelope? + value: Option } struct PkceSessionResult { @@ -22,12 +22,8 @@ struct PkceSessionResult { code: string, selected_mode: string, fallback_used: bool, - begin: PkceBeginEnvelope?, - waited: CallbackEnvelope? -} - -struct AuthorizationInstructionValuePresence { - value: bool? + begin: Option, + waited: Option } struct AuthorizationInstructionValueState { @@ -35,12 +31,24 @@ struct AuthorizationInstructionValueState { value: bool } -struct AuthorizationInstructionErrorPresence { - error: AuthBridgeFailure? +struct BeginPeek { + present: bool, + value: Option +} + +fn no_begin() -> Option { + None } -struct BeginEnvelopePresence { - value: PkceBeginEnvelope? +fn no_callback() -> Option { + None +} + +fn peek_begin(value: Option) -> BeginPeek { + match value { + Some(envelope) => {present: true, value: Some(envelope)}, + None => {present: false, value: no_begin()} + } } fn sanitized_failure(code: string, selected_mode: string, fallback_used: bool) -> PkceSessionResult { @@ -49,8 +57,8 @@ fn sanitized_failure(code: string, selected_mode: string, fallback_used: bool) - code: code, selected_mode: selected_mode, fallback_used: fallback_used, - begin: null, - waited: null + begin: no_begin(), + waited: no_callback() } } @@ -59,35 +67,24 @@ fn begin_success( selected_mode: string, fallback_used: bool ) -> PkceStartResult { - {ok: true, code: "ok", selected_mode: selected_mode, fallback_used: fallback_used, value: begin} + {ok: true, code: "ok", selected_mode: selected_mode, fallback_used: fallback_used, value: Some(begin)} } fn acknowledgment_value_state( ack: AuthorizationInstructionResultEnvelope ) -> AuthorizationInstructionValueState { - let has_value: bool = ack.has("value"); - let holder: AuthorizationInstructionValuePresence = if has_value => { - {value: ack.value.copy()} - } else => { - {value: null} - }; - let present: bool = holder.has("value") && holder.value.copy() != null; - if present => { - let value: bool = holder.value; - {present: true, value: value} - } else => { - {present: false, value: false} + let carrier: Option = ack.value.copy(); + match carrier { + Some(value) => {present: true, value: value}, + None => {present: false, value: false} } } fn acknowledgment_error_present(ack: AuthorizationInstructionResultEnvelope) -> bool { - let has_error: bool = ack.has("error"); - let holder: AuthorizationInstructionErrorPresence = if has_error => { - {error: ack.error.copy()} - } else => { - {error: null} - }; - holder.has("error") && holder.error != null + match ack.error.copy() { + Some(_) => true, + None => false + } } // A genuine acknowledgement is ok, carries a true value and no failure fact. @@ -102,10 +99,10 @@ fn acknowledged(ack: AuthorizationInstructionResultEnvelope) -> bool { fn close_instruction_family( callback: resource, verifier: resource -) -> null { +) -> () { oauth::close_callback(callback); oauth::close_verifier(verifier); - null + () } fn in_ui_failure_set(code: string) -> bool { @@ -132,33 +129,43 @@ fn project_instruction_failure( ) -> PkceSessionResult { let value: AuthorizationInstructionValueState = acknowledgment_value_state(ack.copy()); let failed_cleanly: bool = !ack.ok.copy() && !value.present; + let failure_carrier: Option = ack.error.copy(); if failed_cleanly => { - let has_error: bool = ack.has("error"); - let error_holder: AuthorizationInstructionErrorPresence = if has_error => { - {error: ack.error.copy()} - } else => { - {error: null} - }; - let error_present: bool = error_holder.has("error") && error_holder.error.copy() != null; - if error_present => { - let failure: AuthBridgeFailure = error_holder.error; - let code: string = failure.code.copy(); - if in_ui_failure_set(code) => { - close_instruction_family(callback, verifier); - sanitized_failure(code.copy(), selected_mode, fallback_used) - } else if in_owner_rollback_set(code) => { - sanitized_failure(code.copy(), selected_mode, fallback_used) - } else => { - sanitized_failure("host_failure", selected_mode, fallback_used) - } - } else => { - sanitized_failure("host_failure", selected_mode, fallback_used) + match failure_carrier { + Some(failure) => project_instruction_close( + failure, + callback, + verifier, + selected_mode, + fallback_used + ), + None => sanitized_failure("host_failure", selected_mode, fallback_used) } } else => { sanitized_failure("host_failure", selected_mode, fallback_used) } } +/// Maps one native instruction failure onto the public code, closing this family +/// only for a genuine delivery failure. +fn project_instruction_close( + failure: AuthBridgeFailure, + callback: resource, + verifier: resource, + selected_mode: string, + fallback_used: bool +) -> PkceSessionResult { + let code: string = failure.code.copy(); + if in_ui_failure_set(code) => { + close_instruction_family(callback, verifier); + sanitized_failure(code.copy(), selected_mode, fallback_used) + } else if in_owner_rollback_set(code) => { + sanitized_failure(code.copy(), selected_mode, fallback_used) + } else => { + sanitized_failure("host_failure", selected_mode, fallback_used) + } +} + fn instruction_session(start: PkceStartResult) -> PkceSessionResult { if !start.ok => { // Keep the original typed start intact for the accepted finish path. @@ -168,35 +175,41 @@ fn instruction_session(start: PkceStartResult) -> PkceSessionResult { let selected_mode: string = start.selected_mode; let fallback_used: bool = start.fallback_used; let has_begin: bool = start.has("value"); - let begin_holder: BeginEnvelopePresence = if has_begin => { - {value: start.value} - } else => { - {value: null} - }; - let begin_present: bool = begin_holder.has("value"); - if !begin_present => { + let begin_carrier: Option = if has_begin => { start.value } else => { no_begin() }; + let begin_holder: BeginPeek = peek_begin(begin_carrier); + if !begin_holder.present => { sanitized_failure("host_failure", selected_mode, fallback_used) } else => { - let begin: PkceBeginEnvelope = begin_holder.value; - // Borrow the owned callback capability for exactly one enqueue - // attempt before finish. Acceptance is not actual output delivery. - let ack: AuthorizationInstructionResultEnvelope = - oauth::emit_authorization_instruction_result(&begin.callback_handle); - if acknowledged(ack.copy()) => { - session::finish(begin_success(begin, selected_mode, fallback_used)) - } else => { - project_instruction_failure( - ack, - begin.callback_handle, - begin.verifier_handle, - selected_mode, - fallback_used - ) + match begin_holder.value { + Some(begin) => with_begin(begin, selected_mode, fallback_used), + None => sanitized_failure("host_failure", selected_mode, fallback_used) } } } } +fn with_begin( + begin: PkceBeginEnvelope, + selected_mode: string, + fallback_used: bool +) -> PkceSessionResult { + // Borrow the owned callback capability for exactly one enqueue attempt + // before finish. Acceptance is not actual output delivery. + let ack: AuthorizationInstructionResultEnvelope = + oauth::emit_authorization_instruction_result(&begin.callback_handle); + if acknowledged(ack.copy()) => { + session::finish(begin_success(begin, selected_mode, fallback_used)) + } else => { + project_instruction_failure( + ack, + begin.callback_handle, + begin.verifier_handle, + selected_mode, + fallback_used + ) + } +} + pub fn run( policy_handle: resource, intent: BoundedPublicOAuthIntent, diff --git a/rss/auth/pkce_login.rss b/rss/auth/pkce_login.rss index b580858..81dab61 100644 --- a/rss/auth/pkce_login.rss +++ b/rss/auth/pkce_login.rss @@ -1,13 +1,13 @@ use auth; use oauth; use self::pkce_instruction_session::{run as instruction_run}; -use self::token_metadata::{parse as metadata_parse}; +use self::token_metadata::{parse_frame as metadata_parse_frame}; use self::token_save::{save as token_save}; struct PkceLoginResult { ok: bool, code: string, - metadata: AuthMetadataEnvelope? + metadata: Option } struct PkceLoginRequest { @@ -27,8 +27,8 @@ struct PkceConfiguration { struct PkceSessionValues { ok: bool, code: string, - begin: PkceBeginEnvelope?, - waited: CallbackEnvelope? + begin: Option, + waited: Option } struct PkceSessionResult { @@ -36,20 +36,20 @@ struct PkceSessionResult { code: string, selected_mode: string, fallback_used: bool, - begin: PkceBeginEnvelope?, - waited: CallbackEnvelope? + begin: Option, + waited: Option } struct PkceBeginPresence { - value: PkceBeginEnvelope? + value: Option } struct PkceCallbackPresence { - value: CallbackEnvelope? + value: Option } struct AuthorizationCodePresence { - value: resource? + value: Option> } struct CodeGenerationValue { @@ -61,13 +61,13 @@ struct CodeGenerationValue { } struct TransportResponsePresence { - value: TransportResponseEnvelope? + value: Option } struct MetadataValue { ok: bool, code: string, - value: CredentialMetadata? + value: Option } struct PublicQueryChoice { @@ -82,13 +82,22 @@ struct TokenMetadataIntent { scopes: array, expiry_pointer: string, token_type_pointer: string, - account_pointer: string? + account_pointer: Option +} + +struct MetadataFrame { + ok: bool, + error: Option, + status: int, + body: string, + access_present: bool, + refresh_present: bool } struct TokenMetadataResult { ok: bool, code: string, - value: CredentialMetadata? + value: Option } struct TokenSaveInput { @@ -101,13 +110,191 @@ struct TokenSaveInput { struct TokenSaveResult { ok: bool, code: string, - outcome: string?, - value: AuthMetadata?, - failure: AuthBridgeFailure? + outcome: Option, + value: Option, + failure: Option +} + +// --------------------------------------------------------------------------- +// Logical Option carriers +// --------------------------------------------------------------------------- + +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn oauth_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn envelope_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn metadata_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_text() -> Option { + None +} + +fn no_int() -> Option { + None +} + +fn no_metadata() -> Option { + None +} + +fn no_envelope() -> Option { + None +} + +fn no_metadata_auth() -> Option { + None +} + +fn empty_metadata_auth() -> AuthMetadata { + let account_id: Option = None; + let last_refresh_at_ms: Option = None; + { + credential_id: "", + provider: "", + kind: "", + source: "", + token_type: "", + expires_at_ms: 0, + scopes: [], + account_id: account_id, + generation: 0, + status: "", + last_refresh_at_ms: last_refresh_at_ms, + has_refresh_token: false + } +} + +fn no_slot() -> Option> { + None +} + +fn no_access_slot() -> Option> { + None +} + +fn no_refresh_slot() -> Option> { + None +} + +fn no_code_slot() -> Option> { + None +} + +fn no_verifier_slot() -> Option> { + None +} + +fn no_device_slot() -> Option> { + None +} + +fn no_begin() -> Option { + None +} + +fn no_callback() -> Option { + None +} + +fn no_scopes() -> Option> { + None +} + +fn empty_scopes() -> array { + [] +} + +fn empty_query_entries() -> array { + [] +} + +fn no_query_entries() -> Option> { + None +} + +fn empty_oauth_config() -> OAuthConfig { + { + flow: no_text(), + issuer: no_text(), + client_id: no_text(), + device_user_code_path: no_text(), + device_poll_path: no_text(), + authorization_path: no_text(), + token_endpoint: no_text(), + redirect_uri: no_text(), + refresh_skew_seconds: 0, + scopes: no_scopes(), + authorization_parameters: no_query_entries() + } +} + +struct SlotPeek { + present: bool, + handle: Option> +} + +fn peek_slot(value: Option>) -> SlotPeek { + match value { + Some(handle) => {present: true, handle: Some(handle)}, + None => {present: false, handle: no_slot()} + } +} + +struct BeginPeek { + present: bool, + envelope: Option +} + +fn peek_begin(value: Option) -> BeginPeek { + match value { + Some(envelope) => {present: true, envelope: Some(envelope)}, + None => {present: false, envelope: no_begin()} + } +} + +struct CallbackPeek { + present: bool, + envelope: Option +} + +fn peek_callback(value: Option) -> CallbackPeek { + match value { + Some(envelope) => {present: true, envelope: Some(envelope)}, + None => {present: false, envelope: no_callback()} + } } fn failed(code: string) -> PkceLoginResult { - {ok: false, code: code, metadata: null} + {ok: false, code: code, metadata: no_envelope()} } fn failed_configuration(code: string) -> PkceConfiguration { @@ -163,21 +350,21 @@ fn safe_relative_path(value: string) -> bool { find_text(value, "://") < 0 && find_text(value, "?") < 0 && find_text(value, "#") < 0 } -fn token_path(endpoint: string) -> string? { - let mut selected_path: string? = null; +fn token_path(endpoint: string) -> Option { + let mut selected_path: Option = no_text(); if find_text(endpoint, "https://") == 0 && find_text(endpoint, "?") < 0 && find_text(endpoint, "#") < 0 { let authority_and_path: string = endpoint[8:]; let slash: int = find_text(authority_and_path, "/"); if slash < 0 { if authority_and_path != "" && find_text(authority_and_path, "@") < 0 { - selected_path = "/"; + selected_path = Some("/"); } } else { let authority: string = authority_and_path.copy()[0:slash]; let path: string = authority_and_path[slash:]; if authority != "" && find_text(authority, "@") < 0 && safe_relative_path(path) { - selected_path = path; + selected_path = Some(path); } } } @@ -222,29 +409,31 @@ fn public_query(configured: array, client_id: string) -> Publi } fn configured_pkce(provider: ProviderEntry, credential_id: string) -> PkceConfiguration { - if provider.auth.copy() == null || provider.auth.copy() != credential_id || provider.oauth.copy() == null => { + let auth_holder: Option = provider.auth.copy(); + let oauth_holder: Option = provider.oauth.copy(); + if !text_present(auth_holder) || auth_holder.unwrap_or("") != credential_id || !oauth_present(oauth_holder) => { failed_configuration("invalid_provider_config") } else => { - let oauth_config: OAuthConfig = provider.oauth.copy(); - let client_holder: string? = oauth_config.client_id; - let path_holder: string? = oauth_config.authorization_path; - let token_holder: string? = oauth_config.token_endpoint; - let flow_holder: string? = oauth_config.flow; + let oauth_config: OAuthConfig = oauth_holder.unwrap_or(empty_oauth_config()); + let client_holder: Option = oauth_config.client_id; + let path_holder: Option = oauth_config.authorization_path; + let token_holder: Option = oauth_config.token_endpoint; + let flow_holder: Option = oauth_config.flow; let client_id: string = client_holder.unwrap_or(""); let authorization_path: string = path_holder.unwrap_or(""); let endpoint: string = token_holder.unwrap_or(""); let flow: string = flow_holder.unwrap_or(""); - let path: string? = token_path(endpoint); - let configured_holder: array? = oauth_config.authorization_parameters; - let configured_parameters: array = configured_holder.unwrap_or([]); + let path: Option = token_path(endpoint); + let configured_holder: Option> = oauth_config.authorization_parameters; + let configured_parameters: array = configured_holder.unwrap_or(empty_query_entries()); let query: PublicQueryChoice = public_query(configured_parameters, client_id.copy()); if flow != "authorization_code_pkce" || !safe_client_id(client_id.copy()) || - !safe_relative_path(authorization_path.copy()) || path == null || !query.ok.copy() => { + !safe_relative_path(authorization_path.copy()) || !text_present(path) || !query.ok.copy() => { failed_configuration("invalid_provider_config") } else => { let selected_path: string = path.unwrap_or(""); - let scopes_holder: array? = oauth_config.scopes; - let scopes: array = scopes_holder.unwrap_or([]); + let scopes_holder: Option> = oauth_config.scopes; + let scopes: array = scopes_holder.unwrap_or(empty_scopes()); { ok: true, code: "ok", @@ -263,28 +452,16 @@ fn configured_pkce(provider: ProviderEntry, credential_id: string) -> PkceConfig } fn session_values(session: PkceSessionResult) -> PkceSessionValues { - let begin_key_present: bool = session.has("begin"); - let waited_key_present: bool = session.has("waited"); + let begin_holder: BeginPeek = peek_begin(session.begin); + let waited_holder: CallbackPeek = peek_callback(session.waited); let result_ok: bool = session.ok; let result_code: string = session.code; - let begin_holder: PkceBeginPresence = if begin_key_present => { - {value: session.begin} - } else => { - {value: null} - }; - let waited_holder: PkceCallbackPresence = if waited_key_present => { - {value: session.waited} - } else => { - {value: null} - }; - let begin_present: bool = begin_holder.has("value"); - let waited_present: bool = waited_holder.has("value"); - if result_ok && begin_present && waited_present => { - {ok: true, code: "ok", begin: begin_holder.value, waited: waited_holder.value} + if result_ok && begin_holder.present && waited_holder.present => { + {ok: true, code: "ok", begin: begin_holder.envelope, waited: waited_holder.envelope} } else if !result_ok => { - {ok: false, code: result_code, begin: null, waited: null} + {ok: false, code: result_code, begin: no_begin(), waited: no_callback()} } else => { - {ok: false, code: "host_failure", begin: null, waited: null} + {ok: false, code: "host_failure", begin: no_begin(), waited: no_callback()} } } @@ -294,8 +471,8 @@ fn generation_value( expected_generation: int ) -> CodeGenerationValue { let captured: CodeGenerationResultEnvelope = oauth::code_generation_result(&code_handle, &verifier_handle); - let has_error: bool = captured.has("error") && captured.error.copy() != null; - let value: int? = captured.value; + let has_error: bool = match captured.error { Some(_) => true, None => false }; + let value: Option = captured.value; let generation: int = value.unwrap_or(-1); if !captured.ok.copy() || has_error || generation < 0 || generation != expected_generation => { { @@ -318,7 +495,7 @@ fn generation_value( fn metadata_value( policy: resource, - response: TransportResponseEnvelope, + frame: MetadataFrame, provider: string, source: string, expected_generation: int, @@ -331,14 +508,15 @@ fn metadata_value( scopes: scopes, expiry_pointer: "/expires_in", token_type_pointer: "/token_type", - account_pointer: null + account_pointer: no_text() }; - let parsed: TokenMetadataResult = metadata_parse(policy, response, intent); - let has_value: bool = parsed.has("value") && parsed.value.copy() != null; + let parsed: TokenMetadataResult = metadata_parse_frame(policy, frame, intent); + let parsed_value: Option = parsed.value; + let has_value: bool = metadata_present(parsed_value); if !parsed.ok.copy() || parsed.code.copy() != "ok" || !has_value => { - {ok: false, code: "invalid_token_metadata", value: null} + {ok: false, code: "invalid_token_metadata", value: no_metadata()} } else => { - {ok: true, code: "ok", value: parsed.value} + {ok: true, code: "ok", value: parsed_value} } } @@ -346,18 +524,14 @@ fn saved_metadata( home: resource, policy: resource, request: PkceLoginRequest, - response: TransportResponseEnvelope, parsed_metadata: CredentialMetadata, - expected_generation: int + expected_generation: int, + access_slot: resource, + refresh_slot: Option>, + has_refresh: bool ) -> PkceLoginResult { - let has_access: bool = response.access_slot != null; - let has_refresh: bool = response.refresh_slot != null; - if !has_access => { - failed("invalid_token_metadata") - } else if has_refresh => { - let access_slot: resource = response.access_slot; - let refresh_slot: resource = response.refresh_slot; - let mut metadata: CredentialMetadata = parsed_metadata; + let mut metadata: CredentialMetadata = parsed_metadata; + if has_refresh => { metadata.has_refresh_token = true; let save_input: TokenSaveInput = { credential_id: request.credential_id, @@ -365,11 +539,8 @@ fn saved_metadata( metadata: metadata, refresh_action: "replace" }; - let saved: TokenSaveResult = token_save(home, policy, save_input, access_slot, refresh_slot); - saved_result(saved) + saved_result(token_save(home, policy, save_input, access_slot, refresh_slot)) } else => { - let access_slot: resource = response.access_slot; - let mut metadata: CredentialMetadata = parsed_metadata; metadata.has_refresh_token = request.had_refresh_token; let refresh_action: string = if request.had_refresh_token => { "preserve" @@ -382,24 +553,31 @@ fn saved_metadata( metadata: metadata, refresh_action: refresh_action }; - let saved: TokenSaveResult = token_save(home, policy, save_input, access_slot, null); - saved_result(saved) + saved_result(token_save(home, policy, save_input, access_slot, no_slot())) } } fn saved_result(saved: TokenSaveResult) -> PkceLoginResult { - if !saved.ok.copy() || saved.code.copy() != "ok" || saved.value.copy() == null => { + let saved_value: Option = saved.value; + if !saved.ok.copy() || saved.code.copy() != "ok" || !auth_metadata_present(saved_value) => { failed(saved.code.copy()) } else => { - let metadata: AuthMetadata = saved.value; - let outcome: string? = saved.outcome; + let metadata: AuthMetadata = saved_value.unwrap_or(empty_metadata_auth()); + let outcome: Option = saved.outcome; let envelope: AuthMetadataEnvelope = { ok: true, - error: null, + error: no_text(), metadata: metadata, outcome: outcome.unwrap_or("") }; - {ok: true, code: "ok", metadata: envelope} + {ok: true, code: "ok", metadata: Some(envelope)} + } +} + +fn auth_metadata_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false } } @@ -427,53 +605,112 @@ fn exchange_and_save( }; let usage: TransportCredentialUse = { kind: "code_verifier", - access_handle: null, - refresh_handle: null, - code_handle: code_handle, - verifier_handle: verifier_handle, - device_handle: null + access_handle: no_access_slot(), + refresh_handle: no_refresh_slot(), + code_handle: Some(code_handle), + verifier_handle: Some(verifier_handle), + device_handle: no_device_slot() }; let payload: TransportPayloadIntent = { encoding: "form", secret_fields: "authorization_code", - device_field: null, + device_field: "", code_field: "code" }; let exchanged: TransportResultEnvelope = oauth::transport_payload_result(&transport_request, &usage, payload); let policy_after_exchange: resource = transport_request.policy_handle; - let error_key_present: bool = exchanged.has("error"); - let has_error: bool = if error_key_present => {exchanged.error != null} else => {false}; - let response_holder: TransportResponsePresence = {value: exchanged.value}; - let response_present: bool = response_holder.has("value"); - if !exchanged.ok || has_error || !response_present => { + let exchange_failure: Option = exchanged.error; + let response_holder: Option = exchanged.value; + if !exchanged.ok || failure_present(exchange_failure) => { failed("exchange_failed") } else => { - let response: TransportResponseEnvelope = response_holder.value; - let parsed: MetadataValue = metadata_value( - policy_after_exchange, - response, - request.provider.name.copy(), - "authorization-code-pkce", - generation, - scopes - ); - if !parsed.ok.copy() || parsed.value.copy() == null => { - failed(parsed.code.copy()) - } else => { - let metadata: CredentialMetadata = parsed.value; - let saved: PkceLoginResult = saved_metadata( + match response_holder { + Some(response) => exchange_complete( home, policy_after_exchange, request, + config, response, - metadata, - generation - ); - saved + generation, + scopes + ), + None => failed("exchange_failed") + } + } +} + +/// Completes one exchange: the token response frame is taken apart once here, +/// because the metadata parser consumes it while the slot handles are still +/// needed for the save. +fn exchange_complete( + home: resource, + policy: resource, + request: PkceLoginRequest, + config: PkceConfiguration, + response: TransportResponseEnvelope, + generation: int, + scopes: array +) -> PkceLoginResult { + let access_peek: SlotPeek = peek_slot(response.access_slot); + let refresh_peek: SlotPeek = peek_slot(response.refresh_slot); + // `SlotPeek` owns a handle, so its `present` flag may be read exactly once: + // take both flags here and hand the shell's handle to the save below. + let access_present: bool = access_peek.present; + let refresh_present: bool = refresh_peek.present; + let frame: MetadataFrame = { + ok: response.ok, + error: response.error, + status: response.status, + body: response.body_without_secret_fields, + access_present: access_present, + refresh_present: refresh_present + }; + let parsed: MetadataValue = metadata_value( + policy, + frame, + request.provider.name.copy(), + "authorization-code-pkce", + generation, + scopes + ); + let parsed_metadata: Option = parsed.value; + if !parsed.ok.copy() || !metadata_present(parsed_metadata) => { + failed(parsed.code.copy()) + } else if !access_present => { + failed("invalid_token_metadata") + } else => { + match access_peek.handle { + Some(access_slot) => saved_metadata( + home, + policy, + request, + parsed_metadata.unwrap_or(empty_credential_metadata()), + generation, + access_slot, + refresh_peek.handle, + refresh_present + ), + None => failed("invalid_token_metadata") } } } +fn empty_credential_metadata() -> CredentialMetadata { + { + provider: "", + kind: "", + source: "", + token_type: "", + expires_at_ms: 0, + scopes: [], + account_id: no_text(), + generation: 0, + status: "", + last_refresh_at_ms: no_int(), + has_refresh_token: false + } +} + fn complete_session( home: resource, policy: resource, @@ -481,63 +718,90 @@ fn complete_session( config: PkceConfiguration ) -> PkceLoginResult { let session_result: PkceSessionResult = instruction_run(policy, config.intent, true); - let begin_key_present: bool = session_result.has("begin"); - let waited_key_present: bool = session_result.has("waited"); let session_ok: bool = session_result.ok; let session_code: string = session_result.code; - let begin_holder: PkceBeginPresence = if begin_key_present => { - {value: session_result.begin} - } else => { - {value: null} - }; - let waited_holder: PkceCallbackPresence = if waited_key_present => { - {value: session_result.waited} - } else => { - {value: null} - }; - let begin_present: bool = begin_holder.has("value"); - let waited_present: bool = waited_holder.has("value"); + let begin_holder: BeginPeek = peek_begin(session_result.begin); + let waited_holder: CallbackPeek = peek_callback(session_result.waited); if !session_ok => { failed(session_code) - } else if !begin_present || !waited_present => { + } else if !begin_holder.present || !waited_holder.present => { failed("host_failure") } else => { - let begin: PkceBeginEnvelope = begin_holder.value; - let waited: CallbackEnvelope = waited_holder.value; - let code_holder: AuthorizationCodePresence = {value: waited.code_handle}; - let code_present: bool = code_holder.has("value"); - if !code_present => { - failed("host_failure") - } else => { - let code_handle: resource = code_holder.value; - let verifier_handle: resource = begin.verifier_handle; - let generation: CodeGenerationValue = generation_value( - code_handle, - verifier_handle, - request.expected_generation - ); - let generation_ok: bool = generation.ok; - let generation_code: string = generation.code; - let captured_generation: int = generation.generation; - if !generation_ok => { - failed(generation_code) - } else => { - let code_handle: resource = generation.code_handle; - let verifier_handle: resource = generation.verifier_handle; - exchange_and_save( - home, - policy, - request, - config, - code_handle, - verifier_handle, - captured_generation - ) - } + match begin_holder.envelope { + Some(begin) => match waited_holder.envelope { + Some(waited) => captured_session(home, policy, request, config, begin, waited), + None => failed("host_failure") + }, + None => failed("host_failure") } } } +/// Runs the code generation and exchange once both envelopes are bound, so the +/// returned code handle is the live one the token request consumes. +fn captured_session( + home: resource, + policy: resource, + request: PkceLoginRequest, + config: PkceConfiguration, + begin: PkceBeginEnvelope, + waited: CallbackEnvelope +) -> PkceLoginResult { + let code_peek: CodeSlotPeek = peek_code_slot(waited.code_handle); + if !code_peek.present => { + failed("host_failure") + } else => { + match code_peek.handle { + Some(code_handle) => code_generation_step(home, policy, request, config, begin, code_handle), + None => failed("host_failure") + } + } +} + +fn code_generation_step( + home: resource, + policy: resource, + request: PkceLoginRequest, + config: PkceConfiguration, + begin: PkceBeginEnvelope, + code_handle: resource +) -> PkceLoginResult { + let verifier_handle: resource = begin.verifier_handle; + let generation: CodeGenerationValue = generation_value( + code_handle, + verifier_handle, + request.expected_generation + ); + let generation_ok: bool = generation.ok; + let generation_code: string = generation.code; + let captured_generation: int = generation.generation; + if !generation_ok => { + failed(generation_code) + } else => { + exchange_and_save( + home, + policy, + request, + config, + generation.code_handle, + generation.verifier_handle, + captured_generation + ) + } +} + +struct CodeSlotPeek { + present: bool, + handle: Option> +} + +fn peek_code_slot(value: Option>) -> CodeSlotPeek { + match value { + Some(handle) => {present: true, handle: Some(handle)}, + None => {present: false, handle: no_code_slot()} + } +} + pub fn run( home: resource, policy: resource, diff --git a/rss/auth/pkce_session.rss b/rss/auth/pkce_session.rss index 7d2d7f7..36c1f30 100644 --- a/rss/auth/pkce_session.rss +++ b/rss/auth/pkce_session.rss @@ -5,7 +5,7 @@ struct PkceStartResult { code: string, selected_mode: string, fallback_used: bool, - value: PkceBeginEnvelope? + value: Option } struct PkceSessionResult { @@ -13,20 +13,21 @@ struct PkceSessionResult { code: string, selected_mode: string, fallback_used: bool, - begin: PkceBeginEnvelope?, - waited: CallbackEnvelope? + begin: Option, + waited: Option } -struct BeginEnvelopePresence { - value: PkceBeginEnvelope? -} - -struct CallbackEnvelopePresence { - value: CallbackEnvelope? +/// One begin envelope plus its presence. Both envelopes carry live handles, so a +/// resource handle can only be moved once: the carrier is taken apart here and +/// handed back inside this shell. +struct BeginPeek { + present: bool, + value: Option } -struct AuthFailurePresence { - failure: AuthBridgeFailure? +struct CallbackPeek { + present: bool, + value: Option } struct FailureProjection { @@ -34,8 +35,41 @@ struct FailureProjection { code: string } +fn no_begin() -> Option { + None +} + +fn no_callback() -> Option { + None +} + +fn peek_begin(value: Option) -> BeginPeek { + match value { + Some(envelope) => {present: true, value: Some(envelope)}, + None => {present: false, value: no_begin()} + } +} + +fn peek_callback(value: Option) -> CallbackPeek { + match value { + Some(envelope) => {present: true, value: Some(envelope)}, + None => {present: false, value: no_callback()} + } +} + fn failed_start(code: string, mode: string, fallback_used: bool) -> PkceStartResult { - {ok: false, code: code, selected_mode: mode, fallback_used: fallback_used, value: null} + {ok: false, code: code, selected_mode: mode, fallback_used: fallback_used, value: no_begin()} +} + +fn failed_session(code: string, mode: string, fallback_used: bool) -> PkceSessionResult { + { + ok: false, + code: code, + selected_mode: mode, + fallback_used: fallback_used, + begin: no_begin(), + waited: no_callback() + } } fn retry_intent(intent: BoundedPublicOAuthIntent) -> BoundedPublicOAuthIntent { @@ -49,18 +83,14 @@ fn retry_intent(intent: BoundedPublicOAuthIntent) -> BoundedPublicOAuthIntent { } } -fn project_failure(holder: AuthFailurePresence) -> FailureProjection { - let present: bool = holder.has("failure") && holder.failure != null; - if present => { - let failure: AuthBridgeFailure = holder.failure; - let code: string = failure.code.copy(); - {present: true, code: code} - } else => { - {present: false, code: "host_failure"} +fn project_failure(failure_value: Option) -> FailureProjection { + match failure_value { + Some(failure) => {present: true, code: failure.code.copy()}, + None => {present: false, code: "host_failure"} } } -fn begin_success(value: PkceBeginEnvelope?, mode: string, fallback_used: bool) -> PkceStartResult { +fn begin_success(value: Option, mode: string, fallback_used: bool) -> PkceStartResult { {ok: true, code: "ok", selected_mode: mode, fallback_used: fallback_used, value: value} } @@ -79,50 +109,26 @@ pub fn begin( } else => { let fallback_intent: BoundedPublicOAuthIntent = retry_intent(intent.copy()); let first: PkceBeginResultEnvelope = oauth::pkce_begin_result(&policy_handle, intent); - let first_has_value: bool = first.has("value"); - let first_has_error: bool = first.has("error"); let first_ok: bool = first.ok; - let first_failure_holder: AuthFailurePresence = if first_has_error => { - {failure: first.error} - } else => { - {failure: null} - }; - let first_failure: FailureProjection = project_failure(first_failure_holder); + let first_failure: FailureProjection = project_failure(first.error); let first_failure_present: bool = first_failure.present.copy(); let first_failure_code: string = first_failure.code.copy(); - let first_value_holder: BeginEnvelopePresence = if first_has_value => { - {value: first.value} - } else => { - {value: null} - }; - let first_value_present: bool = first_value_holder.has("value"); + let first_value: BeginPeek = peek_begin(first.value); + let first_value_present: bool = first_value.present; let retry_allowed: bool = !first_ok && !first_value_present && first_failure_present && selected_mode == "browser" && allow_manual_fallback && first_failure_code == "transport_error"; if first_ok && first_value_present && !first_failure_present => { - let value: PkceBeginEnvelope? = first_value_holder.value; - begin_success(value, selected_mode, false) + begin_success(first_value.value, selected_mode, false) } else if retry_allowed => { let second: PkceBeginResultEnvelope = oauth::pkce_begin_result(&policy_handle, fallback_intent); - let second_has_value: bool = second.has("value"); - let second_has_error: bool = second.has("error"); let second_ok: bool = second.ok; - let second_failure_holder: AuthFailurePresence = if second_has_error => { - {failure: second.error} - } else => { - {failure: null} - }; - let second_failure: FailureProjection = project_failure(second_failure_holder); + let second_failure: FailureProjection = project_failure(second.error); let second_failure_present: bool = second_failure.present.copy(); let second_failure_code: string = second_failure.code.copy(); - let second_value_holder: BeginEnvelopePresence = if second_has_value => { - {value: second.value} - } else => { - {value: null} - }; - let second_value_present: bool = second_value_holder.has("value"); + let second_value: BeginPeek = peek_begin(second.value); + let second_value_present: bool = second_value.present; if second_ok && second_value_present && !second_failure_present => { - let value: PkceBeginEnvelope? = second_value_holder.value; - begin_success(value, "manual", true) + begin_success(second_value.value, "manual", true) } else if !second_ok && !second_value_present && second_failure_present => { begin_failure(second_failure_code, "manual", true) } else => { @@ -138,37 +144,25 @@ pub fn begin( fn wait_session(begin: PkceBeginEnvelope, selected_mode: string, fallback_used: bool) -> PkceSessionResult { let native: CallbackResultEnvelope = oauth::callback_wait_result(&begin.callback_handle); - let has_wait_value: bool = native.has("value"); - let has_wait_error: bool = native.has("error"); let wait_ok: bool = native.ok; - let wait_failure_holder: AuthFailurePresence = if has_wait_error => { - {failure: native.error} - } else => { - {failure: null} - }; - let wait_failure: FailureProjection = project_failure(wait_failure_holder); + let wait_failure: FailureProjection = project_failure(native.error); let failure_present: bool = wait_failure.present.copy(); let failure_code: string = wait_failure.code.copy(); - let wait_value_holder: CallbackEnvelopePresence = if has_wait_value => { - {value: native.value} - } else => { - {value: null} - }; - let value_present: bool = wait_value_holder.has("value"); + let wait_value: CallbackPeek = peek_callback(native.value); + let value_present: bool = wait_value.present; if wait_ok && value_present && !failure_present => { - let waited: CallbackEnvelope? = wait_value_holder.value; { ok: true, code: "ok", selected_mode: selected_mode, fallback_used: fallback_used, - begin: begin, - waited: waited + begin: Some(begin), + waited: wait_value.value } } else if !wait_ok && !value_present && failure_present => { - {ok: false, code: failure_code, selected_mode: selected_mode, fallback_used: fallback_used, begin: null, waited: null} + failed_session(failure_code, selected_mode, fallback_used) } else => { - {ok: false, code: "host_failure", selected_mode: selected_mode, fallback_used: fallback_used, begin: null, waited: null} + failed_session("host_failure", selected_mode, fallback_used) } } @@ -178,18 +172,16 @@ pub fn finish(start: PkceStartResult) -> PkceSessionResult { let code: string = start.code; let fallback_used: bool = start.fallback_used; let start_ok: bool = start.ok; - let begin_holder: BeginEnvelopePresence = if has_begin => { - {value: start.value} - } else => { - {value: null} - }; - let begin_present: bool = begin_holder.has("value"); + let begin_carrier: Option = if has_begin => { start.value } else => { no_begin() }; + let begin_holder: BeginPeek = peek_begin(begin_carrier); if !start_ok => { - {ok: false, code: code, selected_mode: selected_mode, fallback_used: fallback_used, begin: null, waited: null} - } else if !begin_present => { - {ok: false, code: "host_failure", selected_mode: selected_mode, fallback_used: fallback_used, begin: null, waited: null} + failed_session(code, selected_mode, fallback_used) + } else if !begin_holder.present => { + failed_session("host_failure", selected_mode, fallback_used) } else => { - let begin: PkceBeginEnvelope = begin_holder.value; - wait_session(begin, selected_mode, fallback_used) + match begin_holder.value { + Some(begin) => wait_session(begin, selected_mode, fallback_used), + None => failed_session("host_failure", selected_mode, fallback_used) + } } } diff --git a/rss/auth/runner_entry.rss b/rss/auth/runner_entry.rss index d04dab4..ad829d6 100644 --- a/rss/auth/runner_entry.rss +++ b/rss/auth/runner_entry.rss @@ -5,17 +5,17 @@ use auth; struct AuthCommandInput { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } struct AuthEntryResult { ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option } fn metadata_result( @@ -26,7 +26,7 @@ fn metadata_result( { ok: snapshot_ok && loaded.ok, operation: operation, - credential_id: loaded.metadata.credential_id, + credential_id: Some(loaded.metadata.credential_id), generation: loaded.metadata.generation, status: loaded.metadata.status, error: loaded.error @@ -36,19 +36,24 @@ fn metadata_result( pub fn run(home: resource, command: AuthCommandInput) -> AuthEntryResult { let snapshot = config::load_snapshot(&home); let policy = snapshot.policy_handle; + let credential_present: bool = match command.credential_id.copy() { + Some(_) => true, + None => false, + }; if command.operation == "metadata" => { - if command.credential_id != null => { - let credential_id: string = command.credential_id; + if credential_present => { + let credential_id: string = command.credential_id.unwrap_or(""); let loaded = auth::load_metadata(credential_id, &policy); metadata_result(command.operation, snapshot.ok, loaded) } else => { + let missing: Option = None; { ok: false, operation: command.operation, - credential_id: null, + credential_id: missing, generation: snapshot.policy_generation, status: "credential_id_required", - error: "credential_id_required" + error: Some("credential_id_required") } } } else if command.operation == "snapshot" => { @@ -57,20 +62,21 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt if credential_refs.length > 0 { credential_id = credential_refs[0]; } - if command.credential_id != null { - credential_id = command.credential_id; + if credential_present { + credential_id = command.credential_id.unwrap_or(""); } if credential_id != "" => { let loaded = auth::load_metadata(credential_id, &policy); metadata_result("snapshot", snapshot.ok, loaded) } else => { + let missing: Option = None; { ok: snapshot.ok, operation: "snapshot", - credential_id: null, + credential_id: missing, generation: snapshot.policy_generation, status: "snapshot_empty", - error: null + error: None } } } else => { @@ -80,7 +86,7 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt credential_id: command.credential_id, generation: snapshot.policy_generation, status: "unsupported_operation", - error: "unsupported_operation" + error: Some("unsupported_operation") } } } diff --git a/rss/auth/store_entry.rss b/rss/auth/store_entry.rss index c6b6d28..64a0d69 100644 --- a/rss/auth/store_entry.rss +++ b/rss/auth/store_entry.rss @@ -170,6 +170,23 @@ fn delete_request(context: map) -> map { } } +/// The fixture adapter accepts one map containing the credential binding. +fn access_handle_contract(context: map) -> map { + auth::access_handle({ + credential_id: map_string(context, "credential_id", ""), + expected_generation: map_int(context, "expected_generation", 0), + policy_handle: context["policy_handle"].copy() + }) +} + +fn refresh_handle_contract(context: map) -> map { + auth::refresh_handle({ + credential_id: map_string(context, "credential_id", ""), + expected_generation: map_int(context, "expected_generation", 0), + policy_handle: context["policy_handle"].copy() + }) +} + fn handle_request(context: map) -> map { { credential_id: map_string(context, "credential_id", ""), @@ -253,9 +270,9 @@ fn run_save_replay(context: map) -> map { fn run_handle(context: map, kind: string) -> map { let issued: map = if kind == "refresh" => { - auth::refresh_handle(handle_request(context)) + refresh_handle_contract(context) } else => { - auth::access_handle(handle_request(context)) + access_handle_contract(context) }; let ok: bool = map_bool(issued, "ok", false); if ok => { @@ -287,9 +304,9 @@ fn issued_or_error(issued: map) -> map { fn probe_handle_copy(context: map, kind: string) -> map { let issued: map = if kind == "refresh_copy" => { - auth::refresh_handle(handle_request(context)) + refresh_handle_contract(context) } else => { - auth::access_handle(handle_request(context)) + access_handle_contract(context) }; let ok: bool = map_bool(issued, "ok", false); if ok => { @@ -300,7 +317,7 @@ fn probe_handle_copy(context: map, kind: string) -> map { } fn probe_handle_forged(context: map) -> map { - let issued: map = auth::access_handle(handle_request(context)); + let issued: map = access_handle_contract(context); let ok: bool = map_bool(issued, "ok", false); if ok => { let forged: map = { @@ -314,7 +331,7 @@ fn probe_handle_forged(context: map) -> map { } fn probe_handle_serialized(context: map) -> map { - let issued: map = auth::access_handle(handle_request(context)); + let issued: map = access_handle_contract(context); let ok: bool = map_bool(issued, "ok", false); if ok => { let handle_type: string = type(issued["handle"].copy()); @@ -333,9 +350,9 @@ fn probe_handle_serialized(context: map) -> map { fn probe_handle_replay(context: map, kind: string) -> map { let issued: map = if kind == "refresh_replay" => { - auth::refresh_handle(handle_request(context)) + refresh_handle_contract(context) } else => { - auth::access_handle(handle_request(context)) + access_handle_contract(context) }; let ok: bool = map_bool(issued, "ok", false); if ok => { @@ -351,7 +368,7 @@ fn probe_handle_replay(context: map, kind: string) -> map { } fn probe_refresh_validate(context: map) -> map { - let issued: map = auth::refresh_handle(handle_request(context)); + let issued: map = refresh_handle_contract(context); let ok: bool = map_bool(issued, "ok", false); if ok => { let mismatched: map = wrap_probe(auth::check_handle(issued["handle"].copy(), { op: "mismatch" })); @@ -367,7 +384,7 @@ fn probe_refresh_validate(context: map) -> map { } fn probe_handle_expire(context: map) -> map { - let issued: map = auth::access_handle(handle_request(context)); + let issued: map = access_handle_contract(context); let ok: bool = map_bool(issued, "ok", false); if ok => { wrap_probe(auth::check_handle(issued["handle"].copy(), { op: "expire" })) @@ -378,9 +395,9 @@ fn probe_handle_expire(context: map) -> map { fn probe_stale_after_save(context: map, kind: string) -> map { let issued: map = if kind == "refresh_stale_after_save" => { - auth::refresh_handle(handle_request(context)) + refresh_handle_contract(context) } else => { - auth::access_handle(handle_request(context)) + access_handle_contract(context) }; let ok: bool = map_bool(issued, "ok", false); if ok => { @@ -397,12 +414,12 @@ fn probe_stale_after_save(context: map, kind: string) -> map { } fn probe_refresh_duplicate(context: map) -> map { - let first: map = auth::refresh_handle(handle_request(context)); - let second: map = auth::refresh_handle(handle_request(context)); + let first: map = refresh_handle_contract(context); + let second: map = refresh_handle_contract(context); let first_ok: bool = map_bool(first, "ok", false); if first_ok => { let consumed: map = wrap_probe(auth::check_handle(first["handle"].copy(), { op: "consume" })); - let third: map = auth::refresh_handle(handle_request(context)); + let third: map = refresh_handle_contract(context); { first_ok: true, second_ok: map_bool(second, "ok", false), diff --git a/rss/auth/timing_contract.rss b/rss/auth/timing_contract.rss index f9acb9e..6464ddb 100644 --- a/rss/auth/timing_contract.rss +++ b/rss/auth/timing_contract.rss @@ -11,6 +11,14 @@ pub fn now(policy: resource) -> int { oauth::now_ms(&policy) } -pub fn wait(policy: resource, millis: int) -> null { +pub fn wait(policy: resource, millis: int) -> () { oauth::wait_ms(&policy, millis) } + +pub fn now_result(policy: resource) -> OAuthNowResultEnvelope { + oauth::now_ms_result(&policy) +} + +pub fn wait_result(policy: resource, millis: int) -> OAuthWaitResultEnvelope { + oauth::wait_ms_result(&policy, millis) +} diff --git a/rss/auth/token_metadata.rss b/rss/auth/token_metadata.rss index e256169..bbf2f79 100644 --- a/rss/auth/token_metadata.rss +++ b/rss/auth/token_metadata.rss @@ -8,13 +8,25 @@ struct TokenMetadataIntent { scopes: array, expiry_pointer: string, token_type_pointer: string, - account_pointer: string? + account_pointer: Option +} + +/// What the transport said, with slot *presence* instead of slot handles. +/// A caller that also has to hand the handles to a later save can snapshot the +/// frame first; the parser's fail-closed access-slot rule still applies. +struct MetadataFrame { + ok: bool, + error: Option, + status: int, + body: string, + access_present: bool, + refresh_present: bool } struct TokenMetadataResult { ok: bool, code: string, - value: CredentialMetadata? + value: Option } struct ParsedExpiry { @@ -37,21 +49,129 @@ fn max_int() -> int { 9223372036854775807 } +// --------------------------------------------------------------------------- +// Logical Option carriers +// --------------------------------------------------------------------------- + +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn int_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn field_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn slot_present(value: Option>) -> bool { + match value { + Some(_) => true, + None => false + } +} + +/// Reads a logical `Option` exactly once: the guest struct field it +/// usually comes from is consumed by the read, so presence and payload have to +/// be taken together. +struct TextPeek { + present: bool, + value: string +} + +fn peek_text(value: Option) -> TextPeek { + match value { + Some(text) => {present: true, value: text}, + None => {present: false, value: ""} + } +} + +fn no_failure() -> Option { + None +} + +fn no_text() -> Option { + None +} + +fn no_int() -> Option { + None +} + +fn no_metadata() -> Option { + None +} + +fn no_field() -> Option { + None +} + +fn empty_field() -> PublicJsonFieldEnvelope { + { + kind: "", + string_value: no_text(), + int_value: no_int(), + float_value: no_float(), + bool_value: no_bool(), + length: no_int() + } +} + +fn no_float() -> Option { + None +} + +fn no_bool() -> Option { + None +} + fn failure(code: string) -> TokenMetadataResult { - {ok: false, code: code, value: null} + {ok: false, code: code, value: no_metadata()} } fn empty_transport_response() -> TransportResponseEnvelope { - {ok: false, error: null, status: 0, public_headers: [], body_without_secret_fields: "", retry_after_ms: null, access_slot: null, refresh_slot: null, device_handle: null} + let access_slot: Option> = None; + let refresh_slot: Option> = None; + let device_handle: Option> = None; + { + ok: false, + error: no_text(), + status: 0, + public_headers: [], + body_without_secret_fields: "", + retry_after_ms: no_int(), + access_slot: access_slot, + refresh_slot: refresh_slot, + device_handle: device_handle + } } fn parse_expiry(envelope: PublicJsonFieldResultEnvelope) -> ParsedExpiry { let mut parsed: ParsedExpiry = {valid: false, seconds: 0}; - let has_error: bool = envelope.has("error") && envelope.error != null; - if envelope.ok && !has_error && envelope.value.copy() != null { - let selected: PublicJsonFieldEnvelope = envelope.value; - if selected.kind == "int" && selected.int_value.copy() != null { - let seconds: int = selected.int_value; + let failure_value: Option = envelope.error; + let envelope_value: Option = envelope.value; + if envelope.ok && !failure_present(failure_value) && field_present(envelope_value) { + let selected: PublicJsonFieldEnvelope = envelope_value.unwrap_or(empty_field()); + let seconds_value: Option = selected.int_value; + if selected.kind == "int" && int_present(seconds_value) { + let seconds: int = seconds_value.unwrap_or(0); if seconds > 0 && seconds <= max_int() / 1000 { parsed = {valid: true, seconds: seconds}; } @@ -62,11 +182,13 @@ fn parse_expiry(envelope: PublicJsonFieldResultEnvelope) -> ParsedExpiry { fn parse_token_type(envelope: PublicJsonFieldResultEnvelope) -> ParsedTokenType { let mut parsed: ParsedTokenType = {valid: false, value: ""}; - let has_error: bool = envelope.has("error") && envelope.error != null; - if envelope.ok && !has_error && envelope.value.copy() != null { - let selected: PublicJsonFieldEnvelope = envelope.value; - if selected.kind == "string" && selected.string_value.copy() != null { - let value: string = selected.string_value; + let failure_value: Option = envelope.error; + let envelope_value: Option = envelope.value; + if envelope.ok && !failure_present(failure_value) && field_present(envelope_value) { + let selected: PublicJsonFieldEnvelope = envelope_value.unwrap_or(empty_field()); + let text_value: Option = selected.string_value; + if selected.kind == "string" && text_present(text_value) { + let value: string = text_value.unwrap_or(""); if value == "Bearer" { parsed = {valid: true, value: value}; } @@ -77,13 +199,15 @@ fn parse_token_type(envelope: PublicJsonFieldResultEnvelope) -> ParsedTokenType fn parse_account(envelope: PublicJsonFieldResultEnvelope) -> ParsedAccount { let mut parsed: ParsedAccount = {valid: false, has_value: false, value: ""}; - let has_error: bool = envelope.has("error") && envelope.error != null; - if envelope.ok && !has_error && envelope.value.copy() != null { - let selected: PublicJsonFieldEnvelope = envelope.value; + let failure_value: Option = envelope.error; + let envelope_value: Option = envelope.value; + if envelope.ok && !failure_present(failure_value) && field_present(envelope_value) { + let selected: PublicJsonFieldEnvelope = envelope_value.unwrap_or(empty_field()); + let text_value: Option = selected.string_value; if selected.kind.copy() == "missing" || selected.kind.copy() == "null" { parsed = {valid: true, has_value: false, value: ""}; - } else if selected.kind == "string" && selected.string_value.copy() != null { - let value: string = selected.string_value; + } else if selected.kind == "string" && text_present(text_value) { + let value: string = text_value.unwrap_or(""); if value != "" { parsed = {valid: true, has_value: true, value: value}; } @@ -96,50 +220,79 @@ pub fn parse( policy_handle: resource, response: TransportResponseEnvelope, intent: TokenMetadataIntent +) -> TokenMetadataResult { + let frame: MetadataFrame = { + ok: response.ok, + error: response.error, + status: response.status, + body: response.body_without_secret_fields, + access_present: slot_present(response.access_slot), + refresh_present: slot_present(response.refresh_slot) + }; + parse_frame(policy_handle, frame, intent) +} + +/// Copies the transport verdict out of a response frame *before* the caller +/// takes the slot handles out of it. A caller that owns the handles for a later +/// save cannot hand them over twice, so it reports their presence here instead: +/// `parse_frame` still fails closed on a frame that arrived without an access +/// slot, but it no longer needs to own the handle to do so. +pub fn parse_frame( + policy_handle: resource, + frame: MetadataFrame, + intent: TokenMetadataIntent ) -> TokenMetadataResult { let mut result: TokenMetadataResult = failure("invalid_token_metadata"); - let valid_intent: bool = intent.provider != "" && intent.source != "" && - intent.expected_generation >= 0 && intent.expiry_pointer != "" && - intent.token_type_pointer != "" && - (!intent.has("account_pointer") || intent.account_pointer == null || intent.account_pointer != ""); + // A guest struct field is read out of its backing map, so every field of a + // struct parameter must be taken exactly once: a second read is a lookup of + // a key the first one already removed. Snapshot the intent here and work + // from the locals. + let provider: string = intent.provider; + let source: string = intent.source; + let expected_generation: int = intent.expected_generation; + let scopes: array = intent.scopes; + let expiry_pointer: string = intent.expiry_pointer; + let token_type_pointer: string = intent.token_type_pointer; + let account_pointer_peek: TextPeek = peek_text(intent.account_pointer); + let account_pointer_present: bool = account_pointer_peek.present; + let account_pointer: string = account_pointer_peek.value; + let valid_intent: bool = provider != "" && source != "" && + expected_generation >= 0 && expiry_pointer != "" && + token_type_pointer != "" && + (!account_pointer_present || account_pointer != ""); + let transport_error: Option = frame.error; if valid_intent { - let has_transport_error: bool = response.has("error") && response.error != null; - if !response.ok || has_transport_error { + if !frame.ok || text_present(transport_error) { result = failure("token_transport_failed"); - } else if response.status != 200 { + } else if frame.status != 200 { result = failure("token_http_rejected"); - } else if !response.has("access_slot") || type(response["access_slot"]) == "null" { + } else if !frame.access_present { result = failure("invalid_token_metadata"); } else { - let body: string = response.body_without_secret_fields; - let expiry_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy_handle, body.copy(), intent.expiry_pointer); - let token_type_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy_handle, body.copy(), intent.token_type_pointer); + let body: string = frame.body; + let expiry_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy_handle, body.copy(), expiry_pointer); + let token_type_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy_handle, body.copy(), token_type_pointer); let expiry: ParsedExpiry = parse_expiry(expiry_field); let token_type: ParsedTokenType = parse_token_type(token_type_field); let mut account: ParsedAccount = {valid: true, has_value: false, value: ""}; - let mut account_pointer: string? = null; - if intent.has("account_pointer") { - account_pointer = intent.account_pointer; - } - if account_pointer != null { - let pointer: string = account_pointer; - let account_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy_handle, body.copy(), pointer); + if account_pointer_present { + let account_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy_handle, body.copy(), account_pointer); account = parse_account(account_field); } if !expiry.valid || !token_type.valid || !account.valid { result = failure("invalid_token_metadata"); } else { let clock: OAuthNowResultEnvelope = oauth::now_ms_result(&policy_handle); - let clock_has_error: bool = clock.has("error") && clock.error.copy() != null; - if !clock.ok || clock_has_error || clock.value.copy() == null { - let mut code: string = "clock_unavailable"; - if clock_has_error { - let clock_error: AuthBridgeFailure = clock.error; - code = clock_error.code; - } + let clock_failure: Option = clock.error; + let clock_value: Option = clock.value; + if !clock.ok || failure_present(clock_failure) || !int_present(clock_value) { + let code: string = match clock_failure { + Some(value) => value.code.copy(), + None => "clock_unavailable" + }; result = failure(code); } else { - let now_ms: int = clock.value; + let now_ms: int = clock_value.unwrap_or(-1); if now_ms < 0 { result = failure("invalid_token_metadata"); } else { @@ -147,25 +300,25 @@ pub fn parse( if now_ms > max_int() - lifetime_ms { result = failure("invalid_token_metadata"); } else { - let mut account_id: string? = null; + let mut account_id: Option = no_text(); if account.has_value { - account_id = account.value; + account_id = Some(account.value); } - let has_refresh_token: bool = response.has("refresh_slot") && type(response["refresh_slot"]) != "null"; + let has_refresh_token: bool = frame.refresh_present; let metadata: CredentialMetadata = { - provider: intent.provider, + provider: provider, kind: "oauth", - source: intent.source, + source: source, token_type: token_type.value, expires_at_ms: now_ms + lifetime_ms, - scopes: intent.scopes, + scopes: scopes, account_id: account_id, - generation: intent.expected_generation, + generation: expected_generation, status: "active", - last_refresh_at_ms: null, + last_refresh_at_ms: no_int(), has_refresh_token: has_refresh_token }; - result = {ok: true, code: "ok", value: metadata}; + result = {ok: true, code: "ok", value: Some(metadata)}; } } } @@ -181,11 +334,13 @@ pub fn parse_transport_result( intent: TokenMetadataIntent ) -> TokenMetadataResult { let mut result: TokenMetadataResult = failure("token_transport_failed"); - let has_error: bool = response.has("error") && response.error != null; - if response.ok && !has_error && response.value != null { - let maybe_value: TransportResponseEnvelope? = response.value; - let value: TransportResponseEnvelope = maybe_value.unwrap_or(empty_transport_response()); - result = parse(policy_handle, value, intent); + let transport_failure: Option = response.error; + let envelope_value: Option = response.value; + if response.ok && !failure_present(transport_failure) { + result = match envelope_value { + Some(value) => parse(policy_handle, value, intent), + None => failure("token_transport_failed") + }; } result } diff --git a/rss/auth/token_save.rss b/rss/auth/token_save.rss index b2a0a0b..a0dfd5b 100644 --- a/rss/auth/token_save.rss +++ b/rss/auth/token_save.rss @@ -12,18 +12,52 @@ struct TokenSaveInput { struct TokenSaveResult { ok: bool, code: string, - outcome: string?, - value: AuthMetadata?, - failure: AuthBridgeFailure? + outcome: Option, + value: Option, + failure: Option +} + +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_failure() -> Option { + None +} + +fn no_text() -> Option { + None +} + +fn no_metadata() -> Option { + None +} + +fn no_metadata_envelope() -> Option { + None +} + +fn no_int() -> Option { + None } fn failed(code: string) -> TokenSaveResult { { ok: false, code: code, - outcome: null, - value: null, - failure: null + outcome: no_text(), + value: no_metadata(), + failure: no_failure() } } @@ -31,13 +65,15 @@ fn succeeded(outcome: string, metadata: AuthMetadata) -> TokenSaveResult { { ok: true, code: "ok", - outcome: outcome, - value: metadata, - failure: null + outcome: Some(outcome), + value: Some(metadata), + failure: no_failure() } } fn empty_metadata() -> AuthMetadata { + let account_id: Option = None; + let last_refresh_at_ms: Option = None; { credential_id: "", provider: "", @@ -46,10 +82,10 @@ fn empty_metadata() -> AuthMetadata { token_type: "", expires_at_ms: 0, scopes: [], - account_id: null, + account_id: account_id, generation: 0, status: "", - last_refresh_at_ms: null, + last_refresh_at_ms: last_refresh_at_ms, has_refresh_token: false } } @@ -57,7 +93,7 @@ fn empty_metadata() -> AuthMetadata { fn empty_metadata_envelope() -> AuthMetadataEnvelope { { ok: false, - error: null, + error: no_text(), metadata: empty_metadata(), outcome: "" } @@ -99,44 +135,43 @@ fn recognized_native_code(code: string, fallback: string) -> string { } } -fn native_failure_code(failure_value: AuthBridgeFailure?, fallback: string) -> string { - if failure_value != null => { - let failure: AuthBridgeFailure = failure_value; - recognized_native_code(failure.code.copy(), fallback) - } else => { - fallback +fn native_failure_code(failure_value: Option, fallback: string) -> string { + match failure_value { + Some(failure) => recognized_native_code(failure.code.copy(), fallback), + None => fallback } } -fn failed_native(failure_value: AuthBridgeFailure?, fallback: string) -> TokenSaveResult { - if failure_value != null => { - let failure: AuthBridgeFailure = failure_value; - let code: string = native_failure_code(failure_value.copy(), fallback.copy()); - { - ok: false, - code: code.copy(), - outcome: null, - value: null, - failure: { - code: code.copy(), - cancelled: failure.cancelled.copy(), - deadline_exceeded: failure.deadline_exceeded.copy(), - revoked: failure.revoked.copy(), - transport_phase: null, - transport_category: null - } - } - } else => { - failed(fallback.copy()) +fn native_failure_result(failure: AuthBridgeFailure, fallback: string) -> TokenSaveResult { + let code: string = native_failure_code(Some(failure), fallback.copy()); + let detail: AuthBridgeFailure = { + code: code.copy(), + cancelled: failure.cancelled.copy(), + deadline_exceeded: failure.deadline_exceeded.copy(), + revoked: failure.revoked.copy(), + transport_phase: failure.transport_phase.copy(), + transport_category: failure.transport_category.copy() + }; + { + ok: false, + code: code.copy(), + outcome: no_text(), + value: no_metadata(), + failure: Some(detail) } } -fn native_inner_code(code_value: string?, fallback: string) -> string { - if code_value != null => { - let code: string = code_value; - recognized_native_code(code.copy(), fallback) - } else => { - fallback +fn failed_native(failure_value: Option, fallback: string) -> TokenSaveResult { + match failure_value { + Some(failure) => native_failure_result(failure, fallback), + None => failed(fallback.copy()) + } +} + +fn native_inner_code(code_value: Option, fallback: string) -> string { + match code_value { + Some(code) => recognized_native_code(code.copy(), fallback), + None => fallback } } @@ -218,7 +253,7 @@ pub fn save( policy: resource, input: TokenSaveInput, access_slot: resource, - refresh_slot: resource? + refresh_slot: Option> ) -> TokenSaveResult { let credential_id: string = input.credential_id; let expected_generation: int = input.expected_generation; @@ -255,14 +290,16 @@ pub fn save( }; let saved: AuthSaveResultEnvelope = auth::save_if_generation_result(&request); let saved_has_value: bool = saved.has("value"); - let saved_metadata: AuthMetadataEnvelope? = if saved_has_value => { saved.value.copy() } else => { null }; + let saved_outer_failure: Option = saved.error.copy(); + let saved_metadata: Option = if saved_has_value => { saved.value.copy() } else => { no_metadata_envelope() }; let saved_envelope: AuthMetadataEnvelope = saved_metadata.unwrap_or(empty_metadata_envelope()); - if !saved.ok.copy() || saved.error.copy() != null => { - failed_native(saved.error.copy(), "save_failed") + let saved_inner_error: Option = saved_envelope.error.copy(); + if !saved.ok.copy() || failure_present(saved_outer_failure) => { + failed_native(saved_outer_failure, "save_failed") } else if !saved_has_value => { failed("save_failed") - } else if !saved_envelope.ok.copy() || saved_envelope.error.copy() != null => { + } else if !saved_envelope.ok.copy() || text_present(saved_inner_error) => { failed(native_inner_code(saved_envelope.error.copy(), "save_failed")) } else => { let saved_metadata: AuthMetadata = saved_envelope.metadata.copy(); @@ -278,7 +315,8 @@ pub fn save( failed("invalid_saved_metadata") } else => { let snapshot: ConfigSnapshotEnvelope = config::load_snapshot(&home); - if !snapshot.ok || snapshot.error != null => { + let snapshot_error: Option = snapshot.error; + if !snapshot.ok || text_present(snapshot_error) => { failed("snapshot_unavailable") } else => { let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result( @@ -286,14 +324,16 @@ pub fn save( &snapshot.policy_handle ); let loaded_has_value: bool = loaded.has("value"); - let loaded_metadata: AuthMetadataEnvelope? = if loaded_has_value => { loaded.value.copy() } else => { null }; + let loaded_outer_failure: Option = loaded.error.copy(); + let loaded_metadata: Option = if loaded_has_value => { loaded.value.copy() } else => { no_metadata_envelope() }; let loaded_envelope: AuthMetadataEnvelope = loaded_metadata.unwrap_or(empty_metadata_envelope()); - if !loaded.ok.copy() || loaded.error.copy() != null => { - failed_native(loaded.error.copy(), "credential_unavailable") + let loaded_inner_error: Option = loaded_envelope.error.copy(); + if !loaded.ok.copy() || failure_present(loaded_outer_failure) => { + failed_native(loaded_outer_failure, "credential_unavailable") } else if !loaded_has_value => { failed("credential_unavailable") - } else if !loaded_envelope.ok.copy() || loaded_envelope.error.copy() != null => { + } else if !loaded_envelope.ok.copy() || text_present(loaded_inner_error) => { failed(native_inner_code(loaded_envelope.error.copy(), "credential_unavailable")) } else => { let readback: AuthMetadata = loaded_envelope.metadata.copy(); @@ -310,10 +350,11 @@ pub fn save( let clock: OAuthNowResultEnvelope = oauth::now_ms_result(&snapshot.policy_handle); let clock_has_value: bool = clock.has("value"); - let clock_value: int? = if clock_has_value => { clock.value.copy() } else => { null }; + let clock_failure: Option = clock.error.copy(); + let clock_value: Option = if clock_has_value => { clock.value.copy() } else => { no_int() }; let now_ms: int = clock_value.unwrap_or(-1); - if !clock.ok.copy() || clock.error.copy() != null => { - failed_native(clock.error.copy(), "clock_unavailable") + if !clock.ok.copy() || failure_present(clock_failure) => { + failed_native(clock_failure, "clock_unavailable") } else if !clock_has_value || now_ms < 0 => { failed("clock_unavailable") } else if readback.expires_at_ms.copy() <= now_ms => { diff --git a/rss/auth/transport_contract.rss b/rss/auth/transport_contract.rss index dae131b..a55fcbc 100644 --- a/rss/auth/transport_contract.rss +++ b/rss/auth/transport_contract.rss @@ -12,7 +12,7 @@ pub fn run(home: resource) -> map { public_headers: [{name: "accept", value: "application/json"}], public_body: "" }; - let usage = {kind: "none"}; + let usage: TransportCredentialUse = {kind: "none"}; let response = oauth::transport(&request, &usage); { ok: response.ok, @@ -37,7 +37,7 @@ pub fn probe(home: resource, id: string, path: string, method: s public_headers: [{name: header_name, value: "application/json"}], public_body: "" }; - let usage = {kind: kind, access_handle: access.handle}; + let usage: TransportCredentialUse = {kind: kind, access_handle: Some(access.handle)}; let response = oauth::transport(&request, &usage); {ok: response.ok, status: response.status} } else => { @@ -49,7 +49,7 @@ pub fn probe(home: resource, id: string, path: string, method: s public_headers: [{name: header_name, value: "application/json"}], public_body: "" }; - let usage = {kind: kind}; + let usage: TransportCredentialUse = {kind: kind}; let response = oauth::transport(&request, &usage); {ok: response.ok, status: response.status} } @@ -70,7 +70,10 @@ pub fn probe_refresh(home: resource) -> map { public_headers: [], public_body: "" }; - let usage = {kind: "none", refresh_handle: refresh.handle}; + // An impossible combination on purpose: `kind: "none"` with a refresh + // handle must be refused by the host, so the handle is carried explicitly + // as a `Some` instead of disappearing into an absent field. + let usage: TransportCredentialUse = {kind: "none", refresh_handle: Some(refresh.handle)}; let response = oauth::transport(&request, &usage); {ok: response.ok} } @@ -84,7 +87,7 @@ pub fn probe_policy(policy: resource) -> map { public_headers: [], public_body: "" }; - let usage = {kind: "none"}; + let usage: TransportCredentialUse = {kind: "none"}; let response = oauth::transport(&request, &usage); {ok: response.ok} } @@ -96,7 +99,7 @@ pub fn access_transport(home: resource) -> map { policy_handle: policy, credential_id: "fixture-codex", method: "GET", path: "/oauth/token", public_headers: [], public_body: "" }; - let usage = {kind: "access", access_handle: access}; + let usage: TransportCredentialUse = {kind: "access", access_handle: Some(access)}; let response = oauth::transport(&request, &usage); {status: response.status, body: response.body_without_secret_fields, access_slot: response.access_slot} } @@ -111,21 +114,21 @@ pub fn issue_refresh(policy: resource) -> resource { pub fn send_access(policy: resource, access: resource, id: string, path: string, kind: string, body: string) -> map { let request = {policy_handle: policy, credential_id: id, method: "GET", path: path, public_headers: [], public_body: body}; - let usage = {kind: kind, access_handle: access}; + let usage: TransportCredentialUse = {kind: kind, access_handle: Some(access)}; let response = oauth::transport(&request, &usage); {status: response.status, access_slot: response.access_slot, refresh_slot: response.refresh_slot} } pub fn send_refresh(policy: resource, refresh: resource, id: string, path: string, kind: string, body: string) -> map { let request = {policy_handle: policy, credential_id: id, method: "POST", path: path, public_headers: [], public_body: body}; - let usage = {kind: kind, refresh_handle: refresh}; + let usage: TransportCredentialUse = {kind: kind, refresh_handle: Some(refresh)}; let response = oauth::transport(&request, &usage); {status: response.status, access_slot: response.access_slot, refresh_slot: response.refresh_slot} } pub fn send_both(policy: resource, access: resource, refresh: resource) -> map { let request = {policy_handle: policy, credential_id: "fixture-codex", method: "POST", path: "/oauth/token", public_headers: [], public_body: ""}; - let usage = {kind: "access", access_handle: access, refresh_handle: refresh}; + let usage: TransportCredentialUse = {kind: "access", access_handle: Some(access), refresh_handle: Some(refresh)}; let response = oauth::transport(&request, &usage); {status: response.status} } @@ -135,13 +138,18 @@ pub fn issue_wrong_generation(policy: resource) -> resource, access: resource, expected: int) -> int { + let scopes: array = []; + let metadata: CredentialMetadata = { + provider: "openai-codex", kind: "oauth", source: "test", token_type: "Bearer", + expires_at_ms: 1900000000000, scopes: scopes, account_id: Some("synthetic"), + generation: expected, status: "active", last_refresh_at_ms: Some(1790000000000), + has_refresh_token: true + }; + let refresh_slot: Option> = None; let saved = auth::save_if_generation({ credential_id: "fixture-codex", expected_generation: expected, policy_handle: policy, - metadata: {provider: "openai-codex", kind: "oauth", source: "test", token_type: "Bearer", - expires_at_ms: 1900000000000, scopes: [], account_id: "synthetic", - generation: expected, status: "active", last_refresh_at_ms: 1790000000000, - has_refresh_token: true}, - access_slot: access, refresh_action: "preserve" + metadata: metadata, + access_slot: access, refresh_action: "preserve", refresh_slot: refresh_slot }); saved.metadata.generation } diff --git a/rss/auth/types.rss b/rss/auth/types.rss index 0e7b6f3..49998a6 100644 --- a/rss/auth/types.rss +++ b/rss/auth/types.rss @@ -18,5 +18,5 @@ struct CodexDeviceLoginResult { ok: bool, code: string, message: string, - metadata: AuthMetadataEnvelope? + metadata: Option } diff --git a/rss/config/path.rss b/rss/config/path.rss index 4da6379..7f0c49e 100644 --- a/rss/config/path.rss +++ b/rss/config/path.rss @@ -2,55 +2,80 @@ use config; struct AuthCommandInput { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } struct AuthEntryResult { ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option } fn entry( ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option ) -> AuthEntryResult { {ok: ok, operation: operation, credential_id: credential_id, generation: generation, status: status, error: error} } +fn path_success(resolved: string) -> AuthEntryResult { + entry(true, "config.path", None, 0, resolved.copy(), None) +} + +fn path_failure(code: string) -> AuthEntryResult { + entry(false, "config.path", None, 0, "", Some(code.copy())) +} + +fn path_value(value: Option) -> AuthEntryResult { + match value { + Some(resolved) => path_success(resolved), + None => path_failure("host_failure") + } +} + +fn path_error(error: Option, value: Option) -> AuthEntryResult { + match value { + Some(_) => path_failure("host_failure"), + None => match error { + Some(failure) => path_failure(failure.code), + None => path_failure("host_failure") + } + } +} + +fn resolved_path(native: ConfigPathResultEnvelope) -> AuthEntryResult { + if !native.ok => { + path_error(native.error, native.value) + } else => { + path_value(native.value) + } +} + pub fn run(home: resource, command: AuthCommandInput) -> AuthEntryResult { let operation: string = command.operation.copy(); + let provider_present: bool = match command.provider.copy() { + Some(_) => true, + None => false, + }; + let credential_present: bool = match command.credential_id.copy() { + Some(_) => true, + None => false, + }; if operation.copy() == "config.path" => { - if command.provider.copy() != null || command.credential_id.copy() != null => { - entry(false, operation.copy(), null, 0, "", "invalid_command") + if provider_present || credential_present => { + entry(false, operation.copy(), None, 0, "", Some("invalid_command")) } else => { - let native = config::resolved_path_result(&home); - if native.ok.copy() => { - if native.error.copy() == null && native.value.copy() != null => { - let resolved: string = native.value.copy(); - entry(true, operation.copy(), null, 0, resolved.copy(), null) - } else => { - entry(false, operation.copy(), null, 0, "", "host_failure") - } - } else => { - if native.error.copy() != null && native.value.copy() == null => { - let failure: AuthBridgeFailure = native.error.copy(); - let code: string = failure.code.copy(); - entry(false, operation.copy(), null, 0, "", code.copy()) - } else => { - entry(false, operation.copy(), null, 0, "", "host_failure") - } - } + resolved_path(config::resolved_path_result(&home)) } } else => { - entry(false, operation.copy(), null, 0, "", "unsupported_operation") + entry(false, operation.copy(), None, 0, "", Some("unsupported_operation")) } } diff --git a/rss/config/selection.rss b/rss/config/selection.rss index 82149cd..0ce1e23 100644 --- a/rss/config/selection.rss +++ b/rss/config/selection.rss @@ -3,20 +3,25 @@ struct EffectiveSelection { provider: string, model: string, source: string, - error: string? + error: Option } fn failed_selection(code: string) -> EffectiveSelection { - {ok: false, provider: "", model: "", source: "", error: code} + let error: Option = Some(code); + {ok: false, provider: "", model: "", source: "", error: error} } pub fn effective_selection( public_config: BoundedPublicConfig, - provider_override: string? + provider_override: Option ) -> EffectiveSelection { let mut provider_name: string = public_config.model.provider; - if provider_override != null { - let requested: string = provider_override; + let override_present: bool = match provider_override.copy() { + Some(_) => true, + None => false, + }; + if override_present { + let requested: string = provider_override.unwrap_or(""); provider_name = requested; } let providers = public_config.providers; @@ -29,17 +34,18 @@ pub fn effective_selection( } index += 1; } - if provider_override != null && provider_name == "" => { + if override_present && provider_name == "" => { failed_selection("provider_required") } else if !found => { failed_selection("provider_not_found") } else => { + let missing: Option = None; { ok: true, provider: provider_name, model: public_config.model.model, source: public_config.agent.source, - error: null + error: missing } } } diff --git a/rss/llm/openai_chat.rss b/rss/llm/openai_chat.rss index 6b29c68..a71598f 100644 --- a/rss/llm/openai_chat.rss +++ b/rss/llm/openai_chat.rss @@ -24,6 +24,7 @@ // example `json::encode` results) live at function top level or in // helpers with declared return types, never in expression-if branches. use json; +use agent; use self::types as types; struct WireTextPart { @@ -55,9 +56,9 @@ struct WireToolCall { struct WireMessage { role: string, - content: string?, - tool_call_id: string?, - tool_calls: array? + content: Option, + tool_call_id: Option, + tool_calls: Option> } struct WireToolError { @@ -66,12 +67,6 @@ struct WireToolError { content: string } -struct WireToolSuccess { - content: string, - result: map, - truncated: bool -} - struct WireStreamOptions { include_usage: bool } @@ -79,14 +74,14 @@ struct WireStreamOptions { struct WireRequest { model: string, messages: array, - tools: array?, - tool_choice: string?, - temperature: float?, - top_p: float?, - max_tokens: int?, + tools: Option>, + tool_choice: Option, + temperature: Option, + top_p: Option, + max_tokens: Option, stream: bool, - stream_options: WireStreamOptions?, - reasoning_effort: string? + stream_options: Option, + reasoning_effort: Option } /// Marker for one tool's raw schema. The encoded body carries the marker as @@ -149,14 +144,14 @@ fn chat_build_wire(request: map, model: string, stream: bool) -> WireRequest { types::request_array(request, "messages") ); let tools: array = chat_build_tools(types::request_array(request, "tools")); - let mut wire_tools: array? = null; + let mut wire_tools: Option> = None; if tools.length > 0 { - wire_tools = tools; + wire_tools = Some(tools); } let tool_choice: string = types::request_string(request, "tool_choice"); - let mut wire_tool_choice: string? = null; + let mut wire_tool_choice: Option = None; if tool_choice != "" { - wire_tool_choice = tool_choice; + wire_tool_choice = Some(tool_choice); } let reasoning: string = types::request_string(request, "reasoning"); let sampling: map = types::request_map(request, "sampling"); @@ -167,25 +162,25 @@ fn chat_build_wire(request: map, model: string, stream: bool) -> WireRequest { let top_p_present: bool = top_p_raw["present"]; let top_p_value: float = top_p_raw["value"]; let max_output: int = types::request_int(request, "max_output_tokens", 0); - let mut temperature: float? = null; - let mut top_p: float? = null; - let mut max_tokens: int? = null; - let mut stream_options: WireStreamOptions? = null; - let mut reasoning_effort: string? = null; + let mut temperature: Option = None; + let mut top_p: Option = None; + let mut max_tokens: Option = None; + let mut stream_options: Option = None; + let mut reasoning_effort: Option = None; if temperature_present { - temperature = temperature_value; + temperature = Some(temperature_value); } if top_p_present { - top_p = top_p_value; + top_p = Some(top_p_value); } if max_output > 0 { - max_tokens = max_output; + max_tokens = Some(max_output); } if stream { - stream_options = { include_usage: true }; + stream_options = Some({ include_usage: true }); } if reasoning != "" { - reasoning_effort = reasoning; + reasoning_effort = Some(reasoning); } { model: model, @@ -234,6 +229,8 @@ fn chat_append_message_by_role(wire: array, message: map, index: in fn chat_append_user_message(wire: array, message: map, index: int) -> array { let parts: array = types::message_content(message); let mut result: array = wire; + let no_tool_calls: Option> = None; + let no_tool_call_id: Option = None; let mut i = 0; while i < parts.length { let part: map = types::array_entry(parts, i); @@ -243,9 +240,9 @@ fn chat_append_user_message(wire: array, message: map, index: int) let content: string = chat_tool_result_output(part); result[result.length] = { role: "tool", - content: content, - tool_call_id: tool_call_id, - tool_calls: null + content: Some(content), + tool_call_id: Some(tool_call_id), + tool_calls: no_tool_calls }; } } @@ -255,9 +252,9 @@ fn chat_append_user_message(wire: array, message: map, index: int) if text_parts.length > 0 { result[result.length] = { role: "user", - content: user_parts_marker(index), - tool_call_id: null, - tool_calls: null + content: Some(user_parts_marker(index)), + tool_call_id: no_tool_call_id, + tool_calls: no_tool_calls }; } result @@ -292,12 +289,14 @@ fn field_is_map(container: map, key: string) -> bool { } fn chat_tool_success(part: map) -> string { - let output: WireToolSuccess = { - content: types::request_string(part, "content"), - result: types::request_map(part, "result"), - truncated: types::request_bool(part, "truncated", false) - }; - json::encode(output) + // Tool-defined result data is serialized at the native open-payload boundary. + let encoded = agent::json_text(types::request_map(part, "result")); + let truncated: bool = types::request_bool(part, "truncated", false); + if encoded.ok => { + "{\"content\":\"\",\"result\":" + encoded.text + ",\"truncated\":" + json::encode::(truncated) + "}" + } else => { + "" + } } /// The `[{type: "text", text}]` parts of one canonical user message. @@ -345,19 +344,20 @@ fn chat_append_assistant_message(wire: array, message: map) -> arra } i += 1; } - let mut content: string? = null; + let mut content: Option = None; if text != "" { - content = text; + content = Some(text); } - let mut wire_calls: array? = null; + let mut wire_calls: Option> = None; if tool_calls.length > 0 { - wire_calls = tool_calls; + wire_calls = Some(tool_calls); } let mut result: array = wire; + let no_tool_call_id: Option = None; result[result.length] = { role: "assistant", content: content, - tool_call_id: null, + tool_call_id: no_tool_call_id, tool_calls: wire_calls }; result @@ -366,6 +366,8 @@ fn chat_append_assistant_message(wire: array, message: map) -> arra fn chat_append_simple_message(wire: array, role: string, message: map) -> array { let parts: array = types::message_content(message); let mut text: string = ""; + let no_tool_call_id: Option = None; + let no_tool_calls: Option> = None; let mut i = 0; while i < parts.length { let part: map = types::array_entry(parts, i); @@ -380,9 +382,9 @@ fn chat_append_simple_message(wire: array, role: string, message: m let mut result: array = wire; result[result.length] = { role: role, - content: text, - tool_call_id: null, - tool_calls: null + content: Some(text), + tool_call_id: no_tool_call_id, + tool_calls: no_tool_calls }; result } @@ -464,167 +466,108 @@ fn chat_splice_user_parts(body: string, messages: array) -> string { // --------------------------------------------------------------------------- -pub fn chat_parse_public_response(body_text: string, status: int, provider: string) -> map { - let body: map = json::decode(body_text); - chat_parse_response_body(body, status, provider) +struct ChatReplyFunction { + name: string, + arguments: Option } -fn chat_parse_response_body(body: map, status: int, provider: string) -> map { - let choices: array = types::request_array(body, "choices"); - if choices.length > 0 => { - chat_parse_first_choice(choices, body, status, provider) - } else => { - types::fail(types::error_malformed( - status, - "chat completion response is missing 'choices'" - )) - } +struct ChatReplyTool { + id: string, + function: ChatReplyFunction } -fn chat_parse_first_choice(choices: array, body: map, status: int, provider: string) -> map { - let first: map = types::array_entry(choices, 0); - let stop_reason: string = types::request_string(first, "finish_reason"); - let usage: map = types::request_map(body, "usage"); - let usage_input: int = types::request_int(usage, "prompt_tokens", 0); - let usage_output: int = types::request_int(usage, "completion_tokens", 0); - let usage_total: int = types::request_int(usage, "total_tokens", 0); - let message: map = types::request_map(first, "message"); - let result: map = if first.has("message") => { - let tool_calls: array = types::request_array(message, "tool_calls"); - chat_parse_message( - message, - stop_reason, - usage_input, - usage_output, - usage_total, - tool_calls, - provider - ) - } else => { - types::fail(types::error_malformed( - status, - "chat completion response is missing 'choices[0].message'" - )) - }; - result +struct ChatReplyMessage { + content: Option, + reasoning_content: Option, + tool_calls: Option> } -fn chat_parse_message( - message: map, - stop_reason: string, - usage_input: int, - usage_output: int, - usage_total: int, - tool_calls: array, - provider: string -) -> map { - let content: string = chat_message_text(message); - let reasoning: string = types::request_string(message, "reasoning_content"); - let calls: array = chat_parse_tool_calls(tool_calls); - types::ok(types::response_new( - content, - calls, - types::usage_new(usage_input, usage_output, usage_total), - reasoning, - stop_reason - )) -} - -/// Reads `message.content` in either standard wire shape: a plain string -/// (chat completions) or an array of `{type, text}` parts (Responses, -/// Anthropic-style transcripts). Part arrays are joined over their text-ish -/// parts (`text`/`output_text`). -/// Tool-call-only assistant messages may omit content and contribute no text. -fn chat_message_text(message: map) -> string { - let direct: string = types::request_string(message, "content"); - let mut text: string = direct; - if message.has("content") && type(message["content"]) == "array" { - let parts: array = message["content"]; - let mut joined: string = ""; - let mut i = 0; - while i < parts.length { - let part: map = types::array_entry(parts, i); - let part_type: string = types::part_type(part); - if part_type == "text" { - joined = joined + types::request_string(part, "text"); - } - if part_type == "output_text" { - joined = joined + types::request_string(part, "text"); - } - i += 1; +struct ChatReplyChoice { + finish_reason: Option, + message: Option +} + +struct ChatReplyUsage { + prompt_tokens: Option, + completion_tokens: Option, + total_tokens: Option +} + +struct ChatReply { + choices: Option>, + usage: Option +} + +pub fn chat_parse_public_response(body_text: string, status: int) -> map { + let reply: ChatReply = json::decode::(body_text); + let empty_choices: array = []; + let choices: array = reply.choices.unwrap_or(empty_choices); + if choices.length > 0 => { + let choice: ChatReplyChoice = choices[0]; + let stop: string = choice.finish_reason.unwrap_or(""); + let usage: map = match reply.usage { + Some(value) => types::usage_new(value.prompt_tokens.unwrap_or(0), value.completion_tokens.unwrap_or(0), value.total_tokens.unwrap_or(0)), + None => types::usage_new(0, 0, 0) + }; + match choice.message { + Some(message) => chat_parse_message(message, stop, usage), + None => types::fail(types::error_malformed(status, "chat completion response is missing 'choices[0].message'")) } - text = joined; + } else => { + types::fail(types::error_malformed(status, "chat completion response is missing 'choices'")) } - text } -fn chat_parse_tool_calls(tool_calls: array) -> array { +fn chat_parse_message(message: ChatReplyMessage, stop: string, usage: map) -> map { + let empty_tools: array = []; + let tools: array = message.tool_calls.unwrap_or(empty_tools); let mut calls: array = []; - let mut i = 0; - while i < tool_calls.length { - let call: map = types::array_entry(tool_calls, i); - if call.has("id") { - let id: string = types::request_string(call, "id"); - let function: map = types::request_map(call, "function"); - let name: string = types::request_string(function, "name"); - let args_text: string = types::request_string(function, "arguments"); - let mut args: map = {}; - if args_text != "" { - let decoded: map = json::decode(args_text); - args = decoded; - } - calls[calls.length] = types::tool_call_new(id, name, args); - } + let mut i: int = 0; + while i < tools.length { + let tool: ChatReplyTool = tools[i].copy(); + let call: ChatReplyFunction = tool.function; + calls[calls.length] = chat_tool_call(tool.id, call.name, call.arguments.unwrap_or("")); i += 1; } - calls + types::ok(types::response_new(message.content.unwrap_or(""), calls, usage, message.reasoning_content.unwrap_or(""), stop)) } +/// Function arguments have a tool-defined schema. Keep the wire JSON text in +/// the canonical call; native tool admission owns parsing and validation. +fn chat_tool_call(id: string, name: string, arguments: string) -> map { + let text: string = if arguments == "" => { "{}" } else => { arguments }; + { id: id, name: name, arguments_json: text } +} -pub fn chat_parse_public_error(body_text: string, status: int) -> map { - chat_parse_error_text(body_text, status) -} - -fn chat_parse_error_text(body_text: string, status: int) -> map { - let payload: map = chat_error_payload(body_text); - let mut error_type: string = "api_error"; - let mut code: string = "http_error"; - let mut message: string = "provider request failed"; - let mut param: string = ""; - let mut request_id: string = ""; - let payload_type: string = types::request_string(payload, "type"); - let payload_code: string = types::request_string(payload, "code"); - let payload_message: string = types::request_string(payload, "message"); - let payload_param: string = types::request_string(payload, "param"); - let payload_request_id: string = types::request_string(payload, "request_id"); - if payload_type != "" { - error_type = payload_type; - } - if payload_code != "" { - code = payload_code; - } - if payload_message != "" { - message = payload_message; - } - if payload_param != "" { - param = payload_param; - } - if payload_request_id != "" { - request_id = payload_request_id; - } - types::fail(types::error_new(status, error_type, code, message, param, request_id)) +struct ChatWireError { + type: Option, + code: Option, + message: Option, + param: Option, + request_id: Option } -/// Extracts the `error` object from a non-2xx JSON body. A structurally -/// invalid body yields an empty map and the caller falls back to the default -/// `api_error`/`http_error` shape; a body that is not JSON at all fails the -/// invocation (the same contract as `chat_parse_response`). -fn chat_error_payload(body_text: string) -> map { - let decoded: map = json::decode(body_text); - if type(decoded) == "map" => { - types::request_map(decoded, "error") - } else => { - {} +struct ChatWireErrorBody { + error: Option +} + +fn chat_error_text(value: Option, default: string) -> string { + let text: string = value.unwrap_or(""); + if text == "" => { default } else => { text } +} + +pub fn chat_parse_public_error(body_text: string, status: int) -> map { + let body: ChatWireErrorBody = json::decode::(body_text); + match body.error { + Some(error) => types::fail(types::error_new( + status, + chat_error_text(error.type, "api_error"), + chat_error_text(error.code, "http_error"), + chat_error_text(error.message, "provider request failed"), + error.param.unwrap_or(""), + error.request_id.unwrap_or("") + )), + None => types::fail(types::error_new(status, "api_error", "http_error", "provider request failed", "", "")) } } @@ -680,24 +623,17 @@ pub fn chat_stream_merge_tool_calls(tools: array, deltas: array) -> array { merged } -/// Converts the accumulator entries into canonical `{id, name, arguments}` -/// tool calls, decoding each accumulated `arguments` JSON fragment. Public so -/// the pure stream codec reuses it unchanged. +/// Converts accumulated arguments to the same text carrier as buffered calls. pub fn chat_stream_parse_tool_calls(tools: array) -> array { let mut calls: array = []; - let mut i = 0; + let mut i: int = 0; while i < tools.length { let entry: map = types::array_entry(tools, i); let id: string = types::request_string(entry, "id"); let name: string = types::request_string(entry, "name"); - let args_text: string = types::request_string(entry, "args"); - let mut args: map = {}; - if args_text != "" { - let decoded: map = json::decode(args_text); - args = decoded; - } - calls[calls.length] = types::tool_call_new(id, name, args); + let args: string = types::request_string(entry, "args"); + calls[calls.length] = chat_tool_call(id, name, args); i += 1; } calls -} \ No newline at end of file +} diff --git a/rss/llm/openai_opaque_chat.rss b/rss/llm/openai_opaque_chat.rss index b2485b3..56715ac 100644 --- a/rss/llm/openai_opaque_chat.rss +++ b/rss/llm/openai_opaque_chat.rss @@ -55,7 +55,6 @@ fn chat_path_or_default(profile: map) -> string { } fn chat_send_buffered(request: map, profile: map, context: map, model: string) -> map { - let provider: string = types::profile_string(profile, "provider"); let body: string = openai_chat::chat_build_public_body(request, model); let binding: OpaqueHostHomeBinding = { "home": context["host_home"], }; let credential_id: string = types::request_string(context, "credential_id"); @@ -70,93 +69,115 @@ fn chat_send_buffered(request: map, profile: map, context: map, model: string) - public_body: body }; let envelope: TransportResultEnvelope = provider_runtime::call(binding, intent); - chat_interpret_transport(envelope, provider) -} - -fn chat_interpret_transport(envelope: TransportResultEnvelope, provider: string) -> map { - let has_response: bool = envelope.has("value"); - let maybe_failure: AuthBridgeFailure? = envelope.error; - let failure: AuthBridgeFailure = maybe_failure.unwrap_or({ - code: "transport_error", - cancelled: false, - deadline_exceeded: false, - revoked: false, - transport_phase: null, - transport_category: null - }); - let maybe_response: TransportResponseEnvelope? = if has_response => { - envelope.value - } else => { - null - }; - let response: TransportResponseEnvelope = maybe_response.unwrap_or({ - ok: false, - error: null, - status: 0, - public_headers: [], - body_without_secret_fields: "", - retry_after_ms: null, - access_slot: null, - refresh_slot: null, - device_handle: null - }); - let result: map = if envelope.ok => { - chat_parse_transport_response(response, provider) + chat_interpret_transport(envelope) +} + +fn chat_interpret_transport(envelope: TransportResultEnvelope) -> map { + if envelope.ok => { + match envelope.value { + Some(response) => chat_parse_transport_response(response), + None => types::fail(types::error_request("transport_error", "provider transport failed")) + } } else => { - types::fail(types::error_request(failure.code, "provider transport failed")) - }; - result + match envelope.error { + Some(failure) => types::fail(types::error_request(failure.code, "provider transport failed")), + None => types::fail(types::error_request("transport_error", "provider transport failed")) + } + } } -fn chat_parse_transport_response(response: TransportResponseEnvelope, provider: string) -> map { - if response.status >= 200 && response.status < 300 => { - openai_chat::chat_parse_public_response( - response.body_without_secret_fields, - response.status, - provider - ) +fn chat_parse_transport_response(response: TransportResponseEnvelope) -> map { + let status: int = response.status; + let body: string = response.body_without_secret_fields; + if status >= 200 && status < 300 => { + openai_chat::chat_parse_public_response(body, status) } else => { - openai_chat::chat_parse_public_error(response.body_without_secret_fields, response.status) + openai_chat::chat_parse_public_error(body, status) } } -struct StreamValuePresence { - value: OAuthStreamEnvelope? +fn stream_transport_failure(failure: Option) -> map { + let detail: AuthBridgeFailure = failure.unwrap_or({code: "transport_error", cancelled: false, deadline_exceeded: false, revoked: false, transport_phase: None, transport_category: None}); + let mut error: map = types::error_request(detail.code, "provider transport failed"); + let category: Option = detail.transport_category; + let phase: Option = detail.transport_phase; + if text_present(category.copy()) && text_present(phase.copy()) { + error.transport_category = category.unwrap_or(""); + error.transport_phase = phase.unwrap_or(""); + } + types::fail(error) } -struct RecordValuePresence { - value: OAuthStreamRecord? +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } } -fn stream_transport_failure(failure: AuthBridgeFailure?) -> map { - let detail: AuthBridgeFailure = failure.unwrap_or({ - code: "transport_error", - cancelled: false, - deadline_exceeded: false, - revoked: false, - transport_phase: null, - transport_category: null - }); - let has_category: bool = detail.has("transport_category"); - let has_phase: bool = detail.has("transport_phase"); - let mut error: map = types::error_request(detail.code, "provider transport failed"); - // Only the fixed native transport facts cross into the public error map; - // any other text is dropped here. - if has_category && has_phase { - let category_value: string? = detail.transport_category; - let phase_value: string? = detail.transport_phase; - if category_value != null && phase_value != null { - error["transport_category"] = category_value.copy(); - error["transport_phase"] = phase_value.copy(); - } +fn failure_present(failure: Option) -> bool { + match failure { + Some(_) => true, + None => false } - types::fail(error) } fn malformed_stream(status: int) -> map { types::fail(types::error_malformed(status, "malformed provider stream record")) } +/// The open result as one carrier: a refused open (`ok == false`) or a present +/// failure means there is no envelope to read records from. +fn open_stream_envelope( + ok: bool, + refused: bool, + value: Option +) -> Option { + if !ok || refused => { + None + } else => { + value + } +} + +/// An absent record. Only reachable if the read envelope reported no value while +/// claiming success; it decodes to the malformed-record refusal. +fn empty_stream_record() -> OAuthStreamRecord { + let literal_index: Option = None; + {kind: "", json: "", literal_index: literal_index} +} + +/// One record carrier plus its presence. The record is taken apart once so the +/// read field is moved exactly one time. +struct RecordPeek { + present: bool, + record: OAuthStreamRecord +} + +fn peek_record(value: Option) -> RecordPeek { + match value { + Some(record) => {present: true, record: record}, + None => {present: false, record: empty_stream_record()} + } +} + +/// A read envelope is usable only when it reported success without a failure and +/// carried a record; every other combination is the transport-failure path. +fn stream_record_ready( + ok: bool, + failure: Option, + record_present: bool +) -> bool { + ok && !failure_present(failure) && record_present +} + +fn first_literal(index: Option) -> bool { + match index { + Some(value) => value == 0, + None => false + } +} + fn chat_send_stream(request: map, profile: map, context: map, model: string) -> map { let binding: OpaqueHostHomeBinding = {home: context["host_home"]}; let intent: OpaqueProviderIntent = { @@ -170,80 +191,71 @@ fn chat_send_stream(request: map, profile: map, context: map, model: string) -> public_body: openai_chat::chat_build_public_stream_body(request, model) }; let started: OAuthStreamStartResultEnvelope = provider_runtime::start_stream(binding, intent, ["[DONE]"]); - let has_value: bool = started.has("value"); let ok: bool = started.ok; - let failure: AuthBridgeFailure? = started.error; - if !ok || failure != null || !has_value => { - stream_transport_failure(failure) - } else => { - let holder: StreamValuePresence = {value: started.value}; - let envelope: OAuthStreamEnvelope = holder.value; - let stream: resource = envelope.stream; - let head: OAuthStreamHead = envelope.head; - let status: int = head.status; - let mut state: map = codec::begin(); - let mut result: map = {}; - let mut finished: bool = false; - while !finished { - let read: OAuthStreamReadResultEnvelope = oauth::stream_read_result(&stream); - let read_has_value: bool = read.has("value"); - let read_ok: bool = read.ok; - let read_failure: AuthBridgeFailure? = read.error; - if !read_ok || read_failure != null || !read_has_value { - result = stream_transport_failure(read_failure); - finished = true; - } else { - let record_holder: RecordValuePresence = {value: read.value}; - let record: OAuthStreamRecord = record_holder.value; - let kind: string = record.kind; - if kind == "json" { - let body: string = record.json; - // Native records are strict canonical JSON objects or arrays. - // Reject arrays before the map-typed decoder is invoked. - if body.copy()[0:1] != "{" { - result = malformed_stream(status); - finished = true; - } else if status < 200 || status >= 300 { - result = openai_chat::chat_parse_public_error(body, status); - finished = true; - } else { - let chunk: map = json::decode(body); - let previous_text: string = types::request_string(state, "text"); - state = codec::push_chunk(state, chunk); - if types::request_bool(state, "failed", false) { - result = codec::finish(state); - finished = true; - } else { - // Only codec-validated public text leaves the child VM. - let text: string = types::request_string(state, "text"); - if text.length > previous_text.length { - let delta: string = text.copy()[previous_text.length:text.length]; - stream::emit({type: "model.delta", delta: delta}); - } - } - } - } else if status < 200 || status >= 300 { + let failure: Option = started.error; + let result: map = match open_stream_envelope(ok, failure_present(failure), started.value) { + Some(envelope) => consume_stream(envelope), + None => stream_transport_failure(failure) + }; + result +} + +fn consume_stream(envelope: OAuthStreamEnvelope) -> map { + let stream: resource = envelope.stream; + let head: OAuthStreamHead = envelope.head; + let status: int = head.status; + let mut state: map = codec::begin(); + let mut result: map = {}; + let mut finished: bool = false; + while !finished { + let read: OAuthStreamReadResultEnvelope = oauth::stream_read_result(&stream); + let read_ok: bool = read.ok; + let read_failure: Option = read.error; + let peeked_record: RecordPeek = peek_record(read.value); + let record_ready: bool = stream_record_ready(read_ok, read_failure, peeked_record.present); + if !record_ready { + result = stream_transport_failure(read_failure); + finished = true; + } else { + let record: OAuthStreamRecord = peeked_record.record; + let kind: string = record.kind; + if kind == "json" { + let body: string = record.json; + // Native records are strict canonical JSON objects or arrays. + // Reject arrays before the map-typed decoder is invoked. + if body.copy()[0:1] != "{" { result = malformed_stream(status); finished = true; - } else if kind == "literal" { - let index: int? = record.literal_index; - if index.unwrap_or(-1) == 0 { - state = codec::push_done(state); + } else if status < 200 || status >= 300 { + result = openai_chat::chat_parse_public_error(body, status); + finished = true; + } else { + state = codec::push_json(state, body); + if types::request_bool(state, "failed", false) { result = codec::finish(state); - } else { - result = malformed_stream(status); + finished = true; } - finished = true; - } else if kind == "eof" { + } + } else if status < 200 || status >= 300 { + result = malformed_stream(status); + finished = true; + } else if kind == "literal" { + if first_literal(record.literal_index) { + state = codec::push_done(state); result = codec::finish(state); - finished = true; } else { result = malformed_stream(status); - finished = true; } + finished = true; + } else if kind == "eof" { + result = codec::finish(state); + finished = true; + } else { + result = malformed_stream(status); + finished = true; } } - oauth::stream_close(stream); - result } + oauth::stream_close(stream); + result } diff --git a/rss/llm/openai_responses.rss b/rss/llm/openai_responses.rss index 69805c1..3c1c8c4 100644 --- a/rss/llm/openai_responses.rss +++ b/rss/llm/openai_responses.rss @@ -46,8 +46,8 @@ pub fn responses_begin() -> map { codec::begin() } -pub fn responses_push_chunk(state: map, chunk: map) -> map { - codec::push_chunk(state, chunk) +pub fn responses_push_json(state: map, text: string) -> map { + codec::push_json(state, text) } pub fn responses_push_done(state: map) -> map { diff --git a/rss/llm/openai_responses_codec.rss b/rss/llm/openai_responses_codec.rss index 0cd7085..0319984 100644 --- a/rss/llm/openai_responses_codec.rss +++ b/rss/llm/openai_responses_codec.rss @@ -15,8 +15,167 @@ // type probes therefore live in the small `field_is_*` helpers, where the // index read is the container's last use. use json; +use agent; use self::types as types; +// Project only the closed fields consumed by the Responses state machine. +// Function arguments stay in their tool-defined JSON text form. +struct ResponsesWirePart { + type: Option, + text: Option, + refusal: Option +} + +struct ResponsesWireItem { + type: Option, + status: Option, + role: Option, + id: Option, + call_id: Option, + name: Option, + arguments: Option, + content: Option>, + summary: Option> +} + +struct ResponsesWireUsage { + input_tokens: Option, + output_tokens: Option, + total_tokens: Option +} + +struct ResponsesWireError { + type: Option, + code: Option, + message: Option +} + +struct ResponsesWireBody { + status: Option, + output: Option>, + usage: Option, + error: Option +} + +struct ResponsesWireEvent { + type: Option, + delta: Option, + output_index: Option, + item: Option, + response: Option +} + +fn project_string(fields: map, key: string, value: Option) -> map { + match value { + Some(text) => project_text(fields, key, text), + None => fields + } +} + +fn project_text(fields: map, key: string, text: string) -> map { + let mut projected: map = fields; + projected[key] = text; + projected +} + +fn project_parts(wire: array) -> array { + let mut parts: array = []; + let mut i = 0; + while i < wire.length { + let part: ResponsesWirePart = wire[i].copy(); + let kind: string = part.type.unwrap_or(""); + let mut base: map = {}; + base["type"] = kind; + let fields: map = project_string(base, "text", part.text); + let projected: map = project_string(fields, "refusal", part.refusal); + parts[parts.length] = projected; + i += 1; + } + parts +} + +fn project_item(wire: ResponsesWireItem) -> map { + let fields: map = { + "type": wire.type.unwrap_or(""), role: wire.role.unwrap_or(""), + id: wire.id.unwrap_or(""), call_id: wire.call_id.unwrap_or(""), + name: wire.name.unwrap_or("") + }; + let fields: map = project_string(fields, "status", wire.status); + let item: map = project_string(fields, "arguments", wire.arguments); + let item: map = match wire.content { + Some(parts) => project_array(item, "content", project_parts(parts)), + None => item + }; + match wire.summary { + Some(parts) => project_array(item, "summary", project_parts(parts)), + None => item + } +} + +fn project_array(fields: map, key: string, value: array) -> map { + let mut projected: map = fields; + projected[key] = value; + projected +} + +fn project_map(fields: map, key: string, value: map) -> map { + let mut projected: map = fields; + projected[key] = value; + projected +} + +fn project_output(wire: array) -> array { + let mut output: array = []; + let mut i = 0; + while i < wire.length { + output[output.length] = project_item(wire[i].copy()); + i += 1; + } + output +} + +fn project_body(wire: ResponsesWireBody) -> map { + let body: map = {status: wire.status.unwrap_or("")}; + let body: map = match wire.output { + Some(items) => project_array(body, "output", project_output(items)), + None => body + }; + let body: map = match wire.usage { + Some(usage) => project_map(body, "usage", types::usage_new( + usage.input_tokens.unwrap_or(0), usage.output_tokens.unwrap_or(0), + usage.total_tokens.unwrap_or(0) + )), + None => body + }; + match wire.error { + Some(_) => project_map(body, "error", {}), + None => body + } +} + +pub fn push_json(state: map, text: string) -> map { + let done: bool = types::request_bool(state, "done", false); + let failed: bool = types::request_bool(state, "failed", false); + if done || failed => { + state + } else => { + let wire: ResponsesWireEvent = json::decode::(text); + let chunk: map = { + "type": wire.type.unwrap_or(""), delta: wire.delta.unwrap_or(""), + output_index: wire.output_index.unwrap_or(-1) + }; + let chunk: map = match wire.item { + Some(item) => project_map(chunk, "item", project_item(item)), + None => chunk + }; + let chunk: map = match wire.response { + Some(response) => project_map(chunk, "response", project_body(response)), + None => chunk + }; + codec_ingest(state, chunk) + } +} + pub fn begin() -> map { { text: "", @@ -26,7 +185,7 @@ pub fn begin() -> map { sealed_items: [], sealed_invalid: false, sealed_conflict: false, - final: null, + final: (), completed_event: false, input_tokens: 0, output_tokens: 0, @@ -94,21 +253,6 @@ fn codec_terminal_error(state: map, code: string, message: string) -> map { // Chunk ingestion // --------------------------------------------------------------------------- -/// Ingests one already-decoded Responses event object. Returns the state -/// unchanged once it is terminal (completed/failed), so late events are -/// ignored. Unknown event types are ignored (forward compatible): only a -/// terminal response event or EOF ends the stream. -pub fn push_chunk(state: map, chunk: map) -> map { - let done: bool = types::request_bool(state, "done", false); - let failed: bool = types::request_bool(state, "failed", false); - let terminal: bool = done || failed; - let result: map = if terminal => { - state - } else => { - codec_ingest(state, chunk) - }; - result -} fn codec_ingest(state: map, chunk: map) -> map { let kind: string = types::request_string(chunk, "type"); @@ -575,7 +719,8 @@ pub fn responses_parse_body(body: map) -> map { } pub fn responses_parse_public_response(body_text: string) -> map { - let body: map = json::decode(body_text); + let wire: ResponsesWireBody = json::decode::(body_text); + let body: map = project_body(wire); responses_parse_body(body) } @@ -648,12 +793,8 @@ fn responses_output_calls(output: array) -> array { } let name: string = types::request_string(item, "name"); let args_text: string = types::request_string(item, "arguments"); - let mut args: map = {}; - if args_text != "" { - let decoded: map = json::decode(args_text); - args = decoded; - } - calls[calls.length] = types::tool_call_new(id, name, args); + let arguments_json: string = if args_text == "" => { "{}" } else => { args_text }; + calls[calls.length] = {id: id, name: name, arguments_json: arguments_json}; } i += 1; } @@ -669,12 +810,8 @@ fn responses_accumulated_calls(tools: array) -> array { let name: string = types::request_string(entry, "name"); if id != "" && name != "" { let args_text: string = types::request_string(entry, "args"); - let mut args: map = {}; - if args_text != "" { - let decoded: map = json::decode(args_text); - args = decoded; - } - calls[calls.length] = types::tool_call_new(id, name, args); + let arguments_json: string = if args_text == "" => { "{}" } else => { args_text }; + calls[calls.length] = {id: id, name: name, arguments_json: arguments_json}; } i += 1; } @@ -702,24 +839,25 @@ fn responses_stop_reason(body: map, calls: array) -> string { /// that is not JSON fails the invocation; a structurally valid body without a /// recognisable error object yields the default `http_error` shape. The /// upstream message is never echoed beyond the provider's own `message` field. +struct ResponsesWireErrorBody { + error: Option +} + pub fn responses_parse_public_error(body_text: string, status: int) -> map { - let payload: map = json::decode(body_text); - let has_error: bool = field_is_map(payload, "error"); - let result: map = if has_error => { - responses_error_from_object(types::request_map(payload, "error"), status) - } else => { - responses_default_error(status) - }; - result + let payload: ResponsesWireErrorBody = json::decode::(body_text); + match payload.error { + Some(error) => responses_error_from_object(error, status), + None => responses_default_error(status) + } } -fn responses_error_from_object(error: map, status: int) -> map { +fn responses_error_from_object(error: ResponsesWireError, status: int) -> map { let mut error_type: string = "api_error"; let mut code: string = "http_error"; let mut message: string = "provider request failed"; - let payload_type: string = types::request_string(error, "type"); - let payload_code: string = types::request_string(error, "code"); - let payload_message: string = types::request_string(error, "message"); + let payload_type: string = error.type.unwrap_or(""); + let payload_code: string = error.code.unwrap_or(""); + let payload_message: string = error.message.unwrap_or(""); if payload_type != "" { error_type = payload_type; } @@ -783,12 +921,6 @@ struct ResponsesWireFunctionCallItem { arguments: string } -struct ResponsesWireToolSuccess { - content: string, - result: map, - truncated: bool -} - struct ResponsesWireToolError { error: string, message: string, @@ -801,75 +933,39 @@ struct ResponsesWireFunctionCallOutputItem { output: string } -struct ResponsesWireTool { - type: string, - name: string, - description: string, - parameters: string -} - /// Public buffered Responses body for the canonical request. `store:false` and /// the derived `instructions` (system/developer text) are always present. pub fn responses_build_public_body(request: map, model: string, stream: bool) -> string { let messages: array = types::request_array(request, "messages"); let instructions: string = responses_collect_instructions(messages); let input_json: string = responses_build_input(messages); - let head: string = "{\"model\":" + json::encode(model) + - ",\"instructions\":" + json::encode(instructions) + + let head: string = "{\"model\":" + json::encode::(model) + + ",\"instructions\":" + json::encode::(instructions) + ",\"input\":" + input_json; - let body: string = responses_finish_body(request, head, stream); - responses_splice_tool_schemas(body, types::request_array(request, "tools")) + responses_finish_body(request, head, stream) } -/// Replaces each tool schema marker (with its surrounding quotes) with the raw -/// schema JSON. Main supplies validated descriptor maps; direct codec callers -/// may still supply the legacy `schema_json` string form. -fn responses_splice_tool_schemas(body: string, tools: array) -> string { - let mut result: string = body; - let mut i = 0; - while i < tools.length { - let tool: map = types::array_entry(tools, i); - if tool.has("schema") { - let schema: map = types::request_map(tool, "schema"); - let schema_json: string = json::encode(schema); - result = string_replace_literal( - result, - "\"" + tool_schema_marker(i) + "\"", - schema_json - ); - } else if tool.has("schema_json") { - let schema_json: string = types::tool_schema_json(tool); - result = string_replace_literal( - result, - "\"" + tool_schema_marker(i) + "\"", - schema_json - ); - } - i += 1; +fn responses_finish_body(request: map, head: string, stream: bool) -> string { + let tools: array = types::request_array(request, "tools"); + let tools_json: string = responses_build_tools(tools); + if tools_json == "" => { + "" + } else => { + responses_finish_valid_body(request, head, stream, tools.length > 0, tools_json) } - result } -fn responses_finish_body(request: map, head: string, stream: bool) -> string { - let tools_json: string = responses_build_tools(types::request_array(request, "tools")); +fn responses_finish_valid_body(request: map, head: string, stream: bool, has_tools: bool, tools_json: string) -> string { let mut body: string = head; - if tools_json != "" { + if has_tools { body = body + ",\"tools\":" + tools_json; let tool_choice: string = types::request_string(request, "tool_choice"); if tool_choice != "" { - body = body + ",\"tool_choice\":" + json::encode(tool_choice); + body = body + ",\"tool_choice\":" + json::encode::(tool_choice); } } body = responses_append_sampling(request, body); - body + ",\"stream\":" + json::encode(stream) + ",\"store\":false}" -} - -/// Marker for one tool's raw schema. The typed wire wrapper stores a string in -/// `parameters`; `responses_splice_tool_schemas` replaces that placeholder with -/// the JSON encoded from the validated descriptor map (or legacy raw string). -fn tool_schema_marker(index: int) -> string { - let encoded: string = json::encode(index); - "__RSS_RESPONSES_SCHEMA_" + encoded + "__" + body + ",\"stream\":" + json::encode::(stream) + ",\"store\":false}" } fn responses_append_sampling(request: map, body: string) -> string { @@ -882,14 +978,14 @@ fn responses_append_sampling(request: map, body: string) -> string { let top_p_present: bool = top_p_raw["present"]; let top_p_value: float = top_p_raw["value"]; if temperature_present { - out = out + ",\"temperature\":" + json::encode(temperature_value); + out = out + ",\"temperature\":" + json::encode::(temperature_value); } if top_p_present { - out = out + ",\"top_p\":" + json::encode(top_p_value); + out = out + ",\"top_p\":" + json::encode::(top_p_value); } let max_output: int = types::request_int(request, "max_output_tokens", 0); if max_output > 0 { - out = out + ",\"max_output_tokens\":" + json::encode(max_output); + out = out + ",\"max_output_tokens\":" + json::encode::(max_output); } out } @@ -957,7 +1053,7 @@ fn responses_append_user(parts: array, message: map) -> array { call_id: call_id, output: output }; - result[result.length] = json::encode(item); + result[result.length] = json::encode::(item); } } i += 1; @@ -965,7 +1061,7 @@ fn responses_append_user(parts: array, message: map) -> array { let text_parts: array = responses_text_parts(content, "input_text"); if text_parts.length > 0 { let item: ResponsesWireMessage = { type: "message", role: "user", content: text_parts }; - result[result.length] = json::encode(item); + result[result.length] = json::encode::(item); } result } @@ -979,7 +1075,7 @@ fn responses_tool_output(part: map) -> string { message: types::request_string(error, "message"), content: content }; - json::encode(failure) + json::encode::(failure) } else if field_is_map(part, "result") => { responses_tool_success(part) } else => { @@ -988,12 +1084,15 @@ fn responses_tool_output(part: map) -> string { } fn responses_tool_success(part: map) -> string { - let output: ResponsesWireToolSuccess = { - content: types::request_string(part, "content"), - result: types::request_map(part, "result"), - truncated: types::request_bool(part, "truncated", false) - }; - json::encode(output) + let content: string = types::request_string(part, "content"); + let truncated: bool = types::request_bool(part, "truncated", false); + let encoded = agent::json_text(types::request_map(part, "result")); + if encoded.ok => { + "{\"content\":" + json::encode::(content) + ",\"result\":" + encoded.text + + ",\"truncated\":" + json::encode::(truncated) + "}" + } else => { + "" + } } /// One assistant message contributes its text as an `output_text` message item @@ -1004,7 +1103,7 @@ fn responses_append_assistant(parts: array, message: map) -> array { let text_parts: array = responses_text_parts(content, "output_text"); if text_parts.length > 0 { let item: ResponsesWireMessage = { type: "message", role: "assistant", content: text_parts }; - result[result.length] = json::encode(item); + result[result.length] = json::encode::(item); } let mut i = 0; while i < content.length { @@ -1020,7 +1119,7 @@ fn responses_append_assistant(parts: array, message: map) -> array { name: name, arguments: arguments }; - result[result.length] = json::encode(item); + result[result.length] = json::encode::(item); } } i += 1; @@ -1047,25 +1146,23 @@ fn responses_text_parts(content: array, wire_type: string) -> array string { - let mut wire: array = []; + let mut wire: array = []; let mut i = 0; + let mut valid = true; while i < tools.length { let tool: map = types::array_entry(tools, i); - if tool.has("name") { - wire[wire.length] = { - type: "function", - name: types::tool_name(tool), - description: types::tool_description(tool), - parameters: tool_schema_marker(i) - }; + let schema = agent::parse_json_object(types::tool_schema_json(tool)); + if schema.ok { + wire[wire.length] = "{\"type\":\"function\",\"name\":" + + json::encode::(types::tool_name(tool)) + ",\"description\":" + + json::encode::(types::tool_description(tool)) + ",\"parameters\":" + + schema.value + "}"; + } else { + valid = false; } i += 1; } - let mut encoded: string = ""; - if wire.length > 0 { - encoded = json::encode(wire); - } - encoded + if valid => { "[" + join_json(wire) + "]" } else => { "" } } /// Comma-joins an array of already-encoded JSON strings. diff --git a/rss/llm/openai_stream_codec.rss b/rss/llm/openai_stream_codec.rss index 4b19ca9..23d2d82 100644 --- a/rss/llm/openai_stream_codec.rss +++ b/rss/llm/openai_stream_codec.rss @@ -14,6 +14,7 @@ // passed or indexed again after one of its elements was moved. Presence and // type probes therefore live in the small `field_is_*` helpers, where the // index read is the container's last use. +use json; use self::types as types; use self::openai_chat as openai_chat; @@ -72,6 +73,109 @@ fn codec_failed(state: map, message: string) -> map { // Chunk ingestion // --------------------------------------------------------------------------- +struct ChatStreamFunction { + name: Option, + arguments: Option +} + +struct ChatStreamTool { + index: int, + id: Option, + function: Option +} + +struct ChatStreamDelta { + content: Option, + tool_calls: Option> +} + +struct ChatStreamChoice { + delta: Option, + finish_reason: Option +} + +struct ChatStreamUsage { + prompt_tokens: Option, + completion_tokens: Option, + total_tokens: Option +} + +struct ChatStreamChunk { + choices: Option>, + usage: Option +} + +fn tool_delta(tool: ChatStreamTool) -> map { + let function: map = match tool.function { + Some(value) => {name: value.name.unwrap_or(""), arguments: value.arguments.unwrap_or("")}, + None => {} + }; + {index: tool.index, id: tool.id.unwrap_or(""), function: function} +} + +fn project_delta(delta: ChatStreamDelta, reason: string) -> map { + let empty_tools: array = []; + let tools: array = delta.tool_calls.unwrap_or(empty_tools); + let mut fragments: array = []; + let mut i: int = 0; + while i < tools.length { + fragments[fragments.length] = tool_delta(tools[i].copy()); + i += 1; + } + {finish_reason: reason, delta: {content: delta.content.unwrap_or(""), tool_calls: fragments}} +} + +fn choice_delta(choice: ChatStreamChoice) -> map { + let reason: string = choice.finish_reason.unwrap_or(""); + match choice.delta { + Some(delta) => project_delta(delta, reason), + None => {finish_reason: reason} + } +} + +fn usage_field(fields: map, key: string, value: Option) -> map { + let mut result: map = fields; + let present: bool = match value { Some(_) => true, None => false }; + if present { + result[key] = value.unwrap_or(0); + } + result +} + +fn project_usage(usage: ChatStreamUsage) -> map { + let prompt: map = usage_field({}, "prompt_tokens", usage.prompt_tokens); + let completion: map = usage_field(prompt, "completion_tokens", usage.completion_tokens); + usage_field(completion, "total_tokens", usage.total_tokens) +} + +fn project_choices(choices: array) -> map { + if choices.length > 0 => { + {choices: [choice_delta(choices[0])]} + } else => { + {choices: []} + } +} + +fn with_usage(chunk: map, usage: ChatStreamUsage) -> map { + let mut result: map = chunk; + result["usage"] = project_usage(usage); + result +} + +/// Decode a native sanitized record using the closed Chat wire shape. +pub fn push_json(state: map, body: string) -> map { + let wire: ChatStreamChunk = json::decode::(body); + let choice_fields: map = match wire.choices { + Some(choices) => project_choices(choices), + None => {} + }; + let chunk: map = match wire.usage { + Some(usage) => with_usage(choice_fields, usage), + None => choice_fields + }; + push_chunk(state, chunk) +} + /// Ingests one already-decoded chunk object. Returns the state unchanged once /// it is terminal (done or failed), so late chunks are ignored. pub fn push_chunk(state: map, chunk: map) -> map { diff --git a/rss/llm/tool_descriptor.rss b/rss/llm/tool_descriptor.rss new file mode 100644 index 0000000..5746eea --- /dev/null +++ b/rss/llm/tool_descriptor.rss @@ -0,0 +1,21 @@ +// The admitted registry supplies a validated, open schema object. Only its +// serialized text belongs in the canonical LLM request. +use agent; +use self::types; + +struct LlmToolDescriptor { + name: string, + description: string, + schema_json: string +} + +pub fn from_native(descriptor: map) -> LlmToolDescriptor { + let schema: map = descriptor["schema"]; + let encoded = agent::json_text(schema); + let schema_json: string = if encoded.ok => { encoded.text } else => { "" }; + { + name: types::tool_name(descriptor), + description: types::tool_description(descriptor), + schema_json: schema_json + } +} diff --git a/rss/providers/opaque_bridge.rss b/rss/providers/opaque_bridge.rss index 44c2d39..3bbb87f 100644 --- a/rss/providers/opaque_bridge.rss +++ b/rss/providers/opaque_bridge.rss @@ -20,15 +20,62 @@ struct PreparedProviderTransport { struct PreparedProviderTransportResult { ok: bool, - error: AuthBridgeFailure?, - value: PreparedProviderTransport? + error: Option, + value: Option } -struct AccessHandlePresence { - handle: resource? +// --------------------------------------------------------------------------- +// Logical Option carriers +// --------------------------------------------------------------------------- + +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn envelope_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_failure() -> Option { + None +} + +fn no_text() -> Option { + None +} + +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_refresh_handle() -> Option> { + None +} + +fn no_code_handle() -> Option> { + None +} + +fn no_verifier_handle() -> Option> { + None +} + +fn no_device_handle() -> Option> { + None } fn empty_metadata() -> AuthMetadata { + let account_id: Option = None; + let last_refresh_at_ms: Option = None; { credential_id: "", provider: "", @@ -37,10 +84,10 @@ fn empty_metadata() -> AuthMetadata { token_type: "", expires_at_ms: 0, scopes: [], - account_id: null, + account_id: account_id, generation: 0, status: "", - last_refresh_at_ms: null, + last_refresh_at_ms: last_refresh_at_ms, has_refresh_token: false } } @@ -48,33 +95,25 @@ fn empty_metadata() -> AuthMetadata { fn empty_metadata_envelope() -> AuthMetadataEnvelope { { ok: false, - error: null, + error: no_text(), metadata: empty_metadata(), outcome: "" } } -fn empty_access_envelope() -> AccessHandleEnvelope { - { - ok: false, - error: null, - handle: null - } -} - fn invalid_metadata_result() -> PreparedProviderTransportResult { let failure: AuthBridgeFailure = { code: "invalid_credential_metadata", cancelled: false, deadline_exceeded: false, revoked: false, - transport_phase: null, - transport_category: null + transport_phase: None, + transport_category: None }; { ok: false, - error: failure, - value: null + error: Some(failure), + value: no_prepared_transport() } } @@ -84,31 +123,75 @@ fn access_unavailable_result() -> PreparedProviderTransportResult { cancelled: false, deadline_exceeded: false, revoked: false, - transport_phase: null, - transport_category: null + transport_phase: None, + transport_category: None }; { ok: false, - error: failure, - value: null + error: Some(failure), + value: no_prepared_transport() + } +} + +fn no_prepared_transport() -> Option { + None +} + +/// The single access credential use. Every unselected handle is a tagged `None` +/// carrier, so the map never carries a physical null. +fn usage_with_access(handle: resource) -> TransportCredentialUse { + { + kind: "access", + access_handle: Some(handle), + refresh_handle: no_refresh_handle(), + code_handle: no_code_handle(), + verifier_handle: no_verifier_handle(), + device_handle: no_device_handle() + } +} + +fn prepared_transport( + binding: OpaquePolicyBinding, + intent: OpaqueProviderIntent, + usage: TransportCredentialUse +) -> PreparedProviderTransportResult { + let request: TransportPublicRequest = { + policy_handle: binding.policy, + credential_id: intent.credential_id, + method: intent.method, + path: intent.path, + public_headers: intent.public_headers, + public_body: intent.public_body + }; + let value: PreparedProviderTransport = {request: request, usage: usage}; + { + ok: true, + error: no_failure(), + value: Some(value) } } fn prepare(binding: OpaquePolicyBinding, intent: OpaqueProviderIntent) -> PreparedProviderTransportResult { let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result(intent.credential_id, &binding.policy); - if loaded.error.copy() != null => { - {ok: false, error: loaded.error.copy(), value: null} + let loaded_failure: Option = loaded.error.copy(); + if failure_present(loaded_failure) => { + { + ok: false, + error: loaded_failure, + value: no_prepared_transport() + } } else if !loaded.ok => { invalid_metadata_result() } else => { - let maybe_metadata: AuthMetadataEnvelope? = loaded.value; - if maybe_metadata == null => { + let maybe_metadata: Option = loaded.value; + if !envelope_present(maybe_metadata) => { invalid_metadata_result() } else => { let metadata: AuthMetadataEnvelope = maybe_metadata.unwrap_or(empty_metadata_envelope()); - if !metadata.ok || metadata.error != null => { + let metadata_failure: Option = metadata.error; + if !metadata.ok || text_present(metadata_failure) => { invalid_metadata_result() } else => { // Metadata presence is a locked read, not an access admission. @@ -122,39 +205,21 @@ fn prepare(binding: OpaquePolicyBinding, intent: OpaqueProviderIntent) -> Prepar ); let issued_has_value: bool = issued.has("value"); let issued_ok: bool = issued.ok; - let maybe_issue_error: AuthBridgeFailure? = issued.error; - if maybe_issue_error != null => { - {ok: false, error: maybe_issue_error, value: null} + let maybe_issue_error: Option = issued.error; + if failure_present(maybe_issue_error) => { + { + ok: false, + error: maybe_issue_error, + value: no_prepared_transport() + } } else if !issued_ok => { access_unavailable_result() } else if !issued_has_value => { access_unavailable_result() } else => { - let maybe_inner: AccessHandleEnvelope? = issued.value; - let inner: AccessHandleEnvelope = - maybe_inner.unwrap_or(empty_access_envelope()); - let access: AccessHandlePresence = { handle: inner.handle }; - let has_access: bool = access.has("handle"); - if !has_access => { - access_unavailable_result() - } else => { - let request: TransportPublicRequest = { - policy_handle: binding.policy, - credential_id: intent.credential_id, - method: intent.method, - path: intent.path, - public_headers: intent.public_headers, - public_body: intent.public_body - }; - let usage: TransportCredentialUse = { - kind: "access", - access_handle: access?.handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - {ok: true, error: null, value: {request: request, usage: usage}} + match issued.value { + Some(inner) => prepared_transport(binding, intent, usage_with_access(inner.handle)), + None => access_unavailable_result() } } } @@ -162,30 +227,52 @@ fn prepare(binding: OpaquePolicyBinding, intent: OpaqueProviderIntent) -> Prepar } } +fn no_transport_response() -> Option { + None +} + pub fn call(binding: OpaquePolicyBinding, intent: OpaqueProviderIntent) -> TransportResultEnvelope { let prepared: PreparedProviderTransportResult = prepare(binding, intent); let has_value: bool = prepared.has("value"); let has_error: bool = prepared.has("error"); let ok: bool = prepared.ok; - let failure: AuthBridgeFailure? = if has_error => { prepared.error } else => { null }; - if !ok || failure != null || !has_value => { - {ok: false, error: failure, value: null} + let failure: Option = if has_error => { prepared.error } else => { no_failure() }; + let inner: Option = if !ok || failure_present(failure) || !has_value => { + no_prepared_transport() } else => { - let inner: PreparedProviderTransport = prepared.value; - oauth::transport_result(&inner.request, &inner.usage) + prepared.value + }; + match inner { + Some(value) => oauth::transport_result(&value.request, &value.usage), + None => { + ok: false, + error: failure, + value: no_transport_response() + } } } +fn no_stream_envelope() -> Option { + None +} + pub fn start_stream(binding: OpaquePolicyBinding, intent: OpaqueProviderIntent, markers: array) -> OAuthStreamStartResultEnvelope { let prepared: PreparedProviderTransportResult = prepare(binding, intent); let has_value: bool = prepared.has("value"); let has_error: bool = prepared.has("error"); let ok: bool = prepared.ok; - let failure: AuthBridgeFailure? = if has_error => { prepared.error } else => { null }; - if !ok || failure != null || !has_value => { - {ok: false, error: failure, value: null} + let failure: Option = if has_error => { prepared.error } else => { no_failure() }; + let inner: Option = if !ok || failure_present(failure) || !has_value => { + no_prepared_transport() } else => { - let inner: PreparedProviderTransport = prepared.value; - oauth::stream_start_result(&inner.request, &inner.usage, markers) + prepared.value + }; + match inner { + Some(value) => oauth::stream_start_result(&value.request, &value.usage, markers), + None => { + ok: false, + error: failure, + value: no_stream_envelope() + } } } diff --git a/rss/providers/opaque_responses.rss b/rss/providers/opaque_responses.rss index f9dcb00..7795bed 100644 --- a/rss/providers/opaque_responses.rss +++ b/rss/providers/opaque_responses.rss @@ -8,7 +8,6 @@ // owns the wire. use auth; use oauth; -use json; use super::llm::types as types; use super::llm::openai_responses as responses; use super::agent::provider_runtime as provider_runtime; @@ -25,14 +24,6 @@ struct ResponsesOpaqueHostHomeBinding { home: resource } -struct ResponsesStreamValuePresence { - value: OAuthStreamEnvelope? -} - -struct ResponsesRecordValuePresence { - value: OAuthStreamRecord? -} - /// Canonical Codex Responses public path for a merged profile. fn responses_path_or_default(profile: map) -> string { let configured: string = types::profile_string(profile, "responses_path"); @@ -76,16 +67,14 @@ fn run_responses_mode(context: map, force_stream: bool) -> map { "config", "credential-bearing provider options are not public" )) - } else if streaming => { - responses_send_stream(request, profile, context, model) } else => { - responses_send_buffered(request, profile, context, model) + responses_send(request, profile, context, model, streaming) }; result } -fn stream_transport_failure(failure: AuthBridgeFailure?) -> map { - let detail: AuthBridgeFailure = failure.unwrap_or({code: "transport_error", cancelled: false, deadline_exceeded: false, revoked: false, transport_phase: null, transport_category: null}); +fn stream_transport_failure(failure: Option) -> map { + let detail: AuthBridgeFailure = failure.unwrap_or({code: "transport_error", cancelled: false, deadline_exceeded: false, revoked: false, transport_phase: None, transport_category: None}); responses_transport_failure(detail) } @@ -96,14 +85,42 @@ fn responses_transport_failure(detail: AuthBridgeFailure) -> map { let mut error: map = types::error_request(detail.code, "provider transport failed"); error.retryable = detail.code == "transport_error" && detail.cancelled == false && detail.deadline_exceeded == false && detail.revoked == false; + let category: Option = detail.transport_category; + let phase: Option = detail.transport_phase; + if responses_text_present(category.copy()) && responses_text_present(phase.copy()) { + error.transport_category = category.unwrap_or(""); + error.transport_phase = phase.unwrap_or(""); + } types::fail(error) } +fn responses_text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + fn malformed_stream(status: int) -> map { types::fail(types::error_malformed(status, "malformed provider stream record")) } -fn responses_send_buffered(request: map, profile: map, context: map, model: string) -> map { +fn responses_send(request: map, profile: map, context: map, model: string, streaming: bool) -> map { + let body: string = if streaming => { + responses::responses_public_stream_body(request, model) + } else => { + responses::responses_public_body(request, model) + }; + if body == "" => { + types::fail(types::error_request("invalid_request", "invalid Responses tool schema")) + } else if streaming => { + responses_send_stream(body, profile, context) + } else => { + responses_send_buffered(body, profile, context) + } +} + +fn responses_send_buffered(body: string, profile: map, context: map) -> map { let binding: ResponsesOpaqueHostHomeBinding = { home: context["host_home"], }; let credential_id: string = types::request_string(context, "credential_id"); let intent: ResponsesOpaqueProviderIntent = { @@ -114,39 +131,58 @@ fn responses_send_buffered(request: map, profile: map, context: map, model: stri { name: "accept", value: "application/json" }, { name: "content-type", value: "application/json" } ], - public_body: responses::responses_public_body(request, model) + public_body: body }; let envelope: TransportResultEnvelope = provider_runtime::call(binding, intent); responses_interpret_transport(envelope) } -fn responses_interpret_transport(envelope: TransportResultEnvelope) -> map { - let has_response: bool = envelope.has("value"); - let maybe_failure: AuthBridgeFailure? = envelope.error; - let failure: AuthBridgeFailure = maybe_failure.unwrap_or({ - code: "transport_error", - cancelled: false, - deadline_exceeded: false, - revoked: false, - transport_phase: null, - transport_category: null - }); - let maybe_response: TransportResponseEnvelope? = if has_response => { - envelope.value - } else => { - null - }; - let response: TransportResponseEnvelope = maybe_response.unwrap_or({ +fn responses_failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn responses_no_text() -> Option { + None +} + +/// The absent-response frame: every logical `Option` leaf is a tagged `None` +/// carrier, never a physical null. +fn responses_empty_response() -> TransportResponseEnvelope { + let retry_after_ms: Option = None; + let access_slot: Option> = None; + let refresh_slot: Option> = None; + let device_handle: Option> = None; + { ok: false, - error: null, + error: responses_no_text(), status: 0, public_headers: [], body_without_secret_fields: "", - retry_after_ms: null, - access_slot: null, - refresh_slot: null, - device_handle: null - }); + retry_after_ms: retry_after_ms, + access_slot: access_slot, + refresh_slot: refresh_slot, + device_handle: device_handle + } +} + +fn responses_transport_error(maybe_failure: Option) -> AuthBridgeFailure { + match maybe_failure { + Some(value) => value, + None => {code: "transport_error", cancelled: false, deadline_exceeded: false, revoked: false, transport_phase: None, transport_category: None} + } +} + +fn responses_interpret_transport(envelope: TransportResultEnvelope) -> map { + let maybe_failure: Option = envelope.error; + let failure: AuthBridgeFailure = responses_transport_error(maybe_failure); + let maybe_response: Option = envelope.value; + let response: TransportResponseEnvelope = match maybe_response { + Some(value) => value, + None => responses_empty_response() + }; let result: map = if envelope.ok => { responses_parse_transport_response(response) } else => { @@ -156,14 +192,65 @@ fn responses_interpret_transport(envelope: TransportResultEnvelope) -> map { } fn responses_parse_transport_response(response: TransportResponseEnvelope) -> map { - if response.status >= 200 && response.status < 300 => { + let status: int = response.status; + if status >= 200 && status < 300 => { responses::parse_buffered_body(response.body_without_secret_fields) } else => { - responses::parse_error_body(response.body_without_secret_fields, response.status) + responses::parse_error_body(response.body_without_secret_fields, status) } } -fn responses_send_stream(request: map, profile: map, context: map, model: string) -> map { +fn responses_open_stream_envelope( + ok: bool, + refused: bool, + value: Option +) -> Option { + if !ok || refused => { + None + } else => { + value + } +} + +/// A read envelope is usable only when it reported success without a failure and +/// carried a record; every other combination is the transport-failure path. +fn responses_stream_record_ready( + ok: bool, + failure: Option, + record_present: bool +) -> bool { + ok && !responses_failure_present(failure) && record_present +} + +/// An absent record. Only reachable if the read envelope reported no value while +/// claiming success; it decodes to the malformed-record refusal. +fn responses_empty_record() -> OAuthStreamRecord { + let literal_index: Option = None; + {kind: "", json: "", literal_index: literal_index} +} + +/// One record carrier plus its presence. The record is taken apart once so the +/// read field is moved exactly one time. +struct RecordPeek { + present: bool, + record: OAuthStreamRecord +} + +fn responses_peek_record(value: Option) -> RecordPeek { + match value { + Some(record) => {present: true, record: record}, + None => {present: false, record: responses_empty_record()} + } +} + +fn responses_first_literal(index: Option) -> bool { + match index { + Some(value) => value == 0, + None => false + } +} + +fn responses_send_stream(body: string, profile: map, context: map) -> map { let binding: ResponsesOpaqueHostHomeBinding = {home: context["host_home"]}; let intent: ResponsesOpaqueProviderIntent = { credential_id: types::request_string(context, "credential_id"), @@ -173,76 +260,75 @@ fn responses_send_stream(request: map, profile: map, context: map, model: string {name: "accept", value: "text/event-stream"}, {name: "content-type", value: "application/json"} ], - public_body: responses::responses_public_stream_body(request, model) + public_body: body }; let started: OAuthStreamStartResultEnvelope = provider_runtime::start_stream(binding, intent, ["[DONE]"]); - let has_value: bool = started.has("value"); let ok: bool = started.ok; - let failure: AuthBridgeFailure? = started.error; - if !ok || failure != null || !has_value => { - stream_transport_failure(failure) - } else => { - let holder: ResponsesStreamValuePresence = {value: started.value}; - let envelope: OAuthStreamEnvelope = holder.value; - let stream: resource = envelope.stream; - let head: OAuthStreamHead = envelope.head; - let status: int = head.status; - let mut state: map = responses::responses_begin(); - let mut result: map = {}; - let mut finished: bool = false; - while !finished { - let read: OAuthStreamReadResultEnvelope = oauth::stream_read_result(&stream); - let read_has_value: bool = read.has("value"); - let read_ok: bool = read.ok; - let read_failure: AuthBridgeFailure? = read.error; - if !read_ok || read_failure != null || !read_has_value { - result = stream_transport_failure(read_failure); - finished = true; - } else { - let record_holder: ResponsesRecordValuePresence = {value: read.value}; - let record: OAuthStreamRecord = record_holder.value; - let kind: string = record.kind; - if kind == "json" { - let body: string = record.json; - // Native records are strict canonical JSON objects or arrays. - if body.copy()[0:1] != "{" { - result = malformed_stream(status); - finished = true; - } else if status < 200 || status >= 300 { - result = responses::parse_error_body(body, status); - finished = true; - } else { - let chunk: map = json::decode(body); - state = responses::responses_push_chunk(state, chunk); - if responses_stream_terminal(state) { - result = responses::responses_finish(state); - finished = true; - } - } - } else if status < 200 || status >= 300 { + let failure: Option = started.error; + let result: map = match responses_open_stream_envelope(ok, responses_failure_present(failure), started.value) { + Some(envelope) => responses_consume_stream(envelope), + None => stream_transport_failure(failure) + }; + result +} + +fn responses_consume_stream(envelope: OAuthStreamEnvelope) -> map { + let stream: resource = envelope.stream; + let head: OAuthStreamHead = envelope.head; + let status: int = head.status; + let mut state: map = responses::responses_begin(); + let mut result: map = {}; + let mut finished: bool = false; + while !finished { + let read: OAuthStreamReadResultEnvelope = oauth::stream_read_result(&stream); + let read_ok: bool = read.ok; + let read_failure: Option = read.error; + let peeked_record: RecordPeek = responses_peek_record(read.value); + let record_ready: bool = responses_stream_record_ready(read_ok, read_failure, peeked_record.present); + if !record_ready { + result = stream_transport_failure(read_failure); + finished = true; + } else { + let record: OAuthStreamRecord = peeked_record.record; + let kind: string = record.kind; + if kind == "json" { + let body: string = record.json; + // Native records are strict canonical JSON objects or arrays. + if body.copy()[0:1] != "{" { result = malformed_stream(status); finished = true; - } else if kind == "literal" { - let index: int? = record.literal_index; - if index.unwrap_or(-1) == 0 { - state = responses::responses_push_done(state); + } else if status < 200 || status >= 300 { + result = responses::parse_error_body(body, status); + finished = true; + } else { + state = responses::responses_push_json(state, body); + if responses_stream_terminal(state) { result = responses::responses_finish(state); - } else { - result = malformed_stream(status); + finished = true; } - finished = true; - } else if kind == "eof" { + } + } else if status < 200 || status >= 300 { + result = malformed_stream(status); + finished = true; + } else if kind == "literal" { + if responses_first_literal(record.literal_index) { + state = responses::responses_push_done(state); result = responses::responses_finish(state); - finished = true; } else { result = malformed_stream(status); - finished = true; } + finished = true; + } else if kind == "eof" { + result = responses::responses_finish(state); + finished = true; + } else { + result = malformed_stream(status); + finished = true; } } - oauth::stream_close(stream); - result } + oauth::stream_close(stream); + result } /// The stream is terminal once the codec recorded a completed or failed event. diff --git a/rss/providers/refresh.rss b/rss/providers/refresh.rss index 012b6e2..886d6d9 100644 --- a/rss/providers/refresh.rss +++ b/rss/providers/refresh.rss @@ -20,8 +20,8 @@ struct ProviderRefreshResult { ok: bool, code: string, outcome: string, - metadata: AuthMetadata?, - failure: AuthBridgeFailure? + metadata: Option, + failure: Option } // Unit-local declarations identical to rss/auth/token_save.rss; the pinned @@ -36,51 +36,182 @@ struct TokenSaveInput { struct TokenSaveResult { ok: bool, code: string, - outcome: string?, - value: AuthMetadata?, - failure: AuthBridgeFailure? + outcome: Option, + value: Option, + failure: Option } -struct RefreshHandlePresence { - handle: resource? +fn max_int() -> int { + 9223372036854775807 } -struct SecretSlotPresence { - handle: resource? +// --------------------------------------------------------------------------- +// Logical Option carriers +// +// Absent leaves arrive as tagged `None` carriers. Each helper consumes one with +// an exhaustive match; the payload is bound where the decision needs it. +// --------------------------------------------------------------------------- + +fn failure_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } } -fn max_int() -> int { - 9223372036854775807 +fn text_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn int_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn field_present(value: Option) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn slot_present(value: Option>) -> bool { + match value { + Some(_) => true, + None => false + } +} + +fn no_failure() -> Option { + None +} + +fn no_text() -> Option { + None +} + +fn no_int() -> Option { + None +} + +fn no_float() -> Option { + None +} + +fn no_bool() -> Option { + None +} + +fn no_metadata() -> Option { + None +} + +fn no_transport_response() -> Option { + None +} + +fn no_metadata_envelope() -> Option { + None +} + +fn no_refresh_envelope() -> Option { + None +} + +fn no_slot() -> Option> { + None +} + +fn no_access_handle() -> Option> { + None +} + +fn no_code_handle() -> Option> { + None +} + +fn no_verifier_handle() -> Option> { + None +} + +fn no_device_handle() -> Option> { + None +} + +/// One slot carrier plus its presence. A resource handle is move-only, so the +/// only way to both test and then use a slot is to take it apart once and hand +/// the carrier back inside this shell. +struct SlotPeek { + present: bool, + slot: Option> +} + +fn peek_slot(slot: Option>) -> SlotPeek { + match slot { + Some(value) => {present: true, slot: Some(value)}, + None => {present: false, slot: no_slot()} + } +} + +/// Closes the access slot together with the optional refresh slot the transport +/// published. The refresh carrier is consumed by a match, so the `Some` arm +/// binds the live handle. +fn close_saved_slots( + access: resource, + refresh_slot: Option> +) -> () { + auth::close_slot(access); + close_slot_option(refresh_slot); + () +} + +/// Closes one published slot. The carrier is consumed here: the `Some` arm hands +/// the live handle to the host close, the `None` arm has nothing to close. +fn close_slot_option(slot: Option>) -> () { + match slot { + Some(value) => auth::close_slot(value), + None => () + } } fn failed(code: string) -> ProviderRefreshResult { - {ok: false, code: code, outcome: "", metadata: null, failure: null} -} - -fn failed_native(failure_value: AuthBridgeFailure?, fallback: string) -> ProviderRefreshResult { - if failure_value != null => { - let failure: AuthBridgeFailure = failure_value; - let code: string = failure_code(failure_value.copy(), fallback.copy()); - { - ok: false, - code: code.copy(), - outcome: "", - metadata: null, - failure: { - code: code.copy(), - cancelled: failure.cancelled.copy(), - deadline_exceeded: failure.deadline_exceeded.copy(), - revoked: failure.revoked.copy(), - transport_phase: null, - transport_category: null - } - } - } else => { - failed(fallback.copy()) + {ok: false, code: code, outcome: "", metadata: no_metadata(), failure: no_failure()} +} + +fn failure_result_with(failure: AuthBridgeFailure, fallback: string) -> ProviderRefreshResult { + let code: string = failure_code(Some(failure), fallback.copy()); + let detail: AuthBridgeFailure = { + code: code.copy(), + cancelled: failure.cancelled.copy(), + deadline_exceeded: failure.deadline_exceeded.copy(), + revoked: failure.revoked.copy(), + transport_phase: failure.transport_phase.copy(), + transport_category: failure.transport_category.copy() + }; + { + ok: false, + code: code.copy(), + outcome: "", + metadata: no_metadata(), + failure: Some(detail) + } +} + +fn failed_native(failure_value: Option, fallback: string) -> ProviderRefreshResult { + match failure_value { + Some(failure) => failure_result_with(failure, fallback), + None => failed(fallback.copy()) } } fn empty_metadata() -> AuthMetadata { + let account_id: Option = None; + let last_refresh_at_ms: Option = None; { credential_id: "", provider: "", @@ -89,44 +220,52 @@ fn empty_metadata() -> AuthMetadata { token_type: "", expires_at_ms: 0, scopes: [], - account_id: null, + account_id: account_id, generation: 0, status: "", - last_refresh_at_ms: null, + last_refresh_at_ms: last_refresh_at_ms, has_refresh_token: false } } fn empty_metadata_envelope() -> AuthMetadataEnvelope { - {ok: false, error: null, metadata: empty_metadata(), outcome: ""} -} - -fn empty_refresh_envelope() -> RefreshHandleEnvelope { - {ok: false, error: null, handle: null} + {ok: false, error: no_text(), metadata: empty_metadata(), outcome: ""} } fn empty_transport_response() -> TransportResponseEnvelope { + let access_slot: Option> = None; + let refresh_slot: Option> = None; + let device_handle: Option> = None; { ok: false, - error: null, + error: no_text(), status: 0, public_headers: [], body_without_secret_fields: "", - retry_after_ms: null, - access_slot: null, - refresh_slot: null, - device_handle: null + retry_after_ms: no_int(), + access_slot: access_slot, + refresh_slot: refresh_slot, + device_handle: device_handle + } +} + +fn empty_public_field() -> PublicJsonFieldEnvelope { + { + kind: "", + string_value: no_text(), + int_value: no_int(), + float_value: no_float(), + bool_value: no_bool(), + length: no_int() } } // Only the host-permitted public envelope code is projected; the private // reason never crosses back into RSS. -fn failure_code(failure_value: AuthBridgeFailure?, fallback: string) -> string { - if failure_value != null => { - let failure: AuthBridgeFailure = failure_value; - failure.code.copy() - } else => { - fallback +fn failure_code(failure_value: Option, fallback: string) -> string { + match failure_value { + Some(failure) => failure.code.copy(), + None => fallback } } @@ -143,15 +282,18 @@ fn admitted(metadata: AuthMetadata, credential_id: string) -> bool { } fn usable(envelope: PublicJsonFieldResultEnvelope) -> bool { - envelope.ok.copy() && envelope.error.copy() == null && envelope.value.copy() != null + let failure: Option = envelope.error.copy(); + let value: Option = envelope.value.copy(); + envelope.ok.copy() && !failure_present(failure) && field_present(value) } fn exact_error(envelope: PublicJsonFieldResultEnvelope) -> string { let mut code: string = ""; if usable(envelope.copy()) { - let selected: PublicJsonFieldEnvelope = envelope.value.copy(); - if selected.kind.copy() == "string" && selected.string_value.copy() != null { - code = selected.string_value.copy(); + let selected: PublicJsonFieldEnvelope = envelope.value.copy().unwrap_or(empty_public_field()); + let selected_text: Option = selected.string_value.copy(); + if selected.kind.copy() == "string" && text_present(selected_text) { + code = selected_text.unwrap_or(""); } } code @@ -162,9 +304,10 @@ fn exact_error(envelope: PublicJsonFieldResultEnvelope) -> string { fn expiry_seconds(envelope: PublicJsonFieldResultEnvelope) -> int { let mut seconds: int = 0; if usable(envelope.copy()) { - let selected: PublicJsonFieldEnvelope = envelope.value.copy(); - if selected.kind.copy() == "int" && selected.int_value.copy() != null { - let value: int = selected.int_value.copy(); + let selected: PublicJsonFieldEnvelope = envelope.value.copy().unwrap_or(empty_public_field()); + let selected_int: Option = selected.int_value.copy(); + if selected.kind.copy() == "int" && int_present(selected_int) { + let value: int = selected_int.unwrap_or(0); if value >= 1 && value <= 86400 { seconds = value; } @@ -176,11 +319,12 @@ fn expiry_seconds(envelope: PublicJsonFieldResultEnvelope) -> int { fn bearer_token_type(envelope: PublicJsonFieldResultEnvelope) -> bool { let mut valid: bool = false; if usable(envelope.copy()) { - let selected: PublicJsonFieldEnvelope = envelope.value.copy(); + let selected: PublicJsonFieldEnvelope = envelope.value.copy().unwrap_or(empty_public_field()); + let selected_text: Option = selected.string_value.copy(); if selected.kind.copy() == "missing" { valid = true; - } else if selected.kind.copy() == "string" && selected.string_value.copy() != null { - if selected.string_value.copy() == "Bearer" { + } else if selected.kind.copy() == "string" && text_present(selected_text) { + if selected_text.unwrap_or("") == "Bearer" { valid = true; } } @@ -203,16 +347,132 @@ fn non_success_code(policy: resource, body: string, status: int) -> } fn close_returned_slots( - access_slot: resource?, - refresh_slot: resource? -) -> null { - if access_slot != null { - auth::close_slot(access_slot); + access_slot: Option>, + refresh_slot: Option> +) -> () { + close_slot_option(access_slot); + close_slot_option(refresh_slot); + () +} + +fn none_result() -> Option { + None +} + +/// The refusal verdict, computed without touching either slot. Every branch here +/// is a pure decision; the caller closes the published slots exactly once. +fn refusal_verdict( + policy: resource, + response_ok: bool, + status: int, + body: string, + expiry_failure: Option, + token_type_failure: Option, + expires_in: int, + token_type_ok: bool +) -> Option { + let mut rejected: Option = none_result(); + if !response_ok { + rejected = Some(failed("refresh_failed")); + } else if status < 200 || status >= 300 { + rejected = Some(failed(non_success_code(policy, body.copy(), status))); + } else if failure_present(expiry_failure) { + rejected = Some(failed_native(expiry_failure, "refresh_response_invalid")); + } else if failure_present(token_type_failure) { + rejected = Some(failed_native(token_type_failure, "refresh_response_invalid")); + } else if expires_in < 1 || !token_type_ok { + rejected = Some(failed("refresh_response_invalid")); } - if refresh_slot != null { - auth::close_slot(refresh_slot); + rejected +} + +/// Closes the published slots and hands back the verdict, so a refusal path owns +/// the handles exactly once. +fn close_then( + result: ProviderRefreshResult, + access: resource, + refresh_slot: Option> +) -> ProviderRefreshResult { + close_saved_slots(access, refresh_slot); + result +} + +fn save_refreshed( + policy: resource, + home: resource, + intent: OpaqueRefreshIntent, + metadata: AuthMetadata, + access: resource, + refresh_field: Option>, + has_returned_refresh: bool, + expires_in: int +) -> ProviderRefreshResult { + let now_field: OAuthNowResultEnvelope = oauth::now_ms_result(&policy); + let now_maybe: Option = now_field.value; + let now_has_value: bool = int_present(now_maybe); + let now_ms: int = now_maybe.unwrap_or(-1); + let now_failure: Option = now_field.error.copy(); + if !now_field.ok.copy() || failure_present(now_failure) => { + close_then(failed_native(now_failure, "clock_unavailable"), access, refresh_field) + } else if !now_has_value || now_ms < 0 || now_ms > 9000000000000000 => { + close_then(failed("clock_unavailable"), access, refresh_field) + } else => { + let lifetime_ms: int = expires_in * 1000; + if now_ms > max_int() - lifetime_ms => { + close_then(failed("refresh_response_invalid"), access, refresh_field) + } else => { + let expires_at_ms: int = now_ms + lifetime_ms; + let refresh_action: string = if has_returned_refresh => { + "replace" + } else => { + "preserve" + }; + let credential_metadata: CredentialMetadata = { + provider: metadata.provider.copy(), + kind: "oauth", + source: metadata.source.copy(), + token_type: "Bearer", + expires_at_ms: expires_at_ms, + scopes: metadata.scopes.copy(), + account_id: metadata.account_id.copy(), + generation: metadata.generation.copy(), + status: "active", + last_refresh_at_ms: Some(now_ms), + has_refresh_token: metadata.has_refresh_token.copy() || has_returned_refresh + }; + let input: TokenSaveInput = { + credential_id: intent.credential_id.copy(), + expected_generation: metadata.generation.copy(), + metadata: credential_metadata, + refresh_action: refresh_action.copy() + }; + let saved: TokenSaveResult = + token_save::save(home, policy, input, access, refresh_field); + let saved_has_value: bool = saved.has("value"); + let saved_maybe: Option = if saved_has_value => { saved.value.copy() } else => { no_metadata() }; + let saved_metadata: AuthMetadata = saved_maybe.unwrap_or(empty_metadata()); + let has_outcome: bool = saved.has("outcome"); + let outcome_maybe: Option = if has_outcome => { saved.outcome.copy() } else => { no_text() }; + let saved_has_failure: bool = saved.has("failure"); + let saved_failure: Option = if saved_has_failure => { saved.failure.copy() } else => { no_failure() }; + if !saved.ok.copy() => { + failed_native(saved_failure, saved.code.copy()) + } else if !has_outcome => { + failed("save_failed") + } else if !saved_has_value => { + failed("save_failed") + } else => { + let outcome: string = outcome_maybe.unwrap_or(""); + { + ok: true, + code: saved.code.copy(), + outcome: outcome.copy(), + metadata: Some(saved_metadata.copy()), + failure: no_failure() + } + } + } } - null } fn complete( @@ -222,116 +482,152 @@ fn complete( metadata: AuthMetadata, response: TransportResponseEnvelope ) -> ProviderRefreshResult { - let mut result: ProviderRefreshResult = failed("refresh_response_invalid"); - let access_present: bool = response.has("access_slot"); - let has_refresh: bool = response.has("refresh_slot"); - let response_ok: bool = response.ok; + let peeked_access: SlotPeek = peek_slot(response.access_slot); + let peeked_refresh: SlotPeek = peek_slot(response.refresh_slot); + let refresh_field: Option> = peeked_refresh.slot; + let has_returned_refresh: bool = peeked_refresh.present; let status: int = response.status; let body: string = response.body_without_secret_fields; - let returned_refresh: SecretSlotPresence = if has_refresh => { - {handle: response.refresh_slot} - } else => { - {handle: null} - }; - let has_returned_refresh: bool = returned_refresh.has("handle"); - let refresh_field: resource? = returned_refresh?.handle; + let response_ok: bool = response.ok; let expiry_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy, body.copy(), "/expires_in"); let token_type_field: PublicJsonFieldResultEnvelope = oauth::public_json_field_result(&policy, body.copy(), "/token_type"); - let expiry_failure: AuthBridgeFailure? = expiry_field.error.copy(); - let token_type_failure: AuthBridgeFailure? = token_type_field.error.copy(); + let expiry_failure: Option = expiry_field.error.copy(); + let token_type_failure: Option = token_type_field.error.copy(); let expires_in: int = expiry_seconds(expiry_field.copy()); let token_type_ok: bool = bearer_token_type(token_type_field.copy()); - if !response_ok { - close_returned_slots(response.access_slot, refresh_field); - result = failed("refresh_failed"); - } else if status < 200 || status >= 300 { - close_returned_slots(response.access_slot, refresh_field); - result = failed(non_success_code(policy, body.copy(), status)); - } else if expiry_failure != null { - close_returned_slots(response.access_slot, refresh_field); - result = failed_native(expiry_failure.copy(), "refresh_response_invalid"); - } else if token_type_failure != null { - close_returned_slots(response.access_slot, refresh_field); - result = failed_native(token_type_failure.copy(), "refresh_response_invalid"); - } else if !access_present { - close_returned_slots(response.access_slot, refresh_field); - result = failed("refresh_response_invalid"); - } else if expires_in < 1 || !token_type_ok { - close_returned_slots(response.access_slot, refresh_field); - result = failed("refresh_response_invalid"); - } else { - let now_field: OAuthNowResultEnvelope = oauth::now_ms_result(&policy); - let now_maybe: int? = now_field.value; - let now_has_value: bool = now_maybe != null; - let now_ms: int = now_maybe.unwrap_or(-1); - if !now_field.ok.copy() || now_field.error.copy() != null { - close_returned_slots(response.access_slot, refresh_field); - result = failed_native(now_field.error.copy(), "clock_unavailable"); - } else if !now_has_value || now_ms < 0 || now_ms > 9000000000000000 { - close_returned_slots(response.access_slot, refresh_field); - result = failed("clock_unavailable"); - } else { - let lifetime_ms: int = expires_in * 1000; - if now_ms > max_int() - lifetime_ms { - close_returned_slots(response.access_slot, refresh_field); - result = failed("refresh_response_invalid"); - } else { - let expires_at_ms: int = now_ms + lifetime_ms; - let refresh_action: string = if has_returned_refresh => { - "replace" - } else => { - "preserve" - }; - let credential_metadata: CredentialMetadata = { - provider: metadata.provider.copy(), - kind: "oauth", - source: metadata.source.copy(), - token_type: "Bearer", - expires_at_ms: expires_at_ms, - scopes: metadata.scopes.copy(), - account_id: metadata.account_id.copy(), - generation: metadata.generation.copy(), - status: "active", - last_refresh_at_ms: now_ms, - has_refresh_token: metadata.has_refresh_token.copy() || has_returned_refresh - }; - let input: TokenSaveInput = { - credential_id: intent.credential_id.copy(), - expected_generation: metadata.generation.copy(), - metadata: credential_metadata, - refresh_action: refresh_action.copy() - }; - let saved: TokenSaveResult = - token_save::save(home, policy, input, response.access_slot, refresh_field); - let saved_has_value: bool = saved.has("value"); - let saved_maybe: AuthMetadata? = if saved_has_value => { saved.value.copy() } else => { null }; - let saved_metadata: AuthMetadata = saved_maybe.unwrap_or(empty_metadata()); - let has_outcome: bool = saved.has("outcome"); - let outcome_maybe: string? = if has_outcome => { saved.outcome.copy() } else => { null }; - let saved_has_failure: bool = saved.has("failure"); - let saved_failure: AuthBridgeFailure? = if saved_has_failure => { saved.failure.copy() } else => { null }; - if !saved.ok.copy() { - result = failed_native(saved_failure, saved.code.copy()); - } else if !has_outcome { - result = failed("save_failed"); - } else if !saved_has_value { - result = failed("save_failed"); - } else { - let outcome: string = outcome_maybe.unwrap_or(""); - result = { - ok: true, - code: saved.code.copy(), - outcome: outcome.copy(), - metadata: saved_metadata.copy(), - failure: null - }; + let rejected: Option = refusal_verdict( + policy, + response_ok, + status, + body.copy(), + expiry_failure, + token_type_failure, + expires_in, + token_type_ok + ); + match peeked_access.slot { + Some(access) => match rejected { + Some(result) => close_then(result, access, refresh_field), + None => save_refreshed( + policy, + home, + intent, + metadata, + access, + refresh_field, + has_returned_refresh, + expires_in + ) + }, + None => failed("refresh_response_invalid") + } +} + +/// Completes the exchange with the published access slot after the transport +/// response; the response envelope is rebuilt with the two slot carriers the +/// caller already holds. +fn finish_refresh_request( + policy: resource, + home: resource, + intent: OpaqueRefreshIntent, + metadata: AuthMetadata, + response: TransportResponseEnvelope +) -> ProviderRefreshResult { + let peeked_access: SlotPeek = peek_slot(response.access_slot); + let response_refresh: Option> = response.refresh_slot; + if !peeked_access.present => { + let response_ok: bool = response.ok; + let status: int = response.status; + let body: string = response.body_without_secret_fields; + close_returned_slots(no_slot(), response_refresh); + if !response_ok => { + failed("refresh_failed") + } else if status >= 200 && status < 300 => { + failed("refresh_response_invalid") + } else => { + failed(non_success_code(policy, body, status)) + } + } else => { + match peeked_access.slot { + Some(access) => complete( + policy, + home, + intent, + metadata, + { + ok: response.ok, + error: response.error, + status: response.status, + public_headers: response.public_headers, + body_without_secret_fields: response.body_without_secret_fields, + retry_after_ms: response.retry_after_ms, + access_slot: Some(access), + refresh_slot: response_refresh, + device_handle: response.device_handle } - } + ), + None => failed("refresh_response_invalid") } } - result +} + +fn send_refresh_request( + policy: resource, + home: resource, + intent: OpaqueRefreshIntent, + metadata: AuthMetadata, + refresh: resource +) -> ProviderRefreshResult { + let request: TransportPublicRequest = { + policy_handle: policy, + credential_id: intent.credential_id.copy(), + method: "POST", + path: intent.path.copy(), + public_headers: [ + {name: "content-type", value: "application/x-www-form-urlencoded"}, + {name: "accept", value: "application/json"} + ], + public_body: intent.public_body.copy() + }; + let usage: TransportCredentialUse = { + kind: "refresh", + access_handle: no_access_handle(), + refresh_handle: Some(refresh), + code_handle: no_code_handle(), + verifier_handle: no_verifier_handle(), + device_handle: no_device_handle() + }; + let sent: TransportResultEnvelope = oauth::transport_result(&request, &usage); + let response_has_value: bool = sent.has("value"); + let sent_ok: bool = sent.ok; + let send_error: Option = sent.error; + let response_value: Option = if response_has_value => { sent.value } else => { no_transport_response() }; + let wanted: Option = if !sent_ok || failure_present(send_error) || !response_has_value => { + no_transport_response() + } else => { + response_value + }; + match wanted { + Some(response) => finish_refresh_request(request.policy_handle, home, intent, metadata, response), + None => failed_native(send_error, "refresh_failed") + } +} + +fn with_issued_handle( + policy: resource, + home: resource, + intent: OpaqueRefreshIntent, + metadata: AuthMetadata, + envelope: RefreshHandleEnvelope +) -> ProviderRefreshResult { + let issued_error: Option = envelope.error; + if !envelope.ok || text_present(issued_error) => { + failed("refresh_unavailable") + } else => { + send_refresh_request(policy, home, intent, metadata, envelope.handle) + } } fn issue_and_send( @@ -346,97 +642,17 @@ fn issue_and_send( auth::refresh_handle_result(intent.credential_id.copy(), generation, &policy); let issued_has_value: bool = issued.has("value"); let issued_ok: bool = issued.ok; - let issue_error: AuthBridgeFailure? = issued.error; - let issued_maybe: RefreshHandleEnvelope? = if issued_has_value => { issued.value } else => { null }; - let issued_envelope: RefreshHandleEnvelope = issued_maybe.unwrap_or(empty_refresh_envelope()); - if !issued_ok || issue_error.copy() != null { - result = failed_native(issue_error.copy(), "refresh_unavailable"); + let issue_error: Option = issued.error; + let issued_value: Option = if issued_has_value => { issued.value } else => { no_refresh_envelope() }; + if !issued_ok || failure_present(issue_error) { + result = failed_native(issue_error, "refresh_unavailable"); } else if !issued_has_value { result = failed("refresh_unavailable"); - } else if !issued_envelope.ok || issued_envelope.error != null { - result = failed(failure_code(issue_error.copy(), "refresh_unavailable")); } else { - let refresh: RefreshHandlePresence = {handle: issued_envelope.handle}; - let has_refresh_handle: bool = refresh.has("handle"); - if !has_refresh_handle { - result = failed("refresh_unavailable"); - } else { - let request: TransportPublicRequest = { - policy_handle: policy, - credential_id: intent.credential_id.copy(), - method: "POST", - path: intent.path.copy(), - public_headers: [ - {name: "content-type", value: "application/x-www-form-urlencoded"}, - {name: "accept", value: "application/json"} - ], - public_body: intent.public_body.copy() - }; - let usage: TransportCredentialUse = { - kind: "refresh", - access_handle: null, - refresh_handle: refresh?.handle, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let sent: TransportResultEnvelope = oauth::transport_result(&request, &usage); - let response_has_value: bool = sent.has("value"); - let sent_ok: bool = sent.ok; - let send_error: AuthBridgeFailure? = sent.error; - let response_maybe: TransportResponseEnvelope? = if response_has_value => { sent.value } else => { null }; - let response: TransportResponseEnvelope = - response_maybe.unwrap_or(empty_transport_response()); - if !sent_ok || send_error.copy() != null { - result = failed_native(send_error.copy(), "refresh_failed"); - } else if !response_has_value { - result = failed("refresh_failed"); - } else { - let response_has_access_field: bool = response.has("access_slot"); - let response_has_refresh_field: bool = response.has("refresh_slot"); - let response_access: SecretSlotPresence = if response_has_access_field => { - {handle: response.access_slot} - } else => { {handle: null} }; - let response_refresh: SecretSlotPresence = if response_has_refresh_field => { - {handle: response.refresh_slot} - } else => { {handle: null} }; - let response_has_access: bool = response_access.has("handle"); - let response_has_refresh: bool = response_refresh.has("handle"); - if !response_has_access { - let response_ok: bool = response.ok; - let status: int = response.status; - let body: string = response.body_without_secret_fields; - if response_has_refresh { - close_returned_slots(null, response_refresh?.handle); - } - if !response_ok { - result = failed("refresh_failed"); - } else if status >= 200 && status < 300 { - result = failed("refresh_response_invalid"); - } else { - result = failed(non_success_code(request.policy_handle, body, status)); - } - } else { - result = complete( - request.policy_handle, - home, - intent.copy(), - metadata.copy(), - { - ok: response.ok, - error: response.error, - status: response.status, - public_headers: response.public_headers, - body_without_secret_fields: response.body_without_secret_fields, - retry_after_ms: response.retry_after_ms, - access_slot: response_access?.handle, - refresh_slot: response_refresh?.handle, - device_handle: response.device_handle - } - ); - } - } - } + result = match issued_value { + Some(envelope) => with_issued_handle(policy, home, intent, metadata, envelope), + None => failed("refresh_unavailable") + }; } result } @@ -444,25 +660,28 @@ fn issue_and_send( pub fn refresh(binding: OpaqueHostHomeBinding, intent: OpaqueRefreshIntent) -> ProviderRefreshResult { let mut result: ProviderRefreshResult = failed("refresh_unavailable"); let snapshot: ConfigSnapshotEnvelope = config::load_snapshot(&binding.home); - if !snapshot.ok || snapshot.error != null { + let snapshot_error: Option = snapshot.error; + if !snapshot.ok || text_present(snapshot_error) { result = failed("refresh_unavailable"); } else { let policy: resource = snapshot.policy_handle; let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result(intent.credential_id.copy(), &policy); let loaded_has_value: bool = loaded.has("value"); - let loaded_maybe: AuthMetadataEnvelope? = if loaded_has_value => { loaded.value.copy() } else => { null }; + let loaded_failure: Option = loaded.error.copy(); + let loaded_maybe: Option = if loaded_has_value => { loaded.value.copy() } else => { no_metadata_envelope() }; let loaded_envelope: AuthMetadataEnvelope = loaded_maybe.unwrap_or(empty_metadata_envelope()); - if loaded.error.copy() != null { - result = failed_native(loaded.error.copy(), "credential_not_admitted"); + let envelope_error: Option = loaded_envelope.error.copy(); + if failure_present(loaded_failure) { + result = failed_native(loaded_failure, "credential_not_admitted"); } else if !loaded_has_value { result = failed("credential_not_admitted"); } else if !loaded.ok.copy() { result = failed("credential_not_admitted"); } else if !loaded_envelope.ok.copy() { result = failed("credential_not_admitted"); - } else if loaded_envelope.error.copy() != null { - result = failed(loaded_envelope.error.copy()); + } else if text_present(envelope_error) { + result = failed(envelope_error.unwrap_or("credential_not_admitted")); } else { let metadata: AuthMetadata = loaded_envelope.metadata.copy(); if !admitted(metadata.copy(), intent.credential_id.copy()) { diff --git a/rss/storage/admission.rss b/rss/storage/admission.rss index 1ce664c..de038a2 100644 --- a/rss/storage/admission.rss +++ b/rss/storage/admission.rss @@ -7,27 +7,33 @@ use json; use self::effects as effects; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } +fn no_params() -> Option> { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } @@ -52,9 +58,12 @@ struct AdmissionReplayInput { } struct RestorePinRequest {checkpoint_id:string} -struct RestorePinGraph {checkpoint_graph_ref_id:string,harness_selection:map} +struct RestorePinGraph {checkpoint_graph_ref_id:string,harness_selection:HarnessSelectionValue} struct RestorePinResult {graph:RestorePinGraph} struct RestorePinSelection {request:RestorePinRequest,result:RestorePinResult} +struct HarnessPolicyValue {package_id:string,artifact_id:string,entry_path:string,prompt_export:string,context_export:string,guardian_export:string} +struct HarnessSelectionValue {mode:string,tool_package:Option,policy:Option} +struct RestoreEnvelope {restore_json:string} struct AdmissionCreateInput { session_id: string, @@ -90,8 +99,17 @@ fn admission_owner_pin_sql() -> string { } fn decode_pin_selection(encoded:string)->map { - let mut selection:map={}; - if encoded != "null" {selection=json::decode(encoded);} + let mut selection:map={mode:"unselected"}; + if encoded != "null" { + let decoded:HarnessSelectionValue=json::decode::(encoded); + let mode:string=decoded.mode.copy(); + if mode == "tool_only" { + selection={mode:mode.copy(),tool_package:match decoded.tool_package {Some(value)=>value,None=>""}}; + } else if mode == "context_prompt" { + let policy:HarnessPolicyValue=match decoded.policy {Some(value)=>value,None=>{package_id:"",artifact_id:"",entry_path:"",prompt_export:"",context_export:"",guardian_export:""}}; + selection={mode:mode.copy(),tool_package:match decoded.tool_package {Some(value)=>value,None=>""},policy:{package_id:policy.package_id,artifact_id:policy.artifact_id,entry_path:policy.entry_path,prompt_export:policy.prompt_export,context_export:policy.context_export,guardian_export:policy.guardian_export}}; + } + } selection } @@ -110,15 +128,15 @@ fn admission_owner_pin(queried: SqliteQueryResult) -> map { if rows.length == 1 { let row: SqliteRow = rows[0].copy(); let cells: array = row.cells.copy(); - graph_id = cells[0].text_value; - ref_id = cells[1].text_value; - generation = cells[2].int_value; - run_id = cells[3].text_value; - session_id = cells[4].text_value; - registry_identity = cells[5].text_value; - workspace = cells[6].text_value; - profile = cells[7].text_value; - harness_selection_json = cells[8].text_value; + graph_id = cells.copy()[0].text_value.unwrap_or(""); + ref_id = cells.copy()[1].text_value.unwrap_or(""); + generation = cells.copy()[2].int_value.unwrap_or(0); + run_id = cells.copy()[3].text_value.unwrap_or(""); + session_id = cells.copy()[4].text_value.unwrap_or(""); + registry_identity = cells.copy()[5].text_value.unwrap_or(""); + workspace = cells.copy()[6].text_value.unwrap_or(""); + profile = cells.copy()[7].text_value.unwrap_or(""); + harness_selection_json = cells.copy()[8].text_value.unwrap_or(""); pinned = graph_id.copy() != "" && ref_id.copy() != ""; } { @@ -136,40 +154,56 @@ fn admission_owner_pin(queried: SqliteQueryResult) -> map { } pub fn storage_admission_create(db_id: resource, payload_json: string) -> map { - let raw_payload: map = json::decode(payload_json); - let mut failpoint: string = ""; - let mut registry_identity: string = ""; - let mut workspace: string = ""; - if raw_payload.has("failpoint") { - failpoint = raw_payload["failpoint"].copy(); - } - if raw_payload.has("tool_registry_identity") { - registry_identity = raw_payload["tool_registry_identity"].copy(); - } - if raw_payload.has("tool_workspace") { - workspace = raw_payload["tool_workspace"].copy(); - } + let raw_params: array = [sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json),sql_text(payload_json)]; + let raw_result = sqlite::query(&db_id, "SELECT json_type(?, '$.failpoint') IS NOT NULL, COALESCE(json_extract(?, '$.failpoint'), ''), json_type(?, '$.tool_registry_identity') IS NOT NULL, COALESCE(json_extract(?, '$.tool_registry_identity'), ''), json_type(?, '$.tool_workspace') IS NOT NULL, COALESCE(json_extract(?, '$.tool_workspace'), ''), json_type(?, '$.restore_json') IS NOT NULL, COALESCE(json_extract(?, '$.restore_json'), ''), json_type(?, '$.plugin_state_migration_json') IS NOT NULL, COALESCE(json_extract(?, '$.plugin_state_migration_json'), ''), json_type(?, '$.plugin_state_guard_json') IS NOT NULL, COALESCE(json_extract(?, '$.plugin_state_guard_json'), ''), json_type(?, '$.plugin_state_failpoint') IS NOT NULL, COALESCE(json_extract(?, '$.plugin_state_failpoint'), ''), json_type(?, '$.managed_binding_json') IS NOT NULL, COALESCE(json_extract(?, '$.managed_binding_json'), ''), json_type(?, '$.checkpoint_id') IS NOT NULL, COALESCE(json_extract(?, '$.checkpoint_id'), ''), json_type(?, '$.checkpoint_graph_ref_id') IS NOT NULL, COALESCE(json_extract(?, '$.checkpoint_graph_ref_id'), '')", raw_params, sql_limits(1,1048576)); + let raw_cells: array = raw_result.rows[0].cells.copy(); + let failpoint_present: bool = raw_cells[0].int_value.unwrap_or(0) != 0; + let mut failpoint: string = raw_cells[1].text_value.unwrap_or(""); + let registry_present: bool = raw_cells[2].int_value.unwrap_or(0) != 0; + let mut registry_identity: string = raw_cells[3].text_value.unwrap_or(""); + let workspace_present: bool = raw_cells[4].int_value.unwrap_or(0) != 0; + let workspace: string = raw_cells[5].text_value.unwrap_or(""); + let restore_present: bool = raw_cells[6].int_value.unwrap_or(0) != 0; + let restore_json_raw: string = raw_cells[7].text_value.unwrap_or(""); + let migration_present: bool = raw_cells[8].int_value.unwrap_or(0) != 0; + let migration_json: string = raw_cells[9].text_value.unwrap_or(""); + let guard_present: bool = raw_cells[10].int_value.unwrap_or(0) != 0; + let guard_json: string = raw_cells[11].text_value.unwrap_or(""); + let state_failpoint_present: bool = raw_cells[12].int_value.unwrap_or(0) != 0; + let state_failpoint: string = raw_cells[13].text_value.unwrap_or(""); + let managed_present: bool = raw_cells[14].int_value.unwrap_or(0) != 0; + let managed_json: string = raw_cells[15].text_value.unwrap_or(""); + let checkpoint_present: bool = raw_cells[16].int_value.unwrap_or(0) != 0; + let checkpoint_id_raw: string = raw_cells[17].text_value.unwrap_or(""); + let checkpoint_ref_present: bool = raw_cells[18].int_value.unwrap_or(0) != 0; + let checkpoint_ref_raw: string = raw_cells[19].text_value.unwrap_or(""); let normalized: string = admission_with_pin_fields(payload_json); let input: AdmissionCreateInput = json::decode::(normalized); + if registry_identity == "" { + registry_identity = input.script_hash.copy(); + } let mut failed: bool = false; let mut failure_code: string = ""; let mut failure_message: string = ""; if input.session_new.copy() == 0 { - if !(sqlite::query(&db_id, existence::session_exists_sql(), [sql_text(input.session_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.session_id.copy())]; + if !(sqlite::query(&db_id, existence::session_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { failed = true; failure_code = "session_not_found"; failure_message = "admission session does not exist"; } } if failed == false { - if input.parent_run_id.copy() != "" && !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.parent_run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.parent_run_id.copy())]; + if input.parent_run_id.copy() != "" && !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { failed = true; failure_code = "parent_not_found"; failure_message = "parent run does not exist"; } } if failed == false && input.session_new.copy() == 0 && input.session_metadata_json.copy() != "{}" { - let selector_conflict = sqlite::query(&db_id, "SELECT 1 FROM sessions existing WHERE existing.id = ? AND json_extract(existing.metadata_json, '$.workspace.selector') IS NOT NULL AND (json_extract(existing.metadata_json, '$.workspace.selector') <> json_extract(?, '$.workspace.selector') OR COALESCE(json_extract(existing.metadata_json, '$.workspace.canonical_path'), '') <> COALESCE(json_extract(?, '$.workspace.canonical_path'), '')) LIMIT 1", [sql_text(input.session_id.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy())], sql_limits(1, 4096)); + let params: array = [sql_text(input.session_id.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy())]; + let selector_conflict = sqlite::query(&db_id, "SELECT 1 FROM sessions existing WHERE existing.id = ? AND json_extract(existing.metadata_json, '$.workspace.selector') IS NOT NULL AND (json_extract(existing.metadata_json, '$.workspace.selector') <> json_extract(?, '$.workspace.selector') OR COALESCE(json_extract(existing.metadata_json, '$.workspace.canonical_path'), '') <> COALESCE(json_extract(?, '$.workspace.canonical_path'), '')) LIMIT 1", params, sql_limits(1, 4096)); if selector_conflict.rows.length > 0 { failed = true; failure_code = "workspace_conflict"; @@ -192,14 +226,15 @@ pub fn storage_admission_create(db_id: resource, payload_json if failed == false { if input.idempotency_key.copy() != "" { // Replay identity uses the original optional selector, never the current session default. - let existing = sqlite::query(&db_id, "SELECT request_hash, resource_id, CASE WHEN json_type(response_json, '$.workspace_identity') IS NULL THEN CASE WHEN ? = '' OR json_extract(?, '$.request_selector_present') = 0 THEN 1 ELSE 0 END WHEN json_type(response_json, '$.workspace_identity') = 'object' AND json_extract(response_json, '$.workspace_identity.request_selector_present') IS CASE WHEN ? = '' THEN 0 ELSE json_extract(?, '$.request_selector_present') END AND json_extract(response_json, '$.workspace_identity.request_selector') IS CASE WHEN ? = '' THEN '' ELSE json_extract(?, '$.request_selector') END THEN 1 ELSE 0 END AS workspace_identity_match FROM idempotency_records WHERE scope = ? AND key = ? LIMIT 1", [sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy())], sql_limits(1, 8192)); + let params: array = [sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy())]; + let existing = sqlite::query(&db_id, "SELECT request_hash, resource_id, CASE WHEN json_type(response_json, '$.workspace_identity') IS NULL THEN CASE WHEN ? = '' OR json_extract(?, '$.request_selector_present') = 0 THEN 1 ELSE 0 END WHEN json_type(response_json, '$.workspace_identity') = 'object' AND json_extract(response_json, '$.workspace_identity.request_selector_present') IS CASE WHEN ? = '' THEN 0 ELSE json_extract(?, '$.request_selector_present') END AND json_extract(response_json, '$.workspace_identity.request_selector') IS CASE WHEN ? = '' THEN '' ELSE json_extract(?, '$.request_selector') END THEN 1 ELSE 0 END AS workspace_identity_match FROM idempotency_records WHERE scope = ? AND key = ? LIMIT 1", params, sql_limits(1, 8192)); let rows = existing.rows; if rows.length > 0 { let row: SqliteRow = rows[0].copy(); let cells: array = row.cells.copy(); - let existing_hash: string = cells[0].text_value; - let existing_resource: string = cells[1].text_value; - let workspace_identity_matches: bool = cells[2].int_value == 1; + let existing_hash: string = cells.copy()[0].text_value.unwrap_or(""); + let existing_resource: string = cells.copy()[1].text_value.unwrap_or(""); + let workspace_identity_matches: bool = cells.copy()[2].int_value.unwrap_or(0) == 1; existing_run_id = existing_resource; if existing_hash == input.request_hash.copy() && workspace_identity_matches { replay = true; @@ -212,103 +247,91 @@ pub fn storage_admission_create(db_id: resource, payload_json if failed == false { if replay == false && conflict == false { let mut statements: array = []; - let mut restore_json:string = ""; - if raw_payload.has("restore_json") { - restore_json = raw_payload["restore_json"].copy(); - } + let mut restore_json:string = restore_json_raw.copy(); if restore_json.copy() != "" { - let restore_statements:array = effects::restore_before_admission(json::encode({restore_json:restore_json.copy()}), input.tool_graph_id.copy(), input.tool_ref_id.copy(), input.run_id.copy()); + let restore_statements:array = effects::restore_before_admission(json::encode::({restore_json:restore_json.copy()}), input.tool_graph_id.copy(), input.tool_ref_id.copy(), input.run_id.copy()); let mut restore_index:int = 0; while restore_index < restore_statements.length { statements[statements.length] = restore_statements[restore_index].copy(); restore_index = restore_index + 1; // Internal fixture cuts exercise actual rollback in this same transaction. if (failpoint.copy() == "managed_statement_4" && restore_index == 4) || (failpoint.copy() == "managed_statement_6" && restore_index == 6) { - statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) VALUES(2)",params:[],query:false,limits:sql_limits(1,4096)}; + statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) VALUES(2)",params: no_params(),query: Some(false),limits: Some(sql_limits(1,4096))}; } } } if input.session_new.copy() == 1 { statements[statements.length] = { sql: "INSERT INTO sessions (id, profile, platform, account_id, chat_id, thread_id, user_id, generation, system_prompt, model, provider, toolset_hash, metadata_json, created_at_ms, updated_at_ms) SELECT ?, ?, ?, ?, '', '', '', 1, ?, ?, ?, '', ?, ?, ? WHERE (NOT EXISTS (SELECT 1 FROM sessions existing WHERE existing.id = ?) AND (? = '' OR EXISTS (SELECT 1 FROM runs parent WHERE parent.id = ?)) AND NOT EXISTS (SELECT 1 FROM runs existing WHERE existing.id = ? AND existing.session_id <> ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(input.session_id.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.system_prompt.copy()), sql_text(input.model.copy()), sql_text(input.provider.copy()), sql_text(input.session_metadata_json.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.system_prompt.copy()), sql_text(input.model.copy()), sql_text(input.provider.copy()), sql_text(input.session_metadata_json.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; } else { statements[statements.length] = { sql: "UPDATE sessions SET model = CASE WHEN ? = '' THEN model ELSE ? END, provider = CASE WHEN ? = '' THEN provider ELSE ? END, system_prompt = CASE WHEN ? = '' THEN system_prompt ELSE ? END, metadata_json = CASE WHEN ? = '{}' THEN metadata_json ELSE json_set(CASE WHEN json_valid(metadata_json) THEN CASE WHEN json_type(metadata_json) = 'object' THEN metadata_json ELSE '{}' END ELSE '{}' END, '$.workspace', json_extract(?, '$.workspace')) END, updated_at_ms = ? WHERE (id = ? AND (? = '{}' OR json_extract(metadata_json, '$.workspace.selector') IS NULL OR json_extract(metadata_json, '$.workspace.selector') = json_extract(?, '$.workspace.selector')) AND (? = '' OR EXISTS (SELECT 1 FROM runs parent WHERE parent.id = ?)) AND NOT EXISTS (SELECT 1 FROM runs existing WHERE existing.id = ? AND existing.session_id <> ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(input.model.copy()), sql_text(input.model.copy()), sql_text(input.provider.copy()), sql_text(input.provider.copy()), sql_text(input.system_prompt.copy()), sql_text(input.system_prompt.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.model.copy()), sql_text(input.model.copy()), sql_text(input.provider.copy()), sql_text(input.provider.copy()), sql_text(input.system_prompt.copy()), sql_text(input.system_prompt.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; } if restore_json.copy() != "" { - let history_statements:array = effects::restore_history_statements(json::encode({restore_json:restore_json.copy()}), input.tool_graph_id.copy(), input.tool_ref_id.copy(), input.run_id.copy()); + let history_statements:array = effects::restore_history_statements(json::encode::({restore_json:restore_json.copy()}), input.tool_graph_id.copy(), input.tool_ref_id.copy(), input.run_id.copy()); let mut history_index:int = 0; while history_index < history_statements.length { statements[statements.length] = history_statements[history_index].copy(); history_index = history_index + 1; } if failpoint.copy() == "managed_statement_10" { - statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())],query:false,limits:sql_limits(1,4096)}; + statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]),query: Some(false),limits: Some(sql_limits(1,4096))}; } } if input.tool_graph_id.copy() != "" { statements[statements.length] = { sql: "INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM plugin_graph_locks WHERE graph_id = ? AND ((json_extract(lock_json, '$.harness_selection.mode') = 'tool_only' AND json_type(?, '$.run_graph_selection') IS NULL) OR (graph_id = json_extract(?, '$.run_graph_selection.graph_id') AND json_extract(?, '$.run_graph_selection.ref_id') = ? AND json_type(?, '$.run_graph_selection.harness_selection') = 'object' AND json(json_extract(lock_json, '$.harness_selection')) = json(json_extract(?, '$.run_graph_selection.harness_selection')))))", - params: [sql_text(input.tool_graph_id.copy()), sql_text(input.input_json.copy()), sql_text(input.input_json.copy()), sql_text(input.input_json.copy()), sql_text(input.tool_ref_id.copy()), sql_text(input.input_json.copy()), sql_text(input.input_json.copy())], - query: false, - limits: sql_limits(1,4096) + params: Some([sql_text(input.tool_graph_id.copy()), sql_text(input.input_json.copy()), sql_text(input.input_json.copy()), sql_text(input.input_json.copy()), sql_text(input.tool_ref_id.copy()), sql_text(input.input_json.copy()), sql_text(input.input_json.copy())]), + query: Some(false), + limits: Some(sql_limits(1,4096)) }; } let user_content = storage_admission_user_content(input.input_json.copy()); let encoded_content = messages::storage_message_encode_content(user_content); statements[statements.length] = { sql: "INSERT INTO messages (id, session_id, ordinal, role, content_json, metadata_json, run_id, finish_reason, created_at_ms) SELECT ?, ?, COALESCE(MAX(ordinal), 0) + 1, 'user', ?, '{}', ?, '', ? FROM messages WHERE session_id = ? HAVING (NOT EXISTS (SELECT 1 FROM messages existing WHERE existing.id = ? AND existing.session_id <> ?) AND EXISTS (SELECT 1 FROM sessions workspace_session WHERE workspace_session.id = ? AND (? = '{}' OR json_extract(workspace_session.metadata_json, '$.workspace.selector') = json_extract(?, '$.workspace.selector'))) AND (? = '' OR EXISTS (SELECT 1 FROM runs parent WHERE parent.id = ?)) AND NOT EXISTS (SELECT 1 FROM runs existing WHERE existing.id = ? AND existing.session_id <> ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(input.message_id.copy()), sql_text(input.session_id.copy()), sql_text(encoded_content), sql_text(input.message_run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.message_id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.message_id.copy()), sql_text(input.session_id.copy()), sql_text(encoded_content), sql_text(input.message_run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.message_id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "INSERT INTO runs (id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, created_at_ms, started_at_ms, updated_at_ms) SELECT ?, ?, ?, 'running', ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE (NOT EXISTS (SELECT 1 FROM runs existing WHERE existing.id = ? AND existing.session_id <> ?) AND EXISTS (SELECT 1 FROM sessions WHERE id = ?) AND (? = '' OR EXISTS (SELECT 1 FROM runs parent WHERE parent.id = ?)) AND EXISTS (SELECT 1 FROM sessions workspace_session WHERE workspace_session.id = ? AND (? = '{}' OR json_extract(workspace_session.metadata_json, '$.workspace.selector') = json_extract(?, '$.workspace.selector')))) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.input_json.copy()), sql_text(input.provider.copy()), sql_text(input.model.copy()), sql_text(input.script_hash.copy()), sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.input_json.copy()), sql_text(input.provider.copy()), sql_text(input.model.copy()), sql_text(input.script_hash.copy()), sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.session_metadata_json.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - if raw_payload.has("plugin_state_migration_json") { - let state_json: string = raw_payload["plugin_state_migration_json"].copy(); - let state_statements: array = plugins::storage_admission_state_statements(state_json); - let mut state_index: int = 0; - while state_index < state_statements.length { - statements[statements.length] = state_statements[state_index].copy(); - state_index = state_index + 1; - } + let state_statements: array = if migration_present => { plugins::storage_admission_state_statements(migration_json.copy()) } else => { [] }; + let mut state_index: int = 0; + while state_index < state_statements.length { + statements[statements.length] = state_statements[state_index].copy(); + state_index = state_index + 1; } - if raw_payload.has("plugin_state_guard_json") { - let guard_json: string = raw_payload["plugin_state_guard_json"].copy(); - let guard_statements: array = plugins::storage_admission_state_guard_statements(guard_json); - let mut guard_index: int = 0; - while guard_index < guard_statements.length { - statements[statements.length] = guard_statements[guard_index].copy(); - guard_index = guard_index + 1; - } + let guard_statements: array = if guard_present => { plugins::storage_admission_state_guard_statements(guard_json.copy()) } else => { [] }; + let mut guard_index: int = 0; + while guard_index < guard_statements.length { + statements[statements.length] = guard_statements[guard_index].copy(); + guard_index = guard_index + 1; } - if raw_payload.has("plugin_state_failpoint") { - let state_failpoint: string = raw_payload["plugin_state_failpoint"].copy(); - if state_failpoint.copy() == "after_root_cas" { - statements[statements.length] = {sql:"INSERT INTO plugin_control_guard(id) VALUES(2)",params:[],query:false,limits:sql_limits(1,4096)}; - } + if state_failpoint_present && state_failpoint == "after_root_cas" { + statements[statements.length] = {sql:"INSERT INTO plugin_control_guard(id) VALUES(2)",params: no_params(),query: Some(false),limits: Some(sql_limits(1,4096))}; } if failpoint.copy() == "managed_statement_12" && restore_json.copy() != "" { - statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())],query:false,limits:sql_limits(1,4096)}; + statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]),query: Some(false),limits: Some(sql_limits(1,4096))}; } if input.parent_run_id.copy() != "" { statements[statements.length] = { sql: "INSERT INTO child_run_links (parent_run_id, child_run_id, ordinal, relation, state, created_at_ms) SELECT ?, ?, COALESCE(MAX(ordinal), -1) + 1, 'subagent', 'pending', ? FROM child_run_links WHERE parent_run_id = ? HAVING (EXISTS (SELECT 1 FROM runs parent WHERE parent.id = ?) AND EXISTS (SELECT 1 FROM runs child WHERE child.id = ? AND child.session_id = ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; } // Unkeyed admissions still need an exact original receipt for restart. @@ -317,56 +340,46 @@ pub fn storage_admission_create(db_id: resource, payload_json if input.idempotency_key.copy() != "" || input.tool_graph_id.copy() != "" { statements[statements.length] = { sql: "INSERT OR IGNORE INTO idempotency_records (scope, key, request_hash, resource_type, resource_id, state, response_json, created_at_ms, expires_at_ms) SELECT ?, ?, ?, 'run', ?, 'completed', json_set(CASE WHEN ? = '' THEN '{\"run_id\":\"' || ? || '\",\"status\":\"running\"}' ELSE json_object('run_id', ?, 'status', 'running', 'workspace_identity', json(?)) END, '$.tool_graph_id', ?, '$.tool_ref_id', ?, '$.tool_generation', ?, '$.registry_identity', ?, '$.workspace', ?, '$.profile', ?, '$.harness_selection', COALESCE(json_extract(?, '$.run_graph_selection.harness_selection'), (SELECT json_extract(lock_json, '$.harness_selection') FROM plugin_graph_locks WHERE graph_id = ?))), ?, ? WHERE (? <> '' AND EXISTS (SELECT 1 FROM runs admitted WHERE admitted.id = ? AND admitted.session_id = ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(receipt_scope.copy()), sql_text(receipt_key.copy()), sql_text(input.request_hash.copy()), sql_text(input.run_id.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy()), sql_int(input.tool_graph_generation.copy()), sql_text(registry_identity.copy()), sql_text(workspace.copy()), sql_text(input.profile.copy()), sql_text(input.input_json.copy()), sql_text(input.tool_graph_id.copy()), sql_int(input.now_ms.copy()), sql_int(input.expires_at_ms.copy()), sql_text(receipt_key.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(receipt_scope.copy()), sql_text(receipt_key.copy()), sql_text(input.request_hash.copy()), sql_text(input.run_id.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.workspace_identity_json.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy()), sql_int(input.tool_graph_generation.copy()), sql_text(registry_identity.copy()), sql_text(workspace.copy()), sql_text(input.profile.copy()), sql_text(input.input_json.copy()), sql_text(input.tool_graph_id.copy()), sql_int(input.now_ms.copy()), sql_int(input.expires_at_ms.copy()), sql_text(receipt_key.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; } statements[statements.length] = { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1, ?, 'run.started', '{\"status\":\"running\",\"session_id\":\"' || ? || '\"}', ? WHERE (EXISTS (SELECT 1 FROM runs WHERE id = ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id.copy()), sql_text(input.session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id.copy()), sql_text(input.session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT ?, COALESCE((SELECT MIN(seq) FROM run_events WHERE run_id = ?), 0), COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id = ?), 0), ? WHERE (EXISTS (SELECT 1 FROM runs WHERE id = ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.tool_graph_id.copy()), sql_text(input.tool_ref_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; if input.tool_graph_id.copy() != "" { - if raw_payload.has("managed_binding_json") { - let managed_json: string = raw_payload["managed_binding_json"].copy(); - let managed_statements: array = effects::admission_statements(managed_json, input.tool_graph_id.copy(), input.tool_ref_id.copy()); - let mut managed_index: int = 0; - while managed_index < managed_statements.length { - statements[statements.length] = managed_statements[managed_index].copy(); - managed_index = managed_index + 1; - } + let managed_statements: array = if managed_present => { effects::admission_statements(managed_json.copy(), input.tool_graph_id.copy(), input.tool_ref_id.copy()) } else => { [] }; + let mut managed_index: int = 0; + while managed_index < managed_statements.length { + statements[statements.length] = managed_statements[managed_index].copy(); + managed_index = managed_index + 1; } if restore_json.copy() != "" { - let receipt_statements:array = effects::restore_receipt_statements(json::encode({restore_json:restore_json.copy()}), input.tool_graph_id.copy(), input.tool_ref_id.copy(), input.run_id.copy()); + let receipt_statements:array = effects::restore_receipt_statements(json::encode::({restore_json:restore_json.copy()}), input.tool_graph_id.copy(), input.tool_ref_id.copy(), input.run_id.copy()); let mut receipt_index:int = 0; while receipt_index < receipt_statements.length { statements[statements.length] = receipt_statements[receipt_index].copy(); receipt_index = receipt_index + 1; } if failpoint.copy() == "managed_statement_17" { - statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) VALUES(2)",params:[],query:false,limits:sql_limits(1,4096)}; + statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) VALUES(2)",params: no_params(),query: Some(false),limits: Some(sql_limits(1,4096))}; } } - let mut checkpoint_id: string = ""; - let mut checkpoint_ref: string = ""; - if raw_payload.has("checkpoint_id") { - checkpoint_id = raw_payload["checkpoint_id"].copy(); - } - if raw_payload.has("checkpoint_graph_ref_id") { - checkpoint_ref = raw_payload["checkpoint_graph_ref_id"].copy(); - } + let checkpoint_id: string = if checkpoint_present => { checkpoint_id_raw.copy() } else => { "" }; + let checkpoint_ref: string = if checkpoint_ref_present => { checkpoint_ref_raw.copy() } else => { "" }; let pin_statements:array = if restore_json.copy() != "" => { let selection:RestorePinSelection = json::decode::(restore_json.copy()); - let restore_harness_json:string=json::encode(selection.result.copy().graph.harness_selection); - statements[statements.length]={sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_graph_locks WHERE graph_id=? AND json(json_extract(lock_json,'$.harness_selection'))=json(?))",params:[sql_text(input.tool_graph_id.copy()),sql_text(restore_harness_json)],query:false,limits:sql_limits(1,4096)}; + statements[statements.length]={sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_graph_locks WHERE graph_id=? AND json(json_extract(lock_json,'$.harness_selection'))=json(json_extract(?, '$.result.graph.harness_selection')))",params: Some([sql_text(input.tool_graph_id.copy()),sql_text(restore_json.copy())]),query: Some(false),limits: Some(sql_limits(1,4096))}; plugins::storage_checkpoint_pin_statements(input.tool_graph_id.copy(),input.tool_graph_generation.copy(),input.tool_ref_id.copy(),input.run_id.copy(),input.session_id.copy(),input.tool_pin_now_ms.copy(),selection.request.checkpoint_id,selection.result.graph.checkpoint_graph_ref_id) } else => { if checkpoint_id.copy() != "" => { @@ -383,7 +396,7 @@ pub fn storage_admission_create(db_id: resource, payload_json pinned = true; } if failpoint.copy() == "managed_statement_99" && restore_json.copy() != "" { - statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) VALUES(2)",params:[],query:false,limits:sql_limits(1,4096)}; + statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) VALUES(2)",params: no_params(),query: Some(false),limits: Some(sql_limits(1,4096))}; } if pinned { statements[statements.length] = plugins::storage_admission_pin_classification(input.tool_graph_id.copy(), input.tool_ref_id.copy(), input.run_id.copy()); @@ -409,9 +422,11 @@ pub fn storage_admission_create(db_id: resource, payload_json } } let query_run_id: string = if replay => { existing_run_id.copy() } else => { input.run_id.copy() }; - let run_result = sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT 1", [sql_text(query_run_id.copy())], sql_limits(1, 65536)); + let params: array = [sql_text(query_run_id.copy())]; + let run_result = sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT 1", params, sql_limits(1, 65536)); let run_result_copy: map = sqlite_query_map(run_result); let run_rows = run_result.rows; + let params: array = [sql_text(input.session_id.copy())]; if failed == false { if run_rows.length == 0 && replay == false { failed = true; @@ -424,17 +439,20 @@ pub fn storage_admission_create(db_id: resource, payload_json failure_message = "replayed idempotency key references a missing run"; } } - let session_result = sqlite::query(&db_id, "SELECT id, profile, platform, account_id, chat_id, thread_id, user_id, generation, status, system_prompt, model, provider, toolset_hash, metadata_json, last_message_seq, created_at_ms, updated_at_ms FROM sessions WHERE id = ? LIMIT 1", [sql_text(input.session_id.copy())], sql_limits(1, 65536)); - let message_result = sqlite::query(&db_id, "SELECT id, session_id, ordinal, role, content_json, name, tool_call_id, parent_message_id, token_estimate, compacted, metadata_json, run_id, finish_reason, created_at_ms FROM messages WHERE id = ? AND session_id = ? LIMIT 1", [sql_text(input.message_id.copy()), sql_text(input.session_id.copy())], sql_limits(1, 65536)); + let session_result = sqlite::query(&db_id, "SELECT id, profile, platform, account_id, chat_id, thread_id, user_id, generation, status, system_prompt, model, provider, toolset_hash, metadata_json, last_message_seq, created_at_ms, updated_at_ms FROM sessions WHERE id = ? LIMIT 1", params, sql_limits(1, 65536)); + let params: array = [sql_text(input.message_id.copy()), sql_text(input.session_id.copy())]; + let message_result = sqlite::query(&db_id, "SELECT id, session_id, ordinal, role, content_json, name, tool_call_id, parent_message_id, token_estimate, compacted, metadata_json, run_id, finish_reason, created_at_ms FROM messages WHERE id = ? AND session_id = ? LIMIT 1", params, sql_limits(1, 65536)); let idempotency_result: map = if input.idempotency_key.copy() == "" => { { columns: [], rows: [] } } else => { - sqlite::query(&db_id, "SELECT scope, key, request_hash, resource_type, resource_id, state, response_json, created_at_ms, expires_at_ms, completed_at_ms FROM idempotency_records WHERE scope = ? AND key = ? LIMIT 1", [sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy())], sql_limits(1, 8192)) + let params: array = [sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy())]; + sqlite::query(&db_id, "SELECT scope, key, request_hash, resource_type, resource_id, state, response_json, created_at_ms, expires_at_ms, completed_at_ms FROM idempotency_records WHERE scope = ? AND key = ? LIMIT 1", params, sql_limits(1, 8192)) }; if replay == true || input.tool_graph_id.copy() != "" { let receipt_scope:string=if input.idempotency_key.copy()=="" => {"native.tool-pin"} else => {input.idempotency_scope.copy()}; let receipt_key:string=if input.idempotency_key.copy()=="" => {query_run_id.copy()} else => {input.idempotency_key.copy()}; - let recovered: map = admission_owner_pin(sqlite::query(&db_id, admission_owner_pin_sql(), [sql_text(receipt_scope.copy()), sql_text(receipt_key.copy()), sql_text(query_run_id.copy())], sql_limits(1, 8192))); + let params: array = [sql_text(receipt_scope.copy()), sql_text(receipt_key.copy()), sql_text(query_run_id.copy())]; + let recovered: map = admission_owner_pin(sqlite::query(&db_id, admission_owner_pin_sql(), params, sql_limits(1, 8192))); let recovered_graph_id: string = recovered["graph_id"]; let recovered_ref_id: string = recovered["ref_id"]; let recovered_generation: int = recovered["generation"]; @@ -536,7 +554,8 @@ pub fn storage_admission_replay(db_id: resource, payload_json if input.request_selector_present.copy() == true { selector_present = 1; } - let existing = sqlite::query(&db_id, "SELECT request_hash, resource_id, CASE WHEN json_type(response_json, '$.workspace_identity') IS NULL THEN CASE WHEN ? = 0 THEN 1 ELSE 0 END WHEN json_type(response_json, '$.workspace_identity') = 'object' AND json_extract(response_json, '$.workspace_identity.request_selector_present') IS ? AND json_extract(response_json, '$.workspace_identity.request_selector') IS ? THEN 1 ELSE 0 END AS workspace_identity_match FROM idempotency_records WHERE scope = ? AND key = ? LIMIT 1", [sql_int(selector_present.copy()), sql_int(selector_present.copy()), sql_text(input.request_selector.copy()), sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy())], sql_limits(1, 8192)); + let params: array = [sql_int(selector_present.copy()), sql_int(selector_present.copy()), sql_text(input.request_selector.copy()), sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy())]; + let existing = sqlite::query(&db_id, "SELECT request_hash, resource_id, CASE WHEN json_type(response_json, '$.workspace_identity') IS NULL THEN CASE WHEN ? = 0 THEN 1 ELSE 0 END WHEN json_type(response_json, '$.workspace_identity') = 'object' AND json_extract(response_json, '$.workspace_identity.request_selector_present') IS ? AND json_extract(response_json, '$.workspace_identity.request_selector') IS ? THEN 1 ELSE 0 END AS workspace_identity_match FROM idempotency_records WHERE scope = ? AND key = ? LIMIT 1", params, sql_limits(1, 8192)); let rows = existing.rows; let empty_run: map = { columns: [], rows: [], truncated: false }; let mut result = { @@ -548,9 +567,9 @@ pub fn storage_admission_replay(db_id: resource, payload_json if rows.length > 0 { let row: SqliteRow = rows[0].copy(); let cells: array = row.cells.copy(); - let existing_hash: string = cells[0].text_value; - let existing_run_id: string = cells[1].text_value; - let workspace_identity_matches: bool = cells[2].int_value == 1; + let existing_hash: string = cells.copy()[0].text_value.unwrap_or(""); + let existing_run_id: string = cells.copy()[1].text_value.unwrap_or(""); + let workspace_identity_matches: bool = cells.copy()[2].int_value.unwrap_or(0) == 1; if existing_hash != input.request_hash.copy() || workspace_identity_matches == false { result = { ok: false, @@ -559,7 +578,8 @@ pub fn storage_admission_replay(db_id: resource, payload_json result: { replayed: false, conflict: true, run: empty_run.copy() } }; } else { - let run_result = sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT 1", [sql_text(existing_run_id.copy())], sql_limits(1, 65536)); + let params: array = [sql_text(existing_run_id.copy())]; + let run_result = sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT 1", params, sql_limits(1, 65536)); let run_result_copy: map = sqlite_query_map(run_result); let run_rows = run_result.rows; if run_rows.length == 0 { @@ -570,7 +590,8 @@ pub fn storage_admission_replay(db_id: resource, payload_json result: { replayed: false, conflict: false, run: run_result_copy } }; } else { - let recovered: map = admission_owner_pin(sqlite::query(&db_id, admission_owner_pin_sql(), [sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy()), sql_text(existing_run_id.copy())], sql_limits(1, 8192))); + let params: array = [sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy()), sql_text(existing_run_id.copy())]; + let recovered: map = admission_owner_pin(sqlite::query(&db_id, admission_owner_pin_sql(), params, sql_limits(1, 8192))); let recovered_graph_id: string = recovered["graph_id"]; let recovered_ref_id: string = recovered["ref_id"]; let recovered_generation: int = recovered["generation"]; @@ -611,37 +632,43 @@ pub fn storage_admission_replay(db_id: resource, payload_json // Prefer the compact envelope's `run_context.input` as the user message body // so conversation rows stay canonical; fall back to the raw payload. fn storage_admission_user_content(input_json: string) -> string { - let db_id = sqlite::open({ - path: ":memory:", - mode: "memory", - root: null, - limits: sql_limits(1, 1048576) - }); - let extracted = sqlite::query(&db_id, "SELECT CASE WHEN json_valid(?) AND json_extract(?, '$.run_context.input') IS NOT NULL THEN CAST(json_extract(?, '$.run_context.input') AS TEXT) ELSE ? END AS content", [sql_text(input_json), sql_text(input_json), sql_text(input_json), sql_text(input_json)], sql_limits(1, 1048576)); + let no_root: Option = None; + let options: SqliteOpenOptions = { + path: Some(":memory:"), + mode: Some("memory"), + root: no_root, + limits: Some(sql_limits(1, 1048576)) + }; + let db_id = sqlite::open(options); + let params: array = [sql_text(input_json), sql_text(input_json), sql_text(input_json), sql_text(input_json)]; + let extracted = sqlite::query(&db_id, "SELECT CASE WHEN json_valid(?) AND json_extract(?, '$.run_context.input') IS NOT NULL THEN CAST(json_extract(?, '$.run_context.input') AS TEXT) ELSE ? END AS content", params, sql_limits(1, 1048576)); let rows = extracted.rows; let mut content: string = input_json.copy(); if rows.length > 0 { let row = rows[0]; - content = row.cells[0].text_value; + content = row.cells[0].text_value.unwrap_or(""); } sqlite::close(db_id); content } fn admission_with_pin_fields(payload_json: string) -> string { - let db_id = sqlite::open({ - path: ":memory:", - mode: "memory", - root: null, - limits: sql_limits(1, 1048576) - }); - let extracted = sqlite::query(&db_id, "SELECT json_set(json_set(json_set(json_set(?, '$.tool_graph_id', COALESCE(json_extract(?, '$.tool_graph_id'), '')), '$.tool_graph_generation', COALESCE(json_extract(?, '$.tool_graph_generation'), 0)), '$.tool_ref_id', COALESCE(json_extract(?, '$.tool_ref_id'), '')), '$.tool_pin_now_ms', COALESCE(json_extract(?, '$.tool_pin_now_ms'), 0))", [sql_text(payload_json.copy()), sql_text(payload_json.copy()), sql_text(payload_json.copy()), sql_text(payload_json.copy()), sql_text(payload_json.copy())], sql_limits(1, 1048576)); + let no_root: Option = None; + let options: SqliteOpenOptions = { + path: Some(":memory:"), + mode: Some("memory"), + root: no_root, + limits: Some(sql_limits(1, 1048576)) + }; + let db_id = sqlite::open(options); + let params: array = [sql_text(payload_json.copy()), sql_text(payload_json.copy()), sql_text(payload_json.copy()), sql_text(payload_json.copy()), sql_text(payload_json.copy())]; + let extracted = sqlite::query(&db_id, "SELECT json_set(json_set(json_set(json_set(?, '$.tool_graph_id', COALESCE(json_extract(?, '$.tool_graph_id'), '')), '$.tool_graph_generation', COALESCE(json_extract(?, '$.tool_graph_generation'), 0)), '$.tool_ref_id', COALESCE(json_extract(?, '$.tool_ref_id'), '')), '$.tool_pin_now_ms', COALESCE(json_extract(?, '$.tool_pin_now_ms'), 0))", params, sql_limits(1, 1048576)); let rows = extracted.rows; let mut normalized: string = payload_json.copy(); if rows.length > 0 { let row: SqliteRow = rows[0].copy(); let cells: array = row.cells.copy(); - normalized = cells[0].text_value; + normalized = cells.copy()[0].text_value.unwrap_or(""); } sqlite::close(db_id); normalized diff --git a/rss/storage/approvals.rss b/rss/storage/approvals.rss index 762764a..57303ae 100644 --- a/rss/storage/approvals.rss +++ b/rss/storage/approvals.rss @@ -1,38 +1,42 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } use self::schema as schema; use self::existence as existence; -fn approvals_query_limits(max_rows: int, max_bytes: int) { +fn approvals_query_limits(max_rows: int, max_bytes: int) -> SqliteLimits { sql_limits(max_rows, max_bytes) } - struct ApprovalRequestInput { id: string, run_id: string, @@ -72,53 +76,54 @@ pub fn storage_approval_request(db_id: resource, payload_json let mut result = { ok: true, code: "ok", message: "", result: { columns: [], rows: [], created: 0 } }; if input.decision_scope.copy() == "human_pending" { result = { ok: false, code: "approval_scope_rejected", message: "request cannot create human_pending", result: { columns: [], rows: [], created: 0 } }; - } else if !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { - result = { ok: false, code: "run_not_found", message: "approval request references an unknown run or session", result: { columns: [], rows: [], created: 0 } }; - } else if !(sqlite::query(&db_id, existence::session_exists_sql(), [sql_text(input.session_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + } else { + let params: array = [sql_text(input.run_id.copy())]; + if !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "run_not_found", message: "approval request references an unknown run or session", result: { columns: [], rows: [], created: 0 } }; } else { - let inserted = sqlite::execute(&db_id, "INSERT OR IGNORE INTO approvals (id, run_id, session_id, tool_call_id, tool_name, arguments_json, risk_class, state, decision_scope, one_time, requested_at_ms, expires_at_ms) SELECT ?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE runs.id = ? AND runs.session_id = ? AND runs.status IN ('running', 'waiting_approval')) AND NOT EXISTS (SELECT 1 FROM approvals existing JOIN runs owner ON owner.id = existing.run_id WHERE existing.id = ? AND owner.session_id <> ?)", [sql_text(input.id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_call_id.copy()), sql_text(input.tool_name.copy()), sql_text(input.arguments_json.copy()), sql_text(input.risk_class.copy()), sql_text(input.decision_scope.copy()), sql_int(input.one_time.copy()), sql_int(input.requested_at_ms.copy()), sql_int(input.expires_at_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy())]); - let queried = sqlite::query(&db_id, "SELECT id, run_id, session_id, tool_call_id, tool_name, arguments_json, risk_class, state, decision_scope, one_time, requested_at_ms, expires_at_ms, resolved_at_ms, resolver, decision_reason FROM approvals WHERE id = ? AND session_id = ? LIMIT ?", [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)], approvals_query_limits(max_rows, max_bytes)); - let mut created: int = 0; - if inserted.rows_affected == 1 { - created = 1; + let params: array = [sql_text(input.session_id.copy())]; + if !(sqlite::query(&db_id, existence::session_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { + result = { ok: false, code: "run_not_found", message: "approval request references an unknown run or session", result: { columns: [], rows: [], created: 0 } }; + } else { + let params: array = [sql_text(input.id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_call_id.copy()), sql_text(input.tool_name.copy()), sql_text(input.arguments_json.copy()), sql_text(input.risk_class.copy()), sql_text(input.decision_scope.copy()), sql_int(input.one_time.copy()), sql_int(input.requested_at_ms.copy()), sql_int(input.expires_at_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy())]; + let inserted: SqliteExecuteResult = sqlite::execute(&db_id, "INSERT OR IGNORE INTO approvals (id, run_id, session_id, tool_call_id, tool_name, arguments_json, risk_class, state, decision_scope, one_time, requested_at_ms, expires_at_ms) SELECT ?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE runs.id = ? AND runs.session_id = ? AND runs.status IN ('running', 'waiting_approval')) AND NOT EXISTS (SELECT 1 FROM approvals existing JOIN runs owner ON owner.id = existing.run_id WHERE existing.id = ? AND owner.session_id <> ?)", params); + let params: array = [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)]; + let queried: SqliteQueryResult = sqlite::query(&db_id, "SELECT id, run_id, session_id, tool_call_id, tool_name, arguments_json, risk_class, state, decision_scope, one_time, requested_at_ms, expires_at_ms, resolved_at_ms, resolver, decision_reason FROM approvals WHERE id = ? AND session_id = ? LIMIT ?", params, approvals_query_limits(max_rows, max_bytes)); + let mut created: int = 0; + if inserted.rows_affected == 1 { created = 1; } + result = { ok: true, code: "ok", message: "", result: { columns: queried.columns, rows: queried.rows, truncated: queried.truncated, created: created } }; } - result = { - ok: true, - code: "ok", - message: "", - result: { - columns: queried.columns, - rows: queried.rows, - truncated: queried.truncated, - created: created - } - }; + } } result } pub fn storage_approval_get(db_id: resource, approval_id: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT id, run_id, session_id, tool_call_id, tool_name, arguments_json, risk_class, state, decision_scope, one_time, requested_at_ms, expires_at_ms, resolved_at_ms, resolver, decision_reason FROM approvals WHERE id = ? LIMIT ?", [sql_text(approval_id), sql_int(max_rows)], approvals_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(approval_id), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT id, run_id, session_id, tool_call_id, tool_name, arguments_json, risk_class, state, decision_scope, one_time, requested_at_ms, expires_at_ms, resolved_at_ms, resolver, decision_reason FROM approvals WHERE id = ? LIMIT ?", params, approvals_query_limits(max_rows, max_bytes)) } pub fn storage_approval_list(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { let input: ApprovalListInput = json::decode::(payload_json); - sqlite::query(&db_id, "SELECT id, run_id, session_id, tool_call_id, tool_name, arguments_json, risk_class, state, decision_scope, one_time, requested_at_ms, expires_at_ms, resolved_at_ms, resolver, decision_reason FROM approvals WHERE run_id = ? AND session_id = ? AND state = 'pending' ORDER BY requested_at_ms ASC, id ASC LIMIT ?", [sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)], approvals_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT id, run_id, session_id, tool_call_id, tool_name, arguments_json, risk_class, state, decision_scope, one_time, requested_at_ms, expires_at_ms, resolved_at_ms, resolver, decision_reason FROM approvals WHERE run_id = ? AND session_id = ? AND state = 'pending' ORDER BY requested_at_ms ASC, id ASC LIMIT ?", params, approvals_query_limits(max_rows, max_bytes)) } pub fn storage_approval_resolve(db_id: resource, payload_json: string) -> SqliteExecuteResult { let input: ApprovalResolveInput = json::decode::(payload_json); assert(input.state.copy() == "approved" || input.state.copy() == "denied"); - sqlite::execute(&db_id, "UPDATE approvals SET state = ?, resolver = ?, decision_reason = ?, resolved_at_ms = ? WHERE id = ? AND run_id = ? AND state = 'pending' AND (expires_at_ms <= 0 OR expires_at_ms > ?) AND EXISTS (SELECT 1 FROM runs WHERE runs.id = approvals.run_id AND runs.status IN ('running', 'waiting_approval'))", [sql_text(input.state.copy()), sql_text(input.resolver.copy()), sql_text(input.decision_reason.copy()), sql_int(input.resolved_at_ms.copy()), sql_text(input.id.copy()), sql_text(input.run_id.copy()), sql_int(input.resolved_at_ms.copy())]) + let params: array = [sql_text(input.state.copy()), sql_text(input.resolver.copy()), sql_text(input.decision_reason.copy()), sql_int(input.resolved_at_ms.copy()), sql_text(input.id.copy()), sql_text(input.run_id.copy()), sql_int(input.resolved_at_ms.copy())]; + sqlite::execute(&db_id, "UPDATE approvals SET state = ?, resolver = ?, decision_reason = ?, resolved_at_ms = ? WHERE id = ? AND run_id = ? AND state = 'pending' AND (expires_at_ms <= 0 OR expires_at_ms > ?) AND EXISTS (SELECT 1 FROM runs WHERE runs.id = approvals.run_id AND runs.status IN ('running', 'waiting_approval'))", params) } pub fn storage_approval_expire(db_id: resource, payload_json: string) -> SqliteExecuteResult { let input: ApprovalExpireInput = json::decode::(payload_json); assert(input.force.copy() == 0 || input.force.copy() == 1); - let mut result: SqliteExecuteResult = sqlite::execute(&db_id, "UPDATE approvals SET state = 'expired', resolved_at_ms = ?, resolver = 'storage-expiry' WHERE state = 'pending' AND expires_at_ms > 0 AND expires_at_ms <= ?", [sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy())]); + let params: array = [sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy())]; + let mut result: SqliteExecuteResult = sqlite::execute(&db_id, "UPDATE approvals SET state = 'expired', resolved_at_ms = ?, resolver = 'storage-expiry' WHERE state = 'pending' AND expires_at_ms > 0 AND expires_at_ms <= ?", params); if input.run_id.copy().length > 0 { - result = sqlite::execute(&db_id, "UPDATE approvals SET state = 'expired', resolved_at_ms = ?, resolver = 'storage-expiry' WHERE state = 'pending' AND run_id = ? AND (? = 1 OR (expires_at_ms > 0 AND expires_at_ms <= ?))", [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_int(input.force.copy()), sql_int(input.now_ms.copy())]); + let params: array = [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_int(input.force.copy()), sql_int(input.now_ms.copy())]; + result = sqlite::execute(&db_id, "UPDATE approvals SET state = 'expired', resolved_at_ms = ?, resolver = 'storage-expiry' WHERE state = 'pending' AND run_id = ? AND (? = 1 OR (expires_at_ms > 0 AND expires_at_ms <= ?))", params); } result } @@ -137,7 +142,8 @@ struct ApprovalHandoffInput { /// `guardian_pending` is the sealed auto insert, and only this command writes `human_pending`. pub fn storage_approval_handoff(db_id: resource, payload_json: string) -> map { let input: ApprovalHandoffInput = json::decode::(payload_json); - let updated = sqlite::execute(&db_id, "UPDATE approvals SET decision_scope = 'human_pending' WHERE id = ? AND run_id = ? AND session_id = ? AND tool_call_id = ? AND tool_name = ? AND state = 'pending' AND decision_scope = 'guardian_pending' AND one_time = 1 AND (expires_at_ms <= 0 OR expires_at_ms > ?) AND EXISTS (SELECT 1 FROM runs WHERE runs.id = approvals.run_id AND runs.session_id = approvals.session_id AND runs.status IN ('running', 'waiting_approval'))", [sql_text(input.id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_call_id.copy()), sql_text(input.tool_name.copy()), sql_int(input.now_ms.copy())]); + let params: array = [sql_text(input.id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.tool_call_id.copy()), sql_text(input.tool_name.copy()), sql_int(input.now_ms.copy())]; + let updated = sqlite::execute(&db_id, "UPDATE approvals SET decision_scope = 'human_pending' WHERE id = ? AND run_id = ? AND session_id = ? AND tool_call_id = ? AND tool_name = ? AND state = 'pending' AND decision_scope = 'guardian_pending' AND one_time = 1 AND (expires_at_ms <= 0 OR expires_at_ms > ?) AND EXISTS (SELECT 1 FROM runs WHERE runs.id = approvals.run_id AND runs.session_id = approvals.session_id AND runs.status IN ('running', 'waiting_approval'))", params); let affected: int = updated.rows_affected.copy(); let mut result = { ok: false, diff --git a/rss/storage/compactions.rss b/rss/storage/compactions.rss index 129c4aa..bdec9c5 100644 --- a/rss/storage/compactions.rss +++ b/rss/storage/compactions.rss @@ -1,27 +1,32 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } @@ -36,11 +41,10 @@ fn sqlite_query_map(result: SqliteQueryResult) -> map { use self::schema as schema; use self::existence as existence; -fn compactions_query_limits(max_rows: int, max_bytes: int) { +fn compactions_query_limits(max_rows: int, max_bytes: int) -> SqliteLimits { sql_limits(max_rows, max_bytes) } - struct CompactionStartInput { id: string, session_id: string, @@ -74,16 +78,17 @@ struct CompactionFailInput { /// DIFFERENT session or generation: reusing an id across sessions or /// generations is a typed conflict, never a SQLite constraint error. fn compaction_id_conflict(db_id: resource, input_id: string, input_session_id: string, input_generation: int) -> bool { - let by_id: SqliteQueryResult = sqlite::query(&db_id, "SELECT id, session_id, generation FROM compactions WHERE id = ? LIMIT 1", [sql_text(input_id)], sql_limits(1, 4096)); + let params: array = [sql_text(input_id)]; + let by_id: SqliteQueryResult = sqlite::query(&db_id, "SELECT id, session_id, generation FROM compactions WHERE id = ? LIMIT 1", params, sql_limits(1, 4096)); let by_id_rows = by_id.rows; let mut conflict = false; if by_id_rows.length > 0 { let by_id_row: SqliteRow = by_id_rows[0].copy(); let by_id_cells: array = by_id_row.cells.copy(); - if by_id_cells[1].text_value != input_session_id { + if by_id_cells.copy()[1].text_value.unwrap_or("") != input_session_id { conflict = true; } - if by_id_cells[2].int_value != input_generation { + if by_id_cells.copy()[2].int_value.unwrap_or(0) != input_generation { conflict = true; } } @@ -93,7 +98,8 @@ fn compaction_id_conflict(db_id: resource, input_id: string, /// The existing compaction row for the target (session_id, generation), if /// any (the unique key of the compactions table). fn compaction_existing_row(db_id: resource, session_id: string, generation: int, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT id, session_id, run_id, generation, source_start_ordinal, source_end_ordinal, retained_tail_ordinal, summary_json, token_estimate, model, state, error_message, created_at_ms, completed_at_ms FROM compactions WHERE session_id = ? AND generation = ? LIMIT 1", [sql_text(session_id.copy()), sql_int(generation)], compactions_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(session_id.copy()), sql_int(generation)]; + sqlite::query(&db_id, "SELECT id, session_id, run_id, generation, source_start_ordinal, source_end_ordinal, retained_tail_ordinal, summary_json, token_estimate, model, state, error_message, created_at_ms, completed_at_ms FROM compactions WHERE session_id = ? AND generation = ? LIMIT 1", params, compactions_query_limits(max_rows, max_bytes)) } /// True when the existing pending row carries the exact same plan payload as @@ -168,8 +174,10 @@ fn compaction_row_matches( /// typed `compaction_start_rejected`, never a silent success. fn compaction_start_insert(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> map { let input: CompactionStartInput = json::decode::(payload_json); - sqlite::execute(&db_id, "INSERT INTO compactions (id, session_id, run_id, generation, source_start_ordinal, source_end_ordinal, retained_tail_ordinal, summary_json, token_estimate, model, state, created_at_ms) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', ? FROM sessions JOIN runs ON runs.session_id = sessions.id WHERE sessions.id = ? AND sessions.generation + 1 = ? AND runs.id = ? AND runs.session_id = ? AND runs.status = 'compacting' AND ? >= 0 AND ? >= ? AND ? >= ? AND ? <= ? AND EXISTS (SELECT 1 FROM messages WHERE messages.session_id = sessions.id AND messages.ordinal = ?) AND EXISTS (SELECT 1 FROM messages WHERE messages.session_id = sessions.id AND messages.ordinal = ?) AND NOT EXISTS (SELECT 1 FROM compactions existing WHERE existing.id = ? AND existing.session_id <> ?) ON CONFLICT (session_id, generation) DO UPDATE SET id = excluded.id, run_id = excluded.run_id, source_start_ordinal = excluded.source_start_ordinal, source_end_ordinal = excluded.source_end_ordinal, retained_tail_ordinal = excluded.retained_tail_ordinal, summary_json = excluded.summary_json, token_estimate = excluded.token_estimate, model = excluded.model, state = 'pending', error_message = '', created_at_ms = excluded.created_at_ms, completed_at_ms = 0 WHERE compactions.state = 'failed' AND compactions.id = excluded.id", [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_text(input.run_id.copy()), sql_int(input.generation.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.source_end_ordinal.copy()), sql_int(input.retained_tail_ordinal.copy()), sql_text(input.summary_json.copy()), sql_int(input.token_estimate.copy()), sql_text(input.model.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_int(input.generation.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.source_end_ordinal.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.retained_tail_ordinal.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.retained_tail_ordinal.copy()), sql_int(input.source_end_ordinal.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.source_end_ordinal.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy())]); - let inserted: SqliteQueryResult = sqlite::query(&db_id, "SELECT id, session_id, run_id, generation, source_start_ordinal, source_end_ordinal, retained_tail_ordinal, summary_json, token_estimate, model, state, error_message, created_at_ms, completed_at_ms FROM compactions WHERE id = ? AND session_id = ? LIMIT ?", [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)], compactions_query_limits(max_rows, max_bytes)); + let params: array = [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_text(input.run_id.copy()), sql_int(input.generation.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.source_end_ordinal.copy()), sql_int(input.retained_tail_ordinal.copy()), sql_text(input.summary_json.copy()), sql_int(input.token_estimate.copy()), sql_text(input.model.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_int(input.generation.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.source_end_ordinal.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.retained_tail_ordinal.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.retained_tail_ordinal.copy()), sql_int(input.source_end_ordinal.copy()), sql_int(input.source_start_ordinal.copy()), sql_int(input.source_end_ordinal.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy())]; + sqlite::execute(&db_id, "INSERT INTO compactions (id, session_id, run_id, generation, source_start_ordinal, source_end_ordinal, retained_tail_ordinal, summary_json, token_estimate, model, state, created_at_ms) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', ? FROM sessions JOIN runs ON runs.session_id = sessions.id WHERE sessions.id = ? AND sessions.generation + 1 = ? AND runs.id = ? AND runs.session_id = ? AND runs.status = 'compacting' AND ? >= 0 AND ? >= ? AND ? >= ? AND ? <= ? AND EXISTS (SELECT 1 FROM messages WHERE messages.session_id = sessions.id AND messages.ordinal = ?) AND EXISTS (SELECT 1 FROM messages WHERE messages.session_id = sessions.id AND messages.ordinal = ?) AND NOT EXISTS (SELECT 1 FROM compactions existing WHERE existing.id = ? AND existing.session_id <> ?) ON CONFLICT (session_id, generation) DO UPDATE SET id = excluded.id, run_id = excluded.run_id, source_start_ordinal = excluded.source_start_ordinal, source_end_ordinal = excluded.source_end_ordinal, retained_tail_ordinal = excluded.retained_tail_ordinal, summary_json = excluded.summary_json, token_estimate = excluded.token_estimate, model = excluded.model, state = 'pending', error_message = '', created_at_ms = excluded.created_at_ms, completed_at_ms = 0 WHERE compactions.state = 'failed' AND compactions.id = excluded.id", params); + let params: array = [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)]; + let inserted: SqliteQueryResult = sqlite::query(&db_id, "SELECT id, session_id, run_id, generation, source_start_ordinal, source_end_ordinal, retained_tail_ordinal, summary_json, token_estimate, model, state, error_message, created_at_ms, completed_at_ms FROM compactions WHERE id = ? AND session_id = ? LIMIT ?", params, compactions_query_limits(max_rows, max_bytes)); let inserted_empty: bool = inserted.rows.copy().length == 0; let mut result = { ok: true, code: "ok", message: "", result: { columns: [], rows: [] } }; if inserted_empty { @@ -183,10 +191,12 @@ fn compaction_start_insert(db_id: resource, payload_json: str pub fn storage_compaction_start(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> map { let input: CompactionStartInput = json::decode::(payload_json); let mut result = { ok: true, code: "ok", message: "", result: { columns: [], rows: [] } }; - if !(sqlite::query(&db_id, existence::session_exists_sql(), [sql_text(input.session_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.session_id.copy())]; + if !(sqlite::query(&db_id, existence::session_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "run_not_found", message: "compaction start references an unknown session or run", result: { columns: [], rows: [] } }; } else { - if !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.run_id.copy())]; + if !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "run_not_found", message: "compaction start references an unknown session or run", result: { columns: [], rows: [] } }; } else { if compaction_id_conflict(db_id, input.id.copy(), input.session_id.copy(), input.generation.copy()) { @@ -202,7 +212,7 @@ pub fn storage_compaction_start(db_id: resource, payload_json if existing_rows.length > 0 { let existing_row: SqliteRow = existing_rows[0].copy(); let existing_cells: array = existing_row.cells.copy(); - let existing_state: string = existing_cells[10].text_value; + let existing_state: string = existing_cells.copy()[10].text_value.unwrap_or(""); if existing_state == "committed" { result = { ok: false, code: "compaction_already_committed", message: "a compaction for this session+generation is already committed", result: { columns: [], rows: [] } }; } else { @@ -213,15 +223,15 @@ pub fn storage_compaction_start(db_id: resource, payload_json // Different payload -> typed conflict: never a // silent ok with an empty row set, never a // clobber of the pending record. - let existing_id: string = existing_cells[0].text_value; - let existing_run_id: string = existing_cells[2].text_value; - let existing_generation: int = existing_cells[3].int_value; - let existing_source_start: int = existing_cells[4].int_value; - let existing_source_end: int = existing_cells[5].int_value; - let existing_retained_tail: int = existing_cells[6].int_value; - let existing_summary: string = existing_cells[7].text_value; - let existing_token_estimate: int = existing_cells[8].int_value; - let existing_model: string = existing_cells[9].text_value; + let existing_id: string = existing_cells.copy()[0].text_value.unwrap_or(""); + let existing_run_id: string = existing_cells.copy()[2].text_value.unwrap_or(""); + let existing_generation: int = existing_cells.copy()[3].int_value.unwrap_or(0); + let existing_source_start: int = existing_cells.copy()[4].int_value.unwrap_or(0); + let existing_source_end: int = existing_cells.copy()[5].int_value.unwrap_or(0); + let existing_retained_tail: int = existing_cells.copy()[6].int_value.unwrap_or(0); + let existing_summary: string = existing_cells.copy()[7].text_value.unwrap_or(""); + let existing_token_estimate: int = existing_cells.copy()[8].int_value.unwrap_or(0); + let existing_model: string = existing_cells.copy()[9].text_value.unwrap_or(""); if compaction_row_matches( existing_id, existing_run_id, @@ -254,7 +264,7 @@ pub fn storage_compaction_start(db_id: resource, payload_json // row's audit identity, so it is a typed // conflict: the caller must resume with the // original id. - let existing_failed_id: string = existing_cells[0].text_value; + let existing_failed_id: string = existing_cells.copy()[0].text_value.unwrap_or(""); if existing_failed_id == input.id.copy() { result = compaction_start_insert(db_id, payload_json, max_rows, max_bytes); } else { @@ -272,11 +282,13 @@ pub fn storage_compaction_start(db_id: resource, payload_json } pub fn storage_compaction_get(db_id: resource, compaction_id: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT id, session_id, run_id, generation, source_start_ordinal, source_end_ordinal, retained_tail_ordinal, summary_json, token_estimate, model, state, error_message, created_at_ms, completed_at_ms FROM compactions WHERE id = ? LIMIT ?", [sql_text(compaction_id), sql_int(max_rows)], compactions_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(compaction_id), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT id, session_id, run_id, generation, source_start_ordinal, source_end_ordinal, retained_tail_ordinal, summary_json, token_estimate, model, state, error_message, created_at_ms, completed_at_ms FROM compactions WHERE id = ? LIMIT ?", params, compactions_query_limits(max_rows, max_bytes)) } pub fn storage_compaction_latest(db_id: resource, session_id: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, compaction_latest_sql("WHERE session_id = ?"), [sql_text(session_id.copy()), sql_int(max_rows)], compactions_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(session_id.copy()), sql_int(max_rows)]; + sqlite::query(&db_id, compaction_latest_sql("WHERE session_id = ?"), params, compactions_query_limits(max_rows, max_bytes)) } fn compaction_latest_sql(predicate: string) -> string { @@ -303,21 +315,21 @@ pub fn storage_compaction_commit(db_id: resource, payload_jso let statements: array = [ { sql: "UPDATE compactions SET state = 'committed', completed_at_ms = ? WHERE id = ? AND state = 'pending' AND session_id = ? AND source_start_ordinal = ? AND source_end_ordinal = ? AND generation = ? AND ? >= 0 AND ? >= ? AND ? <= ? AND EXISTS (SELECT 1 FROM messages WHERE messages.session_id = compactions.session_id AND messages.ordinal = ?) AND EXISTS (SELECT 1 FROM messages WHERE messages.session_id = compactions.session_id AND messages.ordinal = ?) AND EXISTS (SELECT 1 FROM runs WHERE runs.id = compactions.run_id AND runs.session_id = ? AND runs.status = 'compacting') AND EXISTS (SELECT 1 FROM sessions WHERE sessions.id = ? AND sessions.generation = ? - 1)", - params: [sql_int(input.completed_at_ms.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_int(input.generation.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy()), sql_int(input.generation.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.completed_at_ms.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_int(input.generation.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy()), sql_int(input.generation.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE messages SET compacted = 1 WHERE session_id = ? AND ordinal >= ? AND ordinal <= ? AND changes() = 1 AND EXISTS (SELECT 1 FROM compactions WHERE id = ? AND session_id = ? AND source_start_ordinal = ? AND source_end_ordinal = ? AND generation = ? AND state = 'committed')", - params: [sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_int(input.generation.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_int(input.generation.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE sessions SET generation = MAX(generation, ?), updated_at_ms = ? WHERE id = ? AND generation < ? AND EXISTS (SELECT 1 FROM compactions WHERE id = ? AND session_id = ? AND generation = ? AND state = 'committed')", - params: [sql_int(input.generation.copy()), sql_int(input.completed_at_ms.copy()), sql_text(input.session_id.copy()), sql_int(input.generation.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(input.generation.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.generation.copy()), sql_int(input.completed_at_ms.copy()), sql_text(input.session_id.copy()), sql_int(input.generation.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(input.generation.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; sqlite::transaction(&db_id, statements) @@ -332,5 +344,6 @@ pub fn storage_compaction_fail(db_id: resource, payload_json: // means the failure was already recorded or the row was never // created; surfacing a typed error would break those callers without // adding safety). - sqlite::execute(&db_id, "UPDATE compactions SET state = 'failed', error_message = ?, completed_at_ms = ? WHERE id = ? AND state = 'pending'", [sql_text(input.error_message.copy()), sql_int(input.completed_at_ms.copy()), sql_text(input.id.copy())]) + let params: array = [sql_text(input.error_message.copy()), sql_int(input.completed_at_ms.copy()), sql_text(input.id.copy())]; + sqlite::execute(&db_id, "UPDATE compactions SET state = 'failed', error_message = ?, completed_at_ms = ? WHERE id = ? AND state = 'pending'", params) } diff --git a/rss/storage/debug.rss b/rss/storage/debug.rss index e57e74d..56fffa0 100644 --- a/rss/storage/debug.rss +++ b/rss/storage/debug.rss @@ -1,27 +1,33 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } +fn no_params() -> Option> { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } @@ -110,13 +116,13 @@ struct CompactionIdInput { compaction_id: string } - fn storage_open_options(db_path: string, db_mode: string, busy_timeout_ms: int, max_rows: int, max_bytes: int) -> SqliteOpenOptions { + let no_root: Option = None; { - path: db_path, - mode: db_mode, - root: null, - limits: sql_limits(schema::max_rows_limit(max_rows), schema::max_bytes_limit(max_bytes)) + path: Some(db_path), + mode: Some(db_mode), + root: no_root, + limits: Some(sql_limits(schema::max_rows_limit(max_rows), schema::max_bytes_limit(max_bytes))) } } @@ -129,17 +135,17 @@ fn storage_apply_migrations(db_id: resource, now_ms: int) { while statement_index < schema::schema_migration_statement_count(migration_index) { statements[statements.length] = { sql: schema::schema_migration_statement(migration_index, statement_index), - params: [], - query: false, - limits: sql_limits(1000, 4194304) + params: no_params(), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; statement_index += 1; } statements[statements.length] = { sql: schema::schema_migration_record_sql(), - params: [sql_int(schema::schema_migration_version(migration_index)), sql_text(schema::schema_migration_name(migration_index)), sql_text(schema::schema_migration_checksum(migration_index)), sql_int(now_ms)], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(schema::schema_migration_version(migration_index)), sql_text(schema::schema_migration_name(migration_index)), sql_text(schema::schema_migration_checksum(migration_index)), sql_int(now_ms)]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; sqlite::transaction(&db_id, statements); migration_index += 1; @@ -214,55 +220,29 @@ fn storage_dispatch(db_id: resource, command: StorageCommand) let op = command.op.copy(); let mut result = storage_error(request_id.copy(), op.copy(), "unknown_operation", "storage operation is not registered"); - if command.op == "migrate" => { + if command.op == "migrate" { result = storage_ok(request_id, op, {schema_version: schema::schema_migration_count(), host_contract: storage_host_contract()}, 0, false, 0); - result - } else => { null }; - - if command.op == "session.create" => { + } else if command.op == "session.create" { result = storage_raw_ok(request_id, op, sessions::storage_session_create(db_id, command.payload_json, command.max_rows, command.max_bytes)); - result - } else => { null }; - - if command.op == "session.get" => { + } else if command.op == "session.get" { let input: SessionIdInput = json::decode::(command.payload_json); result = storage_raw_ok(request_id, op, sessions::storage_session_get(db_id, input.session_id, command.max_rows, command.max_bytes)); - result - } else => { null }; - - if command.op == "session.list" => { + } else if command.op == "session.list" { let input: SessionListInput = json::decode::(command.payload_json); result = storage_raw_ok(request_id, op, sessions::storage_session_list(db_id, input.profile, input.platform, command.max_rows, command.max_bytes)); - result - } else => { null }; - - if command.op == "session.touch" => { + } else if command.op == "session.touch" { result = storage_raw_ok(request_id, op, sessions::storage_session_touch(db_id, command.payload_json, command.max_rows, command.max_bytes)); - result - } else => { null }; - - if command.op == "message.append" => { + } else if command.op == "message.append" { result = storage_unwrap(request_id, op, messages::storage_message_append(db_id, command.payload_json, command.max_rows, command.max_bytes)); - result - } else => { null }; - - if command.op == "message.get" => { + } else if command.op == "message.get" { let input: MessageIdInput = json::decode::(command.payload_json); result = storage_raw_ok(request_id, op, messages::storage_message_get(db_id, input.message_id, command.max_rows, command.max_bytes)); - result - } else => { null }; - - if command.op == "message.list" => { + } else if command.op == "message.list" { let input: MessageListInput = json::decode::(command.payload_json); result = storage_raw_ok(request_id, op, messages::storage_message_list(db_id, input.session_id, input.after_ordinal.copy(), command.max_messages, command.max_bytes)); - result - } else => { null }; - - if command.op == "message.compact" => { + } else if command.op == "message.compact" { result = storage_execute_ok(request_id, op, messages::storage_message_mark_compacted(db_id, command.payload_json)); - result - } else => { null }; - + } result } diff --git a/rss/storage/effects.rss b/rss/storage/effects.rss index 34e7d05..c2418d6 100644 --- a/rss/storage/effects.rss +++ b/rss/storage/effects.rss @@ -3,62 +3,77 @@ use json; use sqlite; use plugins; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } +fn no_params() -> Option> { None } + struct ManagedObjectIdInput {store_id:string,object_id:string} pub fn object_metadata(db:resource,payload:string)->map{ let input:ManagedObjectIdInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"SELECT record_json FROM managed_objects WHERE store_id=? AND object_id=?",[text(input.store_id),text(input.object_id)],limits());ok(result) + let params: array = [text(input.store_id),text(input.object_id)]; + let result:SqliteQueryResult=sqlite::query(&db,"SELECT record_json FROM managed_objects WHERE store_id=? AND object_id=?",params,limits());ok(result) } pub fn object_census(db:resource,payload:string)->map{ let statements:array = [ - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (SELECT COUNT(*) FROM managed_objects)>128",params:[],query:false,limits:limits()}, - {sql:"SELECT record_json FROM managed_objects ORDER BY object_id LIMIT 128",params:[],query:true,limits:limits()} + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (SELECT COUNT(*) FROM managed_objects)>128",params: no_params(),query: Some(false),limits: Some(limits())}, + {sql:"SELECT record_json FROM managed_objects ORDER BY object_id LIMIT 128",params: no_params(),query: Some(true),limits: Some(limits())} ]; - let results:array = sqlite::transaction(&db,statements);let last:SqliteTransactionResult=results[1].copy();let query:SqliteQueryResult=last.query;ok(query) + let results:array = sqlite::transaction(&db,statements);let last:SqliteTransactionResult=results[1].copy();let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, };ok(query) } pub fn object_refs(db:resource,payload:string)->map{ let input:ManagedObjectIdInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"WITH target AS(SELECT ? AS store_id,? AS object_id) SELECT m.store_id,m.object_id,'registered_object' AS reference_kind,m.object_id AS reference_id FROM managed_objects m,target t WHERE m.store_id=t.store_id AND m.object_id=t.object_id UNION ALL SELECT w.store_id,t.object_id,'seed',w.workspace_id FROM managed_workspaces w,target t WHERE w.store_id=t.store_id AND w.seed_object_id=t.object_id UNION ALL SELECT b.store_id,t.object_id,'branch_head',b.branch_id FROM managed_branches b,target t WHERE b.store_id=t.store_id AND b.head_object_id=t.object_id UNION ALL SELECT b.store_id,t.object_id,'history',h.workspace_id||':'||h.head_version FROM managed_head_history h JOIN managed_branches b ON b.workspace_id=h.workspace_id AND b.branch_id=h.branch_id,target t WHERE b.store_id=t.store_id AND h.head_object_id=t.object_id UNION ALL SELECT t.store_id,t.object_id,'call_base',c.effect_id FROM managed_effect_calls c,target t WHERE json_extract(c.identity_json,'$.workspace.store_id')=t.store_id AND json_extract(c.identity_json,'$.expected_head')=t.object_id UNION ALL SELECT t.store_id,t.object_id,json_extract(o.value,'$.kind'),c.effect_id FROM managed_effect_calls c,target t,json_each(json_array(json_object('kind','prepared_candidate','id',json_extract(c.record_json,'$.prepared.candidate_root.object_id')),json_object('kind','prepared_result','id',json_extract(c.record_json,'$.prepared.canonical_result.object_id')),json_object('kind','prepared_conflict','id',json_extract(c.record_json,'$.prepared.conflict_result.object_id')),json_object('kind','selected_result','id',json_extract(c.record_json,'$.outcome.canonical_result_ref.object_id')),json_object('kind','intent_details','id',json_extract(c.identity_json,'$.argument_identity.details.object_id')),json_object('kind','delegated_arguments','id',json_extract(c.record_json,'$.delegation.proof.child_arguments.details.object_id')),json_object('kind','delegated_child_result','id',json_extract(c.record_json,'$.delegation.child_result.object_id')))) o WHERE json_extract(c.identity_json,'$.workspace.store_id')=t.store_id AND json_extract(o.value,'$.id')=t.object_id LIMIT 513",[text(input.store_id),text(input.object_id)],limits());ok(result) + let params: array = [text(input.store_id),text(input.object_id)]; + let result:SqliteQueryResult=sqlite::query(&db,"WITH target AS(SELECT ? AS store_id,? AS object_id) SELECT m.store_id,m.object_id,'registered_object' AS reference_kind,m.object_id AS reference_id FROM managed_objects m,target t WHERE m.store_id=t.store_id AND m.object_id=t.object_id UNION ALL SELECT w.store_id,t.object_id,'seed',w.workspace_id FROM managed_workspaces w,target t WHERE w.store_id=t.store_id AND w.seed_object_id=t.object_id UNION ALL SELECT b.store_id,t.object_id,'branch_head',b.branch_id FROM managed_branches b,target t WHERE b.store_id=t.store_id AND b.head_object_id=t.object_id UNION ALL SELECT b.store_id,t.object_id,'history',h.workspace_id||':'||h.head_version FROM managed_head_history h JOIN managed_branches b ON b.workspace_id=h.workspace_id AND b.branch_id=h.branch_id,target t WHERE b.store_id=t.store_id AND h.head_object_id=t.object_id UNION ALL SELECT t.store_id,t.object_id,'call_base',c.effect_id FROM managed_effect_calls c,target t WHERE json_extract(c.identity_json,'$.workspace.store_id')=t.store_id AND json_extract(c.identity_json,'$.expected_head')=t.object_id UNION ALL SELECT t.store_id,t.object_id,json_extract(o.value,'$.kind'),c.effect_id FROM managed_effect_calls c,target t,json_each(json_array(json_object('kind','prepared_candidate','id',json_extract(c.record_json,'$.prepared.candidate_root.object_id')),json_object('kind','prepared_result','id',json_extract(c.record_json,'$.prepared.canonical_result.object_id')),json_object('kind','prepared_conflict','id',json_extract(c.record_json,'$.prepared.conflict_result.object_id')),json_object('kind','selected_result','id',json_extract(c.record_json,'$.outcome.canonical_result_ref.object_id')),json_object('kind','intent_details','id',json_extract(c.identity_json,'$.argument_identity.details.object_id')),json_object('kind','delegated_arguments','id',json_extract(c.record_json,'$.delegation.proof.child_arguments.details.object_id')),json_object('kind','delegated_child_result','id',json_extract(c.record_json,'$.delegation.child_result.object_id')))) o WHERE json_extract(c.identity_json,'$.workspace.store_id')=t.store_id AND json_extract(o.value,'$.id')=t.object_id LIMIT 513",params,limits());ok(result) } struct ManagedRunIdInput {run_id:string} struct ManagedRunEffectListInput {run_id:string,profile:string,session_id:string,workspace_id:string,branch_id:string,after_effect_id:string,limit:int} pub fn run_binding(db:resource,payload:string)->map{ let input:ManagedRunIdInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"SELECT json_object('owner',json_object('profile',b.profile,'session_id',b.session_id,'run_id',b.run_id),'workspace',json_set(json_extract(w.record_json,'$.identity'),'$.branch_id',b.branch_id),'epoch',b.epoch) AS record_json FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id AND r.session_id=b.session_id JOIN sessions s ON s.id=b.session_id JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.profile=b.profile JOIN managed_branches h ON h.workspace_id=b.workspace_id AND h.branch_id=b.branch_id AND h.store_id=w.store_id WHERE b.run_id=? LIMIT 1",[text(input.run_id)],limits());ok(result) + let params: array = [text(input.run_id)]; + let result:SqliteQueryResult=sqlite::query(&db,"SELECT json_object('owner',json_object('profile',b.profile,'session_id',b.session_id,'run_id',b.run_id),'workspace',json_set(json_extract(w.record_json,'$.identity'),'$.branch_id',b.branch_id),'epoch',b.epoch) AS record_json FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id AND r.session_id=b.session_id JOIN sessions s ON s.id=b.session_id JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.profile=b.profile JOIN managed_branches h ON h.workspace_id=b.workspace_id AND h.branch_id=b.branch_id AND h.store_id=w.store_id WHERE b.run_id=? LIMIT 1",params,limits());ok(result) } pub fn run_list(db:resource,payload:string)->map{ let input:ManagedRunEffectListInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id,run_id,call_id,CASE state WHEN 'intent' THEN 'indeterminate' WHEN 'prepared' THEN 'staged' ELSE state END AS state,workspace_id,branch_id,json_extract(identity_json,'$.epoch') AS epoch,json_extract(record_json,'$.prepared.prepared_at_ms') AS prepared_at_ms,(SELECT created_at_ms FROM messages WHERE id=json_extract(managed_effect_calls.record_json,'$.outcome.message.id')) AS decided_at_ms FROM managed_effect_calls WHERE run_id=? AND json_extract(identity_json,'$.owner.run_id')=run_id AND json_extract(identity_json,'$.owner.profile')=? AND json_extract(identity_json,'$.owner.session_id')=? AND workspace_id=? AND branch_id=? AND effect_id>? ORDER BY effect_id LIMIT ?",[text(input.run_id),text(input.profile),text(input.session_id),text(input.workspace_id),text(input.branch_id),text(input.after_effect_id),integer(input.limit)],limits());ok(result) + let params: array = [text(input.run_id),text(input.profile),text(input.session_id),text(input.workspace_id),text(input.branch_id),text(input.after_effect_id),integer(input.limit)]; + let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id,run_id,call_id,CASE state WHEN 'intent' THEN 'indeterminate' WHEN 'prepared' THEN 'staged' ELSE state END AS state,workspace_id,branch_id,json_extract(identity_json,'$.epoch') AS epoch,json_extract(record_json,'$.prepared.prepared_at_ms') AS prepared_at_ms,(SELECT created_at_ms FROM messages WHERE id=json_extract(managed_effect_calls.record_json,'$.outcome.message.id')) AS decided_at_ms FROM managed_effect_calls WHERE run_id=? AND json_extract(identity_json,'$.owner.run_id')=run_id AND json_extract(identity_json,'$.owner.profile')=? AND json_extract(identity_json,'$.owner.session_id')=? AND workspace_id=? AND branch_id=? AND effect_id>? ORDER BY effect_id LIMIT ?",params,limits());ok(result) } pub fn run_list_has_more(db:resource,payload:string)->map{ let input:ManagedRunEffectListInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id,run_id,call_id,CASE state WHEN 'intent' THEN 'indeterminate' WHEN 'prepared' THEN 'staged' ELSE state END AS state,workspace_id,branch_id,json_extract(identity_json,'$.epoch') AS epoch,json_extract(record_json,'$.prepared.prepared_at_ms') AS prepared_at_ms,(SELECT created_at_ms FROM messages WHERE id=json_extract(managed_effect_calls.record_json,'$.outcome.message.id')) AS decided_at_ms FROM managed_effect_calls WHERE run_id=? AND json_extract(identity_json,'$.owner.run_id')=run_id AND json_extract(identity_json,'$.owner.profile')=? AND json_extract(identity_json,'$.owner.session_id')=? AND workspace_id=? AND branch_id=? AND effect_id>? ORDER BY effect_id LIMIT 1",[text(input.run_id),text(input.profile),text(input.session_id),text(input.workspace_id),text(input.branch_id),text(input.after_effect_id)],limits());ok(result) + let params: array = [text(input.run_id),text(input.profile),text(input.session_id),text(input.workspace_id),text(input.branch_id),text(input.after_effect_id)]; + let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id,run_id,call_id,CASE state WHEN 'intent' THEN 'indeterminate' WHEN 'prepared' THEN 'staged' ELSE state END AS state,workspace_id,branch_id,json_extract(identity_json,'$.epoch') AS epoch,json_extract(record_json,'$.prepared.prepared_at_ms') AS prepared_at_ms,(SELECT created_at_ms FROM messages WHERE id=json_extract(managed_effect_calls.record_json,'$.outcome.message.id')) AS decided_at_ms FROM managed_effect_calls WHERE run_id=? AND json_extract(identity_json,'$.owner.run_id')=run_id AND json_extract(identity_json,'$.owner.profile')=? AND json_extract(identity_json,'$.owner.session_id')=? AND workspace_id=? AND branch_id=? AND effect_id>? ORDER BY effect_id LIMIT 1",params,limits());ok(result) } pub fn run_cursor(db:resource,payload:string)->map{ let input:ManagedRunEffectListInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id,run_id,call_id,CASE state WHEN 'intent' THEN 'indeterminate' WHEN 'prepared' THEN 'staged' ELSE state END AS state,workspace_id,branch_id,json_extract(identity_json,'$.epoch') AS epoch,json_extract(record_json,'$.prepared.prepared_at_ms') AS prepared_at_ms,(SELECT created_at_ms FROM messages WHERE id=json_extract(managed_effect_calls.record_json,'$.outcome.message.id')) AS decided_at_ms FROM managed_effect_calls WHERE run_id=? AND json_extract(identity_json,'$.owner.run_id')=run_id AND json_extract(identity_json,'$.owner.profile')=? AND json_extract(identity_json,'$.owner.session_id')=? AND workspace_id=? AND branch_id=? AND effect_id=? ORDER BY effect_id LIMIT 1",[text(input.run_id),text(input.profile),text(input.session_id),text(input.workspace_id),text(input.branch_id),text(input.after_effect_id)],limits());ok(result) + let params: array = [text(input.run_id),text(input.profile),text(input.session_id),text(input.workspace_id),text(input.branch_id),text(input.after_effect_id)]; + let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id,run_id,call_id,CASE state WHEN 'intent' THEN 'indeterminate' WHEN 'prepared' THEN 'staged' ELSE state END AS state,workspace_id,branch_id,json_extract(identity_json,'$.epoch') AS epoch,json_extract(record_json,'$.prepared.prepared_at_ms') AS prepared_at_ms,(SELECT created_at_ms FROM messages WHERE id=json_extract(managed_effect_calls.record_json,'$.outcome.message.id')) AS decided_at_ms FROM managed_effect_calls WHERE run_id=? AND json_extract(identity_json,'$.owner.run_id')=run_id AND json_extract(identity_json,'$.owner.profile')=? AND json_extract(identity_json,'$.owner.session_id')=? AND workspace_id=? AND branch_id=? AND effect_id=? ORDER BY effect_id LIMIT 1",params,limits());ok(result) } struct ManagedEffectListInput {workspace_id:string,after_effect_id:string,limit:int} struct ManagedEffectIdInput {effect_id:string} pub fn list(db:resource,payload:string)->map{ let input:ManagedEffectListInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id,run_id,call_id,CASE state WHEN 'intent' THEN 'indeterminate' WHEN 'prepared' THEN 'staged' ELSE state END AS state,workspace_id,branch_id,json_extract(identity_json,'$.epoch') AS epoch,json_extract(record_json,'$.prepared.prepared_at_ms') AS prepared_at_ms,(SELECT created_at_ms FROM messages WHERE id=json_extract(managed_effect_calls.record_json,'$.outcome.message.id')) AS decided_at_ms FROM managed_effect_calls WHERE workspace_id=? AND effect_id>? ORDER BY effect_id LIMIT ?",[text(input.workspace_id),text(input.after_effect_id),integer(input.limit)],limits());ok(result) + let params: array = [text(input.workspace_id),text(input.after_effect_id),integer(input.limit)]; + let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id,run_id,call_id,CASE state WHEN 'intent' THEN 'indeterminate' WHEN 'prepared' THEN 'staged' ELSE state END AS state,workspace_id,branch_id,json_extract(identity_json,'$.epoch') AS epoch,json_extract(record_json,'$.prepared.prepared_at_ms') AS prepared_at_ms,(SELECT created_at_ms FROM messages WHERE id=json_extract(managed_effect_calls.record_json,'$.outcome.message.id')) AS decided_at_ms FROM managed_effect_calls WHERE workspace_id=? AND effect_id>? ORDER BY effect_id LIMIT ?",params,limits());ok(result) } pub fn list_has_more(db:resource,payload:string)->map{ let input:ManagedEffectListInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id FROM managed_effect_calls WHERE workspace_id=? AND effect_id>? ORDER BY effect_id LIMIT 1",[text(input.workspace_id),text(input.after_effect_id)],limits());ok(result) + let params: array = [text(input.workspace_id),text(input.after_effect_id)]; + let result:SqliteQueryResult=sqlite::query(&db,"SELECT effect_id FROM managed_effect_calls WHERE workspace_id=? AND effect_id>? ORDER BY effect_id LIMIT 1",params,limits());ok(result) } pub fn by_id(db:resource,payload:string)->map{ let input:ManagedEffectIdInput=json::decode::(payload); - let result:SqliteQueryResult=sqlite::query(&db,"SELECT record_json FROM managed_effect_calls WHERE effect_id=?",[text(input.effect_id)],limits());ok(result) + let params: array = [text(input.effect_id)]; + let result:SqliteQueryResult=sqlite::query(&db,"SELECT record_json FROM managed_effect_calls WHERE effect_id=?",params,limits());ok(result) } -fn text(value: string) -> SqliteValue { { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } } -fn integer(value: int) -> SqliteValue { { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } } -fn limits() -> SqliteLimits { { max_connections: 16, max_statements: 128, max_rows: 512, max_columns: 16, max_result_bytes: 1048576, max_statement_bytes: 1048576, max_parameters: 128, max_parameter_bytes: 1048576, max_pending_operations: 32, max_transaction_ms: 5000, busy_timeout_ms: 5000 } } +fn text(value: string) -> SqliteValue { { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } +fn integer(value: int) -> SqliteValue { { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } +fn limits() -> SqliteLimits { { max_connections: Some(16), max_statements: Some(128), max_rows: Some(512), max_columns: Some(16), max_result_bytes: Some(1048576), max_statement_bytes: Some(1048576), max_parameters: Some(128), max_parameter_bytes: Some(1048576), max_pending_operations: Some(32), max_transaction_ms: Some(5000), busy_timeout_ms: Some(5000) } } fn ok(result: SqliteQueryResult) -> map { { ok: true, code: "ok", message: "", result: { columns: result.columns, rows: result.rows, truncated: result.truncated } } } struct ManagedStorageInput { record_json: string, prepared_json: string, workspace_json: string, projections_json: string, effect_id: string, receipt_id: string, commit_id: string, run_id: string, call_id: string, store_id: string, profile: string, source_device: string, source_inode: string, workspace_id: string, branch_id: string, now_ms: int, max_events: int, cut: string } struct ManagedBeginInput { record_json: string, prepared_json: string, workspace_json: string, projections_json: string, effect_id: string, receipt_id: string, commit_id: string, run_id: string, call_id: string, store_id: string, profile: string, source_device: string, source_inode: string, workspace_id: string, branch_id: string, now_ms: int, max_events: int, cut: string, sealed_class: string } @@ -71,67 +86,71 @@ pub fn migration(index: int) -> string { fn effect_schema(index: int) -> string { if index == 0 => { "CREATE TABLE managed_objects (store_id TEXT NOT NULL, object_id TEXT NOT NULL, schema_version INTEGER NOT NULL CHECK(schema_version=1), kind TEXT NOT NULL CHECK(kind IN ('tree','canonical_result','intent_details')), record_json TEXT NOT NULL CHECK(json_valid(record_json)), readiness TEXT NOT NULL CHECK(readiness='synced'), PRIMARY KEY(store_id,object_id))" } else => { if index == 1 => { "CREATE TABLE managed_workspaces (workspace_id TEXT PRIMARY KEY, store_id TEXT NOT NULL, profile TEXT NOT NULL, source_device TEXT NOT NULL, source_inode TEXT NOT NULL, schema_version INTEGER NOT NULL CHECK(schema_version=1), seed_object_id TEXT NOT NULL, current_branch_id TEXT NOT NULL, record_json TEXT NOT NULL CHECK(json_valid(record_json)), UNIQUE(store_id,profile,source_device,source_inode), FOREIGN KEY(store_id,seed_object_id) REFERENCES managed_objects(store_id,object_id))" } else => { if index == 2 => { "CREATE TABLE managed_branches (workspace_id TEXT NOT NULL REFERENCES managed_workspaces(workspace_id), branch_id TEXT NOT NULL, epoch INTEGER NOT NULL CHECK(epoch>0), status TEXT NOT NULL DEFAULT 'active' CHECK(status IN ('active','retired')), lineage_json TEXT NOT NULL DEFAULT '{}', process_lineage_tainted INTEGER NOT NULL DEFAULT 0 CHECK(process_lineage_tainted IN (0,1)), head_object_id TEXT NOT NULL, head_version INTEGER NOT NULL CHECK(head_version>=1), store_id TEXT NOT NULL, schema_version INTEGER NOT NULL CHECK(schema_version=1), PRIMARY KEY(workspace_id,branch_id), FOREIGN KEY(store_id,head_object_id) REFERENCES managed_objects(store_id,object_id))" } else => { if index == 3 => { "CREATE TABLE managed_run_bindings (run_id TEXT PRIMARY KEY REFERENCES runs(id), profile TEXT NOT NULL, session_id TEXT NOT NULL REFERENCES sessions(id), workspace_id TEXT NOT NULL, branch_id TEXT NOT NULL, epoch INTEGER NOT NULL CHECK(epoch>0), graph_ref_id TEXT NOT NULL, original_deadline_ms INTEGER NOT NULL, schema_version INTEGER NOT NULL CHECK(schema_version=1), FOREIGN KEY(workspace_id,branch_id) REFERENCES managed_branches(workspace_id,branch_id))" } else => { if index == 4 => { "CREATE TABLE managed_effect_calls (effect_id TEXT PRIMARY KEY, run_id TEXT NOT NULL REFERENCES runs(id), call_id TEXT NOT NULL, workspace_id TEXT NOT NULL, branch_id TEXT NOT NULL, state TEXT NOT NULL CHECK(state IN ('intent','prepared','committed','read_only','not_published','conflict','indeterminate')), schema_version INTEGER NOT NULL CHECK(schema_version=1), identity_json TEXT NOT NULL CHECK(json_valid(identity_json)), record_json TEXT NOT NULL CHECK(json_valid(record_json)), commit_id TEXT, observed_head TEXT, observed_version INTEGER, UNIQUE(run_id,call_id), FOREIGN KEY(workspace_id,branch_id) REFERENCES managed_branches(workspace_id,branch_id))" } else => { if index == 5 => { "CREATE TABLE managed_effect_receipts (effect_id TEXT PRIMARY KEY REFERENCES managed_effect_calls(effect_id), receipt_id TEXT NOT NULL UNIQUE, prepared_record_json TEXT NOT NULL CHECK(json_valid(prepared_record_json)), outcome_json TEXT CHECK(outcome_json IS NULL OR json_valid(outcome_json)), schema_version INTEGER NOT NULL CHECK(schema_version=1))" } else => { if index == 6 => { "CREATE TABLE managed_head_history (workspace_id TEXT NOT NULL, branch_id TEXT NOT NULL, head_version INTEGER NOT NULL, epoch INTEGER NOT NULL, effect_id TEXT UNIQUE REFERENCES managed_effect_calls(effect_id), previous_head TEXT, head_object_id TEXT NOT NULL, schema_version INTEGER NOT NULL CHECK(schema_version=1), PRIMARY KEY(workspace_id,branch_id,head_version), FOREIGN KEY(workspace_id,branch_id) REFERENCES managed_branches(workspace_id,branch_id))" } else => { if index == 7 => { "CREATE TABLE managed_effect_guard (id INTEGER PRIMARY KEY CHECK(id=1))" } else => { if index == 8 => { "CREATE INDEX managed_effect_recovery ON managed_effect_calls(workspace_id,branch_id,state,effect_id)" } else => { if index == 9 => { "CREATE INDEX managed_effect_owner ON managed_effect_calls(run_id,call_id,effect_id)" } else => { "" } } } } } } } } } } } -pub fn workspace_get(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); ok(sqlite::query(&db, "SELECT record_json FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=?", [text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy())], limits())) } +pub fn workspace_get(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); let params: array = [text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy())]; +ok(sqlite::query(&db, "SELECT record_json FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=?", params, limits())) } -pub fn head(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); ok(sqlite::query(&db, "SELECT json_object('workspace_id',workspace_id,'branch_id',branch_id,'epoch',epoch,'head_object_id',head_object_id,'head_version',head_version,'store_id',store_id,'schema_version',schema_version,'process_lineage_tainted',json(CASE process_lineage_tainted WHEN 1 THEN 'true' ELSE 'false' END)) AS record_json FROM managed_branches WHERE workspace_id=? AND branch_id=COALESCE(NULLIF(?,''),(SELECT current_branch_id FROM managed_workspaces WHERE managed_workspaces.workspace_id=managed_branches.workspace_id))", [text(input.workspace_id.copy()), text(input.branch_id.copy())], limits())) } +pub fn head(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); let params: array = [text(input.workspace_id.copy()), text(input.branch_id.copy())]; +ok(sqlite::query(&db, "SELECT json_object('workspace_id',workspace_id,'branch_id',branch_id,'epoch',epoch,'head_object_id',head_object_id,'head_version',head_version,'store_id',store_id,'schema_version',schema_version,'process_lineage_tainted',json(CASE process_lineage_tainted WHEN 1 THEN 'true' ELSE 'false' END)) AS record_json FROM managed_branches WHERE workspace_id=? AND branch_id=COALESCE(NULLIF(?,''),(SELECT current_branch_id FROM managed_workspaces WHERE managed_workspaces.workspace_id=managed_branches.workspace_id))", params, limits())) } -pub fn get(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); ok(sqlite::query(&db, "SELECT record_json FROM managed_effect_calls WHERE run_id=? AND call_id=? AND json_extract(identity_json,'$.owner.profile')=?", [text(input.run_id.copy()), text(input.call_id.copy()), text(input.profile.copy())], limits())) } +pub fn get(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); let params: array = [text(input.run_id.copy()), text(input.call_id.copy()), text(input.profile.copy())]; +ok(sqlite::query(&db, "SELECT record_json FROM managed_effect_calls WHERE run_id=? AND call_id=? AND json_extract(identity_json,'$.owner.profile')=?", params, limits())) } pub fn workspace_register(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); let statements: array = [ -{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(?,'$.seed_root'))) WHERE value IS NOT NULL ON CONFLICT(store_id,object_id) DO NOTHING", params: [text(input.workspace_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_workspaces(workspace_id,store_id,profile,source_device,source_inode,schema_version,seed_object_id,record_json,current_branch_id) SELECT json_extract(?,'$.identity.workspace_id'),json_extract(?,'$.identity.store_id'),json_extract(?,'$.profile'),json_extract(?,'$.identity.source_identity.device_decimal'),json_extract(?,'$.identity.source_identity.inode_decimal'),1,json_extract(?,'$.seed_root.object_id'),?,json_extract(?,'$.identity.branch_id') ON CONFLICT(store_id,profile,source_device,source_inode) DO NOTHING", params: [text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()),text(input.workspace_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=? AND json_extract(record_json,'$.max_entries')=json_extract(?,'$.max_entries') AND json_extract(record_json,'$.max_bytes')=json_extract(?,'$.max_bytes') AND json_extract(record_json,'$.max_depth')=json_extract(?,'$.max_depth'))", params: [text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_branches(workspace_id,branch_id,epoch,head_object_id,head_version,store_id,schema_version) SELECT workspace_id,json_extract(record_json,'$.identity.branch_id'),1,seed_object_id,1,store_id,1 FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=? ON CONFLICT(workspace_id,branch_id) DO NOTHING", params: [text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT workspace_id,branch_id,1,1,NULL,NULL,head_object_id,1 FROM managed_branches WHERE workspace_id=(SELECT workspace_id FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=?) ON CONFLICT(workspace_id,branch_id,head_version) DO NOTHING", params: [text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy())], query: false, limits: limits() }, -{ sql: "SELECT record_json FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=?", params: [text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy())], query: true, limits: limits() } +{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(?,'$.seed_root'))) WHERE value IS NOT NULL ON CONFLICT(store_id,object_id) DO NOTHING", params: Some([text(input.workspace_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_workspaces(workspace_id,store_id,profile,source_device,source_inode,schema_version,seed_object_id,record_json,current_branch_id) SELECT json_extract(?,'$.identity.workspace_id'),json_extract(?,'$.identity.store_id'),json_extract(?,'$.profile'),json_extract(?,'$.identity.source_identity.device_decimal'),json_extract(?,'$.identity.source_identity.inode_decimal'),1,json_extract(?,'$.seed_root.object_id'),?,json_extract(?,'$.identity.branch_id') ON CONFLICT(store_id,profile,source_device,source_inode) DO NOTHING", params: Some([text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()),text(input.workspace_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=? AND json_extract(record_json,'$.max_entries')=json_extract(?,'$.max_entries') AND json_extract(record_json,'$.max_bytes')=json_extract(?,'$.max_bytes') AND json_extract(record_json,'$.max_depth')=json_extract(?,'$.max_depth'))", params: Some([text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy()), text(input.workspace_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_branches(workspace_id,branch_id,epoch,head_object_id,head_version,store_id,schema_version) SELECT workspace_id,json_extract(record_json,'$.identity.branch_id'),1,seed_object_id,1,store_id,1 FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=? ON CONFLICT(workspace_id,branch_id) DO NOTHING", params: Some([text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT workspace_id,branch_id,1,1,NULL,NULL,head_object_id,1 FROM managed_branches WHERE workspace_id=(SELECT workspace_id FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=?) ON CONFLICT(workspace_id,branch_id,head_version) DO NOTHING", params: Some([text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "SELECT record_json FROM managed_workspaces WHERE store_id=? AND profile=? AND source_device=? AND source_inode=?", params: Some([text(input.store_id.copy()), text(input.profile.copy()), text(input.source_device.copy()), text(input.source_inode.copy())]), query: Some(true), limits: Some(limits()) } ]; let result: array = sqlite::transaction(&db, statements); let last: SqliteTransactionResult = result[result.length - 1].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok(query) } pub fn admission_statements(payload: string, graph_id: string, ref_id: string) -> array { let input: ManagedStorageInput = json::decode::(payload); [ -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE b.workspace_id=? AND b.branch_id=? AND b.epoch=json_extract(?,'$.epoch') AND b.head_object_id=json_extract(?,'$.head_object_id') AND b.head_version=json_extract(?,'$.head_version') AND b.store_id=json_extract(?,'$.store_id') AND b.status='active' AND (w.current_branch_id=b.branch_id OR EXISTS(SELECT 1 FROM managed_branch_receipts r WHERE r.workspace_id=b.workspace_id AND json_extract(r.receipt_json,'$.branch.branch_id')=b.branch_id AND json_extract(r.origin_json,'$.workspace_id')=b.workspace_id AND ((json_extract(r.receipt_json,'$.branch.epoch')=b.epoch AND json_extract(r.receipt_json,'$.branch.head_object_id')=b.head_object_id AND json_extract(r.receipt_json,'$.branch.head_version')=b.head_version AND json_extract(r.origin_json,'$.head_object_id')=b.head_object_id) OR json_extract(r.origin_json,'$.kind')='checkpoint'))))) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(input.run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{ sql: "INSERT INTO managed_run_bindings(run_id,profile,session_id,workspace_id,branch_id,epoch,graph_ref_id,original_deadline_ms,schema_version) SELECT ?,?,json_extract(?,'$.session_id'),?,?,json_extract(?,'$.epoch'),json_extract(?,'$.graph_ref_id'),json_extract(?,'$.original_deadline_ms'),1 WHERE (?<>'') AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", params: [text(input.run_id.copy()), text(input.profile.copy()), text(input.record_json.copy()), text(input.workspace_id.copy()), text(input.branch_id.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.workspace_id.copy()), text(graph_id.copy()), text(graph_id.copy()), text(input.run_id.copy()), text(graph_id.copy()), text(ref_id.copy())], query: false, limits: limits() } +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE b.workspace_id=? AND b.branch_id=? AND b.epoch=json_extract(?,'$.epoch') AND b.head_object_id=json_extract(?,'$.head_object_id') AND b.head_version=json_extract(?,'$.head_version') AND b.store_id=json_extract(?,'$.store_id') AND b.status='active' AND (w.current_branch_id=b.branch_id OR EXISTS(SELECT 1 FROM managed_branch_receipts r WHERE r.workspace_id=b.workspace_id AND json_extract(r.receipt_json,'$.branch.branch_id')=b.branch_id AND json_extract(r.origin_json,'$.workspace_id')=b.workspace_id AND ((json_extract(r.receipt_json,'$.branch.epoch')=b.epoch AND json_extract(r.receipt_json,'$.branch.head_object_id')=b.head_object_id AND json_extract(r.receipt_json,'$.branch.head_version')=b.head_version AND json_extract(r.origin_json,'$.head_object_id')=b.head_object_id) OR json_extract(r.origin_json,'$.kind')='checkpoint'))))) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(input.run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{ sql: "INSERT INTO managed_run_bindings(run_id,profile,session_id,workspace_id,branch_id,epoch,graph_ref_id,original_deadline_ms,schema_version) SELECT ?,?,json_extract(?,'$.session_id'),?,?,json_extract(?,'$.epoch'),json_extract(?,'$.graph_ref_id'),json_extract(?,'$.original_deadline_ms'),1 WHERE (?<>'') AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", params: Some([text(input.run_id.copy()), text(input.profile.copy()), text(input.record_json.copy()), text(input.workspace_id.copy()), text(input.branch_id.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.workspace_id.copy()), text(graph_id.copy()), text(graph_id.copy()), text(input.run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]), query: Some(false), limits: Some(limits()) } ] } pub fn begin(db: resource, payload: string) -> map { let input: ManagedBeginInput = json::decode::(payload.copy()); - let existing:SqliteQueryResult=sqlite::query(&db,"SELECT record_json FROM managed_effect_calls WHERE effect_id=? AND run_id=? AND call_id=? AND identity_json=json_extract(?,'$.identity')",[text(input.effect_id),text(input.run_id),text(input.call_id),text(input.record_json)],limits()); + let params: array = [text(input.effect_id),text(input.run_id),text(input.call_id),text(input.record_json)]; + let existing:SqliteQueryResult=sqlite::query(&db,"SELECT record_json FROM managed_effect_calls WHERE effect_id=? AND run_id=? AND call_id=? AND identity_json=json_extract(?,'$.identity')",params,limits()); let inspected:SqliteQueryResult=existing.copy();let count:int=inspected.rows.length;let truncated:bool=inspected.truncated; if count == 1 && truncated == false => {ok(existing)} else => {begin_new(db,payload)} } fn begin_new(db: resource, payload: string) -> map { let input: ManagedBeginInput = json::decode::(payload); let statements: array = [ -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (? NOT IN ('workspace_read','workspace_write','foreground_process','process_control')) AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [text(input.sealed_class.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "UPDATE managed_branches SET process_lineage_tainted=1 WHERE workspace_id=? AND branch_id=? AND ?='foreground_process' AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.sealed_class.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(?,'$.identity.argument_identity.details'))) WHERE value IS NOT NULL AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id)) ON CONFLICT(store_id,object_id) DO NOTHING", params: [text(input.record_json.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (json_extract(?,'$.identity.argument_identity.kind')='object' AND NOT EXISTS(SELECT 1 FROM managed_objects WHERE store_id=json_extract(?,'$.identity.argument_identity.details.store_id') AND object_id=json_extract(?,'$.identity.argument_identity.details.object_id') AND kind='intent_details' AND record_json=json_extract(?,'$.identity.argument_identity.details'))) AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (NOT EXISTS (SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.run_id=? AND r.status IN ('running','queued') AND b.workspace_id=json_extract(?,'$.identity.workspace.workspace_id') AND b.branch_id=json_extract(?,'$.identity.workspace.branch_id') AND b.graph_ref_id=json_extract(?,'$.identity.code.graph_ref_id') AND ?=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=? AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (? = 'BeginAfterFirstEvent') AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [text(input.cut.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO run_events(run_id,seq,event_id,event_type,payload_json,created_at_ms) SELECT c.run_id,json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.seq'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_id'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_type'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.payload_json'),(SELECT CASE WHEN json_type(draft,'$.created_at_ms')='integer' AND typeof(json_extract(draft,'$.created_at_ms'))='integer' AND json_extract(draft,'$.created_at_ms')>=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=? AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "UPDATE runs SET updated_at_ms=? WHERE id=? AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [integer(input.now_ms.copy()), text(input.run_id.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "DELETE FROM run_events WHERE run_id=? AND seq NOT IN (SELECT seq FROM run_events WHERE run_id=? ORDER BY seq DESC LIMIT ?) AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [text(input.run_id.copy()), text(input.run_id.copy()), integer(input.max_events.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO run_retention(run_id,first_seq,high_water_seq,updated_at_ms) SELECT ?,COALESCE(MIN(seq),0),COALESCE(MAX(seq),0),? FROM run_events WHERE run_id=? HAVING EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id)) ON CONFLICT(run_id) DO UPDATE SET first_seq=excluded.first_seq,high_water_seq=MAX(run_retention.high_water_seq,excluded.high_water_seq),updated_at_ms=excluded.updated_at_ms", params: [text(input.run_id.copy()), integer(input.now_ms.copy()), text(input.run_id.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "UPDATE managed_effect_calls SET commit_id=NULL WHERE effect_id=? AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: [text(input.effect_id.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", params: [text(input.effect_id.copy())], query: true, limits: limits() }, -{ sql: "SELECT CASE WHEN EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN plugin_graph_revocations r ON r.graph_id=q.graph_id WHERE q.run_id=?) THEN 'plugin_graph_revoked' WHEN EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id)) THEN 'ok' ELSE 'plugin_graph_pin_required' END", params: [text(input.run_id.copy()), text(input.run_id.copy())], query: true, limits: limits() } +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (? NOT IN ('workspace_read','workspace_write','foreground_process','process_control')) AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([text(input.sealed_class.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE managed_branches SET process_lineage_tainted=1 WHERE workspace_id=? AND branch_id=? AND ?='foreground_process' AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.sealed_class.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(?,'$.identity.argument_identity.details'))) WHERE value IS NOT NULL AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id)) ON CONFLICT(store_id,object_id) DO NOTHING", params: Some([text(input.record_json.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (json_extract(?,'$.identity.argument_identity.kind')='object' AND NOT EXISTS(SELECT 1 FROM managed_objects WHERE store_id=json_extract(?,'$.identity.argument_identity.details.store_id') AND object_id=json_extract(?,'$.identity.argument_identity.details.object_id') AND kind='intent_details' AND record_json=json_extract(?,'$.identity.argument_identity.details'))) AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (NOT EXISTS (SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.run_id=? AND r.status IN ('running','queued') AND b.workspace_id=json_extract(?,'$.identity.workspace.workspace_id') AND b.branch_id=json_extract(?,'$.identity.workspace.branch_id') AND b.graph_ref_id=json_extract(?,'$.identity.code.graph_ref_id') AND ?=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=? AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (? = 'BeginAfterFirstEvent') AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([text(input.cut.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO run_events(run_id,seq,event_id,event_type,payload_json,created_at_ms) SELECT c.run_id,json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.seq'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_id'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_type'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.payload_json'),(SELECT CASE WHEN json_type(draft,'$.created_at_ms')='integer' AND typeof(json_extract(draft,'$.created_at_ms'))='integer' AND json_extract(draft,'$.created_at_ms')>=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=? AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE runs SET updated_at_ms=? WHERE id=? AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([integer(input.now_ms.copy()), text(input.run_id.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "DELETE FROM run_events WHERE run_id=? AND seq NOT IN (SELECT seq FROM run_events WHERE run_id=? ORDER BY seq DESC LIMIT ?) AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([text(input.run_id.copy()), text(input.run_id.copy()), integer(input.max_events.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO run_retention(run_id,first_seq,high_water_seq,updated_at_ms) SELECT ?,COALESCE(MIN(seq),0),COALESCE(MAX(seq),0),? FROM run_events WHERE run_id=? HAVING EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id)) ON CONFLICT(run_id) DO UPDATE SET first_seq=excluded.first_seq,high_water_seq=MAX(run_retention.high_water_seq,excluded.high_water_seq),updated_at_ms=excluded.updated_at_ms", params: Some([text(input.run_id.copy()), integer(input.now_ms.copy()), text(input.run_id.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE managed_effect_calls SET commit_id=NULL WHERE effect_id=? AND EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id))", params: Some([text(input.effect_id.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", params: Some([text(input.effect_id.copy())]), query: Some(true), limits: Some(limits()) }, +{ sql: "SELECT CASE WHEN EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN plugin_graph_revocations r ON r.graph_id=q.graph_id WHERE q.run_id=?) THEN 'plugin_graph_revoked' WHEN EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN managed_run_bindings b ON b.run_id=q.run_id AND b.graph_ref_id=q.ref_id JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=q.run_id AND g.graph_id=q.graph_id AND g.ref_id=q.ref_id WHERE q.run_id=? AND NOT EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id=q.graph_id)) THEN 'ok' ELSE 'plugin_graph_pin_required' END", params: Some([text(input.run_id.copy()), text(input.run_id.copy())]), query: Some(true), limits: Some(limits()) } ]; let result: array = sqlite::transaction(&db, statements); let classification: SqliteTransactionResult = result[result.length - 1].copy(); let code: string = plugins::storage_pin_text(classification, 0); if code.copy() == "ok" => { let last: SqliteTransactionResult = result[result.length - 2].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok(query) } else => { {ok:false,code:code,message:"tool graph pin is required",result:{}} @@ -140,7 +159,8 @@ fn begin_new(db: resource, payload: string) -> map { pub fn prepare(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload.copy()); - let existing:SqliteQueryResult=sqlite::query(&db,"SELECT c.record_json FROM managed_effect_calls c JOIN managed_effect_receipts p ON p.effect_id=c.effect_id WHERE c.effect_id=? AND p.receipt_id=? AND p.prepared_record_json=?",[text(input.effect_id),text(input.receipt_id),text(input.prepared_json)],limits()); + let params: array = [text(input.effect_id),text(input.receipt_id),text(input.prepared_json)]; + let existing:SqliteQueryResult=sqlite::query(&db,"SELECT c.record_json FROM managed_effect_calls c JOIN managed_effect_receipts p ON p.effect_id=c.effect_id WHERE c.effect_id=? AND p.receipt_id=? AND p.prepared_record_json=?",params,limits()); let inspected:SqliteQueryResult=existing.copy();let count:int=inspected.rows.length;let truncated:bool=inspected.truncated; if count == 1 && truncated == false => {ok(existing)} else => {prepare_new(db,payload)} } @@ -149,19 +169,19 @@ fn prepare_new(db: resource, payload: string) -> map { let update_json: string = if input.record_json.copy() == "" => { input.prepared_json.copy() } else => { input.record_json.copy() }; let update_sql: string = if input.record_json.copy() == "" => { "UPDATE managed_effect_calls SET state='prepared',record_json=json_set(record_json,'$.state','staged','$.prepared',json(?)) WHERE effect_id=?" } else => { "UPDATE managed_effect_calls SET state='prepared',record_json=json(?) WHERE effect_id=? AND state='intent'" }; let statements: array = [ -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_effect_calls WHERE effect_id=? AND state='intent' AND identity_json=json_extract(?,'$.identity') AND CASE WHEN json_extract(COALESCE(NULLIF(?,''),'{}'),'$.delegation') IS NULL THEN json_extract(record_json,'$.delegation') IS NULL ELSE (json_extract(record_json,'$.delegation.phase')='intent' AND json_extract(record_json,'$.delegation.proof')=json_extract(COALESCE(NULLIF(?,''),'{}'),'$.delegation.proof')) END)", params: [text(input.effect_id.copy()), text(update_json.copy()), text(input.record_json.copy()), text(input.record_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(?,'$.candidate_root'),json_extract(?,'$.canonical_result'),json_extract(?,'$.conflict_result'))) WHERE value IS NOT NULL ON CONFLICT(store_id,object_id) DO NOTHING", params: [text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(COALESCE(NULLIF(?,''),'{}'),'$.delegation.child_result'))) WHERE value IS NOT NULL ON CONFLICT(store_id,object_id) DO NOTHING", params: [text(input.record_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'PrepareAfterObjectRegistry'", params: [text(input.cut.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_receipts(effect_id,receipt_id,prepared_record_json,schema_version) VALUES (?,?,?,1)", params: [text(input.effect_id.copy()), text(input.receipt_id.copy()), text(input.prepared_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: limits() }, -{ sql: update_sql, params: [text(update_json.copy()), text(input.effect_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: limits() }, -{ sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", params: [text(input.effect_id.copy())], query: true, limits: limits() } +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_effect_calls WHERE effect_id=? AND state='intent' AND identity_json=json_extract(?,'$.identity') AND CASE WHEN json_extract(COALESCE(NULLIF(?,''),'{}'),'$.delegation') IS NULL THEN json_extract(record_json,'$.delegation') IS NULL ELSE (json_extract(record_json,'$.delegation.phase')='intent' AND json_extract(record_json,'$.delegation.proof')=json_extract(COALESCE(NULLIF(?,''),'{}'),'$.delegation.proof')) END)", params: Some([text(input.effect_id.copy()), text(update_json.copy()), text(input.record_json.copy()), text(input.record_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(?,'$.candidate_root'),json_extract(?,'$.canonical_result'),json_extract(?,'$.conflict_result'))) WHERE value IS NOT NULL ON CONFLICT(store_id,object_id) DO NOTHING", params: Some([text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(COALESCE(NULLIF(?,''),'{}'),'$.delegation.child_result'))) WHERE value IS NOT NULL ON CONFLICT(store_id,object_id) DO NOTHING", params: Some([text(input.record_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'PrepareAfterObjectRegistry'", params: Some([text(input.cut.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_receipts(effect_id,receipt_id,prepared_record_json,schema_version) VALUES (?,?,?,1)", params: Some([text(input.effect_id.copy()), text(input.receipt_id.copy()), text(input.prepared_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(limits()) }, +{ sql: update_sql, params: Some([text(update_json.copy()), text(input.effect_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(limits()) }, +{ sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", params: Some([text(input.effect_id.copy())]), query: Some(true), limits: Some(limits()) } ]; let result: array = sqlite::transaction(&db, statements); let last: SqliteTransactionResult = result[result.length - 1].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok(query) } @@ -172,14 +192,14 @@ pub fn delegate_intent(db: resource, payload: string) -> map // proof. A zero-row update aborts the whole transaction, including the child // detail reference registration, so a losing contender writes nothing. let statements: array = [ -{ sql: "UPDATE managed_effect_calls SET record_json=json_set(record_json,'$.delegation',json(json_extract(?,'$.delegation'))) WHERE effect_id=? AND run_id=? AND call_id=? AND workspace_id=? AND branch_id=? AND state='intent' AND json_extract(record_json,'$.delegation') IS NULL AND json_extract(?,'$.delegation.phase')='intent' AND json_extract(identity_json,'$.workspace.store_id')=json_extract(?,'$.identity.workspace.store_id') AND json_extract(identity_json,'$.epoch')=json_extract(?,'$.delegation.proof.epoch') AND json_extract(identity_json,'$.expected_head')=json_extract(?,'$.delegation.proof.expected_head') AND json_extract(identity_json,'$.expected_head_version')=json_extract(?,'$.delegation.proof.expected_head_version') AND identity_json=json_extract(?,'$.identity')", params: [text(input.record_json.copy()), text(input.effect_id.copy()), text(input.run_id.copy()), text(input.call_id.copy()), text(input.workspace_id.copy()), text(input.branch_id.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(?,'$.delegation.proof.child_arguments.details'))) WHERE value IS NOT NULL ON CONFLICT(store_id,object_id) DO NOTHING", params: [text(input.record_json.copy())], query: false, limits: limits() }, -{ sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", params: [text(input.effect_id.copy())], query: true, limits: limits() } +{ sql: "UPDATE managed_effect_calls SET record_json=json_set(record_json,'$.delegation',json(json_extract(?,'$.delegation'))) WHERE effect_id=? AND run_id=? AND call_id=? AND workspace_id=? AND branch_id=? AND state='intent' AND json_extract(record_json,'$.delegation') IS NULL AND json_extract(?,'$.delegation.phase')='intent' AND json_extract(identity_json,'$.workspace.store_id')=json_extract(?,'$.identity.workspace.store_id') AND json_extract(identity_json,'$.epoch')=json_extract(?,'$.delegation.proof.epoch') AND json_extract(identity_json,'$.expected_head')=json_extract(?,'$.delegation.proof.expected_head') AND json_extract(identity_json,'$.expected_head_version')=json_extract(?,'$.delegation.proof.expected_head_version') AND identity_json=json_extract(?,'$.identity')", params: Some([text(input.record_json.copy()), text(input.effect_id.copy()), text(input.run_id.copy()), text(input.call_id.copy()), text(input.workspace_id.copy()), text(input.branch_id.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy()), text(input.record_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_objects(store_id,object_id,schema_version,kind,record_json,readiness) SELECT json_extract(value,'$.store_id'),json_extract(value,'$.object_id'),1,json_extract(value,'$.object_kind'),value,'synced' FROM json_each(json_array(json_extract(?,'$.delegation.proof.child_arguments.details'))) WHERE value IS NOT NULL ON CONFLICT(store_id,object_id) DO NOTHING", params: Some([text(input.record_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", params: Some([text(input.effect_id.copy())]), query: Some(true), limits: Some(limits()) } ]; let result: array = sqlite::transaction(&db, statements); let last: SqliteTransactionResult = result[result.length - 1].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok(query) } @@ -187,7 +207,8 @@ pub fn commit(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload.copy()); // Final rows are immutable and retained. Exact same-attempt acknowledgement // recovery is a read, never a new head decision or another event insertion. - let existing:SqliteQueryResult=sqlite::query(&db,"SELECT c.record_json FROM managed_effect_calls c JOIN managed_effect_receipts p ON p.effect_id=c.effect_id WHERE c.effect_id=? AND c.run_id=? AND c.workspace_id=? AND c.branch_id=? AND c.commit_id=? AND p.receipt_id=? AND p.prepared_record_json=? AND c.state IN ('committed','read_only','not_published','conflict') AND json_extract(c.record_json,'$.outcome') IS NOT NULL",[text(input.effect_id),text(input.run_id),text(input.workspace_id),text(input.branch_id),text(input.commit_id),text(input.receipt_id),text(input.prepared_json)],limits()); + let params: array = [text(input.effect_id),text(input.run_id),text(input.workspace_id),text(input.branch_id),text(input.commit_id),text(input.receipt_id),text(input.prepared_json)]; + let existing:SqliteQueryResult=sqlite::query(&db,"SELECT c.record_json FROM managed_effect_calls c JOIN managed_effect_receipts p ON p.effect_id=c.effect_id WHERE c.effect_id=? AND c.run_id=? AND c.workspace_id=? AND c.branch_id=? AND c.commit_id=? AND p.receipt_id=? AND p.prepared_record_json=? AND c.state IN ('committed','read_only','not_published','conflict') AND json_extract(c.record_json,'$.outcome') IS NOT NULL",params,limits()); let inspected:SqliteQueryResult=existing.copy(); let row_count:int=inspected.rows.length; let truncated:bool=inspected.truncated; @@ -197,34 +218,34 @@ pub fn commit(db: resource, payload: string) -> map { fn commit_new(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); let statements: array = [ -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_effect_calls c JOIN managed_effect_receipts p ON p.effect_id=c.effect_id JOIN runs r ON r.id=c.run_id JOIN managed_run_bindings b ON b.run_id=c.run_id JOIN plugin_graph_refs g ON g.owner_id=c.run_id AND g.ref_id=b.graph_ref_id AND g.owner_kind='run' WHERE c.effect_id=? AND c.state='prepared' AND c.identity_json=json_extract(?,'$.identity') AND p.prepared_record_json=? AND r.status IN ('queued','running') AND ?1", params: [text(input.effect_id.copy())], query: false, limits: limits() }, -{ sql: "UPDATE plugin_state_run_bindings SET package_id=json_extract(?,'$.plugin_state.package_id'),artifact_id=json_extract(?,'$.plugin_state.artifact_id'),schema_version=json_extract(?,'$.plugin_state.schema_version'),revision=json_extract(?,'$.plugin_state.expected_revision')+1,object_id=json_extract(?,'$.plugin_state.object_id') WHERE run_id=json_extract(?,'$.identity.owner.run_id') AND revision=json_extract(?,'$.plugin_state.expected_revision') AND object_id=json_extract(?,'$.plugin_state.expected_object_id') AND owner_kind=json_extract(?,'$.plugin_state.owner_kind') AND owner_id=json_extract(?,'$.plugin_state.owner_id') AND workspace_id=json_extract(?,'$.plugin_state.workspace_id') AND branch_id=json_extract(?,'$.plugin_state.branch_id') AND plugin_id=json_extract(?,'$.plugin_state.plugin_id') AND EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind=json_extract(?,'$.plugin_state.owner_kind') AND owner_id=json_extract(?,'$.plugin_state.owner_id') AND workspace_id=json_extract(?,'$.plugin_state.workspace_id') AND branch_id=json_extract(?,'$.plugin_state.branch_id') AND plugin_id=json_extract(?,'$.plugin_state.plugin_id') AND revision=json_extract(?,'$.plugin_state.expected_revision') AND object_id=json_extract(?,'$.plugin_state.expected_object_id')) AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=json_extract(?,'$.plugin_state.object_id') AND package_id=json_extract(?,'$.plugin_state.package_id') AND artifact_id=json_extract(?,'$.plugin_state.artifact_id') AND schema_version=json_extract(?,'$.plugin_state.schema_version'))", params: [text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.plugin_state') IS NOT NULL AND json_extract(?,'$.plugin_state.ephemeral')<>1 AND changes()<>1", params: [text(input.prepared_json.copy()), text(input.prepared_json.copy())], query: false, limits: limits() }, -{ sql: "UPDATE plugin_state_roots SET schema_version=json_extract(?,'$.plugin_state.schema_version'),revision=json_extract(?,'$.plugin_state.expected_revision')+1,object_id=json_extract(?,'$.plugin_state.object_id'),updated_at_ms=? WHERE owner_kind=json_extract(?,'$.plugin_state.owner_kind') AND owner_id=json_extract(?,'$.plugin_state.owner_id') AND workspace_id=json_extract(?,'$.plugin_state.workspace_id') AND branch_id=json_extract(?,'$.plugin_state.branch_id') AND plugin_id=json_extract(?,'$.plugin_state.plugin_id') AND revision=json_extract(?,'$.plugin_state.expected_revision') AND object_id=json_extract(?,'$.plugin_state.expected_object_id') AND EXISTS(SELECT 1 FROM plugin_state_run_bindings WHERE run_id=json_extract(?,'$.identity.owner.run_id') AND revision=json_extract(?,'$.plugin_state.expected_revision')+1 AND object_id=json_extract(?,'$.plugin_state.object_id'))", params: [text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), integer(input.now_ms.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.plugin_state') IS NOT NULL AND json_extract(?,'$.plugin_state.ephemeral')<>1 AND changes()<>1", params: [text(input.prepared_json.copy()), text(input.prepared_json.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT b.workspace_id,b.branch_id,b.head_version,b.epoch,c.effect_id,c.observed_head,b.head_object_id,1 FROM managed_effect_calls c JOIN managed_branches b ON b.workspace_id=c.workspace_id AND b.branch_id=c.branch_id WHERE c.effect_id=? AND c.state='committed' AND c.commit_id=?", params: [text(input.effect_id.copy()), text(input.commit_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'CommitAfterHead'", params: [text(input.cut.copy())], query: false, limits: limits() }, -{ sql: "UPDATE managed_effect_calls SET record_json=json_set(record_json,'$.state',state,'$.outcome',json_extract(?,CASE WHEN state='conflict' THEN '$.conflict.outcome' ELSE '$.normal.outcome' END),'$.outcome.disposition',state,'$.outcome.publication.status',state,'$.outcome.publication.observed_head',observed_head,'$.outcome.publication.observed_head_version',observed_version) WHERE effect_id=? AND commit_id=?", params: [text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy())], query: false, limits: limits() }, -{ sql: "UPDATE managed_effect_receipts SET outcome_json=(SELECT json_extract(record_json,'$.outcome') FROM managed_effect_calls WHERE effect_id=?) WHERE effect_id=?", params: [text(input.effect_id.copy()), text(input.effect_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'CommitAfterReceipt'", params: [text(input.cut.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO run_events(run_id,seq,event_id,event_type,payload_json,created_at_ms) SELECT c.run_id,json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.seq'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.event_id'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.event_type'),json_set(json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.payload_json'),'$.managed_publication',json_extract(c.record_json,'$.outcome.publication')),(SELECT CASE WHEN json_type(draft,'$.created_at_ms')='integer' AND typeof(json_extract(draft,'$.created_at_ms'))='integer' AND json_extract(draft,'$.created_at_ms')>=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=?", params: [text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'CommitAfterFirstEvent'", params: [text(input.cut.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO run_events(run_id,seq,event_id,event_type,payload_json,created_at_ms) SELECT c.run_id,json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.seq'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_id'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_type'),json_set(json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.payload_json'),'$.managed_publication',json_extract(c.record_json,'$.outcome.publication')),(SELECT CASE WHEN json_type(draft,'$.created_at_ms')='integer' AND typeof(json_extract(draft,'$.created_at_ms'))='integer' AND json_extract(draft,'$.created_at_ms')>=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=?", params: [text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO messages(id,session_id,role,content_json,created_at_ms,run_id,name,tool_call_id,parent_message_id,token_estimate,metadata_json,ordinal,finish_reason) SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.message.id' ELSE '$.normal.message.id' END),json_extract(c.identity_json,'$.owner.session_id'),'user',json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.message.content_json' ELSE '$.normal.message.content_json' END),?,c.run_id,json_extract(c.identity_json,'$.tool_name'),c.call_id,json_extract(?,'$.normal.message.parent_message_id'),0,json_extract(c.record_json,'$.outcome.publication'),json_extract(?,'$.normal.message.ordinal'),'' FROM managed_effect_calls c WHERE effect_id=? AND commit_id=?", params: [text(input.projections_json.copy()), text(input.projections_json.copy()), integer(input.now_ms.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'CommitAfterMessage'", params: [text(input.cut.copy())], query: false, limits: limits() }, -{ sql: "UPDATE sessions SET last_message_seq=MAX(last_message_seq,COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id=sessions.id),0)),updated_at_ms=? WHERE id=(SELECT session_id FROM runs WHERE id=?)", params: [integer(input.now_ms.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "UPDATE runs SET updated_at_ms=? WHERE id=?", params: [integer(input.now_ms.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "DELETE FROM run_events WHERE run_id=? AND seq NOT IN (SELECT seq FROM run_events WHERE run_id=? ORDER BY seq DESC LIMIT ?)", params: [text(input.run_id.copy()), text(input.run_id.copy()), integer(input.max_events.copy())], query: false, limits: limits() }, -{ sql: "INSERT INTO run_retention(run_id,first_seq,high_water_seq,updated_at_ms) SELECT ?,COALESCE(MIN(seq),0),COALESCE(MAX(seq),0),? FROM run_events WHERE run_id=? ON CONFLICT(run_id) DO UPDATE SET first_seq=excluded.first_seq,high_water_seq=MAX(run_retention.high_water_seq,excluded.high_water_seq),updated_at_ms=excluded.updated_at_ms", params: [text(input.run_id.copy()), integer(input.now_ms.copy()), text(input.run_id.copy())], query: false, limits: limits() }, -{ sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", params: [text(input.effect_id.copy())], query: true, limits: limits() } +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_effect_calls c JOIN managed_effect_receipts p ON p.effect_id=c.effect_id JOIN runs r ON r.id=c.run_id JOIN managed_run_bindings b ON b.run_id=c.run_id JOIN plugin_graph_refs g ON g.owner_id=c.run_id AND g.ref_id=b.graph_ref_id AND g.owner_kind='run' WHERE c.effect_id=? AND c.state='prepared' AND c.identity_json=json_extract(?,'$.identity') AND p.prepared_record_json=? AND r.status IN ('queued','running') AND ?1", params: Some([text(input.effect_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE plugin_state_run_bindings SET package_id=json_extract(?,'$.plugin_state.package_id'),artifact_id=json_extract(?,'$.plugin_state.artifact_id'),schema_version=json_extract(?,'$.plugin_state.schema_version'),revision=json_extract(?,'$.plugin_state.expected_revision')+1,object_id=json_extract(?,'$.plugin_state.object_id') WHERE run_id=json_extract(?,'$.identity.owner.run_id') AND revision=json_extract(?,'$.plugin_state.expected_revision') AND object_id=json_extract(?,'$.plugin_state.expected_object_id') AND owner_kind=json_extract(?,'$.plugin_state.owner_kind') AND owner_id=json_extract(?,'$.plugin_state.owner_id') AND workspace_id=json_extract(?,'$.plugin_state.workspace_id') AND branch_id=json_extract(?,'$.plugin_state.branch_id') AND plugin_id=json_extract(?,'$.plugin_state.plugin_id') AND EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind=json_extract(?,'$.plugin_state.owner_kind') AND owner_id=json_extract(?,'$.plugin_state.owner_id') AND workspace_id=json_extract(?,'$.plugin_state.workspace_id') AND branch_id=json_extract(?,'$.plugin_state.branch_id') AND plugin_id=json_extract(?,'$.plugin_state.plugin_id') AND revision=json_extract(?,'$.plugin_state.expected_revision') AND object_id=json_extract(?,'$.plugin_state.expected_object_id')) AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=json_extract(?,'$.plugin_state.object_id') AND package_id=json_extract(?,'$.plugin_state.package_id') AND artifact_id=json_extract(?,'$.plugin_state.artifact_id') AND schema_version=json_extract(?,'$.plugin_state.schema_version'))", params: Some([text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.plugin_state') IS NOT NULL AND json_extract(?,'$.plugin_state.ephemeral')<>1 AND changes()<>1", params: Some([text(input.prepared_json.copy()), text(input.prepared_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE plugin_state_roots SET schema_version=json_extract(?,'$.plugin_state.schema_version'),revision=json_extract(?,'$.plugin_state.expected_revision')+1,object_id=json_extract(?,'$.plugin_state.object_id'),updated_at_ms=? WHERE owner_kind=json_extract(?,'$.plugin_state.owner_kind') AND owner_id=json_extract(?,'$.plugin_state.owner_id') AND workspace_id=json_extract(?,'$.plugin_state.workspace_id') AND branch_id=json_extract(?,'$.plugin_state.branch_id') AND plugin_id=json_extract(?,'$.plugin_state.plugin_id') AND revision=json_extract(?,'$.plugin_state.expected_revision') AND object_id=json_extract(?,'$.plugin_state.expected_object_id') AND EXISTS(SELECT 1 FROM plugin_state_run_bindings WHERE run_id=json_extract(?,'$.identity.owner.run_id') AND revision=json_extract(?,'$.plugin_state.expected_revision')+1 AND object_id=json_extract(?,'$.plugin_state.object_id'))", params: Some([text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), integer(input.now_ms.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy()), text(input.prepared_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.plugin_state') IS NOT NULL AND json_extract(?,'$.plugin_state.ephemeral')<>1 AND changes()<>1", params: Some([text(input.prepared_json.copy()), text(input.prepared_json.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT b.workspace_id,b.branch_id,b.head_version,b.epoch,c.effect_id,c.observed_head,b.head_object_id,1 FROM managed_effect_calls c JOIN managed_branches b ON b.workspace_id=c.workspace_id AND b.branch_id=c.branch_id WHERE c.effect_id=? AND c.state='committed' AND c.commit_id=?", params: Some([text(input.effect_id.copy()), text(input.commit_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'CommitAfterHead'", params: Some([text(input.cut.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE managed_effect_calls SET record_json=json_set(record_json,'$.state',state,'$.outcome',json_extract(?,CASE WHEN state='conflict' THEN '$.conflict.outcome' ELSE '$.normal.outcome' END),'$.outcome.disposition',state,'$.outcome.publication.status',state,'$.outcome.publication.observed_head',observed_head,'$.outcome.publication.observed_head_version',observed_version) WHERE effect_id=? AND commit_id=?", params: Some([text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE managed_effect_receipts SET outcome_json=(SELECT json_extract(record_json,'$.outcome') FROM managed_effect_calls WHERE effect_id=?) WHERE effect_id=?", params: Some([text(input.effect_id.copy()), text(input.effect_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'CommitAfterReceipt'", params: Some([text(input.cut.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO run_events(run_id,seq,event_id,event_type,payload_json,created_at_ms) SELECT c.run_id,json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.seq'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.event_id'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.event_type'),json_set(json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.payload_json'),'$.managed_publication',json_extract(c.record_json,'$.outcome.publication')),(SELECT CASE WHEN json_type(draft,'$.created_at_ms')='integer' AND typeof(json_extract(draft,'$.created_at_ms'))='integer' AND json_extract(draft,'$.created_at_ms')>=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=?", params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'CommitAfterFirstEvent'", params: Some([text(input.cut.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO run_events(run_id,seq,event_id,event_type,payload_json,created_at_ms) SELECT c.run_id,json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.seq'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_id'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_type'),json_set(json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.payload_json'),'$.managed_publication',json_extract(c.record_json,'$.outcome.publication')),(SELECT CASE WHEN json_type(draft,'$.created_at_ms')='integer' AND typeof(json_extract(draft,'$.created_at_ms'))='integer' AND json_extract(draft,'$.created_at_ms')>=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=?", params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO messages(id,session_id,role,content_json,created_at_ms,run_id,name,tool_call_id,parent_message_id,token_estimate,metadata_json,ordinal,finish_reason) SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.message.id' ELSE '$.normal.message.id' END),json_extract(c.identity_json,'$.owner.session_id'),'user',json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.message.content_json' ELSE '$.normal.message.content_json' END),?,c.run_id,json_extract(c.identity_json,'$.tool_name'),c.call_id,json_extract(?,'$.normal.message.parent_message_id'),0,json_extract(c.record_json,'$.outcome.publication'),json_extract(?,'$.normal.message.ordinal'),'' FROM managed_effect_calls c WHERE effect_id=? AND commit_id=?", params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), integer(input.now_ms.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE ? = 'CommitAfterMessage'", params: Some([text(input.cut.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE sessions SET last_message_seq=MAX(last_message_seq,COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id=sessions.id),0)),updated_at_ms=? WHERE id=(SELECT session_id FROM runs WHERE id=?)", params: Some([integer(input.now_ms.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "UPDATE runs SET updated_at_ms=? WHERE id=?", params: Some([integer(input.now_ms.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "DELETE FROM run_events WHERE run_id=? AND seq NOT IN (SELECT seq FROM run_events WHERE run_id=? ORDER BY seq DESC LIMIT ?)", params: Some([text(input.run_id.copy()), text(input.run_id.copy()), integer(input.max_events.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "INSERT INTO run_retention(run_id,first_seq,high_water_seq,updated_at_ms) SELECT ?,COALESCE(MIN(seq),0),COALESCE(MAX(seq),0),? FROM run_events WHERE run_id=? ON CONFLICT(run_id) DO UPDATE SET first_seq=excluded.first_seq,high_water_seq=MAX(run_retention.high_water_seq,excluded.high_water_seq),updated_at_ms=excluded.updated_at_ms", params: Some([text(input.run_id.copy()), integer(input.now_ms.copy()), text(input.run_id.copy())]), query: Some(false), limits: Some(limits()) }, +{ sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", params: Some([text(input.effect_id.copy())]), query: Some(true), limits: Some(limits()) } ]; let result: array = sqlite::transaction(&db, statements); let last: SqliteTransactionResult = result[result.length - 1].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok(query) } @@ -236,62 +257,62 @@ pub fn recover_one(db: resource, payload: string) -> map { let statements: array = [ { sql: "UPDATE managed_effect_calls SET state=CASE WHEN state='intent' THEN 'indeterminate' ELSE state END,commit_id=?,record_json=json_set(record_json,'$.state',CASE WHEN state='prepared' THEN 'staged' ELSE 'indeterminate' END) WHERE effect_id=? AND state IN ('intent','prepared','indeterminate') AND json_extract(record_json,'$.recovery') IS NULL", - params: [text(input.commit_id.copy()), text(input.effect_id.copy())], - query: false, - limits: limits() + params: Some([text(input.commit_id.copy()), text(input.effect_id.copy())]), + query: Some(false), + limits: Some(limits()) }, { sql: "INSERT INTO run_events(run_id,seq,event_id,event_type,payload_json,created_at_ms) SELECT c.run_id,json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.seq'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.event_id'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.event_type'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.payload_json'),(SELECT CASE WHEN json_type(draft,'$.created_at_ms')='integer' AND typeof(json_extract(draft,'$.created_at_ms'))='integer' AND json_extract(draft,'$.created_at_ms')>=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=? AND c.state IN ('intent','prepared','indeterminate') AND json_extract(c.record_json,'$.recovery') IS NULL AND NOT EXISTS (SELECT 1 FROM run_events existing WHERE existing.event_id=json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.output' ELSE '$.normal.output' END||'.event_id'))", - params: [text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.projections_json.copy())], - query: false, - limits: limits() + params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.projections_json.copy())]), + query: Some(false), + limits: Some(limits()) }, { sql: "INSERT INTO run_events(run_id,seq,event_id,event_type,payload_json,created_at_ms) SELECT c.run_id,json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.seq'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_id'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_type'),json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.payload_json'),(SELECT CASE WHEN json_type(draft,'$.created_at_ms')='integer' AND typeof(json_extract(draft,'$.created_at_ms'))='integer' AND json_extract(draft,'$.created_at_ms')>=0 THEN json_extract(draft,'$.created_at_ms') END FROM (SELECT json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END) AS draft)) FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=? AND c.state IN ('intent','prepared','indeterminate') AND json_extract(c.record_json,'$.recovery') IS NULL AND NOT EXISTS (SELECT 1 FROM run_events existing WHERE existing.event_id=json_extract(?,CASE WHEN c.state='conflict' THEN '$.conflict.terminal' ELSE '$.normal.terminal' END||'.event_id'))", - params: [text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.projections_json.copy())], - query: false, - limits: limits() + params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.projections_json.copy())]), + query: Some(false), + limits: Some(limits()) }, { sql: "INSERT INTO messages(id,session_id,role,content_json,created_at_ms,run_id,name,tool_call_id,parent_message_id,token_estimate,metadata_json,ordinal,finish_reason) SELECT json_extract(?,'$.normal.message.id'),json_extract(c.identity_json,'$.owner.session_id'),'user',json_extract(?,'$.normal.message.content_json'),?,c.run_id,json_extract(c.identity_json,'$.tool_name'),c.call_id,json_extract(?,'$.normal.message.parent_message_id'),0,'{}',json_extract(?,'$.normal.message.ordinal'),'' FROM managed_effect_calls c WHERE c.effect_id=? AND c.commit_id=? AND c.state IN ('intent','prepared','indeterminate') AND json_extract(c.record_json,'$.recovery') IS NULL AND NOT EXISTS (SELECT 1 FROM messages existing WHERE existing.id=json_extract(?,'$.normal.message.id'))", - params: [text(input.projections_json.copy()), text(input.projections_json.copy()), integer(input.now_ms.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.projections_json.copy())], - query: false, - limits: limits() + params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), integer(input.now_ms.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy()), text(input.commit_id.copy()), text(input.projections_json.copy())]), + query: Some(false), + limits: Some(limits()) }, { sql: "UPDATE managed_effect_calls SET record_json=json_set(record_json,'$.recovery',json_set(json_extract(?,'$.normal.recovery'),'$.output_event.seq',COALESCE((SELECT events.seq FROM run_events events WHERE events.event_id=json_extract(?,'$.normal.output.event_id')),json_extract(?,'$.normal.recovery.output_event.seq')),'$.terminal_event.seq',COALESCE((SELECT events.seq FROM run_events events WHERE events.event_id=json_extract(?,'$.normal.terminal.event_id')),json_extract(?,'$.normal.recovery.terminal_event.seq')),'$.message.ordinal',COALESCE((SELECT existing.ordinal FROM messages existing WHERE existing.id=json_extract(?,'$.normal.message.id')),json_extract(?,'$.normal.recovery.message.ordinal')))) WHERE effect_id=? AND state IN ('intent','prepared','indeterminate') AND json_extract(record_json,'$.recovery') IS NULL", - params: [text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy())], - query: false, - limits: limits() + params: Some([text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.projections_json.copy()), text(input.effect_id.copy())]), + query: Some(false), + limits: Some(limits()) }, { sql: "UPDATE runs SET updated_at_ms=? WHERE id=?", - params: [integer(input.now_ms.copy()), text(input.run_id.copy())], - query: false, - limits: limits() + params: Some([integer(input.now_ms.copy()), text(input.run_id.copy())]), + query: Some(false), + limits: Some(limits()) }, { sql: "DELETE FROM run_events WHERE run_id=? AND seq NOT IN (SELECT seq FROM run_events WHERE run_id=? ORDER BY seq DESC LIMIT ?)", - params: [text(input.run_id.copy()), text(input.run_id.copy()), integer(input.max_events.copy())], - query: false, - limits: limits() + params: Some([text(input.run_id.copy()), text(input.run_id.copy()), integer(input.max_events.copy())]), + query: Some(false), + limits: Some(limits()) }, { sql: "INSERT INTO run_retention(run_id,first_seq,high_water_seq,updated_at_ms) SELECT ?,COALESCE(MIN(seq),0),COALESCE(MAX(seq),0),? FROM run_events WHERE run_id=? ON CONFLICT(run_id) DO UPDATE SET first_seq=excluded.first_seq,high_water_seq=MAX(run_retention.high_water_seq,excluded.high_water_seq),updated_at_ms=excluded.updated_at_ms", - params: [text(input.run_id.copy()), integer(input.now_ms.copy()), text(input.run_id.copy())], - query: false, - limits: limits() + params: Some([text(input.run_id.copy()), integer(input.now_ms.copy()), text(input.run_id.copy())]), + query: Some(false), + limits: Some(limits()) }, { sql: "SELECT record_json FROM managed_effect_calls WHERE effect_id=?", - params: [text(input.effect_id.copy())], - query: true, - limits: limits() + params: Some([text(input.effect_id.copy())]), + query: Some(true), + limits: Some(limits()) } ]; let result: array = sqlite::transaction(&db, statements); let last: SqliteTransactionResult = result[result.length - 1].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok(query) } @@ -299,148 +320,160 @@ struct ManagedMaintenanceInput {maintenance_json:string,cut:string} pub fn checkpoint_context(db:resource,payload:string)->map { let input:ManagedStorageInput=json::decode::(payload); - ok(sqlite::query(&db,"SELECT json_object('workspace',json(w.record_json),'head',json_object('workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version,'store_id',b.store_id,'schema_version',b.schema_version,'process_lineage_tainted',json(CASE b.process_lineage_tainted WHEN 1 THEN 'true' ELSE 'false' END)),'session_id',r.session_id,'session_generation',s.generation,'last_ordinal',s.last_message_seq,'messages',json((SELECT json_group_array(json(row)) FROM (SELECT json_object('id',m.id,'ordinal',m.ordinal,'role',m.role,'content_json',m.content_json,'metadata_json',m.metadata_json,'run_id',NULLIF(m.run_id,''),'name',NULLIF(m.name,''),'tool_call_id',NULLIF(m.tool_call_id,''),'parent_message_id',NULLIF(m.parent_message_id,''),'finish_reason',NULLIF(m.finish_reason,''),'token_estimate',m.token_estimate,'compacted',json(CASE m.compacted WHEN 1 THEN 'true' ELSE 'false' END),'created_at_ms',m.created_at_ms) AS row FROM messages m WHERE m.session_id=r.session_id ORDER BY m.ordinal LIMIT 129))),'effects',json((SELECT json_group_array(json(record_json)) FROM (SELECT c.record_json FROM managed_effect_calls c JOIN runs cr ON cr.id=c.run_id WHERE cr.session_id=r.session_id ORDER BY c.effect_id LIMIT 129))),'external',json((SELECT json_group_array(json(row)) FROM (SELECT json_object('run_id',er.id,'event_seq',COALESCE(rt.high_water_seq,0),'provider_request_count',COALESCE((SELECT SUM(request_count) FROM provider_usage WHERE run_id=er.id),0),'input_tokens',er.input_tokens,'output_tokens',er.output_tokens) AS row FROM runs er LEFT JOIN run_retention rt ON rt.run_id=er.id WHERE er.session_id=r.session_id ORDER BY er.id LIMIT 33))),'source_runs',json((SELECT json_group_array(json(row)) FROM (SELECT json_object('run_id',sr.run_id,'session_id',sr.session_id,'status',rr.status,'original_deadline_ms',sr.original_deadline_ms) AS row FROM managed_run_bindings sr JOIN runs rr ON rr.id=sr.run_id WHERE sr.workspace_id=b.workspace_id AND sr.branch_id=b.branch_id ORDER BY sr.run_id LIMIT 129))),'unsettled_runs',(SELECT COUNT(*) FROM managed_run_bindings mb JOIN runs mr ON mr.id=mb.run_id WHERE mb.workspace_id=b.workspace_id AND mb.branch_id=b.branch_id AND mr.status NOT IN ('completed','failed','cancelled')),'pending_compactions',(SELECT COUNT(*) FROM compactions WHERE session_id=r.session_id AND state='pending'),'compaction',json((SELECT json_object('id',id,'generation',generation,'source_start_ordinal',source_start_ordinal,'source_end_ordinal',source_end_ordinal,'retained_tail_ordinal',retained_tail_ordinal,'summary_json',summary_json,'token_estimate',token_estimate,'model',model,'created_at_ms',created_at_ms,'completed_at_ms',completed_at_ms) FROM compactions WHERE session_id=r.session_id AND state='committed' ORDER BY generation DESC LIMIT 1))) AS record_json FROM managed_run_bindings rb JOIN runs r ON r.id=rb.run_id JOIN sessions s ON s.id=r.session_id JOIN managed_workspaces w ON w.workspace_id=rb.workspace_id JOIN managed_branches b ON b.workspace_id=rb.workspace_id AND b.branch_id=rb.branch_id WHERE rb.run_id=? AND rb.workspace_id=? AND rb.branch_id=? AND rb.profile=?",[text(input.run_id),text(input.workspace_id),text(input.branch_id),text(input.profile)],limits())) + let params: array = [text(input.run_id),text(input.workspace_id),text(input.branch_id),text(input.profile)]; + ok(sqlite::query(&db,"SELECT json_object('workspace',json(w.record_json),'head',json_object('workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version,'store_id',b.store_id,'schema_version',b.schema_version,'process_lineage_tainted',json(CASE b.process_lineage_tainted WHEN 1 THEN 'true' ELSE 'false' END)),'session_id',r.session_id,'session_generation',s.generation,'last_ordinal',s.last_message_seq,'messages',json((SELECT json_group_array(json(row)) FROM (SELECT json_object('id',m.id,'ordinal',m.ordinal,'role',m.role,'content_json',m.content_json,'metadata_json',m.metadata_json,'run_id',NULLIF(m.run_id,''),'name',NULLIF(m.name,''),'tool_call_id',NULLIF(m.tool_call_id,''),'parent_message_id',NULLIF(m.parent_message_id,''),'finish_reason',NULLIF(m.finish_reason,''),'token_estimate',m.token_estimate,'compacted',json(CASE m.compacted WHEN 1 THEN 'true' ELSE 'false' END),'created_at_ms',m.created_at_ms) AS row FROM messages m WHERE m.session_id=r.session_id ORDER BY m.ordinal LIMIT 129))),'effects',json((SELECT json_group_array(json(record_json)) FROM (SELECT c.record_json FROM managed_effect_calls c JOIN runs cr ON cr.id=c.run_id WHERE cr.session_id=r.session_id ORDER BY c.effect_id LIMIT 129))),'external',json((SELECT json_group_array(json(row)) FROM (SELECT json_object('run_id',er.id,'event_seq',COALESCE(rt.high_water_seq,0),'provider_request_count',COALESCE((SELECT SUM(request_count) FROM provider_usage WHERE run_id=er.id),0),'input_tokens',er.input_tokens,'output_tokens',er.output_tokens) AS row FROM runs er LEFT JOIN run_retention rt ON rt.run_id=er.id WHERE er.session_id=r.session_id ORDER BY er.id LIMIT 33))),'source_runs',json((SELECT json_group_array(json(row)) FROM (SELECT json_object('run_id',sr.run_id,'session_id',sr.session_id,'status',rr.status,'original_deadline_ms',sr.original_deadline_ms) AS row FROM managed_run_bindings sr JOIN runs rr ON rr.id=sr.run_id WHERE sr.workspace_id=b.workspace_id AND sr.branch_id=b.branch_id ORDER BY sr.run_id LIMIT 129))),'unsettled_runs',(SELECT COUNT(*) FROM managed_run_bindings mb JOIN runs mr ON mr.id=mb.run_id WHERE mb.workspace_id=b.workspace_id AND mb.branch_id=b.branch_id AND mr.status NOT IN ('completed','failed','cancelled')),'pending_compactions',(SELECT COUNT(*) FROM compactions WHERE session_id=r.session_id AND state='pending'),'compaction',json((SELECT json_object('id',id,'generation',generation,'source_start_ordinal',source_start_ordinal,'source_end_ordinal',source_end_ordinal,'retained_tail_ordinal',retained_tail_ordinal,'summary_json',summary_json,'token_estimate',token_estimate,'model',model,'created_at_ms',created_at_ms,'completed_at_ms',completed_at_ms) FROM compactions WHERE session_id=r.session_id AND state='committed' ORDER BY generation DESC LIMIT 1))) AS record_json FROM managed_run_bindings rb JOIN runs r ON r.id=rb.run_id JOIN sessions s ON s.id=r.session_id JOIN managed_workspaces w ON w.workspace_id=rb.workspace_id JOIN managed_branches b ON b.workspace_id=rb.workspace_id AND b.branch_id=rb.branch_id WHERE rb.run_id=? AND rb.workspace_id=? AND rb.branch_id=? AND rb.profile=?",params,limits())) } - struct ManagedRestoreStorageInput {restore_json:string} pub fn restore_before_admission(payload:string, graph_id:string, ref_id:string, run_id:string)->array { let input:ManagedRestoreStorageInput=json::decode::(payload); [ -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (EXISTS(SELECT 1 FROM compactions c JOIN managed_run_bindings b ON b.session_id=c.session_id WHERE b.workspace_id=json_extract(?,'$.request.workspace_id') AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND c.state='pending')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.current_branch_id=b.branch_id WHERE b.store_id=json_extract(?,'$.request.expected_source.store_id') AND b.workspace_id=json_extract(?,'$.request.workspace_id') AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND b.epoch=json_extract(?,'$.request.expected_source.epoch') AND b.head_object_id=json_extract(?,'$.request.expected_source.head_object_id') AND b.head_version=json_extract(?,'$.request.expected_source.head_version') AND b.status='active' AND b.process_lineage_tainted=0)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (EXISTS(SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.workspace_id=json_extract(?,'$.request.workspace_id') AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND r.status NOT IN ('completed','failed','cancelled')) OR (SELECT COUNT(*) FROM managed_restore_receipts WHERE workspace_id=json_extract(?,'$.request.workspace_id'))>=64) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM managed_checkpoints c JOIN plugin_graph_refs g ON g.owner_kind='checkpoint' AND g.owner_id=c.checkpoint_id WHERE c.checkpoint_id=json_extract(?,'$.request.checkpoint_id') AND c.workspace_id=json_extract(?,'$.request.workspace_id') AND json_extract(c.receipt_json,'$.checkpoint.frozen_root.object_id')=json_extract(?,'$.result.new_branch.head_object_id') AND g.ref_id=json_extract(?,'$.result.graph.checkpoint_graph_ref_id'))) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"UPDATE managed_branches SET epoch=json_extract(?,'$.result.source_after.epoch'),head_version=json_extract(?,'$.result.source_after.head_version'),status='retired' WHERE (workspace_id=json_extract(?,'$.request.workspace_id') AND branch_id=json_extract(?,'$.request.source_branch_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT workspace_id,branch_id,head_version,epoch,NULL,head_object_id,head_object_id,1 FROM managed_branches WHERE (workspace_id=json_extract(?,'$.request.workspace_id') AND branch_id=json_extract(?,'$.request.source_branch_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_branches(workspace_id,branch_id,epoch,head_object_id,head_version,store_id,schema_version,status,lineage_json) SELECT json_extract(?,'$.result.new_branch.workspace_id'),json_extract(?,'$.result.new_branch.branch_id'),json_extract(?,'$.result.new_branch.epoch'),json_extract(?,'$.result.new_branch.head_object_id'),1,json_extract(?,'$.result.new_branch.store_id'),1,'active',json_extract(?,'$.result') WHERE (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT workspace_id,branch_id,head_version,epoch,NULL,NULL,head_object_id,1 FROM managed_branches WHERE (workspace_id=json_extract(?,'$.request.workspace_id') AND branch_id=json_extract(?,'$.result.new_branch.branch_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"UPDATE managed_workspaces SET current_branch_id=json_extract(?,'$.result.new_branch.branch_id') WHERE (workspace_id=json_extract(?,'$.request.workspace_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()} +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (EXISTS(SELECT 1 FROM compactions c JOIN managed_run_bindings b ON b.session_id=c.session_id WHERE b.workspace_id=json_extract(?,'$.request.workspace_id') AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND c.state='pending')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.current_branch_id=b.branch_id WHERE b.store_id=json_extract(?,'$.request.expected_source.store_id') AND b.workspace_id=json_extract(?,'$.request.workspace_id') AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND b.epoch=json_extract(?,'$.request.expected_source.epoch') AND b.head_object_id=json_extract(?,'$.request.expected_source.head_object_id') AND b.head_version=json_extract(?,'$.request.expected_source.head_version') AND b.status='active' AND b.process_lineage_tainted=0)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (EXISTS(SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.workspace_id=json_extract(?,'$.request.workspace_id') AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND r.status NOT IN ('completed','failed','cancelled')) OR (SELECT COUNT(*) FROM managed_restore_receipts WHERE workspace_id=json_extract(?,'$.request.workspace_id'))>=64) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM managed_checkpoints c JOIN plugin_graph_refs g ON g.owner_kind='checkpoint' AND g.owner_id=c.checkpoint_id WHERE c.checkpoint_id=json_extract(?,'$.request.checkpoint_id') AND c.workspace_id=json_extract(?,'$.request.workspace_id') AND json_extract(c.receipt_json,'$.checkpoint.frozen_root.object_id')=json_extract(?,'$.result.new_branch.head_object_id') AND g.ref_id=json_extract(?,'$.result.graph.checkpoint_graph_ref_id'))) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"UPDATE managed_branches SET epoch=json_extract(?,'$.result.source_after.epoch'),head_version=json_extract(?,'$.result.source_after.head_version'),status='retired' WHERE (workspace_id=json_extract(?,'$.request.workspace_id') AND branch_id=json_extract(?,'$.request.source_branch_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT workspace_id,branch_id,head_version,epoch,NULL,head_object_id,head_object_id,1 FROM managed_branches WHERE (workspace_id=json_extract(?,'$.request.workspace_id') AND branch_id=json_extract(?,'$.request.source_branch_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_branches(workspace_id,branch_id,epoch,head_object_id,head_version,store_id,schema_version,status,lineage_json) SELECT json_extract(?,'$.result.new_branch.workspace_id'),json_extract(?,'$.result.new_branch.branch_id'),json_extract(?,'$.result.new_branch.epoch'),json_extract(?,'$.result.new_branch.head_object_id'),1,json_extract(?,'$.result.new_branch.store_id'),1,'active',json_extract(?,'$.result') WHERE (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT workspace_id,branch_id,head_version,epoch,NULL,NULL,head_object_id,1 FROM managed_branches WHERE (workspace_id=json_extract(?,'$.request.workspace_id') AND branch_id=json_extract(?,'$.result.new_branch.branch_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"UPDATE managed_workspaces SET current_branch_id=json_extract(?,'$.result.new_branch.branch_id') WHERE (workspace_id=json_extract(?,'$.request.workspace_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())} ] } pub fn restore_history_statements(payload:string, graph_id:string, ref_id:string, run_id:string)->array { let input:ManagedRestoreStorageInput=json::decode::(payload); [ -{sql:"INSERT INTO messages(id,session_id,ordinal,role,content_json,metadata_json,run_id,name,tool_call_id,parent_message_id,token_estimate,finish_reason,created_at_ms,compacted) SELECT 'restored-'||json_extract(?,'$.result.new_session_id')||'-'||h.ordinal,json_extract(?,'$.result.new_session_id'),h.ordinal,json_extract(h.record_json,'$.role'),json_extract(h.record_json,'$.content_json'),json_object('origin',json_object('checkpoint_id',json_extract(?,'$.request.checkpoint_id'),'source_message_id',h.source_message_id)),'',COALESCE(json_extract(h.record_json,'$.name'),''),COALESCE(json_extract(h.record_json,'$.tool_call_id'),''),CASE WHEN COALESCE(json_extract(h.record_json,'$.parent_message_id'),'')='' THEN '' ELSE 'restored-'||json_extract(?,'$.result.new_session_id')||'-'||(SELECT p.ordinal FROM managed_checkpoint_history p WHERE p.history_root_id=h.history_root_id AND p.source_message_id=json_extract(h.record_json,'$.parent_message_id')) END,COALESCE(json_extract(h.record_json,'$.token_estimate'),0),COALESCE(json_extract(h.record_json,'$.finish_reason'),''),json_extract(h.record_json,'$.created_at_ms'),json_extract(h.record_json,'$.compacted') FROM managed_checkpoint_history h WHERE (h.history_root_id=json_extract(?,'$.history_root_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ORDER BY h.ordinal",params:[text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()} +{sql:"INSERT INTO messages(id,session_id,ordinal,role,content_json,metadata_json,run_id,name,tool_call_id,parent_message_id,token_estimate,finish_reason,created_at_ms,compacted) SELECT 'restored-'||json_extract(?,'$.result.new_session_id')||'-'||h.ordinal,json_extract(?,'$.result.new_session_id'),h.ordinal,json_extract(h.record_json,'$.role'),json_extract(h.record_json,'$.content_json'),json_object('origin',json_object('checkpoint_id',json_extract(?,'$.request.checkpoint_id'),'source_message_id',h.source_message_id)),'',COALESCE(json_extract(h.record_json,'$.name'),''),COALESCE(json_extract(h.record_json,'$.tool_call_id'),''),CASE WHEN COALESCE(json_extract(h.record_json,'$.parent_message_id'),'')='' THEN '' ELSE 'restored-'||json_extract(?,'$.result.new_session_id')||'-'||(SELECT p.ordinal FROM managed_checkpoint_history p WHERE p.history_root_id=h.history_root_id AND p.source_message_id=json_extract(h.record_json,'$.parent_message_id')) END,COALESCE(json_extract(h.record_json,'$.token_estimate'),0),COALESCE(json_extract(h.record_json,'$.finish_reason'),''),json_extract(h.record_json,'$.created_at_ms'),json_extract(h.record_json,'$.compacted') FROM managed_checkpoint_history h WHERE (h.history_root_id=json_extract(?,'$.history_root_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ORDER BY h.ordinal",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())} ] } pub fn restore_receipt_statements(payload:string, graph_id:string, ref_id:string, run_id:string)->array { let input:ManagedRestoreStorageInput=json::decode::(payload); [ -{sql:"INSERT INTO compactions(id,session_id,run_id,generation,source_start_ordinal,source_end_ordinal,retained_tail_ordinal,summary_json,token_estimate,model,state,created_at_ms,completed_at_ms) SELECT 'restored-'||json_extract(?,'$.result.new_session_id'),json_extract(?,'$.result.new_session_id'),json_extract(?,'$.result.new_run_id'),json_extract(c.compaction_json,'$.generation'),json_extract(c.compaction_json,'$.source_start_ordinal'),json_extract(c.compaction_json,'$.source_end_ordinal'),json_extract(c.compaction_json,'$.retained_tail_ordinal'),json_extract(c.compaction_json,'$.summary_json'),json_extract(c.compaction_json,'$.token_estimate'),json_extract(c.compaction_json,'$.model'),'committed',json_extract(c.compaction_json,'$.created_at_ms'),json_extract(c.compaction_json,'$.completed_at_ms') FROM managed_checkpoints c WHERE (c.checkpoint_id=json_extract(?,'$.request.checkpoint_id') AND json_type(c.compaction_json)='object') AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"UPDATE sessions SET generation=(SELECT json_extract(c.receipt_json,'$.checkpoint.history.session_generation') FROM managed_checkpoints c WHERE c.checkpoint_id=json_extract(?,'$.request.checkpoint_id')) WHERE (id=json_extract(?,'$.result.new_session_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_restore_receipts(workspace_id,request_id,checkpoint_id,original_request_proof,envelope_json,result_json) SELECT json_extract(?,'$.request.workspace_id'),json_extract(?,'$.request.request_id'),json_extract(?,'$.request.checkpoint_id'),json_extract(?,'$.original_request_proof'),?,json_extract(?,'$.result') WHERE (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())],query:false,limits:limits()} +{sql:"INSERT INTO compactions(id,session_id,run_id,generation,source_start_ordinal,source_end_ordinal,retained_tail_ordinal,summary_json,token_estimate,model,state,created_at_ms,completed_at_ms) SELECT 'restored-'||json_extract(?,'$.result.new_session_id'),json_extract(?,'$.result.new_session_id'),json_extract(?,'$.result.new_run_id'),json_extract(c.compaction_json,'$.generation'),json_extract(c.compaction_json,'$.source_start_ordinal'),json_extract(c.compaction_json,'$.source_end_ordinal'),json_extract(c.compaction_json,'$.retained_tail_ordinal'),json_extract(c.compaction_json,'$.summary_json'),json_extract(c.compaction_json,'$.token_estimate'),json_extract(c.compaction_json,'$.model'),'committed',json_extract(c.compaction_json,'$.created_at_ms'),json_extract(c.compaction_json,'$.completed_at_ms') FROM managed_checkpoints c WHERE (c.checkpoint_id=json_extract(?,'$.request.checkpoint_id') AND json_type(c.compaction_json)='object') AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"UPDATE sessions SET generation=(SELECT json_extract(c.receipt_json,'$.checkpoint.history.session_generation') FROM managed_checkpoints c WHERE c.checkpoint_id=json_extract(?,'$.request.checkpoint_id')) WHERE (id=json_extract(?,'$.result.new_session_id')) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_restore_receipts(workspace_id,request_id,checkpoint_id,original_request_proof,envelope_json,result_json) SELECT json_extract(?,'$.request.workspace_id'),json_extract(?,'$.request.request_id'),json_extract(?,'$.request.checkpoint_id'),json_extract(?,'$.original_request_proof'),?,json_extract(?,'$.result') WHERE (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()),text(input.restore_json.copy()), text(graph_id.copy()), text(graph_id.copy()), text(run_id.copy()), text(graph_id.copy()), text(ref_id.copy())]),query: Some(false),limits: Some(limits())} ] } pub fn maintenance_receipt(db:resource,payload:string)->map { let input:ManagedStorageInput=json::decode::(payload); if input.cut == "checkpoint" => { - ok(sqlite::query(&db,"SELECT json_object('original_request_proof',original_request_proof,'receipt',json(receipt_json)) AS record_json FROM managed_checkpoints WHERE workspace_id=? AND request_id=?",[text(input.workspace_id),text(input.receipt_id)],limits())) + let params: array = [text(input.workspace_id),text(input.receipt_id)]; + ok(sqlite::query(&db,"SELECT json_object('original_request_proof',original_request_proof,'receipt',json(receipt_json)) AS record_json FROM managed_checkpoints WHERE workspace_id=? AND request_id=?",params,limits())) } else => { - ok(sqlite::query(&db,"SELECT json_object('original_request_proof',r.original_request_proof,'envelope',json(r.envelope_json),'checkpoint_graph',(SELECT json_extract(c.receipt_json,'$.checkpoint.graph') FROM managed_checkpoints c WHERE c.workspace_id=r.workspace_id AND c.checkpoint_id=r.checkpoint_id)) AS record_json FROM managed_restore_receipts r WHERE r.workspace_id=? AND r.request_id=?",[text(input.workspace_id),text(input.receipt_id)],limits())) + let params: array = [text(input.workspace_id),text(input.receipt_id)]; + ok(sqlite::query(&db,"SELECT json_object('original_request_proof',r.original_request_proof,'envelope',json(r.envelope_json),'checkpoint_graph',(SELECT json_extract(c.receipt_json,'$.checkpoint.graph') FROM managed_checkpoints c WHERE c.workspace_id=r.workspace_id AND c.checkpoint_id=r.checkpoint_id)) AS record_json FROM managed_restore_receipts r WHERE r.workspace_id=? AND r.request_id=?",params,limits())) } } pub fn branch_publication_history(db:resource,payload:string)->map { let input:ManagedStorageInput=json::decode::(payload); if input.cut.copy() == "checkpoint_rollback" => { - ok(sqlite::query(&db,"SELECT json_object('source',json_object('store_id',b.store_id,'workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version),'current_branch_id',w.current_branch_id,'target_rows',(SELECT COUNT(*) FROM managed_checkpoints WHERE checkpoint_id=json_extract(?,'$.receipt.checkpoint.checkpoint_id') OR (workspace_id=b.workspace_id AND request_id=json_extract(?,'$.request.request_id')))+(SELECT COUNT(*) FROM managed_checkpoint_history WHERE history_root_id=json_extract(?,'$.receipt.checkpoint.history.history_root_id'))+(SELECT COUNT(*) FROM managed_checkpoint_public_configs WHERE config_ref_id=json_extract(?,'$.receipt.checkpoint.public_config.config_ref_id'))+(SELECT COUNT(*) FROM managed_checkpoint_refs WHERE checkpoint_id=json_extract(?,'$.receipt.checkpoint.checkpoint_id'))+(SELECT COUNT(*) FROM plugin_graph_refs WHERE ref_id=json_extract(?,'$.receipt.checkpoint.graph.checkpoint_graph_ref_id'))) AS record_json FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE b.workspace_id=? AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND b.status='active'",[text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.workspace_id.copy()),text(input.record_json.copy())],limits())) + let params: array = [text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.workspace_id.copy()),text(input.record_json.copy())]; + ok(sqlite::query(&db,"SELECT json_object('source',json_object('store_id',b.store_id,'workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version),'current_branch_id',w.current_branch_id,'target_rows',(SELECT COUNT(*) FROM managed_checkpoints WHERE checkpoint_id=json_extract(?,'$.receipt.checkpoint.checkpoint_id') OR (workspace_id=b.workspace_id AND request_id=json_extract(?,'$.request.request_id')))+(SELECT COUNT(*) FROM managed_checkpoint_history WHERE history_root_id=json_extract(?,'$.receipt.checkpoint.history.history_root_id'))+(SELECT COUNT(*) FROM managed_checkpoint_public_configs WHERE config_ref_id=json_extract(?,'$.receipt.checkpoint.public_config.config_ref_id'))+(SELECT COUNT(*) FROM managed_checkpoint_refs WHERE checkpoint_id=json_extract(?,'$.receipt.checkpoint.checkpoint_id'))+(SELECT COUNT(*) FROM plugin_graph_refs WHERE ref_id=json_extract(?,'$.receipt.checkpoint.graph.checkpoint_graph_ref_id'))) AS record_json FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE b.workspace_id=? AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND b.status='active'",params,limits())) } else => { if input.cut.copy() == "rollback" => { - ok(sqlite::query(&db,"SELECT json_object('source',json_object('store_id',b.store_id,'workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version),'current_branch_id',w.current_branch_id,'target_rows',(SELECT COUNT(*) FROM managed_branches WHERE branch_id=json_extract(?,'$.result.new_branch.branch_id'))+(SELECT COUNT(*) FROM sessions WHERE id=json_extract(?,'$.result.new_session_id'))+(SELECT COUNT(*) FROM runs WHERE id=json_extract(?,'$.result.new_run_id'))+(SELECT COUNT(*) FROM messages WHERE session_id=json_extract(?,'$.result.new_session_id'))+(SELECT COUNT(*) FROM plugin_graph_refs WHERE ref_id=json_extract(?,'$.admission_pin.ref_id'))+(SELECT COUNT(*) FROM managed_restore_receipts WHERE workspace_id=b.workspace_id AND request_id=json_extract(?,'$.request.request_id'))+(SELECT COUNT(*) FROM managed_head_history WHERE workspace_id=b.workspace_id AND ((branch_id=b.branch_id AND head_version>json_extract(?,'$.result.source_before.head_version')) OR branch_id=json_extract(?,'$.result.new_branch.branch_id')))+(SELECT COUNT(*) FROM managed_run_bindings WHERE run_id=json_extract(?,'$.result.new_run_id'))+(SELECT COUNT(*) FROM run_events WHERE run_id=json_extract(?,'$.result.new_run_id'))+(SELECT COUNT(*) FROM idempotency_records WHERE resource_id=json_extract(?,'$.result.new_run_id'))+(SELECT COUNT(*) FROM run_retention WHERE run_id=json_extract(?,'$.result.new_run_id'))) AS record_json FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE b.workspace_id=? AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND b.status='active'",[text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.workspace_id.copy()),text(input.record_json.copy())],limits())) + let params: array = [text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.record_json.copy()),text(input.workspace_id.copy()),text(input.record_json.copy())]; + ok(sqlite::query(&db,"SELECT json_object('source',json_object('store_id',b.store_id,'workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version),'current_branch_id',w.current_branch_id,'target_rows',(SELECT COUNT(*) FROM managed_branches WHERE branch_id=json_extract(?,'$.result.new_branch.branch_id'))+(SELECT COUNT(*) FROM sessions WHERE id=json_extract(?,'$.result.new_session_id'))+(SELECT COUNT(*) FROM runs WHERE id=json_extract(?,'$.result.new_run_id'))+(SELECT COUNT(*) FROM messages WHERE session_id=json_extract(?,'$.result.new_session_id'))+(SELECT COUNT(*) FROM plugin_graph_refs WHERE ref_id=json_extract(?,'$.admission_pin.ref_id'))+(SELECT COUNT(*) FROM managed_restore_receipts WHERE workspace_id=b.workspace_id AND request_id=json_extract(?,'$.request.request_id'))+(SELECT COUNT(*) FROM managed_head_history WHERE workspace_id=b.workspace_id AND ((branch_id=b.branch_id AND head_version>json_extract(?,'$.result.source_before.head_version')) OR branch_id=json_extract(?,'$.result.new_branch.branch_id')))+(SELECT COUNT(*) FROM managed_run_bindings WHERE run_id=json_extract(?,'$.result.new_run_id'))+(SELECT COUNT(*) FROM run_events WHERE run_id=json_extract(?,'$.result.new_run_id'))+(SELECT COUNT(*) FROM idempotency_records WHERE resource_id=json_extract(?,'$.result.new_run_id'))+(SELECT COUNT(*) FROM run_retention WHERE run_id=json_extract(?,'$.result.new_run_id'))) AS record_json FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE b.workspace_id=? AND b.branch_id=json_extract(?,'$.request.source_branch_id') AND b.status='active'",params,limits())) } else => { - ok(sqlite::query(&db,"SELECT json_object('source',json_object('store_id',s.store_id,'workspace_id',s.workspace_id,'branch_id',s.branch_id,'epoch',s.epoch,'head_object_id',s.head_object_id,'head_version',s.head_version),'current',json_object('store_id',n.store_id,'workspace_id',n.workspace_id,'branch_id',n.branch_id,'epoch',n.epoch,'head_object_id',n.head_object_id,'head_version',n.head_version),'history_count',(SELECT COUNT(*) FROM managed_head_history h WHERE h.workspace_id=r.workspace_id AND ((h.branch_id=s.branch_id AND h.epoch=s.epoch AND h.head_version=s.head_version AND h.head_object_id=s.head_object_id) OR (h.branch_id=n.branch_id AND h.epoch=n.epoch AND h.head_version=1 AND h.head_object_id=n.head_object_id))),'admission_pin',json_extract(r.envelope_json,'$.admission_pin'),'graph',json_extract(r.envelope_json,'$.result.graph')) AS record_json FROM managed_restore_receipts r JOIN managed_branches s ON s.workspace_id=r.workspace_id AND s.branch_id=json_extract(r.envelope_json,'$.request.source_branch_id') AND s.status='retired' JOIN managed_branches n ON n.workspace_id=r.workspace_id AND n.branch_id=json_extract(r.result_json,'$.new_branch.branch_id') AND n.status='active' JOIN managed_workspaces w ON w.workspace_id=r.workspace_id AND w.current_branch_id=n.branch_id JOIN runs a ON a.id=json_extract(r.result_json,'$.new_run_id') AND a.session_id=json_extract(r.result_json,'$.new_session_id') JOIN managed_run_bindings b ON b.run_id=a.id AND b.workspace_id=n.workspace_id AND b.branch_id=n.branch_id AND b.epoch=n.epoch AND b.original_deadline_ms=json_extract(r.result_json,'$.new_run_deadline_at_ms') JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=a.id AND g.ref_id=b.graph_ref_id AND g.ref_id=json_extract(r.envelope_json,'$.admission_pin.ref_id') AND g.graph_id=json_extract(r.envelope_json,'$.admission_pin.graph_id') JOIN managed_checkpoints c ON c.checkpoint_id=r.checkpoint_id AND c.workspace_id=r.workspace_id AND json_extract(c.receipt_json,'$.checkpoint.frozen_root.object_id')=n.head_object_id WHERE r.workspace_id=? AND r.request_id=?",[text(input.workspace_id),text(input.receipt_id)],limits())) + let params: array = [text(input.workspace_id),text(input.receipt_id)]; + ok(sqlite::query(&db,"SELECT json_object('source',json_object('store_id',s.store_id,'workspace_id',s.workspace_id,'branch_id',s.branch_id,'epoch',s.epoch,'head_object_id',s.head_object_id,'head_version',s.head_version),'current',json_object('store_id',n.store_id,'workspace_id',n.workspace_id,'branch_id',n.branch_id,'epoch',n.epoch,'head_object_id',n.head_object_id,'head_version',n.head_version),'history_count',(SELECT COUNT(*) FROM managed_head_history h WHERE h.workspace_id=r.workspace_id AND ((h.branch_id=s.branch_id AND h.epoch=s.epoch AND h.head_version=s.head_version AND h.head_object_id=s.head_object_id) OR (h.branch_id=n.branch_id AND h.epoch=n.epoch AND h.head_version=1 AND h.head_object_id=n.head_object_id))),'admission_pin',json_extract(r.envelope_json,'$.admission_pin'),'graph',json_extract(r.envelope_json,'$.result.graph')) AS record_json FROM managed_restore_receipts r JOIN managed_branches s ON s.workspace_id=r.workspace_id AND s.branch_id=json_extract(r.envelope_json,'$.request.source_branch_id') AND s.status='retired' JOIN managed_branches n ON n.workspace_id=r.workspace_id AND n.branch_id=json_extract(r.result_json,'$.new_branch.branch_id') AND n.status='active' JOIN managed_workspaces w ON w.workspace_id=r.workspace_id AND w.current_branch_id=n.branch_id JOIN runs a ON a.id=json_extract(r.result_json,'$.new_run_id') AND a.session_id=json_extract(r.result_json,'$.new_session_id') JOIN managed_run_bindings b ON b.run_id=a.id AND b.workspace_id=n.workspace_id AND b.branch_id=n.branch_id AND b.epoch=n.epoch AND b.original_deadline_ms=json_extract(r.result_json,'$.new_run_deadline_at_ms') JOIN plugin_graph_refs g ON g.owner_kind='run' AND g.owner_id=a.id AND g.ref_id=b.graph_ref_id AND g.ref_id=json_extract(r.envelope_json,'$.admission_pin.ref_id') AND g.graph_id=json_extract(r.envelope_json,'$.admission_pin.graph_id') JOIN managed_checkpoints c ON c.checkpoint_id=r.checkpoint_id AND c.workspace_id=r.workspace_id AND json_extract(c.receipt_json,'$.checkpoint.frozen_root.object_id')=n.head_object_id WHERE r.workspace_id=? AND r.request_id=?",params,limits())) } } } pub fn checkpoint_get(db:resource,payload:string)->map { let input:ManagedStorageInput=json::decode::(payload); - ok(sqlite::query(&db,"SELECT json_object('checkpoint',json_extract(c.receipt_json,'$.checkpoint'),'compaction',json(c.compaction_json),'execution_config',json(c.execution_config_json),'messages',json((SELECT json_group_array(json(record_json)) FROM (SELECT h.record_json FROM managed_checkpoint_history h WHERE h.history_root_id=json_extract(c.receipt_json,'$.checkpoint.history.history_root_id') ORDER BY h.ordinal LIMIT 129)))) AS record_json FROM managed_checkpoints c WHERE c.workspace_id=? AND c.checkpoint_id=?",[text(input.workspace_id),text(input.commit_id)],limits())) + let params: array = [text(input.workspace_id),text(input.commit_id)]; + ok(sqlite::query(&db,"SELECT json_object('checkpoint',json_extract(c.receipt_json,'$.checkpoint'),'compaction',json(c.compaction_json),'execution_config',json(c.execution_config_json),'messages',json((SELECT json_group_array(json(record_json)) FROM (SELECT h.record_json FROM managed_checkpoint_history h WHERE h.history_root_id=json_extract(c.receipt_json,'$.checkpoint.history.history_root_id') ORDER BY h.ordinal LIMIT 129)))) AS record_json FROM managed_checkpoints c WHERE c.workspace_id=? AND c.checkpoint_id=?",params,limits())) } pub fn checkpoint_create(db:resource,payload:string)->map { let input:ManagedMaintenanceInput=json::decode::(payload); let mut statements:array = [ -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS(SELECT 1 FROM compactions WHERE session_id=json_extract(?,'$.receipt.checkpoint.history.source_session_id') AND state='pending')",params:[text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.captured_source.workspace_id') AND branch_id=json_extract(?,'$.captured_source.branch_id') AND store_id=json_extract(?,'$.captured_source.store_id') AND epoch=json_extract(?,'$.captured_source.epoch') AND head_object_id=json_extract(?,'$.captured_source.head_object_id') AND head_version=json_extract(?,'$.captured_source.head_version') AND process_lineage_tainted=0)",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS(SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.request.workspace_id') AND state IN ('intent','prepared','indeterminate'))",params:[text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM sessions WHERE id=json_extract(?,'$.receipt.checkpoint.history.source_session_id') AND generation=json_extract(?,'$.receipt.checkpoint.history.session_generation') AND last_message_seq=json_extract(?,'$.receipt.checkpoint.history.last_ordinal')) OR (SELECT COUNT(*) FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.request.workspace_id'))>=64",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_graph_refs WHERE owner_kind='run' AND owner_id=json_extract(?,'$.request.source_run_id') AND graph_id=json_extract(?,'$.receipt.checkpoint.graph.graph_id'))",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_checkpoints(checkpoint_id,workspace_id,request_id,original_request_proof,receipt_json,compaction_json,execution_config_json) SELECT json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(?,'$.request.workspace_id'),json_extract(?,'$.request.request_id'),json_extract(?,'$.original_request_proof'),json_extract(?,'$.receipt'),COALESCE(json_extract(?,'$.compaction'),'null'),json_extract(?,'$.execution_config')",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_checkpoint_history(history_root_id,ordinal,source_message_id,record_json) SELECT json_extract(?,'$.receipt.checkpoint.history.history_root_id'),json_extract(value,'$.ordinal'),json_extract(value,'$.id'),value FROM json_each(json_extract(?,'$.messages'))",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (SELECT COUNT(*) FROM managed_checkpoint_history h JOIN messages m ON m.id=h.source_message_id WHERE h.history_root_id=json_extract(?,'$.receipt.checkpoint.history.history_root_id') AND m.content_json=json_extract(h.record_json,'$.content_json') AND m.metadata_json=json_extract(h.record_json,'$.metadata_json') AND m.ordinal=h.ordinal)<>json_extract(?,'$.receipt.checkpoint.history.message_count')",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_checkpoint_public_configs(config_ref_id,checkpoint_id,record_json) SELECT json_extract(?,'$.receipt.checkpoint.public_config.config_ref_id'),json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(?,'$.receipt.checkpoint.public_config.config')",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_checkpoint_refs(checkpoint_id,store_id,object_id) SELECT json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(value,'$.store_id'),json_extract(value,'$.object_id') FROM json_each(json_array(json_extract(?,'$.receipt.checkpoint.frozen_root'))) UNION SELECT json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(value,'$.result.store_id'),json_extract(value,'$.result.object_id') FROM json_each(json_extract(?,'$.receipt.checkpoint.history.closed_output_refs'))",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS(SELECT 1 FROM managed_checkpoint_refs c LEFT JOIN managed_objects o ON o.store_id=c.store_id AND o.object_id=c.object_id WHERE c.checkpoint_id=json_extract(?,'$.receipt.checkpoint.checkpoint_id') AND o.object_id IS NULL)",params:[text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"INSERT INTO plugin_graph_refs(graph_id,owner_kind,owner_id,ref_id,retained_at_ms) SELECT json_extract(?,'$.receipt.checkpoint.graph.graph_id'),'checkpoint',json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(?,'$.receipt.checkpoint.graph.checkpoint_graph_ref_id'),json_extract(?,'$.receipt.committed_at_ms')",params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())],query:false,limits:limits()}, -{sql:"SELECT receipt_json AS record_json FROM managed_checkpoints WHERE checkpoint_id=json_extract(?,'$.receipt.checkpoint.checkpoint_id')",params:[text(input.maintenance_json.copy())],query:true,limits:limits()} +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS(SELECT 1 FROM compactions WHERE session_id=json_extract(?,'$.receipt.checkpoint.history.source_session_id') AND state='pending')",params: Some([text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.captured_source.workspace_id') AND branch_id=json_extract(?,'$.captured_source.branch_id') AND store_id=json_extract(?,'$.captured_source.store_id') AND epoch=json_extract(?,'$.captured_source.epoch') AND head_object_id=json_extract(?,'$.captured_source.head_object_id') AND head_version=json_extract(?,'$.captured_source.head_version') AND process_lineage_tainted=0)",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS(SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.request.workspace_id') AND state IN ('intent','prepared','indeterminate'))",params: Some([text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM sessions WHERE id=json_extract(?,'$.receipt.checkpoint.history.source_session_id') AND generation=json_extract(?,'$.receipt.checkpoint.history.session_generation') AND last_message_seq=json_extract(?,'$.receipt.checkpoint.history.last_ordinal')) OR (SELECT COUNT(*) FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.request.workspace_id'))>=64",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_graph_refs WHERE owner_kind='run' AND owner_id=json_extract(?,'$.request.source_run_id') AND graph_id=json_extract(?,'$.receipt.checkpoint.graph.graph_id'))",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_checkpoints(checkpoint_id,workspace_id,request_id,original_request_proof,receipt_json,compaction_json,execution_config_json) SELECT json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(?,'$.request.workspace_id'),json_extract(?,'$.request.request_id'),json_extract(?,'$.original_request_proof'),json_extract(?,'$.receipt'),COALESCE(json_extract(?,'$.compaction'),'null'),json_extract(?,'$.execution_config')",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_checkpoint_history(history_root_id,ordinal,source_message_id,record_json) SELECT json_extract(?,'$.receipt.checkpoint.history.history_root_id'),json_extract(value,'$.ordinal'),json_extract(value,'$.id'),value FROM json_each(json_extract(?,'$.messages'))",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (SELECT COUNT(*) FROM managed_checkpoint_history h JOIN messages m ON m.id=h.source_message_id WHERE h.history_root_id=json_extract(?,'$.receipt.checkpoint.history.history_root_id') AND m.content_json=json_extract(h.record_json,'$.content_json') AND m.metadata_json=json_extract(h.record_json,'$.metadata_json') AND m.ordinal=h.ordinal)<>json_extract(?,'$.receipt.checkpoint.history.message_count')",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_checkpoint_public_configs(config_ref_id,checkpoint_id,record_json) SELECT json_extract(?,'$.receipt.checkpoint.public_config.config_ref_id'),json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(?,'$.receipt.checkpoint.public_config.config')",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_checkpoint_refs(checkpoint_id,store_id,object_id) SELECT json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(value,'$.store_id'),json_extract(value,'$.object_id') FROM json_each(json_array(json_extract(?,'$.receipt.checkpoint.frozen_root'))) UNION SELECT json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(value,'$.result.store_id'),json_extract(value,'$.result.object_id') FROM json_each(json_extract(?,'$.receipt.checkpoint.history.closed_output_refs'))",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS(SELECT 1 FROM managed_checkpoint_refs c LEFT JOIN managed_objects o ON o.store_id=c.store_id AND o.object_id=c.object_id WHERE c.checkpoint_id=json_extract(?,'$.receipt.checkpoint.checkpoint_id') AND o.object_id IS NULL)",params: Some([text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"INSERT INTO plugin_graph_refs(graph_id,owner_kind,owner_id,ref_id,retained_at_ms) SELECT json_extract(?,'$.receipt.checkpoint.graph.graph_id'),'checkpoint',json_extract(?,'$.receipt.checkpoint.checkpoint_id'),json_extract(?,'$.receipt.checkpoint.graph.checkpoint_graph_ref_id'),json_extract(?,'$.receipt.committed_at_ms')",params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]),query: Some(false),limits: Some(limits())}, +{sql:"SELECT receipt_json AS record_json FROM managed_checkpoints WHERE checkpoint_id=json_extract(?,'$.receipt.checkpoint.checkpoint_id')",params: Some([text(input.maintenance_json.copy())]),query: Some(true),limits: Some(limits())} ]; if input.cut == "managed_rollback" { - statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) VALUES(2)",params:[],query:false,limits:limits()}; + statements[statements.length] = {sql:"INSERT INTO managed_effect_guard(id) VALUES(2)",params: no_params(),query: Some(false),limits: Some(limits())}; } let result:array = sqlite::transaction(&db,statements); let last:SqliteTransactionResult=result[result.length-1].copy(); - let query:SqliteQueryResult=last.query;ok(query) + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, };ok(query) } fn checkpoint_schema(index: int) -> string { if index == 0 => { "CREATE TABLE managed_checkpoints(checkpoint_id TEXT PRIMARY KEY,workspace_id TEXT NOT NULL,request_id TEXT NOT NULL,original_request_proof TEXT NOT NULL,receipt_json TEXT NOT NULL CHECK(json_valid(receipt_json)),compaction_json TEXT NOT NULL CHECK(json_valid(compaction_json)),execution_config_json TEXT NOT NULL CHECK(json_valid(execution_config_json)),UNIQUE(workspace_id,request_id))" } else => { if index == 1 => { "CREATE TABLE managed_checkpoint_history(history_root_id TEXT NOT NULL,ordinal INTEGER NOT NULL,source_message_id TEXT NOT NULL,record_json TEXT NOT NULL CHECK(json_valid(record_json)),PRIMARY KEY(history_root_id,ordinal))" } else => { if index == 2 => { "CREATE TABLE managed_checkpoint_refs(checkpoint_id TEXT NOT NULL,store_id TEXT NOT NULL,object_id TEXT NOT NULL,PRIMARY KEY(checkpoint_id,store_id,object_id))" } else => { if index == 3 => { "CREATE TABLE managed_checkpoint_public_configs(config_ref_id TEXT PRIMARY KEY,checkpoint_id TEXT NOT NULL UNIQUE,record_json TEXT NOT NULL CHECK(json_valid(record_json)))" } else => { "" } } } } } pub fn recovery_context(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); - ok(sqlite::query(&db, "SELECT json_object('next_seq',MAX(COALESCE((SELECT high_water_seq FROM run_retention WHERE run_id=r.id),0),COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id=r.id),0))+1,'ordinal',COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id=r.session_id),0)+1,'parent',COALESCE((SELECT id FROM messages m WHERE m.run_id=r.id AND m.role='assistant' AND EXISTS (SELECT 1 FROM json_each(m.content_json) b WHERE json_extract(b.value,'$.type')='tool_call' AND json_extract(b.value,'$.tool_call_id')=?)),'')) AS record_json FROM runs r WHERE r.id=?", [text(input.call_id.copy()),text(input.run_id.copy())], limits())) + let params: array = [text(input.call_id.copy()),text(input.run_id.copy())]; + ok(sqlite::query(&db, "SELECT json_object('next_seq',MAX(COALESCE((SELECT high_water_seq FROM run_retention WHERE run_id=r.id),0),COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id=r.id),0))+1,'ordinal',COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id=r.session_id),0)+1,'parent',COALESCE((SELECT id FROM messages m WHERE m.run_id=r.id AND m.role='assistant' AND EXISTS (SELECT 1 FROM json_each(m.content_json) b WHERE json_extract(b.value,'$.type')='tool_call' AND json_extract(b.value,'$.tool_call_id')=?)),'')) AS record_json FROM runs r WHERE r.id=?", params, limits())) } pub fn pending(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); ok(sqlite::query(&db, "SELECT record_json FROM managed_effect_calls WHERE state IN ('intent','prepared','indeterminate') AND json_extract(record_json,'$.recovery') IS NULL ORDER BY effect_id LIMIT 128", [], limits())) } pub fn branch_receipt(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); - ok(sqlite::query(&db, "SELECT json_object('proof',original_request_proof,'receipt',json(receipt_json),'origin',json(origin_json)) AS record_json FROM managed_branch_receipts WHERE workspace_id=? AND request_id=?", [text(input.workspace_id.copy()), text(input.receipt_id.copy())], limits())) + let params: array = [text(input.workspace_id.copy()), text(input.receipt_id.copy())]; + ok(sqlite::query(&db, "SELECT json_object('proof',original_request_proof,'receipt',json(receipt_json),'origin',json(origin_json)) AS record_json FROM managed_branch_receipts WHERE workspace_id=? AND request_id=?", params, limits())) } pub fn branch_origin(db: resource, payload: string) -> map { let input: ManagedStorageInput = json::decode::(payload); - ok(sqlite::query(&db, "SELECT CASE WHEN EXISTS (SELECT 1 FROM managed_branch_receipts r JOIN managed_branches b ON b.workspace_id=r.workspace_id AND b.branch_id=? AND b.status='active' WHERE r.workspace_id=? AND json_extract(r.receipt_json,'$.branch.branch_id')=b.branch_id) THEN (SELECT json_object('proof',r.original_request_proof,'receipt',json(r.receipt_json),'origin',json(r.origin_json)) FROM managed_branch_receipts r WHERE r.workspace_id=? AND json_extract(r.receipt_json,'$.branch.branch_id')=? LIMIT 1) WHEN EXISTS (SELECT 1 FROM managed_restore_receipts r JOIN managed_branches b ON b.workspace_id=r.workspace_id AND b.branch_id=? AND b.status='active' WHERE r.workspace_id=? AND json_extract(r.result_json,'$.new_branch.branch_id')=b.branch_id) THEN (SELECT json_object('proof',r.original_request_proof,'receipt',json_object('request_id',r.request_id,'checkpoint_id',r.checkpoint_id,'source',json(json_extract(r.result_json,'$.source_before')),'branch',json(json_extract(r.result_json,'$.new_branch')),'replayed',json('false')),'origin',json_object('kind','checkpoint','workspace_id',r.workspace_id,'source_branch_id',json_extract(r.envelope_json,'$.request.source_branch_id'),'checkpoint_id',r.checkpoint_id,'head_object_id',json_extract(r.result_json,'$.new_branch.head_object_id'),'source_epoch',json_extract(r.result_json,'$.source_before.epoch'))) FROM managed_restore_receipts r WHERE r.workspace_id=? AND json_extract(r.result_json,'$.new_branch.branch_id')=? LIMIT 1) WHEN EXISTS (SELECT 1 FROM managed_branches b WHERE b.workspace_id=? AND b.branch_id=? AND b.status='active') THEN (SELECT json_object('proof','','receipt',json_object('request_id','','checkpoint_id',json('null'),'source',json_object('store_id',b.store_id,'workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version),'branch',json_object('store_id',b.store_id,'workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version),'replayed',json('false')),'origin',json_object('kind','head','workspace_id',b.workspace_id,'head_object_id',b.head_object_id)) FROM managed_branches b WHERE b.workspace_id=? AND b.branch_id=? AND b.status='active' LIMIT 1) WHEN EXISTS (SELECT 1 FROM managed_branches WHERE branch_id=? AND status='active' AND workspace_id<>?) THEN json_object('foreign', json('true')) ELSE json_object('missing', json('true')) END AS record_json", [text(input.branch_id.copy()),text(input.workspace_id.copy()),text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.branch_id.copy()),text(input.workspace_id.copy()),text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.branch_id.copy()),text(input.workspace_id.copy())], limits())) + let params: array = [text(input.branch_id.copy()),text(input.workspace_id.copy()),text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.branch_id.copy()),text(input.workspace_id.copy()),text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.workspace_id.copy()),text(input.branch_id.copy()),text(input.branch_id.copy()),text(input.workspace_id.copy())]; + ok(sqlite::query(&db, "SELECT CASE WHEN EXISTS (SELECT 1 FROM managed_branch_receipts r JOIN managed_branches b ON b.workspace_id=r.workspace_id AND b.branch_id=? AND b.status='active' WHERE r.workspace_id=? AND json_extract(r.receipt_json,'$.branch.branch_id')=b.branch_id) THEN (SELECT json_object('proof',r.original_request_proof,'receipt',json(r.receipt_json),'origin',json(r.origin_json)) FROM managed_branch_receipts r WHERE r.workspace_id=? AND json_extract(r.receipt_json,'$.branch.branch_id')=? LIMIT 1) WHEN EXISTS (SELECT 1 FROM managed_restore_receipts r JOIN managed_branches b ON b.workspace_id=r.workspace_id AND b.branch_id=? AND b.status='active' WHERE r.workspace_id=? AND json_extract(r.result_json,'$.new_branch.branch_id')=b.branch_id) THEN (SELECT json_object('proof',r.original_request_proof,'receipt',json_object('request_id',r.request_id,'checkpoint_id',r.checkpoint_id,'source',json(json_extract(r.result_json,'$.source_before')),'branch',json(json_extract(r.result_json,'$.new_branch')),'replayed',json('false')),'origin',json_object('kind','checkpoint','workspace_id',r.workspace_id,'source_branch_id',json_extract(r.envelope_json,'$.request.source_branch_id'),'checkpoint_id',r.checkpoint_id,'head_object_id',json_extract(r.result_json,'$.new_branch.head_object_id'),'source_epoch',json_extract(r.result_json,'$.source_before.epoch'))) FROM managed_restore_receipts r WHERE r.workspace_id=? AND json_extract(r.result_json,'$.new_branch.branch_id')=? LIMIT 1) WHEN EXISTS (SELECT 1 FROM managed_branches b WHERE b.workspace_id=? AND b.branch_id=? AND b.status='active') THEN (SELECT json_object('proof','','receipt',json_object('request_id','','checkpoint_id',json('null'),'source',json_object('store_id',b.store_id,'workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version),'branch',json_object('store_id',b.store_id,'workspace_id',b.workspace_id,'branch_id',b.branch_id,'epoch',b.epoch,'head_object_id',b.head_object_id,'head_version',b.head_version),'replayed',json('false')),'origin',json_object('kind','head','workspace_id',b.workspace_id,'head_object_id',b.head_object_id)) FROM managed_branches b WHERE b.workspace_id=? AND b.branch_id=? AND b.status='active' LIMIT 1) WHEN EXISTS (SELECT 1 FROM managed_branches WHERE branch_id=? AND status='active' AND workspace_id<>?) THEN json_object('foreign', json('true')) ELSE json_object('missing', json('true')) END AS record_json", params, limits())) } pub fn branch_create(db: resource, payload: string) -> map { let input: ManagedMaintenanceInput = json::decode::(payload); let mut statements: array = [ - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND store_id=json_extract(?,'$.source.store_id') AND epoch=json_extract(?,'$.source.epoch') AND head_object_id=json_extract(?,'$.source.head_object_id') AND head_version=json_extract(?,'$.source.head_version'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.checkpoint_id') IS NOT NULL AND NOT EXISTS (SELECT 1 FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND checkpoint_id=json_extract(?,'$.checkpoint_id') AND json_extract(receipt_json,'$.checkpoint.source_branch_id')=json_extract(?,'$.source.branch_id') AND json_extract(receipt_json,'$.checkpoint.frozen_root.object_id')=json_extract(?,'$.branch.head_object_id') AND json_extract(receipt_json,'$.checkpoint.workspace_id')=json_extract(?,'$.source.workspace_id'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.checkpoint_id') IS NULL AND (NOT EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND status='active') OR EXISTS (SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND state IN ('intent','prepared','indeterminate')))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND epoch+1=json_extract(?,'$.branch.epoch') AND json_extract(?,'$.branch.head_version')=1 AND json_extract(?,'$.branch.branch_id')<>branch_id AND json_extract(?,'$.branch.workspace_id')=workspace_id AND json_extract(?,'$.branch.store_id')=store_id)", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (SELECT CASE WHEN json_type(lineage_json)='array' THEN json_array_length(lineage_json) ELSE 1 END FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id'))>=64", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND process_lineage_tainted=1)", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.branch.workspace_id') AND branch_id=json_extract(?,'$.branch.branch_id'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof<>json_extract(?,'$.proof'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_branches(workspace_id,branch_id,epoch,head_object_id,head_version,store_id,schema_version,status,lineage_json,process_lineage_tainted) SELECT json_extract(?,'$.branch.workspace_id'),json_extract(?,'$.branch.branch_id'),json_extract(?,'$.branch.epoch'),json_extract(?,'$.branch.head_object_id'),1,json_extract(?,'$.branch.store_id'),1,'active',CASE WHEN json_type(lineage_json)='array' THEN json_insert(lineage_json,'$[#]',json_extract(?,'$.branch.branch_id')) ELSE json_array(branch_id,json_extract(?,'$.branch.branch_id')) END,process_lineage_tainted FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND NOT EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND request_id=json_extract(?,'$.request_id'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT workspace_id,branch_id,1,epoch,NULL,NULL,head_object_id,1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.branch.workspace_id') AND branch_id=json_extract(?,'$.branch.branch_id') AND NOT EXISTS (SELECT 1 FROM managed_head_history WHERE workspace_id=json_extract(?,'$.branch.workspace_id') AND branch_id=json_extract(?,'$.branch.branch_id') AND head_version=1)", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_branch_receipts(workspace_id,request_id,checkpoint_id,source_branch_id,original_request_proof,receipt_json,origin_json) SELECT json_extract(?,'$.source.workspace_id'),json_extract(?,'$.request_id'),COALESCE(json_extract(?,'$.checkpoint_id'),''),json_extract(?,'$.source.branch_id'),json_extract(?,'$.proof'),json_extract(?,'$.receipt'),json_extract(?,'$.origin') WHERE EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.branch.workspace_id') AND branch_id=json_extract(?,'$.branch.branch_id')) AND NOT EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND request_id=json_extract(?,'$.request_id'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"SELECT receipt_json AS record_json FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:true, limits:limits()} + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND store_id=json_extract(?,'$.source.store_id') AND epoch=json_extract(?,'$.source.epoch') AND head_object_id=json_extract(?,'$.source.head_object_id') AND head_version=json_extract(?,'$.source.head_version'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.checkpoint_id') IS NOT NULL AND NOT EXISTS (SELECT 1 FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND checkpoint_id=json_extract(?,'$.checkpoint_id') AND json_extract(receipt_json,'$.checkpoint.source_branch_id')=json_extract(?,'$.source.branch_id') AND json_extract(receipt_json,'$.checkpoint.frozen_root.object_id')=json_extract(?,'$.branch.head_object_id') AND json_extract(receipt_json,'$.checkpoint.workspace_id')=json_extract(?,'$.source.workspace_id'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.checkpoint_id') IS NULL AND (NOT EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND status='active') OR EXISTS (SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND state IN ('intent','prepared','indeterminate')))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND epoch+1=json_extract(?,'$.branch.epoch') AND json_extract(?,'$.branch.head_version')=1 AND json_extract(?,'$.branch.branch_id')<>branch_id AND json_extract(?,'$.branch.workspace_id')=workspace_id AND json_extract(?,'$.branch.store_id')=store_id)", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (SELECT CASE WHEN json_type(lineage_json)='array' THEN json_array_length(lineage_json) ELSE 1 END FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id'))>=64", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND process_lineage_tainted=1)", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.branch.workspace_id') AND branch_id=json_extract(?,'$.branch.branch_id'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof<>json_extract(?,'$.proof'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_branches(workspace_id,branch_id,epoch,head_object_id,head_version,store_id,schema_version,status,lineage_json,process_lineage_tainted) SELECT json_extract(?,'$.branch.workspace_id'),json_extract(?,'$.branch.branch_id'),json_extract(?,'$.branch.epoch'),json_extract(?,'$.branch.head_object_id'),1,json_extract(?,'$.branch.store_id'),1,'active',CASE WHEN json_type(lineage_json)='array' THEN json_insert(lineage_json,'$[#]',json_extract(?,'$.branch.branch_id')) ELSE json_array(branch_id,json_extract(?,'$.branch.branch_id')) END,process_lineage_tainted FROM managed_branches WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND branch_id=json_extract(?,'$.source.branch_id') AND NOT EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND request_id=json_extract(?,'$.request_id'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_head_history(workspace_id,branch_id,head_version,epoch,effect_id,previous_head,head_object_id,schema_version) SELECT workspace_id,branch_id,1,epoch,NULL,NULL,head_object_id,1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.branch.workspace_id') AND branch_id=json_extract(?,'$.branch.branch_id') AND NOT EXISTS (SELECT 1 FROM managed_head_history WHERE workspace_id=json_extract(?,'$.branch.workspace_id') AND branch_id=json_extract(?,'$.branch.branch_id') AND head_version=1)", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_branch_receipts(workspace_id,request_id,checkpoint_id,source_branch_id,original_request_proof,receipt_json,origin_json) SELECT json_extract(?,'$.source.workspace_id'),json_extract(?,'$.request_id'),COALESCE(json_extract(?,'$.checkpoint_id'),''),json_extract(?,'$.source.branch_id'),json_extract(?,'$.proof'),json_extract(?,'$.receipt'),json_extract(?,'$.origin') WHERE EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.branch.workspace_id') AND branch_id=json_extract(?,'$.branch.branch_id')) AND NOT EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND request_id=json_extract(?,'$.request_id'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"SELECT receipt_json AS record_json FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.source.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(true), limits: Some(limits())} ]; let result: array = sqlite::transaction(&db, statements); let last: SqliteTransactionResult = result[result.length - 1].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok(query) } pub fn publish_eligibility(db: resource, payload: string) -> map { let input: ManagedMaintenanceInput = json::decode::(payload); - ok(sqlite::query(&db, "SELECT CASE WHEN EXISTS (SELECT 1 FROM managed_branches WHERE branch_id=json_extract(?,'$.branch_id') AND status='active' AND workspace_id<>json_extract(?,'$.workspace_id')) THEN json_object('code','foreign') WHEN NOT EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')) THEN json_object('code','missing') WHEN EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND status<>'active') THEN json_object('code','missing') WHEN NOT EXISTS (SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.profile=json_extract(?,'$.profile') WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND b.status='active' AND b.store_id=json_extract(?,'$.expected_head.store_id') AND b.epoch=json_extract(?,'$.expected_head.epoch') AND b.head_object_id=json_extract(?,'$.expected_head.head_object_id') AND b.head_version=json_extract(?,'$.expected_head.head_version') AND b.schema_version=json_extract(?,'$.expected_head.schema_version') AND b.process_lineage_tainted=(CASE WHEN json_extract(?,'$.expected_head.process_lineage_tainted') THEN 1 ELSE 0 END) AND b.workspace_id=json_extract(?,'$.expected_head.workspace_id') AND b.branch_id=json_extract(?,'$.expected_head.branch_id')) THEN json_object('code','stale') WHEN EXISTS (SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND state IN ('intent','prepared','indeterminate')) OR EXISTS (SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND r.status NOT IN ('completed','failed','cancelled')) THEN json_object('code','busy') WHEN EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof<>json_extract(?,'$.proof')) OR EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_restore_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) THEN json_object('code','conflict') ELSE json_object('code','idle','revision',COALESCE((SELECT revision FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id')),0)) END AS record_json", [text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], limits())) + let params: array = [text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]; + ok(sqlite::query(&db, "SELECT CASE WHEN EXISTS (SELECT 1 FROM managed_branches WHERE branch_id=json_extract(?,'$.branch_id') AND status='active' AND workspace_id<>json_extract(?,'$.workspace_id')) THEN json_object('code','foreign') WHEN NOT EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')) THEN json_object('code','missing') WHEN EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND status<>'active') THEN json_object('code','missing') WHEN NOT EXISTS (SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.profile=json_extract(?,'$.profile') WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND b.status='active' AND b.store_id=json_extract(?,'$.expected_head.store_id') AND b.epoch=json_extract(?,'$.expected_head.epoch') AND b.head_object_id=json_extract(?,'$.expected_head.head_object_id') AND b.head_version=json_extract(?,'$.expected_head.head_version') AND b.schema_version=json_extract(?,'$.expected_head.schema_version') AND b.process_lineage_tainted=(CASE WHEN json_extract(?,'$.expected_head.process_lineage_tainted') THEN 1 ELSE 0 END) AND b.workspace_id=json_extract(?,'$.expected_head.workspace_id') AND b.branch_id=json_extract(?,'$.expected_head.branch_id')) THEN json_object('code','stale') WHEN EXISTS (SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND state IN ('intent','prepared','indeterminate')) OR EXISTS (SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND r.status NOT IN ('completed','failed','cancelled')) THEN json_object('code','busy') WHEN EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof<>json_extract(?,'$.proof')) OR EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_restore_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) THEN json_object('code','conflict') ELSE json_object('code','idle','revision',COALESCE((SELECT revision FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id')),0)) END AS record_json", params, limits())) } pub fn publish_receipt(db: resource, payload: string) -> map { let input: ManagedMaintenanceInput = json::decode::(payload); - ok(sqlite::query(&db, "SELECT json_object('proof',original_request_proof,'receipt',json(receipt_json)) AS record_json FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')", [text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], limits())) + let params: array = [text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]; + ok(sqlite::query(&db, "SELECT json_object('proof',original_request_proof,'receipt',json(receipt_json)) AS record_json FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')", params, limits())) } pub fn publication_get(db: resource, payload: string) -> map { let input: ManagedMaintenanceInput = json::decode::(payload); - ok(sqlite::query(&db, "SELECT record_json FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND profile=json_extract(?,'$.profile')", [text(input.maintenance_json.copy()), text(input.maintenance_json.copy())], limits())) + let params: array = [text(input.maintenance_json.copy()), text(input.maintenance_json.copy())]; + ok(sqlite::query(&db, "SELECT record_json FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND profile=json_extract(?,'$.profile')", params, limits())) } /// Native-only read-only settlement snapshot for one frozen completion request. @@ -449,26 +482,27 @@ pub fn publication_get(db: resource, payload: string) -> map /// hit, a proven absence and a timeout stay distinct outcomes for the caller. pub fn finalize_settle(db: resource, payload: string) -> map { let input: ManagedMaintenanceInput = json::decode::(payload); - ok(sqlite::query(&db, "SELECT json_object('receipt',CASE WHEN EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) THEN (SELECT json(receipt_json) FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) ELSE json('null') END,'proof',COALESCE((SELECT original_request_proof FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')),''),'run_status',COALESCE((SELECT status FROM runs WHERE id=json_extract(?,'$.run_id')),''),'publication',CASE WHEN EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id')) THEN (SELECT json(record_json) FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id')) ELSE json('null') END,'events',json((SELECT json_group_array(json(event_row)) FROM (SELECT json_object('seq',seq,'event_id',event_id,'event_type',event_type,'payload_json',payload_json) AS event_row FROM run_events WHERE run_id=json_extract(?,'$.run_id') ORDER BY seq ASC LIMIT 512))))", [text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], limits())) + let params: array = [text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]; + ok(sqlite::query(&db, "SELECT json_object('receipt',CASE WHEN EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) THEN (SELECT json(receipt_json) FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) ELSE json('null') END,'proof',COALESCE((SELECT original_request_proof FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')),''),'run_status',COALESCE((SELECT status FROM runs WHERE id=json_extract(?,'$.run_id')),''),'publication',CASE WHEN EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id')) THEN (SELECT json(record_json) FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id')) ELSE json('null') END,'events',json((SELECT json_group_array(json(event_row)) FROM (SELECT json_object('seq',seq,'event_id',event_id,'event_type',event_type,'payload_json',payload_json) AS event_row FROM run_events WHERE run_id=json_extract(?,'$.run_id') ORDER BY seq ASC LIMIT 512))))", params, limits())) } pub fn publish_submit(db: resource, payload: string) -> map { let input: ManagedMaintenanceInput = json::decode::(payload); let mut statements: array = [ - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof<>json_extract(?,'$.proof')) OR EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_restore_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')) AND EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND process_lineage_tainted=1)", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')) AND NOT EXISTS (SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.profile=json_extract(?,'$.profile') WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND b.status='active' AND b.store_id=json_extract(?,'$.expected_head.store_id') AND b.epoch=json_extract(?,'$.expected_head.epoch') AND b.head_object_id=json_extract(?,'$.expected_head.head_object_id') AND b.head_version=json_extract(?,'$.expected_head.head_version') AND b.schema_version=json_extract(?,'$.expected_head.schema_version') AND b.process_lineage_tainted=(CASE WHEN json_extract(?,'$.expected_head.process_lineage_tainted') THEN 1 ELSE 0 END) AND b.workspace_id=json_extract(?,'$.expected_head.workspace_id') AND b.branch_id=json_extract(?,'$.expected_head.branch_id'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')) AND (EXISTS (SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND state IN ('intent','prepared','indeterminate')) OR EXISTS (SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND r.status NOT IN ('completed','failed','cancelled')))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')) AND ((json_extract(?,'$.expected_revision')=0 AND EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id'))) OR (json_extract(?,'$.expected_revision')<>0 AND NOT EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND revision=json_extract(?,'$.expected_revision'))))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_workspace_publications(workspace_id,profile,branch_id,epoch,head_object_id,head_version,previous_head_object_id,request_id,published_at_ms,revision,store_id,schema_version,record_json) SELECT b.workspace_id,w.profile,b.branch_id,b.epoch,b.head_object_id,b.head_version,NULL,json_extract(?,'$.request_id'),json_extract(?,'$.now_ms'),1,b.store_id,b.schema_version,'{}' FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE json_extract(?,'$.expected_revision')=0 AND b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND NOT EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=b.workspace_id) AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"UPDATE managed_workspace_publications SET profile=(SELECT profile FROM managed_workspaces WHERE workspace_id=managed_workspace_publications.workspace_id), branch_id=json_extract(?,'$.branch_id'), epoch=(SELECT epoch FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), head_object_id=(SELECT head_object_id FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), head_version=(SELECT head_version FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), previous_head_object_id=head_object_id, request_id=json_extract(?,'$.request_id'), published_at_ms=json_extract(?,'$.now_ms'), revision=revision+1, store_id=(SELECT store_id FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), schema_version=(SELECT schema_version FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')) WHERE workspace_id=json_extract(?,'$.workspace_id') AND revision=json_extract(?,'$.expected_revision') AND json_extract(?,'$.expected_revision')<>0 AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"UPDATE managed_workspace_publications SET record_json=json_object('workspace_id',workspace_id,'profile',profile,'branch_id',branch_id,'epoch',epoch,'head_object_id',head_object_id,'head_version',head_version,'previous_published_head',CASE WHEN previous_head_object_id IS NULL THEN json('null') ELSE json_quote(previous_head_object_id) END,'request_id',request_id,'published_at_ms',published_at_ms,'revision',revision,'store_id',store_id,'schema_version',schema_version) WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_publish_receipts(workspace_id,request_id,branch_id,original_request_proof,receipt_json,action) SELECT workspace_id,request_id,branch_id,json_extract(?,'$.proof'),json_object('status','committed','workspace_id',workspace_id,'profile',profile,'branch_id',branch_id,'epoch',epoch,'head_object_id',head_object_id,'head_version',head_version,'previous_published_head',CASE WHEN previous_head_object_id IS NULL THEN json('null') ELSE json_quote(previous_head_object_id) END,'request_id',request_id,'published_at_ms',published_at_ms),'publish' FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof'))", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:false, limits:limits()}, - {sql:"SELECT receipt_json AS record_json FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')", params:[text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())], query:true, limits:limits()} + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof<>json_extract(?,'$.proof')) OR EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_restore_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')) AND EXISTS (SELECT 1 FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND process_lineage_tainted=1)", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')) AND NOT EXISTS (SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.profile=json_extract(?,'$.profile') WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND b.status='active' AND b.store_id=json_extract(?,'$.expected_head.store_id') AND b.epoch=json_extract(?,'$.expected_head.epoch') AND b.head_object_id=json_extract(?,'$.expected_head.head_object_id') AND b.head_version=json_extract(?,'$.expected_head.head_version') AND b.schema_version=json_extract(?,'$.expected_head.schema_version') AND b.process_lineage_tainted=(CASE WHEN json_extract(?,'$.expected_head.process_lineage_tainted') THEN 1 ELSE 0 END) AND b.workspace_id=json_extract(?,'$.expected_head.workspace_id') AND b.branch_id=json_extract(?,'$.expected_head.branch_id'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')) AND (EXISTS (SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND state IN ('intent','prepared','indeterminate')) OR EXISTS (SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND r.status NOT IN ('completed','failed','cancelled')))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')) AND ((json_extract(?,'$.expected_revision')=0 AND EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id'))) OR (json_extract(?,'$.expected_revision')<>0 AND NOT EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND revision=json_extract(?,'$.expected_revision'))))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_workspace_publications(workspace_id,profile,branch_id,epoch,head_object_id,head_version,previous_head_object_id,request_id,published_at_ms,revision,store_id,schema_version,record_json) SELECT b.workspace_id,w.profile,b.branch_id,b.epoch,b.head_object_id,b.head_version,NULL,json_extract(?,'$.request_id'),json_extract(?,'$.now_ms'),1,b.store_id,b.schema_version,'{}' FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE json_extract(?,'$.expected_revision')=0 AND b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND NOT EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=b.workspace_id) AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"UPDATE managed_workspace_publications SET profile=(SELECT profile FROM managed_workspaces WHERE workspace_id=managed_workspace_publications.workspace_id), branch_id=json_extract(?,'$.branch_id'), epoch=(SELECT epoch FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), head_object_id=(SELECT head_object_id FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), head_version=(SELECT head_version FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), previous_head_object_id=head_object_id, request_id=json_extract(?,'$.request_id'), published_at_ms=json_extract(?,'$.now_ms'), revision=revision+1, store_id=(SELECT store_id FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), schema_version=(SELECT schema_version FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')) WHERE workspace_id=json_extract(?,'$.workspace_id') AND revision=json_extract(?,'$.expected_revision') AND json_extract(?,'$.expected_revision')<>0 AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"UPDATE managed_workspace_publications SET record_json=json_object('workspace_id',workspace_id,'profile',profile,'branch_id',branch_id,'epoch',epoch,'head_object_id',head_object_id,'head_version',head_version,'previous_published_head',CASE WHEN previous_head_object_id IS NULL THEN json('null') ELSE json_quote(previous_head_object_id) END,'request_id',request_id,'published_at_ms',published_at_ms,'revision',revision,'store_id',store_id,'schema_version',schema_version) WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_publish_receipts(workspace_id,request_id,branch_id,original_request_proof,receipt_json,action) SELECT workspace_id,request_id,branch_id,json_extract(?,'$.proof'),json_object('status','committed','workspace_id',workspace_id,'profile',profile,'branch_id',branch_id,'epoch',epoch,'head_object_id',head_object_id,'head_version',head_version,'previous_published_head',CASE WHEN previous_head_object_id IS NULL THEN json('null') ELSE json_quote(previous_head_object_id) END,'request_id',request_id,'published_at_ms',published_at_ms),'publish' FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof'))", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(false), limits: Some(limits())}, + {sql:"SELECT receipt_json AS record_json FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')", params: Some([text(input.maintenance_json.copy()),text(input.maintenance_json.copy()),text(input.maintenance_json.copy())]), query: Some(true), limits: Some(limits())} ]; let result: array = sqlite::transaction(&db, statements); let last: SqliteTransactionResult = result[result.length - 1].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok(query) } diff --git a/rss/storage/events.rss b/rss/storage/events.rss index 6fd996f..28a01fe 100644 --- a/rss/storage/events.rss +++ b/rss/storage/events.rss @@ -2,27 +2,32 @@ use json; use sqlite; use plugins; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } @@ -30,18 +35,18 @@ use self::schema as schema; use self::existence as existence; use self::messages as messages; -fn events_query_limits(max_rows: int, max_bytes: int) { +fn events_query_limits(max_rows: int, max_bytes: int) -> SqliteLimits { sql_limits(max_rows, if max_bytes < 4096 => { 4096 } else => { max_bytes }) } - struct EventAppendInput { run_id: string, event_id: string, event_type: string, payload_json: string, now_ms: int, - max_events: int + max_events: int, + seq: Option } struct EventReplayInput { @@ -73,45 +78,49 @@ struct EventAppendPairInput { event_type_b: string, payload_json_b: string, now_ms: int, - max_events: int + max_events: int, + failpoint: Option, + seq_a: Option, + seq_b: Option } pub fn storage_event_append(db_id: resource, payload_json: string) -> map { - let raw_payload: map = json::decode(payload_json); let input: EventAppendInput = json::decode::(payload_json); - let mut reserved_seq: int = 0; - if raw_payload.has("seq") { - reserved_seq = raw_payload["seq"].copy(); - } + let reserved_seq: int = match input.seq { Some(value) => value, None => 0 }; let mut result = { ok: true, code: "ok", message: "", result: [] }; - if !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.run_id.copy())]; + if !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "run_not_found", message: "event append targets an unknown run", result: [] }; } else { let max_events: int = schema::max_events_limit(input.max_events.copy()); + let stmt_params_1: array = [sql_text(input.run_id.copy()), sql_int(reserved_seq.copy()), sql_int(reserved_seq.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id.copy()), sql_text(input.event_type.copy()), sql_text(input.payload_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.event_id.copy())]; + let stmt_params_2: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy())]; + let stmt_params_3: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy())]; + let stmt_params_4: array = [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]; let statements: array = [ { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, CASE WHEN ? > 0 THEN ? ELSE COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1 END, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM run_events existing WHERE existing.event_id = ?)", - params: [sql_text(input.run_id.copy()), sql_int(reserved_seq.copy()), sql_int(reserved_seq.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id.copy()), sql_text(input.event_type.copy()), sql_text(input.payload_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.event_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_1), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM run_events WHERE rowid IN (SELECT rowid FROM run_events WHERE run_id = ? ORDER BY seq ASC LIMIT CASE WHEN (SELECT COUNT(*) FROM run_events WHERE run_id = ?) > ? THEN (SELECT COUNT(*) FROM run_events WHERE run_id = ?) - ? ELSE 0 END)", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_2), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT ?, COALESCE((SELECT MIN(seq) FROM run_events WHERE run_id = ?), 0), COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id = ?), 0), ? ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_3), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE runs SET updated_at_ms = ? WHERE id = ?", - params: [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_4), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; result = { ok: true, code: "ok", message: "", result: sqlite::transaction(&db_id, statements) }; @@ -120,70 +129,68 @@ pub fn storage_event_append(db_id: resource, payload_json: st } pub fn storage_event_append_pair(db_id: resource, payload_json: string) -> map { - let raw_payload: map = json::decode(payload_json); - let mut failpoint: string = ""; - if raw_payload.has("failpoint") { - failpoint = raw_payload["failpoint"].copy(); - } let input: EventAppendPairInput = json::decode::(payload_json); - let mut reserved_seq_a: int = 0; - let mut reserved_seq_b: int = 0; - if raw_payload.has("seq_a") { - reserved_seq_a = raw_payload["seq_a"].copy(); - } - if raw_payload.has("seq_b") { - reserved_seq_b = raw_payload["seq_b"].copy(); - } + let failpoint: string = match input.failpoint.copy() { Some(value) => value, None => "" }; + let reserved_seq_a: int = match input.seq_a { Some(value) => value, None => 0 }; + let reserved_seq_b: int = match input.seq_b { Some(value) => value, None => 0 }; let mut result = { ok: true, code: "ok", message: "", result: [] }; if input.payload_json_a.copy().length > 65536 || input.payload_json_b.copy().length > 65536 { result = { ok: false, code: "payload_too_large", message: "pair event payload exceeds 65536 bytes", result: [] }; } else { - if !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.run_id.copy())]; + if !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "run_not_found", message: "event append targets an unknown run", result: [] }; } else { let max_events: int = schema::max_events_limit(input.max_events.copy()); + let stmt_params_5: array = [sql_text(input.run_id.copy()), sql_int(reserved_seq_a.copy()), sql_int(reserved_seq_a.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id_a.copy()), sql_text(input.event_type_a.copy()), sql_text(input.payload_json_a.copy()), sql_int(input.now_ms.copy()), sql_text(input.event_id_a.copy()), sql_text(input.run_id.copy())]; + let stmt_params_6: array = [sql_text(input.run_id.copy()), sql_int(reserved_seq_b.copy()), sql_int(reserved_seq_b.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id_b.copy()), sql_text(input.event_type_b.copy()), sql_text(input.payload_json_b.copy()), sql_int(input.now_ms.copy()), sql_text(input.event_id_b.copy()), sql_text(input.run_id.copy())]; + let stmt_params_7: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy())]; + let stmt_params_8: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]; + let stmt_params_9: array = [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy())]; + let stmt_params_10: array = [sql_text(failpoint), sql_text(input.run_id.copy()), sql_text(input.run_id.copy())]; + let stmt_params_11: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy())]; let statements: array = [ { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, CASE WHEN ? > 0 THEN ? ELSE COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1 END, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM run_events existing WHERE existing.event_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id = q.graph_id) OR NOT EXISTS (SELECT 1 FROM plugin_graph_refs g WHERE g.owner_kind = 'run' AND g.owner_id = q.run_id AND g.graph_id = q.graph_id AND g.ref_id = q.ref_id)))", - params: [sql_text(input.run_id.copy()), sql_int(reserved_seq_a.copy()), sql_int(reserved_seq_a.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id_a.copy()), sql_text(input.event_type_a.copy()), sql_text(input.payload_json_a.copy()), sql_int(input.now_ms.copy()), sql_text(input.event_id_a.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_5), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, CASE WHEN ? > 0 THEN ? ELSE COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1 END, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM run_events existing WHERE existing.event_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id = q.graph_id) OR NOT EXISTS (SELECT 1 FROM plugin_graph_refs g WHERE g.owner_kind = 'run' AND g.owner_id = q.run_id AND g.graph_id = q.graph_id AND g.ref_id = q.ref_id)))", - params: [sql_text(input.run_id.copy()), sql_int(reserved_seq_b.copy()), sql_int(reserved_seq_b.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id_b.copy()), sql_text(input.event_type_b.copy()), sql_text(input.payload_json_b.copy()), sql_int(input.now_ms.copy()), sql_text(input.event_id_b.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_6), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM run_events WHERE NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id = q.graph_id) OR NOT EXISTS (SELECT 1 FROM plugin_graph_refs g WHERE g.owner_kind = 'run' AND g.owner_id = q.run_id AND g.graph_id = q.graph_id AND g.ref_id = q.ref_id))) AND rowid IN (SELECT rowid FROM run_events WHERE run_id = ? ORDER BY seq ASC LIMIT CASE WHEN (SELECT COUNT(*) FROM run_events WHERE run_id = ?) > ? THEN (SELECT COUNT(*) FROM run_events WHERE run_id = ?) - ? ELSE 0 END)", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_7), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT ?, COALESCE((SELECT MIN(seq) FROM run_events WHERE run_id = ?), 0), COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id = ?), 0), ? WHERE NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id = q.graph_id) OR NOT EXISTS (SELECT 1 FROM plugin_graph_refs g WHERE g.owner_kind = 'run' AND g.owner_id = q.run_id AND g.graph_id = q.graph_id AND g.ref_id = q.ref_id))) ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_8), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE runs SET updated_at_ms = ? WHERE id = ? AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id = q.graph_id) OR NOT EXISTS (SELECT 1 FROM plugin_graph_refs g WHERE g.owner_kind = 'run' AND g.owner_id = q.run_id AND g.graph_id = q.graph_id AND g.ref_id = q.ref_id)))", - params: [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_9), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE runs SET status = 'after_partial_write' WHERE ? = 'after_partial_write' AND id = ? AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id = q.graph_id) OR NOT EXISTS (SELECT 1 FROM plugin_graph_refs g WHERE g.owner_kind = 'run' AND g.owner_id = q.run_id AND g.graph_id = q.graph_id AND g.ref_id = q.ref_id)))", - params: [sql_text(failpoint), sql_text(input.run_id.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_10), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "SELECT CASE WHEN EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN plugin_graph_revocations r ON r.graph_id=q.graph_id WHERE q.run_id=?) THEN 'plugin_graph_revoked' WHEN EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (EXISTS (SELECT 1 FROM plugin_graph_revocations r WHERE r.graph_id = q.graph_id) OR NOT EXISTS (SELECT 1 FROM plugin_graph_refs g WHERE g.owner_kind = 'run' AND g.owner_id = q.run_id AND g.graph_id = q.graph_id AND g.ref_id = q.ref_id))) THEN 'plugin_graph_pin_required' ELSE 'ok' END", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy())], - query: true, - limits: sql_limits(1, 4096) + params: Some(stmt_params_11), + query: Some(true), + limits: Some(sql_limits(1, 4096)) } ]; let transaction: array = sqlite::transaction(&db_id, statements); @@ -206,17 +213,19 @@ pub fn storage_event_append_pair(db_id: resource, payload_jso /// events report floor 1 / high-water 0. The query always yields exactly one /// row (COALESCE), so extraction is unconditional. pub fn storage_event_retention(db_id: resource, run_id: string) -> map { - let result: SqliteQueryResult = sqlite::query(&db_id, "SELECT COALESCE((SELECT first_seq FROM run_retention WHERE run_id = ?), 1) AS first_seq, COALESCE((SELECT high_water_seq FROM run_retention WHERE run_id = ?), 0) AS high_water_seq", [sql_text(run_id.copy()), sql_text(run_id.copy())], sql_limits(1, 4096)); + let params: array = [sql_text(run_id.copy()), sql_text(run_id.copy())]; + let result: SqliteQueryResult = sqlite::query(&db_id, "SELECT COALESCE((SELECT first_seq FROM run_retention WHERE run_id = ?), 1) AS first_seq, COALESCE((SELECT high_water_seq FROM run_retention WHERE run_id = ?), 0) AS high_water_seq", params, sql_limits(1, 4096)); let retention_row: SqliteRow = result.rows[0].copy(); let retention_cells: array = retention_row.cells.copy(); - let first_seq: int = retention_cells[0].int_value; - let high_water_seq: int = retention_cells[1].int_value; + let first_seq: int = retention_cells.copy()[0].int_value.unwrap_or(0); + let high_water_seq: int = retention_cells.copy()[1].int_value.unwrap_or(0); { first_seq: first_seq, high_water_seq: high_water_seq } } pub fn storage_event_replay(db_id: resource, payload_json: string) -> SqliteQueryResult { let input: EventReplayInput = json::decode::(payload_json); - sqlite::query(&db_id, "SELECT events.seq, events.run_id, events.event_id, events.event_type, CASE WHEN length(CAST(events.payload_json AS BLOB)) > CASE WHEN ? <= 0 THEN 16384 WHEN ? > 32768 THEN 16384 WHEN ? < 128 THEN 64 ELSE ? / 2 END THEN '{\"truncated\":true,\"original_bytes\":' || length(CAST(events.payload_json AS BLOB)) || '}' ELSE events.payload_json END AS payload_json, events.created_at_ms FROM run_events events WHERE events.run_id = ? AND events.seq >= ? ORDER BY events.seq ASC LIMIT (CASE WHEN ? <= 0 THEN 128 WHEN ? > 256 THEN 256 ELSE ? END) + 1", [sql_int(input.max_bytes.copy()), sql_int(input.max_bytes.copy()), sql_int(input.max_bytes.copy()), sql_int(input.max_bytes.copy()), sql_text(input.run_id.copy()), sql_int(input.after_seq.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy())], events_query_limits(input.max_events.copy(), input.max_bytes.copy())) + let params: array = [sql_int(input.max_bytes.copy()), sql_int(input.max_bytes.copy()), sql_int(input.max_bytes.copy()), sql_int(input.max_bytes.copy()), sql_text(input.run_id.copy()), sql_int(input.after_seq.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy())]; + sqlite::query(&db_id, "SELECT events.seq, events.run_id, events.event_id, events.event_type, CASE WHEN length(CAST(events.payload_json AS BLOB)) > CASE WHEN ? <= 0 THEN 16384 WHEN ? > 32768 THEN 16384 WHEN ? < 128 THEN 64 ELSE ? / 2 END THEN '{\"truncated\":true,\"original_bytes\":' || length(CAST(events.payload_json AS BLOB)) || '}' ELSE events.payload_json END AS payload_json, events.created_at_ms FROM run_events events WHERE events.run_id = ? AND events.seq >= ? ORDER BY events.seq ASC LIMIT (CASE WHEN ? <= 0 THEN 128 WHEN ? > 256 THEN 256 ELSE ? END) + 1", params, events_query_limits(input.max_events.copy(), input.max_bytes.copy())) } /// Prunes a run's retained events down to `max_events` and updates the @@ -224,25 +233,28 @@ pub fn storage_event_replay(db_id: resource, payload_json: st /// floor always matches what replay can actually serve. pub fn storage_event_prune(db_id: resource, payload_json: string) -> array { let input: EventPruneInput = json::decode::(payload_json); + let stmt_params_12: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_text(input.run_id.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy())]; + let stmt_params_13: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy())]; let statements: array = [ { sql: "DELETE FROM run_events WHERE rowid IN (SELECT rowid FROM run_events WHERE run_id = ? ORDER BY seq ASC LIMIT CASE WHEN (SELECT COUNT(*) FROM run_events WHERE run_id = ?) > CASE WHEN ? <= 0 THEN 128 WHEN ? > 256 THEN 256 ELSE ? END THEN (SELECT COUNT(*) FROM run_events WHERE run_id = ?) - CASE WHEN ? <= 0 THEN 128 WHEN ? > 256 THEN 256 ELSE ? END ELSE 0 END)", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_text(input.run_id.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_12), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT ?, COALESCE((SELECT MIN(seq) FROM run_events WHERE run_id = ?), 0), COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id = ?), 0), ? ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_13), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; sqlite::transaction(&db_id, statements) } pub fn storage_delivery_cursor_get(db_id: resource, session_id: string, consumer: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT session_id, consumer, last_event_seq, updated_at_ms FROM delivery_cursors WHERE session_id = ? AND consumer = ? LIMIT ?", [sql_text(session_id.copy()), sql_text(consumer.copy()), sql_int(max_rows)], events_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(session_id.copy()), sql_text(consumer.copy()), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT session_id, consumer, last_event_seq, updated_at_ms FROM delivery_cursors WHERE session_id = ? AND consumer = ? LIMIT ?", params, events_query_limits(max_rows, max_bytes)) } /// Monotonic unvalidated cursor upsert for values that are not run-event @@ -252,12 +264,14 @@ pub fn storage_delivery_cursor_get(db_id: resource, session_i /// to `run_events.seq`. pub fn storage_delivery_cursor_set(db_id: resource, payload_json: string) -> SqliteExecuteResult { let input: CursorInput = json::decode::(payload_json); - sqlite::execute(&db_id, "INSERT INTO delivery_cursors (session_id, consumer, last_event_seq, updated_at_ms) VALUES (?, ?, ?, ?) ON CONFLICT (session_id, consumer) DO UPDATE SET last_event_seq = MAX(delivery_cursors.last_event_seq, excluded.last_event_seq), updated_at_ms = excluded.updated_at_ms", [sql_text(input.session_id.copy()), sql_text(input.consumer.copy()), sql_int(input.event_seq.copy()), sql_int(input.now_ms.copy())]) + let params: array = [sql_text(input.session_id.copy()), sql_text(input.consumer.copy()), sql_int(input.event_seq.copy()), sql_int(input.now_ms.copy())]; + sqlite::execute(&db_id, "INSERT INTO delivery_cursors (session_id, consumer, last_event_seq, updated_at_ms) VALUES (?, ?, ?, ?) ON CONFLICT (session_id, consumer) DO UPDATE SET last_event_seq = MAX(delivery_cursors.last_event_seq, excluded.last_event_seq), updated_at_ms = excluded.updated_at_ms", params) } pub fn storage_delivery_cursor_advance(db_id: resource, payload_json: string) -> SqliteExecuteResult { let input: CursorInput = json::decode::(payload_json); - sqlite::execute(&db_id, "INSERT INTO delivery_cursors (session_id, consumer, last_event_seq, updated_at_ms) SELECT ?, ?, ?, ? WHERE ? >= 0 AND ? <= COALESCE((SELECT MAX(events.seq) FROM run_events events JOIN runs event_runs ON event_runs.id = events.run_id WHERE event_runs.session_id = ?), 0) ON CONFLICT (session_id, consumer) DO UPDATE SET last_event_seq = MAX(delivery_cursors.last_event_seq, excluded.last_event_seq), updated_at_ms = excluded.updated_at_ms", [sql_text(input.session_id.copy()), sql_text(input.consumer.copy()), sql_int(input.event_seq.copy()), sql_int(input.now_ms.copy()), sql_int(input.event_seq.copy()), sql_int(input.event_seq.copy()), sql_text(input.session_id.copy())]) + let params: array = [sql_text(input.session_id.copy()), sql_text(input.consumer.copy()), sql_int(input.event_seq.copy()), sql_int(input.now_ms.copy()), sql_int(input.event_seq.copy()), sql_int(input.event_seq.copy()), sql_text(input.session_id.copy())]; + sqlite::execute(&db_id, "INSERT INTO delivery_cursors (session_id, consumer, last_event_seq, updated_at_ms) SELECT ?, ?, ?, ? WHERE ? >= 0 AND ? <= COALESCE((SELECT MAX(events.seq) FROM run_events events JOIN runs event_runs ON event_runs.id = events.run_id WHERE event_runs.session_id = ?), 0) ON CONFLICT (session_id, consumer) DO UPDATE SET last_event_seq = MAX(delivery_cursors.last_event_seq, excluded.last_event_seq), updated_at_ms = excluded.updated_at_ms", params) } struct StepCommitInput { @@ -276,7 +290,10 @@ struct StepCommitInput { parent_message_id: string, token_estimate: int, metadata_json: string, - finish_reason: string + finish_reason: string, + failpoint: Option, + seq: Option, + ordinal: Option } struct ReconcileEffectsInput { @@ -289,74 +306,73 @@ struct ReconcileEffectsInput { /// append once. The store lock/transaction is held only for this command; /// callers publish after it returns. pub fn storage_step_commit(db_id: resource, payload_json: string) -> map { - let raw_payload: map = json::decode(payload_json); - let mut failpoint: string = ""; - if raw_payload.has("failpoint") { - failpoint = raw_payload["failpoint"].copy(); - } let input: StepCommitInput = json::decode::(payload_json); + let failpoint: string = match input.failpoint.copy() { Some(value) => value, None => "" }; let mut result = { ok: true, code: "ok", message: "", result: [] }; if input.payload_json.copy().length > 65536 { result = { ok: false, code: "payload_too_large", message: "step event payload exceeds 65536 bytes", result: [] }; } else { - if !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.run_id.copy())]; + if !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "run_not_found", message: "step commit targets an unknown run", result: [] }; } else { - if input.message_id.copy() != "" && !(sqlite::query(&db_id, existence::session_exists_sql(), [sql_text(input.session_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.session_id.copy())]; + if input.message_id.copy() != "" && !(sqlite::query(&db_id, existence::session_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "session_not_found", message: "step commit targets an unknown session", result: [] }; } else { let encoded: string = messages::storage_message_encode_content(input.content_json.copy()); let max_events: int = schema::max_events_limit(input.max_events.copy()); - let mut reserved_seq: int = 0; - let mut reserved_ordinal: int = 0; - if raw_payload.has("seq") { - reserved_seq = raw_payload["seq"].copy(); - } - if raw_payload.has("ordinal") { - reserved_ordinal = raw_payload["ordinal"].copy(); - } - let mut statements = [ + let reserved_seq: int = match input.seq { Some(value) => value, None => 0 }; + let reserved_ordinal: int = match input.ordinal { Some(value) => value, None => 0 }; + let stmt_params_101: array = [sql_text(input.run_id.copy()), sql_int(reserved_seq.copy()), sql_int(reserved_seq.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id.copy()), sql_text(input.event_type.copy()), sql_text(input.payload_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.event_id.copy())]; + let stmt_params_102: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy())]; + let stmt_params_103: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy())]; + let stmt_params_104: array = [sql_text(input.message_id.copy()), sql_text(input.session_id.copy()), sql_int(reserved_ordinal.copy()), sql_int(reserved_ordinal.copy()), sql_text(input.session_id.copy()), sql_text(input.role.copy()), sql_text(encoded), sql_text(input.name.copy()), sql_text(input.tool_call_id.copy()), sql_text(input.parent_message_id.copy()), sql_int(input.token_estimate.copy()), sql_text(input.metadata_json.copy()), sql_text(input.run_id.copy()), sql_text(input.finish_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_id.copy())]; + let stmt_params_105: array = [sql_text(input.session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.message_id.copy())]; + let stmt_params_106: array = [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]; + let stmt_params_107: array = [sql_text(failpoint), sql_text(input.message_id.copy())]; + let mut statements: array = [ { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, CASE WHEN ? > 0 THEN ? ELSE COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1 END, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM run_events existing WHERE existing.event_id = ?)", - params: [sql_text(input.run_id.copy()), sql_int(reserved_seq.copy()), sql_int(reserved_seq.copy()), sql_text(input.run_id.copy()), sql_text(input.event_id.copy()), sql_text(input.event_type.copy()), sql_text(input.payload_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.event_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_101), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM run_events WHERE rowid IN (SELECT rowid FROM run_events WHERE run_id = ? ORDER BY seq ASC LIMIT CASE WHEN (SELECT COUNT(*) FROM run_events WHERE run_id = ?) > ? THEN (SELECT COUNT(*) FROM run_events WHERE run_id = ?) - ? ELSE 0 END)", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy()), sql_text(input.run_id.copy()), sql_int(max_events.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_102), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT ?, COALESCE((SELECT MIN(seq) FROM run_events WHERE run_id = ?), 0), COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id = ?), 0), ? ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_103), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT OR IGNORE INTO messages (id, session_id, ordinal, role, content_json, name, tool_call_id, parent_message_id, token_estimate, metadata_json, run_id, finish_reason, created_at_ms) SELECT ?, ?, CASE WHEN ? > 0 THEN ? ELSE COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id = ?), 0) + 1 END, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE ? != ''", - params: [sql_text(input.message_id.copy()), sql_text(input.session_id.copy()), sql_int(reserved_ordinal.copy()), sql_int(reserved_ordinal.copy()), sql_text(input.session_id.copy()), sql_text(input.role.copy()), sql_text(encoded), sql_text(input.name.copy()), sql_text(input.tool_call_id.copy()), sql_text(input.parent_message_id.copy()), sql_int(input.token_estimate.copy()), sql_text(input.metadata_json.copy()), sql_text(input.run_id.copy()), sql_text(input.finish_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_104), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE sessions SET last_message_seq = COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id = ?), 0), updated_at_ms = ? WHERE id = ? AND ? != ''", - params: [sql_text(input.session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.message_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_105), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE runs SET updated_at_ms = ? WHERE id = ?", - params: [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_106), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE messages SET compacted = 2 WHERE ? = 'after_partial_write' AND id = ?", - params: [sql_text(failpoint), sql_text(input.message_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_107), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; result = { ok: true, code: "ok", message: "", result: sqlite::transaction(&db_id, statements) }; @@ -378,30 +394,34 @@ pub fn storage_effect_reconcile(db_id: resource, payload_json let limit: int = if input.max_rows.copy() <= 0 => { 64 } else => { if input.max_rows.copy() > 256 => { 256 } else => { input.max_rows.copy() } }; + let stmt_params_14: array = [sql_int(input.now_ms.copy()), sql_int(limit.copy())]; + let stmt_params_15: array = [sql_int(input.now_ms.copy()), sql_int(limit.copy())]; + let stmt_params_16: array = [sql_int(input.now_ms.copy())]; + let stmt_params_17: array = [sql_int(input.now_ms.copy())]; let statements: array = [ { sql: "INSERT OR IGNORE INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT pending.run_id, COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = pending.run_id), 0) + pending.rn, substr('recovery-effect:' || pending.run_id || ':' || pending.tool_call_id, 1, 128), 'tool.failed', CAST(json_object('status', 'failed', 'error_code', 'interrupted_effect', 'tool_call_id', pending.tool_call_id, 'reason', 'interrupted_effect') AS TEXT), ? FROM (SELECT grouped.run_id AS run_id, grouped.tool_call_id AS tool_call_id, ROW_NUMBER() OVER (PARTITION BY grouped.run_id ORDER BY grouped.min_seq) AS rn FROM (SELECT events.run_id AS run_id, json_extract(events.payload_json, '$.tool_call_id') AS tool_call_id, MIN(events.seq) AS min_seq FROM run_events events WHERE events.event_type IN ('tool.requested', 'tool.started') AND NOT EXISTS (SELECT 1 FROM managed_effect_calls managed WHERE managed.run_id=events.run_id AND managed.call_id=json_extract(events.payload_json, '$.tool_call_id')) AND json_extract(events.payload_json, '$.tool_call_id') IS NOT NULL AND json_extract(events.payload_json, '$.tool_call_id') != '' AND NOT EXISTS (SELECT 1 FROM run_events done WHERE done.run_id = events.run_id AND done.event_type IN ('tool.output', 'tool.completed', 'tool.failed') AND json_extract(done.payload_json, '$.tool_call_id') = json_extract(events.payload_json, '$.tool_call_id')) GROUP BY events.run_id, json_extract(events.payload_json, '$.tool_call_id') LIMIT ?) grouped) pending", - params: [sql_int(input.now_ms.copy()), sql_int(limit.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_14), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT OR IGNORE INTO messages (id, session_id, ordinal, role, content_json, name, tool_call_id, parent_message_id, token_estimate, metadata_json, run_id, finish_reason, created_at_ms) SELECT substr('recovery-msg:' || pending.run_id || ':' || pending.tool_call_id, 1, 128), pending.session_id, COALESCE((SELECT MAX(ordinal) FROM messages existing WHERE existing.session_id = pending.session_id), 0) + pending.rn, 'user', CAST(json_array(json_object('type', 'tool_result', 'tool_call_id', pending.tool_call_id, 'content', '', 'is_error', json('true'), 'error', json_object('code', 'interrupted_effect', 'message', 'effect interrupted by restart'), 'truncated', json('false'))) AS TEXT), '', pending.tool_call_id, '', 0, CAST(json_object('interrupted_effect', json('true')) AS TEXT), pending.run_id, '', ? FROM (SELECT grouped.run_id AS run_id, grouped.session_id AS session_id, grouped.tool_call_id AS tool_call_id, ROW_NUMBER() OVER (PARTITION BY grouped.session_id ORDER BY grouped.min_seq) AS rn FROM (SELECT events.run_id AS run_id, runs.session_id AS session_id, json_extract(events.payload_json, '$.tool_call_id') AS tool_call_id, MIN(events.seq) AS min_seq FROM run_events events JOIN runs ON runs.id = events.run_id WHERE events.event_type = 'tool.failed' AND json_extract(events.payload_json, '$.error_code') = 'interrupted_effect' GROUP BY events.run_id, json_extract(events.payload_json, '$.tool_call_id') LIMIT ?) grouped) pending", - params: [sql_int(input.now_ms.copy()), sql_int(limit.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_15), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE sessions SET last_message_seq = COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id = sessions.id), last_message_seq), updated_at_ms = ? WHERE id IN (SELECT runs.session_id FROM runs JOIN run_events events ON events.run_id = runs.id WHERE events.event_type = 'tool.failed' AND json_extract(events.payload_json, '$.error_code') = 'interrupted_effect')", - params: [sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_16), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT runs.id, COALESCE((SELECT MIN(seq) FROM run_events events WHERE events.run_id = runs.id), 0), COALESCE((SELECT MAX(seq) FROM run_events events WHERE events.run_id = runs.id), 0), ? FROM runs WHERE EXISTS (SELECT 1 FROM run_events events WHERE events.run_id = runs.id AND events.event_type = 'tool.failed' AND json_extract(events.payload_json, '$.error_code') = 'interrupted_effect') ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(stmt_params_17), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; let results: array = sqlite::transaction(&db_id, statements); diff --git a/rss/storage/gateway.rss b/rss/storage/gateway.rss index 8807c75..b3d273b 100644 --- a/rss/storage/gateway.rss +++ b/rss/storage/gateway.rss @@ -11,31 +11,35 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } - struct GatewayCommand { op: string, kind: string, @@ -56,12 +60,14 @@ fn gateway_limits(command: GatewayCommand) -> SqliteLimits { } fn gateway_open(command: GatewayCommand) -> resource { - sqlite::open({ - path: command.db_path, - mode: command.db_mode, - root: null, - limits: sql_limits(command.max_rows, command.max_bytes) - }) + let no_root: Option = None; + let options: SqliteOpenOptions = { + path: Some(command.db_path), + mode: Some(command.db_mode), + root: no_root, + limits: Some(sql_limits(command.max_rows, command.max_bytes)) + }; + sqlite::open(options) } /// One per-record upsert: sessions/runs/jobs/idempotency replace their row by @@ -69,32 +75,39 @@ fn gateway_open(command: GatewayCommand) -> resource { /// configured `max_events` so durable history matches in-memory retention. fn gateway_put(db_id: resource, command: GatewayCommand) -> map { if command.kind.copy() == "session" => { - sqlite::execute(&db_id, "INSERT INTO gateway_sessions (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]) + let params: array = [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]; + sqlite::execute(&db_id, "INSERT INTO gateway_sessions (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", params) } else => { if command.kind.copy() == "run" => { - sqlite::execute(&db_id, "INSERT INTO gateway_runs (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]) + let params: array = [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]; + sqlite::execute(&db_id, "INSERT INTO gateway_runs (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", params) } else => { if command.kind.copy() == "job" => { - sqlite::execute(&db_id, "INSERT INTO gateway_jobs (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]) + let params: array = [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]; + sqlite::execute(&db_id, "INSERT INTO gateway_jobs (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", params) } else => { if command.kind.copy() == "event" => { - { results: sqlite::transaction(&db_id, [ + let upsert_params: array = [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]; + let prune_params: array = [sql_text(command.record_id.copy()), sql_text(command.record_id.copy()), sql_text(command.record_id.copy()), sql_text(command.record_id.copy()), sql_int(command.max_events.copy()), sql_int(command.max_events.copy()), sql_int(command.max_events.copy())]; + let event_statements: array = [ { sql: "INSERT INTO gateway_events (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", - params: [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(upsert_params), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM gateway_events WHERE substr(record_id, 1, instr(record_id, ':') - 1) = substr(?, 1, instr(?, ':') - 1) AND record_id NOT IN (SELECT record_id FROM gateway_events WHERE substr(record_id, 1, instr(record_id, ':') - 1) = substr(?, 1, instr(?, ':') - 1) ORDER BY record_id DESC LIMIT CASE WHEN ? <= 0 THEN 128 WHEN ? > 256 THEN 256 ELSE ? END)", - params: [sql_text(command.record_id.copy()), sql_text(command.record_id.copy()), sql_text(command.record_id.copy()), sql_text(command.record_id.copy()), sql_int(command.max_events.copy()), sql_int(command.max_events.copy()), sql_int(command.max_events.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(prune_params), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } - ]) } + ]; + { results: sqlite::transaction(&db_id, event_statements) } } else => { if command.kind.copy() == "idempotency" => { - sqlite::execute(&db_id, "INSERT INTO gateway_idempotency (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]) + let params: array = [sql_text(command.record_id.copy()), sql_text(command.payload_json.copy()), sql_int(command.now_ms.copy())]; + sqlite::execute(&db_id, "INSERT INTO gateway_idempotency (record_id, payload_json, updated_at_ms) VALUES (?, ?, ?) ON CONFLICT (record_id) DO UPDATE SET payload_json = excluded.payload_json, updated_at_ms = excluded.updated_at_ms", params) } else => { sqlite::execute(&db_id, "SELECT 1", []) } @@ -108,19 +121,24 @@ fn gateway_put(db_id: resource, command: GatewayCommand) -> m /// session/run cascade can remove a run's whole retained history. fn gateway_delete(db_id: resource, command: GatewayCommand) -> map { if command.kind.copy() == "event" => { - sqlite::execute(&db_id, "DELETE FROM gateway_events WHERE record_id = ? OR record_id LIKE ? || ':%'", [sql_text(command.record_id.copy()), sql_text(command.record_id.copy())]) + let params: array = [sql_text(command.record_id.copy()), sql_text(command.record_id.copy())]; + sqlite::execute(&db_id, "DELETE FROM gateway_events WHERE record_id = ? OR record_id LIKE ? || ':%'", params) } else => { if command.kind.copy() == "session" => { - sqlite::execute(&db_id, "DELETE FROM gateway_sessions WHERE record_id = ?", [sql_text(command.record_id.copy())]) + let params: array = [sql_text(command.record_id.copy())]; + sqlite::execute(&db_id, "DELETE FROM gateway_sessions WHERE record_id = ?", params) } else => { if command.kind.copy() == "run" => { - sqlite::execute(&db_id, "DELETE FROM gateway_runs WHERE record_id = ?", [sql_text(command.record_id.copy())]) + let params: array = [sql_text(command.record_id.copy())]; + sqlite::execute(&db_id, "DELETE FROM gateway_runs WHERE record_id = ?", params) } else => { if command.kind.copy() == "job" => { - sqlite::execute(&db_id, "DELETE FROM gateway_jobs WHERE record_id = ?", [sql_text(command.record_id.copy())]) + let params: array = [sql_text(command.record_id.copy())]; + sqlite::execute(&db_id, "DELETE FROM gateway_jobs WHERE record_id = ?", params) } else => { if command.kind.copy() == "idempotency" => { - sqlite::execute(&db_id, "DELETE FROM gateway_idempotency WHERE record_id = ?", [sql_text(command.record_id.copy())]) + let params: array = [sql_text(command.record_id.copy())]; + sqlite::execute(&db_id, "DELETE FROM gateway_idempotency WHERE record_id = ?", params) } else => { sqlite::execute(&db_id, "SELECT 1", []) } @@ -138,7 +156,8 @@ sqlite::execute(&db_id, "CREATE TABLE IF NOT EXISTS gateway_jobs (record_id TEXT sqlite::execute(&db_id, "CREATE TABLE IF NOT EXISTS gateway_events (record_id TEXT PRIMARY KEY, payload_json TEXT NOT NULL, updated_at_ms INTEGER NOT NULL)", []); sqlite::execute(&db_id, "CREATE TABLE IF NOT EXISTS gateway_idempotency (record_id TEXT PRIMARY KEY, payload_json TEXT NOT NULL, updated_at_ms INTEGER NOT NULL)", []); let raw_result: map = if command.op.copy() == "load" => { - sqlite::query(&db_id, "SELECT 'session' AS kind, record_id, payload_json FROM gateway_sessions UNION ALL SELECT 'run', record_id, payload_json FROM gateway_runs UNION ALL SELECT 'job', record_id, payload_json FROM gateway_jobs UNION ALL SELECT 'event', record_id, payload_json FROM gateway_events UNION ALL SELECT 'idempotency', record_id, payload_json FROM gateway_idempotency ORDER BY kind, record_id LIMIT ?", [sql_int(command.max_rows.copy())], gateway_limits(command.copy())) + let params: array = [sql_int(command.max_rows.copy())]; + sqlite::query(&db_id, "SELECT 'session' AS kind, record_id, payload_json FROM gateway_sessions UNION ALL SELECT 'run', record_id, payload_json FROM gateway_runs UNION ALL SELECT 'job', record_id, payload_json FROM gateway_jobs UNION ALL SELECT 'event', record_id, payload_json FROM gateway_events UNION ALL SELECT 'idempotency', record_id, payload_json FROM gateway_idempotency ORDER BY kind, record_id LIMIT ?", params, gateway_limits(command.copy())) } else => { if command.op.copy() == "put" => { gateway_put(db_id, command.copy()) diff --git a/rss/storage/jobs.rss b/rss/storage/jobs.rss index 765a035..fc52ef0 100644 --- a/rss/storage/jobs.rss +++ b/rss/storage/jobs.rss @@ -4,27 +4,32 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } @@ -36,8 +41,7 @@ fn sqlite_query_map(result: SqliteQueryResult) -> map { } } - -fn jobs_query_limits(max_rows: int, max_bytes: int) { +fn jobs_query_limits(max_rows: int, max_bytes: int) -> SqliteLimits { sql_limits(max_rows, max_bytes) } @@ -71,19 +75,22 @@ struct JobIdInput { pub fn storage_job_create(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> map { let input: JobCreateInput = json::decode::(payload_json); - sqlite::execute(&db_id, "INSERT INTO jobs (id, name, schedule_json, prompt, deliver_json, skills_json, repeat_count, enabled, created_at_ms, updated_at_ms) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET name = excluded.name, schedule_json = excluded.schedule_json, prompt = excluded.prompt, deliver_json = excluded.deliver_json, skills_json = excluded.skills_json, repeat_count = excluded.repeat_count, enabled = excluded.enabled, updated_at_ms = excluded.updated_at_ms", [sql_text(input.id.copy()), sql_text(input.name.copy()), sql_text(input.schedule_json.copy()), sql_text(input.prompt.copy()), sql_text(input.deliver_json.copy()), sql_text(input.skills_json.copy()), sql_int(input.repeat_count.copy()), sql_int(input.enabled.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy())]); + let params: array = [sql_text(input.id.copy()), sql_text(input.name.copy()), sql_text(input.schedule_json.copy()), sql_text(input.prompt.copy()), sql_text(input.deliver_json.copy()), sql_text(input.skills_json.copy()), sql_int(input.repeat_count.copy()), sql_int(input.enabled.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy())]; + sqlite::execute(&db_id, "INSERT INTO jobs (id, name, schedule_json, prompt, deliver_json, skills_json, repeat_count, enabled, created_at_ms, updated_at_ms) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET name = excluded.name, schedule_json = excluded.schedule_json, prompt = excluded.prompt, deliver_json = excluded.deliver_json, skills_json = excluded.skills_json, repeat_count = excluded.repeat_count, enabled = excluded.enabled, updated_at_ms = excluded.updated_at_ms", params); { ok: true, result: sqlite_query_map(storage_job_query(db_id, input.id, max_rows, max_bytes)) } } pub fn storage_job_update(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> map { let input: JobUpdateInput = json::decode::(payload_json); - sqlite::execute(&db_id, "UPDATE jobs SET name = ?, schedule_json = ?, prompt = ?, deliver_json = ?, skills_json = ?, repeat_count = ?, enabled = ?, updated_at_ms = ? WHERE id = ?", [sql_text(input.name.copy()), sql_text(input.schedule_json.copy()), sql_text(input.prompt.copy()), sql_text(input.deliver_json.copy()), sql_text(input.skills_json.copy()), sql_int(input.repeat_count.copy()), sql_int(input.enabled.copy()), sql_int(input.now_ms.copy()), sql_text(input.id.copy())]); + let params: array = [sql_text(input.name.copy()), sql_text(input.schedule_json.copy()), sql_text(input.prompt.copy()), sql_text(input.deliver_json.copy()), sql_text(input.skills_json.copy()), sql_int(input.repeat_count.copy()), sql_int(input.enabled.copy()), sql_int(input.now_ms.copy()), sql_text(input.id.copy())]; + sqlite::execute(&db_id, "UPDATE jobs SET name = ?, schedule_json = ?, prompt = ?, deliver_json = ?, skills_json = ?, repeat_count = ?, enabled = ?, updated_at_ms = ? WHERE id = ?", params); { ok: true, result: sqlite_query_map(storage_job_query(db_id, input.id, max_rows, max_bytes)) } } pub fn storage_job_delete(db_id: resource, payload_json: string) -> SqliteExecuteResult { let input: JobIdInput = json::decode::(payload_json); - sqlite::execute(&db_id, "DELETE FROM jobs WHERE id = ?", [sql_text(input.job_id.copy())]) + let params: array = [sql_text(input.job_id.copy())]; + sqlite::execute(&db_id, "DELETE FROM jobs WHERE id = ?", params) } pub fn storage_job_get(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> map { @@ -92,10 +99,12 @@ pub fn storage_job_get(db_id: resource, payload_json: string, } pub fn storage_job_list(db_id: resource, max_rows: int, max_bytes: int) -> map { - let result = sqlite::query(&db_id, "SELECT id, name, schedule_json, prompt, deliver_json, skills_json, repeat_count, enabled, output_json, created_at_ms, updated_at_ms, last_run_at_ms FROM jobs ORDER BY created_at_ms ASC, id ASC LIMIT ?", [sql_int(max_rows)], jobs_query_limits(max_rows, max_bytes)); + let params: array = [sql_int(max_rows)]; + let result = sqlite::query(&db_id, "SELECT id, name, schedule_json, prompt, deliver_json, skills_json, repeat_count, enabled, output_json, created_at_ms, updated_at_ms, last_run_at_ms FROM jobs ORDER BY created_at_ms ASC, id ASC LIMIT ?", params, jobs_query_limits(max_rows, max_bytes)); { ok: true, result: sqlite_query_map(result) } } fn storage_job_query(db_id: resource, job_id: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT id, name, schedule_json, prompt, deliver_json, skills_json, repeat_count, enabled, output_json, created_at_ms, updated_at_ms, last_run_at_ms FROM jobs WHERE id = ? LIMIT ?", [sql_text(job_id), sql_int(max_rows)], jobs_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(job_id), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT id, name, schedule_json, prompt, deliver_json, skills_json, repeat_count, enabled, output_json, created_at_ms, updated_at_ms, last_run_at_ms FROM jobs WHERE id = ? LIMIT ?", params, jobs_query_limits(max_rows, max_bytes)) } diff --git a/rss/storage/load.rss b/rss/storage/load.rss index 6a589b8..264f31e 100644 --- a/rss/storage/load.rss +++ b/rss/storage/load.rss @@ -18,27 +18,32 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } @@ -50,7 +55,6 @@ fn sqlite_query_map(result: SqliteQueryResult) -> map { } } - /// The `load.all` command payload: page bounds and the hard total-row cap. /// `load_cap` is what turns an absurd state into the typed `load_too_large` /// error (the production gateway sends 1,000,000); tests parameterize it. @@ -73,7 +77,7 @@ fn stage_limits() -> SqliteLimits { /// One bounded stage statement (execute, original connection limits). fn stage_statement(sql: string, params: array) -> SqliteStatement { - { sql: sql, params: params, query: false, limits: stage_limits() } + { sql: sql, params: Some(params), query: Some(false), limits: Some(stage_limits()) } } /// Capture all tables in one transaction; indexed original rowids preserve @@ -176,11 +180,12 @@ pub fn storage_load_all(db_id: resource, payload_json: string let mut session_index = 0; while session_index < session_count { let session_row: array = session_rows[session_index].copy(); - let session_id: string = session_row.copy()[0].text_value; + let session_id: string = session_row.copy()[0].text_value.unwrap_or(""); let mut ordinal_cursor: int = 0; let mut page_done = false; while page_done == false { - let page: SqliteQueryResult = sqlite::query(&db_id, "SELECT ordinal, id, session_id, role, content_json, name, tool_call_id, parent_message_id, token_estimate, compacted, metadata_json, run_id, finish_reason, created_at_ms FROM temp.load_stage_messages WHERE session_id = ? AND ordinal > ? ORDER BY ordinal ASC LIMIT ?", [sql_text(session_id.copy()), sql_int(ordinal_cursor), sql_int(page_size + 1)], sql_limits(page_size, 2097152)); + let params: array = [sql_text(session_id.copy()), sql_int(ordinal_cursor), sql_int(page_size + 1)]; + let page: SqliteQueryResult = sqlite::query(&db_id, "SELECT ordinal, id, session_id, role, content_json, name, tool_call_id, parent_message_id, token_estimate, compacted, metadata_json, run_id, finish_reason, created_at_ms FROM temp.load_stage_messages WHERE session_id = ? AND ordinal > ? ORDER BY ordinal ASC LIMIT ?", params, sql_limits(page_size, 2097152)); let page_rows: array = page.rows.copy(); let truncated: bool = page.truncated; let page_count: int = page_rows.length; @@ -189,7 +194,7 @@ pub fn storage_load_all(db_id: resource, payload_json: string let row: SqliteRow = page_rows[row_index].copy(); let cells: array = row.cells.copy(); if row_index == page_count - 1 { - ordinal_cursor = cells.copy()[0].int_value; + ordinal_cursor = cells.copy()[0].int_value.unwrap_or(0); } messages[messages.length] = cells; row_index += 1; @@ -216,11 +221,12 @@ pub fn storage_load_all(db_id: resource, payload_json: string let mut run_index = 0; while run_index < run_count { let run_row: array = run_rows[run_index].copy(); - let run_id: string = run_row.copy()[0].text_value; + let run_id: string = run_row.copy()[0].text_value.unwrap_or(""); let mut seq_cursor: int = 0; let mut event_page_done = false; while event_page_done == false { - let page: SqliteQueryResult = sqlite::query(&db_id, "SELECT seq, run_id, event_id, event_type, payload_json, created_at_ms FROM temp.load_stage_events WHERE run_id = ? AND seq > ? ORDER BY seq ASC LIMIT ?", [sql_text(run_id.copy()), sql_int(seq_cursor), sql_int(page_size + 1)], sql_limits(page_size, 2097152)); + let params: array = [sql_text(run_id.copy()), sql_int(seq_cursor), sql_int(page_size + 1)]; + let page: SqliteQueryResult = sqlite::query(&db_id, "SELECT seq, run_id, event_id, event_type, payload_json, created_at_ms FROM temp.load_stage_events WHERE run_id = ? AND seq > ? ORDER BY seq ASC LIMIT ?", params, sql_limits(page_size, 2097152)); let page_rows: array = page.rows.copy(); let truncated: bool = page.truncated; let page_count: int = page_rows.length; @@ -229,7 +235,7 @@ pub fn storage_load_all(db_id: resource, payload_json: string let row: SqliteRow = page_rows[row_index].copy(); let cells: array = row.cells.copy(); if row_index == page_count - 1 { - seq_cursor = cells.copy()[0].int_value; + seq_cursor = cells.copy()[0].int_value.unwrap_or(0); } events[events.length] = cells; row_index += 1; @@ -291,7 +297,8 @@ fn load_page(db_id: resource, sql: string, page_size: int, lo // Fetch page_size + 1 rows: the SQL LIMIT alone would exhaust // exactly at the host's max_rows cap without setting `truncated`, // silently losing the rest of the table. - let result: SqliteQueryResult = sqlite::query(&db_id, sql, [sql_int(last_rowid), sql_int(page_size + 1)], sql_limits(page_size, 2097152)); + let params: array = [sql_int(last_rowid), sql_int(page_size + 1)]; + let result: SqliteQueryResult = sqlite::query(&db_id, sql, params, sql_limits(page_size, 2097152)); if first { let cols: array = result.columns; columns = drop_first_column(cols); @@ -306,7 +313,7 @@ fn load_page(db_id: resource, sql: string, page_size: int, lo let row: SqliteRow = page[index].copy(); let cells: array = row.cells.copy(); if index == page_count - 1 { - last_rowid = cells.copy()[0].int_value; + last_rowid = cells.copy()[0].int_value.unwrap_or(0); } rows[rows.length] = drop_first_cells_from(cells); index += 1; @@ -333,7 +340,6 @@ fn drop_first_column(row: array) -> array { out } - fn drop_first_cells_keep_all(row: SqliteRow) -> array { let cells: array = row.cells.copy(); let mut out: array = []; diff --git a/rss/storage/main.rss b/rss/storage/main.rss index a19844f..effcf77 100644 --- a/rss/storage/main.rss +++ b/rss/storage/main.rss @@ -1,27 +1,37 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } +fn no_params() -> Option> { None } +fn has_int(value: Option) -> bool { match value { Some(_) => true, None => false } } +fn has_float(value: Option) -> bool { match value { Some(_) => true, None => false } } +fn has_string(value: Option) -> bool { match value { Some(_) => true, None => false } } +fn has_bytes(value: Option) -> bool { match value { Some(_) => true, None => false } } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int, busy_timeout_ms: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: busy_timeout_ms + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(busy_timeout_ms) } } @@ -52,7 +62,6 @@ struct StorageCommand { payload_json: string } - struct StorageResult { request_id: string, op: string, @@ -74,12 +83,10 @@ struct StorageOpenOptions { limits: map } - struct SessionIdInput { session_id: string } - struct CompactionContextSnapshot { session: SqliteQueryResult, messages: SqliteQueryResult, @@ -91,24 +98,20 @@ struct MessageIdInput { message_id: string } - struct MessageListInput { session_id: string, after_ordinal: int } - struct RunIdInput { run_id: string } - struct SessionListInput { profile: string, platform: string } - struct RunListInput { session_id: string, status: string @@ -119,13 +122,11 @@ struct ReplayFloorInput { after_seq: int } - struct DeliveryGetInput { session_id: string, consumer: string } - struct ApprovalIdInput { approval_id: string } @@ -139,15 +140,13 @@ struct CompactionIdInput { compaction_id: string } - - - fn storage_open_options(db_path: string, db_mode: string, busy_timeout_ms: int, max_rows: int, max_bytes: int) -> SqliteOpenOptions { + let no_root: Option = None; { - path: db_path, - mode: db_mode, - root: null, - limits: sql_limits(max_rows, max_bytes, busy_timeout_ms) + path: Some(db_path), + mode: Some(db_mode), + root: no_root, + limits: Some(sql_limits(max_rows, max_bytes, busy_timeout_ms)) } } @@ -163,17 +162,17 @@ fn storage_apply_migrations(db_id: resource, now_ms: int) { while statement_index < schema::schema_migration_statement_count(migration_index) { statements[statements.length] = { sql: schema::schema_migration_statement(migration_index, statement_index), - params: [], - query: false, - limits: sql_limits(1000, 4194304, 5000) + params: no_params(), + query: Some(false), + limits: Some(sql_limits(1000, 4194304, 5000)) }; statement_index += 1; } statements[statements.length] = { sql: schema::schema_migration_record_sql(), - params: [sql_int(schema::schema_migration_version(migration_index)), sql_text(schema::schema_migration_name(migration_index)), sql_text(schema::schema_migration_checksum(migration_index)), sql_int(now_ms)], - query: false, - limits: sql_limits(1000, 4194304, 5000) + params: Some([sql_int(schema::schema_migration_version(migration_index)), sql_text(schema::schema_migration_name(migration_index)), sql_text(schema::schema_migration_checksum(migration_index)), sql_int(now_ms)]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304, 5000)) }; sqlite::transaction(&db_id, statements); applied_version = migration_version; @@ -193,7 +192,7 @@ fn storage_apply_migrations(db_id: resource, now_ms: int) { /// unconditional. fn storage_applied_version(db_id: resource) -> int { let result: SqliteQueryResult = sqlite::query(&db_id, "SELECT COALESCE(MAX(version), 0) AS version FROM schema_migrations", [], sql_limits(1, 4096, 5000)); - let version: int = result.rows[0].cells[0].int_value; + let version: int = result.rows[0].cells[0].int_value.unwrap_or(0); version } @@ -294,7 +293,7 @@ fn storage_unwrap_array(request_id: string, op: string, wrapped: map) -> Storage fn storage_tx_rows_affected(entry: SqliteTransactionResult) -> int { let mut affected: int = 0; if entry.kind == "execute" { - let exec: SqliteExecuteResult = entry.execute; + let exec: SqliteExecuteResult = match entry.execute { Some(value) => value, None => {rows_affected: 0, last_insert_rowid: 0}, }; affected = exec.rows_affected; } affected @@ -320,10 +319,11 @@ fn storage_nth_rows_affected(results: array, index: int) -> int { /// One bounded SELECT owns the postmigration read snapshot. Normal entry /// migration remains separate; this path takes no explicit write reservation. fn storage_compaction_context(db_id: resource, command: StorageCommand) -> StorageResult { - // json::decode of a named record validates fields but permits extra keys. - // This internal command has exactly one data selector, never op/path authority. - let fields: map = json::decode::(command.payload_json.copy()); - if fields.length != 1 => { + // The single-selector check runs natively before the command reaches this + // bundle (GatewayStore::command validates the payload keys), because this + // dialect admits no guest-side view of an open JSON object. + let input: SessionIdInput = json::decode::(command.payload_json.copy()); + if input.session_id == "" => { storage_error(command.request_id, command.op, "invalid_payload", "context snapshot accepts only session_id") } else => { storage_compaction_context_read(db_id, command) @@ -358,20 +358,20 @@ fn storage_context_kinds(tag: int) -> array { /// Native scalar semantics stay intact: unpadding never decodes content JSON. fn storage_context_cell_valid(cell: SqliteValue, kind: string) -> bool { - let mut valid = cell.kind == kind && cell.float_value == null && cell.blob_value == null; + let mut valid = cell.kind == kind && !has_float(cell.float_value) && !has_bytes(cell.blob_value); if kind == "int" { - valid = valid && cell.int_value != null && cell.text_value == null; + valid = valid && has_int(cell.int_value) && !has_string(cell.text_value); } else if kind == "text" { - valid = valid && cell.text_value != null && cell.int_value == null; + valid = valid && has_string(cell.text_value) && !has_int(cell.int_value); } else { - valid = valid && cell.int_value == null && cell.text_value == null; + valid = valid && !has_int(cell.int_value) && !has_string(cell.text_value); } valid } fn storage_context_unpack(packed: SqliteQueryResult, command: StorageCommand) -> StorageResult { // SDK cursor is the last first-column integer: the complete footer tag. - let mut invalid = packed.next_cursor != 4; + let mut invalid = packed.next_cursor.copy().unwrap_or(-1) != 4; let mut invalid_message = "context snapshot tagged result is incomplete or invalid"; let expected: array = ["context_tag", "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12", "c13", "c14", "c15", "c16", "c17", "c18"]; let columns = packed.columns; @@ -395,7 +395,7 @@ fn storage_context_unpack(packed: SqliteQueryResult, command: StorageCommand) -> } else if !storage_context_cell_valid(cells[0].copy(), "int") { invalid = true; } else { - let tag: int = cells[0].copy().int_value; + let tag: int = cells[0].copy().int_value.unwrap_or(0); if tag < last_tag || tag < 0 || tag > 4 { invalid = true; } last_tag = tag; if tag == 0 || tag == 4 { @@ -406,7 +406,7 @@ fn storage_context_unpack(packed: SqliteQueryResult, command: StorageCommand) -> if !storage_context_cell_valid(cells[field].copy(), kind) { invalid = true; } else if field < 5 { - let value: int = cells[field].copy().int_value; + let value: int = cells[field].copy().int_value.unwrap_or(0); if tag == 0 { counts[counts.length] = value; } else if counts.length != 4 { invalid = true; } else if value != counts[field - 1].copy() { invalid = true; } @@ -447,10 +447,10 @@ fn storage_context_unpack(packed: SqliteQueryResult, command: StorageCommand) -> result = storage_error(command.request_id.copy(), command.op.copy(), "session_not_found", "context snapshot session does not exist"); } else if session_rows.length == 1 { let snapshot: CompactionContextSnapshot = { - session: {columns: storage_context_columns(1), rows: session_rows, truncated: false, next_cursor: null}, - messages: {columns: storage_context_columns(2), rows: message_rows, truncated: false, next_cursor: null}, + session: {columns: storage_context_columns(1), rows: session_rows, truncated: false, next_cursor: no_int()}, + messages: {columns: storage_context_columns(2), rows: message_rows, truncated: false, next_cursor: no_int()}, summary_loaded: loaded, - latest: {columns: storage_context_columns(3), rows: latest_rows, truncated: false, next_cursor: null} + latest: {columns: storage_context_columns(3), rows: latest_rows, truncated: false, next_cursor: no_int()} }; result = storage_ok(command.request_id.copy(), command.op.copy(), {session: snapshot.session, messages: snapshot.messages, summary_loaded: snapshot.summary_loaded, latest: snapshot.latest}, 0, false, 0); } @@ -464,7 +464,8 @@ fn storage_compaction_context_read(db_id: resource, command: let max_rows = if command.max_rows < 512 => { command.max_rows } else => { 512 }; let max_bytes = if command.max_bytes < 2097152 => { command.max_bytes } else => { 2097152 }; let page_size = storage_context_page_size(); - let packed: SqliteQueryResult = sqlite::query(&db_id, storage_context_sql(), [sql_text(input.session_id.copy()), sql_int(1), sql_text(input.session_id.copy()), sql_int(0), sql_int(page_size), sql_text(input.session_id.copy()), sql_int(page_size), sql_int(max_rows), sql_int(page_size), sql_int(page_size)], sql_limits(max_rows, max_bytes, 5000)); + let params: array = [sql_text(input.session_id.copy()), sql_int(1), sql_text(input.session_id.copy()), sql_int(0), sql_int(page_size), sql_text(input.session_id.copy()), sql_int(page_size), sql_int(max_rows), sql_int(page_size), sql_int(page_size)]; + let packed: SqliteQueryResult = sqlite::query(&db_id, storage_context_sql(), params, sql_limits(max_rows, max_bytes, 5000)); storage_context_unpack(packed, command) } diff --git a/rss/storage/messages.rss b/rss/storage/messages.rss index d3e6631..d4f99e8 100644 --- a/rss/storage/messages.rss +++ b/rss/storage/messages.rss @@ -1,34 +1,48 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } + +fn empty_params() -> array { + [] +} + +fn empty_limits() -> SqliteLimits { + let empty: SqliteLimits = {}; + empty +} + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } use self::schema as schema; use self::existence as existence; -fn messages_query_limits(max_rows: int, max_bytes: int) { +fn messages_query_limits(max_rows: int, max_bytes: int) -> SqliteLimits { sql_limits(max_rows, if max_bytes < 4096 => { 4096 } else => { max_bytes }) } @@ -39,18 +53,21 @@ fn messages_query_limits(max_rows: int, max_bytes: int) { /// characters so the 1 MiB CHECK cannot split a multi-byte scalar. pub fn storage_message_encode_content(content_json: string) -> string { let source: string = content_json.copy(); - let db_id = sqlite::open({ - path: ":memory:", - mode: "memory", - root: null, - limits: sql_limits(1, 1048576) - }); - let classified: SqliteQueryResult = sqlite::query(&db_id, "SELECT CASE WHEN json_valid(payload) AND json_type(payload) = 'array' THEN 1 ELSE 0 END, CASE WHEN json_valid(payload) AND json_type(payload) = 'object' AND json_extract(payload, '$.type') IS NOT NULL THEN 1 ELSE 0 END FROM (SELECT ? AS payload)", [sql_text(source.copy())], sql_limits(1, 4096)); + let no_root: Option = None; + let options: SqliteOpenOptions = { + path: Some(":memory:"), + mode: Some("memory"), + root: no_root, + limits: Some(sql_limits(1, 1048576)) + }; + let db_id = sqlite::open(options); + let params: array = [sql_text(source.copy())]; + let classified: SqliteQueryResult = sqlite::query(&db_id, "SELECT CASE WHEN json_valid(payload) AND json_type(payload) = 'array' THEN 1 ELSE 0 END, CASE WHEN json_valid(payload) AND json_type(payload) = 'object' AND json_extract(payload, '$.type') IS NOT NULL THEN 1 ELSE 0 END FROM (SELECT ? AS payload)", params, sql_limits(1, 4096)); let class_rows: array = classified.rows; let class_row: SqliteRow = class_rows[0].copy(); let class_cells: array = class_row.cells.copy(); - let is_array: int = class_cells[0].int_value; - let is_block: int = class_cells[1].int_value; + let is_array: int = class_cells.copy()[0].int_value.unwrap_or(0); + let is_block: int = class_cells.copy()[1].int_value.unwrap_or(0); let mut encoded: string = source.copy(); if is_array.copy() == 1 { encoded = source.copy(); @@ -58,10 +75,11 @@ pub fn storage_message_encode_content(content_json: string) -> string { if is_block.copy() == 1 { encoded = "[" + source.copy() + "]"; } else { - let cut: SqliteQueryResult = sqlite::query(&db_id, "SELECT CASE WHEN length(extracted) > 65536 THEN CAST(json_array(json_object('type', 'text', 'text', substr(extracted, 1, 65536), 'truncated', json('true'))) AS TEXT) ELSE CAST(json_array(json_object('type', 'text', 'text', extracted)) AS TEXT) END AS encoded FROM (SELECT CASE WHEN json_valid(payload) AND json_type(payload) = 'object' AND json_extract(payload, '$.text') IS NOT NULL THEN json_extract(payload, '$.text') WHEN json_valid(payload) AND json_type(payload) = 'text' THEN json_extract(payload, '$') ELSE payload END AS extracted FROM (SELECT ? AS payload))", [sql_text(source.copy())], sql_limits(1, 1048576)); + let params: array = [sql_text(source.copy())]; + let cut: SqliteQueryResult = sqlite::query(&db_id, "SELECT CASE WHEN length(extracted) > 65536 THEN CAST(json_array(json_object('type', 'text', 'text', substr(extracted, 1, 65536), 'truncated', json('true'))) AS TEXT) ELSE CAST(json_array(json_object('type', 'text', 'text', extracted)) AS TEXT) END AS encoded FROM (SELECT CASE WHEN json_valid(payload) AND json_type(payload) = 'object' AND json_extract(payload, '$.text') IS NOT NULL THEN json_extract(payload, '$.text') WHEN json_valid(payload) AND json_type(payload) = 'text' THEN json_extract(payload, '$') ELSE payload END AS extracted FROM (SELECT ? AS payload))", params, sql_limits(1, 1048576)); let cut_rows: array = cut.rows; let cut_row: SqliteRow = cut_rows[0].copy(); - encoded = cut_row.cells[0].text_value; + encoded = cut_row.cells[0].text_value.unwrap_or(""); } } sqlite::close(db_id); @@ -101,43 +119,46 @@ struct MessageCompactInput { pub fn storage_message_append(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> map { let input: MessageAppendInput = json::decode::(payload_json); let mut result = { ok: true, code: "ok", message: "", result: {} }; - if !(sqlite::query(&db_id, existence::session_exists_sql(), [sql_text(input.session_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.session_id.copy())]; + if !(sqlite::query(&db_id, existence::session_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "session_not_found", message: "message append targets an unknown session", result: {} }; } else { let encoded: string = storage_message_encode_content(input.content_json.copy()); let statements: array = [ { sql: "INSERT OR IGNORE INTO messages (id, session_id, ordinal, role, content_json, name, tool_call_id, parent_message_id, token_estimate, metadata_json, run_id, finish_reason, created_at_ms) SELECT ?, ?, COALESCE(MAX(ordinal), 0) + 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM messages WHERE session_id = ? AND NOT EXISTS (SELECT 1 FROM messages existing WHERE existing.id = ? AND existing.session_id <> ?)", - params: [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_text(input.role.copy()), sql_text(encoded), sql_text(input.name.copy()), sql_text(input.tool_call_id.copy()), sql_text(input.parent_message_id.copy()), sql_int(input.token_estimate.copy()), sql_text(input.metadata_json.copy()), sql_text(input.run_id.copy()), sql_text(input.finish_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_text(input.role.copy()), sql_text(encoded), sql_text(input.name.copy()), sql_text(input.tool_call_id.copy()), sql_text(input.parent_message_id.copy()), sql_int(input.token_estimate.copy()), sql_text(input.metadata_json.copy()), sql_text(input.run_id.copy()), sql_text(input.finish_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE sessions SET last_message_seq = COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id = ?), 0), updated_at_ms = ? WHERE id = ?", - params: [sql_text(input.session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; sqlite::transaction(&db_id, statements); + let params: array = [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)]; result = { ok: true, code: "ok", message: "", - result: sqlite::query(&db_id, storage_message_select_sql("WHERE id = ? AND session_id = ? LIMIT ?"), [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)], messages_query_limits(max_rows, max_bytes)) + result: sqlite::query(&db_id, storage_message_select_sql("WHERE id = ? AND session_id = ? LIMIT ?"), params, messages_query_limits(max_rows, max_bytes)) }; } result } pub fn storage_message_get(db_id: resource, message_id: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, storage_message_select_sql("WHERE id = ? LIMIT ?"), [sql_text(message_id), sql_int(max_rows)], messages_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(message_id), sql_int(max_rows)]; + sqlite::query(&db_id, storage_message_select_sql("WHERE id = ? LIMIT ?"), params, messages_query_limits(max_rows, max_bytes)) } pub fn storage_message_list(db_id: resource, session_id: string, after_ordinal: int, max_rows: int, max_bytes: int) -> SqliteQueryResult { let statement = storage_message_list_statement(session_id, after_ordinal, max_rows, max_bytes); - let params: array = statement.params; - let limits: SqliteLimits = statement.limits; + let params: array = statement.params.unwrap_or(empty_params()); + let limits: SqliteLimits = statement.limits.unwrap_or(empty_limits()); sqlite::query(&db_id, statement.sql, params, limits) } @@ -146,10 +167,11 @@ pub fn storage_message_list_sql() -> string { } pub fn storage_message_list_statement(session_id: string, after_ordinal: int, max_rows: int, max_bytes: int) -> SqliteStatement { - { sql: storage_message_list_sql(), params: [sql_text(session_id.copy()), sql_int(after_ordinal), sql_int(max_rows)], query: true, limits: sql_limits(max_rows, if max_bytes < 4096 => { 4096 } else => { max_bytes }) } + { sql: storage_message_list_sql(), params: Some([sql_text(session_id.copy()), sql_int(after_ordinal), sql_int(max_rows)]), query: Some(true), limits: Some(sql_limits(max_rows, if max_bytes < 4096 => { 4096 } else => { max_bytes })) } } pub fn storage_message_mark_compacted(db_id: resource, payload_json: string) -> SqliteExecuteResult { let input: MessageCompactInput = json::decode::(payload_json); - sqlite::execute(&db_id, "UPDATE messages SET compacted = 1 WHERE session_id = ? AND ordinal >= ? AND ordinal <= ? AND EXISTS (SELECT 1 FROM compactions WHERE session_id = ? AND source_start_ordinal = ? AND source_end_ordinal = ? AND state = 'committed')", [sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy())]) + let params: array = [sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy()), sql_text(input.session_id.copy()), sql_int(input.start_ordinal.copy()), sql_int(input.end_ordinal.copy())]; + sqlite::execute(&db_id, "UPDATE messages SET compacted = 1 WHERE session_id = ? AND ordinal >= ? AND ordinal <= ? AND EXISTS (SELECT 1 FROM compactions WHERE session_id = ? AND source_start_ordinal = ? AND source_end_ordinal = ? AND state = 'committed')", params) } diff --git a/rss/storage/plugins.rss b/rss/storage/plugins.rss index 2d18e99..871837e 100644 --- a/rss/storage/plugins.rss +++ b/rss/storage/plugins.rss @@ -4,27 +4,35 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } +fn no_params() -> Option> { None } +fn has_int(value: Option) -> bool { match value { Some(_) => true, None => false } } +fn has_string(value: Option) -> bool { match value { Some(_) => true, None => false } } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn plugin_limits() -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: 512, - max_columns: 24, - max_result_bytes: 1048576, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(512), + max_columns: Some(24), + max_result_bytes: Some(1048576), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } @@ -51,7 +59,7 @@ fn plugin_control_error(code: string, message: string) -> map { fn plugin_tx_rows(entry: SqliteTransactionResult) -> int { let mut affected: int = 0; if entry.kind == "execute" { - let exec: SqliteExecuteResult = entry.execute; + let exec: SqliteExecuteResult = match entry.execute { Some(value) => value, None => {rows_affected: 0, last_insert_rowid: 0}, }; affected = exec.rows_affected; } affected @@ -60,15 +68,15 @@ fn plugin_tx_rows(entry: SqliteTransactionResult) -> int { fn plugin_query_int(entry: SqliteTransactionResult, column: int) -> int { let mut value: int = -1; if entry.kind == "query" { - let queried: SqliteQueryResult = entry.query; + let queried: SqliteQueryResult = match entry.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; let rows = queried.rows.copy(); if rows.length > 0 { let row: SqliteRow = rows[0].copy(); let cells = row.cells.copy(); if cells.length > column { let cell: SqliteValue = cells[column].copy(); - if cell.kind == "int" && cell.int_value.copy() != null { - value = cell.int_value; + if cell.kind == "int" && has_int(cell.int_value.copy()) { + value = cell.int_value.copy().unwrap_or(value); } } } @@ -79,15 +87,15 @@ fn plugin_query_int(entry: SqliteTransactionResult, column: int) -> int { fn plugin_query_text(entry: SqliteTransactionResult, column: int) -> string { let mut value: string = ""; if entry.kind == "query" { - let queried: SqliteQueryResult = entry.query; + let queried: SqliteQueryResult = match entry.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; let rows = queried.rows.copy(); if rows.length > 0 { let row: SqliteRow = rows[0].copy(); let cells = row.cells.copy(); if cells.length > column { let cell: SqliteValue = cells[column].copy(); - if cell.kind == "text" && cell.text_value.copy() != null { - value = cell.text_value; + if cell.kind == "text" && has_string(cell.text_value.copy()) { + value = cell.text_value.copy().unwrap_or(value.copy()); } } } @@ -139,21 +147,21 @@ pub fn storage_plugin_lock_put(db_id: resource, payload_json: let statements: array = [ { sql: "INSERT INTO plugin_package_refs (graph_id, package_id, version, kind, artifact_id) SELECT ?, json_extract(value, '$.package_id'), json_extract(value, '$.version'), json_extract(value, '$.kind'), json_extract(value, '$.artifact_id') FROM json_each(?)", - params: [sql_text(input.graph_id.copy()), sql_text(input.packages_json.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_text(input.packages_json.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_graph_locks (graph_id, lock_json, stored_at_ms) SELECT ?, ?, ? WHERE (SELECT COUNT(*) FROM plugin_package_refs WHERE graph_id = ?) = ? AND NOT EXISTS (SELECT 1 FROM plugin_graph_locks WHERE graph_id = ?)", - params: [sql_text(input.graph_id.copy()), sql_text(input.lock_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy()), sql_text(input.graph_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_text(input.lock_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy()), sql_text(input.graph_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE ? = 'after_partial_write' OR (SELECT changes()) = 0", - params: [sql_text(input.failpoint.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.failpoint.copy())]), + query: Some(false), + limits: Some(plugin_limits()), } ]; let transaction_result = sqlite::transaction(&db_id, statements); @@ -164,7 +172,8 @@ pub fn storage_plugin_lock_put(db_id: resource, payload_json: pub fn storage_plugin_lock_get(db_id: resource, payload_json: string) -> map { let input: PluginGraphInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT lock_json FROM plugin_graph_locks WHERE graph_id = ? LIMIT 1", [sql_text(input.graph_id.copy())], plugin_limits())) + let params: array = [sql_text(input.graph_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT lock_json FROM plugin_graph_locks WHERE graph_id = ? LIMIT 1", params, plugin_limits())) } pub fn storage_plugin_lock_list(db_id: resource) -> map { @@ -173,7 +182,8 @@ pub fn storage_plugin_lock_list(db_id: resource) -> map { pub fn storage_plugin_ref_list(db_id: resource, payload_json: string) -> map { let input: PluginGraphInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT package_id, version, kind, artifact_id FROM plugin_package_refs WHERE graph_id = ? ORDER BY rowid", [sql_text(input.graph_id.copy())], plugin_limits())) + let params: array = [sql_text(input.graph_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT package_id, version, kind, artifact_id FROM plugin_package_refs WHERE graph_id = ? ORDER BY rowid", params, plugin_limits())) } pub fn storage_plugin_activate(db_id: resource, payload_json: string) -> map { @@ -182,27 +192,27 @@ pub fn storage_plugin_activate(db_id: resource, payload_json: let statements: array = [ { sql: "INSERT INTO plugin_activation_history (graph_id, generation, previous_graph_id, activated_at_ms) SELECT ?, ?, COALESCE((SELECT graph_id FROM plugin_activation), ''), ? WHERE EXISTS (SELECT 1 FROM plugin_graph_locks WHERE graph_id = ?) AND (SELECT COUNT(*) FROM plugin_package_refs WHERE graph_id = ?) = ? AND (SELECT COUNT(*) FROM json_each(?) AS provided WHERE EXISTS (SELECT 1 FROM plugin_package_refs AS stored WHERE stored.graph_id = ? AND stored.package_id = json_extract(provided.value, '$.package_id') AND stored.version = json_extract(provided.value, '$.version') AND stored.kind = json_extract(provided.value, '$.kind') AND stored.artifact_id = json_extract(provided.value, '$.artifact_id'))) = ? AND (((SELECT COUNT(*) FROM plugin_activation) = 0 AND ? = 0) OR (SELECT generation FROM plugin_activation) = ?)", - params: [sql_text(input.graph_id.copy()), sql_int(next_generation.copy()), sql_int(input.now_ms.copy()), sql_text(input.graph_id.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy()), sql_text(input.packages_json.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy()), sql_int(input.expected_generation.copy()), sql_int(input.expected_generation.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_int(next_generation.copy()), sql_int(input.now_ms.copy()), sql_text(input.graph_id.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy()), sql_text(input.packages_json.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy()), sql_int(input.expected_generation.copy()), sql_int(input.expected_generation.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_activation (id, graph_id, generation, updated_at_ms) SELECT 1, ?, ?, ? WHERE (SELECT changes()) = 1 ON CONFLICT (id) DO UPDATE SET graph_id = excluded.graph_id, generation = excluded.generation, updated_at_ms = excluded.updated_at_ms", - params: [sql_text(input.graph_id.copy()), sql_int(next_generation.copy()), sql_int(input.now_ms.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_int(next_generation.copy()), sql_int(input.now_ms.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE ? = 'after_partial_write'", - params: [sql_text(input.failpoint.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.failpoint.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT CASE WHEN EXISTS (SELECT 1 FROM plugin_graph_locks WHERE graph_id = ?) THEN 1 ELSE 0 END, COALESCE((SELECT generation FROM plugin_activation), -1), CASE WHEN (SELECT COUNT(*) FROM plugin_package_refs WHERE graph_id = ?) = ? AND (SELECT COUNT(*) FROM json_each(?) AS provided WHERE EXISTS (SELECT 1 FROM plugin_package_refs AS stored WHERE stored.graph_id = ? AND stored.package_id = json_extract(provided.value, '$.package_id') AND stored.version = json_extract(provided.value, '$.version') AND stored.kind = json_extract(provided.value, '$.kind') AND stored.artifact_id = json_extract(provided.value, '$.artifact_id'))) = ? THEN 1 ELSE 0 END", - params: [sql_text(input.graph_id.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy()), sql_text(input.packages_json.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy()), sql_text(input.packages_json.copy()), sql_text(input.graph_id.copy()), sql_int(input.package_count.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let transaction_result: array = sqlite::transaction(&db_id, statements); @@ -243,7 +253,8 @@ pub fn storage_plugin_activation_history(db_id: resource) -> pub fn storage_plugin_retention_get(db_id: resource, payload_json: string) -> map { let input: PluginGraphInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT graph_id, owner_kind, owner_id, ref_id FROM plugin_graph_refs WHERE graph_id = ? ORDER BY owner_kind, owner_id, ref_id", [sql_text(input.graph_id.copy())], plugin_limits())) + let params: array = [sql_text(input.graph_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT graph_id, owner_kind, owner_id, ref_id FROM plugin_graph_refs WHERE graph_id = ? ORDER BY owner_kind, owner_id, ref_id", params, plugin_limits())) } pub fn storage_plugin_retain(db_id: resource, payload_json: string) -> map { @@ -251,21 +262,21 @@ pub fn storage_plugin_retain(db_id: resource, payload_json: s let statements: array = [ { sql: "INSERT INTO plugin_graph_refs (graph_id, owner_kind, owner_id, ref_id, retained_at_ms) SELECT ?, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM plugin_graph_locks WHERE graph_id = ?) AND ? IN ('run', 'checkpoint') AND NOT EXISTS (SELECT 1 FROM plugin_graph_refs WHERE graph_id = ? AND owner_kind = ? AND owner_id = ? AND ref_id <> ?) ON CONFLICT (graph_id, owner_kind, owner_id) DO NOTHING", - params: [sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.ref_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.ref_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.ref_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.ref_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE ? = 'after_partial_write'", - params: [sql_text(input.failpoint.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.failpoint.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT CASE WHEN EXISTS (SELECT 1 FROM plugin_graph_locks WHERE graph_id = ?) THEN 1 ELSE 0 END, COALESCE((SELECT ref_id FROM plugin_graph_refs WHERE graph_id = ? AND owner_kind = ? AND owner_id = ?), '')", - params: [sql_text(input.graph_id.copy()), sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let transaction_result: array = sqlite::transaction(&db_id, statements); @@ -307,21 +318,21 @@ pub fn storage_plugin_release(db_id: resource, payload_json: let statements: array = [ { sql: "DELETE FROM plugin_graph_refs WHERE graph_id = ? AND owner_kind = ? AND owner_id = ? AND ref_id = ?", - params: [sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.ref_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.ref_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE ? = 'after_partial_write'", - params: [sql_text(input.failpoint.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.failpoint.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT COALESCE((SELECT ref_id FROM plugin_graph_refs WHERE graph_id = ? AND owner_kind = ? AND owner_id = ?), '')", - params: [sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let transaction_result: array = sqlite::transaction(&db_id, statements); @@ -346,12 +357,12 @@ struct PluginPinFixtureInput { owner_id: string, mutation: string } pub fn storage_fixture_pin_state(db_id: resource, payload_json: string) -> map { let input: PluginPinFixtureInput = json::decode::(payload_json); let statements: array = [ - {sql:"DELETE FROM plugin_graph_pin_requirements WHERE run_id=? AND ?='drop_requirement'",params:[sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())],query:false,limits:plugin_limits()}, - {sql:"DELETE FROM managed_run_bindings WHERE run_id=? AND ?='drop_binding'",params:[sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())],query:false,limits:plugin_limits()}, - {sql:"UPDATE plugin_graph_refs SET ref_id='fixture-replacement-ref' WHERE owner_kind='run' AND owner_id=? AND ?='replace_ref'",params:[sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())],query:false,limits:plugin_limits()}, - {sql:"UPDATE managed_restore_receipts SET envelope_json=json_set(envelope_json,'$.result.graph.harness_selection',json_object('mode','tool_only','tool_package',json_extract(envelope_json,'$.result.graph.harness_selection.tool_package'))),result_json=json_set(result_json,'$.graph.harness_selection',json_object('mode','tool_only','tool_package',json_extract(result_json,'$.graph.harness_selection.tool_package'))) WHERE request_id=? AND ?='forge_restore_selection'",params:[sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())],query:false,limits:plugin_limits()}, - {sql:"DELETE FROM plugin_package_refs WHERE kind='library' AND graph_id IN (SELECT graph_id FROM plugin_graph_pin_requirements WHERE run_id=?) AND ?='drop_package_ref'",params:[sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())],query:false,limits:plugin_limits()}, - {sql:"UPDATE idempotency_records SET response_json=json_set(response_json,'$.harness_selection',json_object('mode','tool_only','tool_package',json_extract(response_json,'$.harness_selection.tool_package'))) WHERE resource_id=? AND ?='forge_run_selection'",params:[sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())],query:false,limits:plugin_limits()} + {sql:"DELETE FROM plugin_graph_pin_requirements WHERE run_id=? AND ?='drop_requirement'",params: Some([sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"DELETE FROM managed_run_bindings WHERE run_id=? AND ?='drop_binding'",params: Some([sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"UPDATE plugin_graph_refs SET ref_id='fixture-replacement-ref' WHERE owner_kind='run' AND owner_id=? AND ?='replace_ref'",params: Some([sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"UPDATE managed_restore_receipts SET envelope_json=json_set(envelope_json,'$.result.graph.harness_selection',json_object('mode','tool_only','tool_package',json_extract(envelope_json,'$.result.graph.harness_selection.tool_package'))),result_json=json_set(result_json,'$.graph.harness_selection',json_object('mode','tool_only','tool_package',json_extract(result_json,'$.graph.harness_selection.tool_package'))) WHERE request_id=? AND ?='forge_restore_selection'",params: Some([sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"DELETE FROM plugin_package_refs WHERE kind='library' AND graph_id IN (SELECT graph_id FROM plugin_graph_pin_requirements WHERE run_id=?) AND ?='drop_package_ref'",params: Some([sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"UPDATE idempotency_records SET response_json=json_set(response_json,'$.harness_selection',json_object('mode','tool_only','tool_package',json_extract(response_json,'$.harness_selection.tool_package'))) WHERE resource_id=? AND ?='forge_run_selection'",params: Some([sql_text(input.owner_id.copy()),sql_text(input.mutation.copy())]),query: Some(false),limits: Some(plugin_limits())} ]; let result = sqlite::transaction(&db_id, statements); let _used = result; @@ -360,14 +371,15 @@ pub fn storage_fixture_pin_state(db_id: resource, payload_jso pub fn storage_plugin_pin_requirement_get(db_id: resource, payload_json: string) -> map { let input: PluginOwnerInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT run_id, graph_id, ref_id, required_mode FROM plugin_graph_pin_requirements WHERE run_id = ? LIMIT 1", [sql_text(input.owner_id.copy())], plugin_limits())) + let params: array = [sql_text(input.owner_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT run_id, graph_id, ref_id, required_mode FROM plugin_graph_pin_requirements WHERE run_id = ? LIMIT 1", params, plugin_limits())) } - struct HarnessBindingQuery {owner_id:string,profile:string,session_id:string,workspace_id:string,branch_id:string,epoch:int,deadline_ms:int,graph_id:string,ref_id:string} pub fn storage_harness_binding_matches(db_id:resource,payload_json:string)->map { let input:HarnessBindingQuery=json::decode::(payload_json); - ok_query(sqlite::query(&db_id,"SELECT 1 FROM managed_run_bindings b JOIN plugin_graph_pin_requirements q ON q.run_id=b.run_id AND q.ref_id=b.graph_ref_id AND q.required_mode='required' JOIN plugin_graph_refs g ON g.graph_id=q.graph_id AND g.ref_id=q.ref_id AND g.owner_kind='run' AND g.owner_id=b.run_id WHERE b.run_id=? AND b.profile=? AND b.session_id=? AND b.workspace_id=? AND b.branch_id=? AND b.epoch=? AND b.original_deadline_ms=? AND q.graph_id=? AND q.ref_id=? LIMIT 1",[sql_text(input.owner_id.copy()),sql_text(input.profile.copy()),sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_int(input.epoch.copy()),sql_int(input.deadline_ms.copy()),sql_text(input.graph_id.copy()),sql_text(input.ref_id.copy())],plugin_limits())) + let params: array = [sql_text(input.owner_id.copy()),sql_text(input.profile.copy()),sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_int(input.epoch.copy()),sql_int(input.deadline_ms.copy()),sql_text(input.graph_id.copy()),sql_text(input.ref_id.copy())]; + ok_query(sqlite::query(&db_id,"SELECT 1 FROM managed_run_bindings b JOIN plugin_graph_pin_requirements q ON q.run_id=b.run_id AND q.ref_id=b.graph_ref_id AND q.required_mode='required' JOIN plugin_graph_refs g ON g.graph_id=q.graph_id AND g.ref_id=q.ref_id AND g.owner_kind='run' AND g.owner_id=b.run_id WHERE b.run_id=? AND b.profile=? AND b.session_id=? AND b.workspace_id=? AND b.branch_id=? AND b.epoch=? AND b.original_deadline_ms=? AND q.graph_id=? AND q.ref_id=? LIMIT 1",params,plugin_limits())) } pub fn storage_plugin_pin_for_owner(db_id: resource, payload_json: string) -> map { @@ -386,32 +398,33 @@ pub fn storage_plugin_graph_revoke(db_id: resource, payload_j let statements: array = [ { sql: "INSERT INTO plugin_graph_revocations (graph_id, revoked_at_ms) SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?)", - params: [sql_text(input.graph_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.graph_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.graph_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE ? = 'after_partial_write'", - params: [sql_text(input.failpoint.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.failpoint.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT graph_id, revoked_at_ms FROM plugin_graph_revocations WHERE graph_id = ? LIMIT 1", - params: [sql_text(input.graph_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.graph_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let result: array = sqlite::transaction(&db_id, statements); let last: SqliteTransactionResult = result[2].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; ok_query(query) } pub fn storage_plugin_graph_revocation_get(db_id: resource, payload_json: string) -> map { let input: PluginGraphInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT graph_id, revoked_at_ms FROM plugin_graph_revocations WHERE graph_id = ? LIMIT 1", [sql_text(input.graph_id.copy())], plugin_limits())) + let params: array = [sql_text(input.graph_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT graph_id, revoked_at_ms FROM plugin_graph_revocations WHERE graph_id = ? LIMIT 1", params, plugin_limits())) } pub fn storage_plugin_collect(db_id: resource, payload_json: string) -> map { @@ -420,21 +433,21 @@ pub fn storage_plugin_collect(db_id: resource, payload_json: let statements: array = [ { sql: "DELETE FROM plugin_package_refs WHERE graph_id IN (SELECT graph_id FROM plugin_graph_locks WHERE graph_id NOT IN (SELECT graph_id FROM plugin_activation) AND graph_id NOT IN (SELECT graph_id FROM plugin_graph_refs))", - params: [], - query: false, - limits: plugin_limits(), + params: no_params(), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "DELETE FROM plugin_graph_locks WHERE graph_id NOT IN (SELECT graph_id FROM plugin_activation) AND graph_id NOT IN (SELECT graph_id FROM plugin_graph_refs)", - params: [], - query: false, - limits: plugin_limits(), + params: no_params(), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE ? = 'after_partial_write'", - params: [sql_text(input.failpoint.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.failpoint.copy())]), + query: Some(false), + limits: Some(plugin_limits()), } ]; let transaction_result = sqlite::transaction(&db_id, statements); @@ -446,9 +459,9 @@ pub fn storage_plugin_collect(db_id: resource, payload_json: fn storage_pin_requirement_statement(graph_id: string, ref_id: string, run_id: string, session_id: string) -> SqliteStatement { { sql: "INSERT INTO plugin_graph_pin_requirements (run_id, graph_id, ref_id, required_mode) SELECT ?, ?, ?, 'required' WHERE (EXISTS (SELECT 1 FROM runs WHERE id = ? AND session_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements existing WHERE existing.run_id = ? AND (existing.graph_id <> ? OR existing.ref_id <> ?))) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy()), sql_text(run_id.copy()), sql_text(session_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy()), sql_text(run_id.copy()), sql_text(session_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), } } @@ -456,48 +469,48 @@ pub fn storage_admission_pin_statements(graph_id: string, generation: int, ref_i [ { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE (NOT EXISTS (SELECT 1 FROM plugin_activation WHERE graph_id = ? AND generation = ?) OR NOT EXISTS (SELECT 1 FROM plugin_graph_locks WHERE graph_id = ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(graph_id.copy()), sql_int(generation.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(graph_id.copy()), sql_int(generation.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_graph_refs (graph_id, owner_kind, owner_id, ref_id, retained_at_ms) SELECT ?, 'run', ?, ?, ? WHERE (EXISTS (SELECT 1 FROM plugin_activation WHERE graph_id = ? AND generation = ?) AND EXISTS (SELECT 1 FROM runs WHERE id = ? AND session_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_refs existing WHERE existing.owner_kind = 'run' AND existing.owner_id = ? AND (existing.graph_id <> ? OR existing.ref_id <> ?))) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(ref_id.copy()), sql_int(now_ms.copy()), sql_text(graph_id.copy()), sql_int(generation.copy()), sql_text(run_id.copy()), sql_text(session_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(ref_id.copy()), sql_int(now_ms.copy()), sql_text(graph_id.copy()), sql_int(generation.copy()), sql_text(run_id.copy()), sql_text(session_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE (NOT EXISTS (SELECT 1 FROM plugin_graph_refs WHERE owner_kind = 'run' AND owner_id = ? AND graph_id = ? AND ref_id = ?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ", - params: [sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, storage_pin_requirement_statement(graph_id.copy(), ref_id.copy(), run_id.copy(), session_id.copy()), { sql: "SELECT refs.graph_id, refs.ref_id, activation.generation FROM plugin_graph_refs refs JOIN plugin_activation activation ON activation.graph_id = refs.graph_id AND activation.generation = ? WHERE refs.owner_kind = 'run' AND refs.owner_id = ? AND refs.ref_id = ?", - params: [sql_int(generation.copy()), sql_text(run_id.copy()), sql_text(ref_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_int(generation.copy()), sql_text(run_id.copy()), sql_text(ref_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ] } pub fn storage_checkpoint_pin_statements(graph_id:string,generation:int,ref_id:string,run_id:string,session_id:string,now_ms:int,checkpoint_id:string,checkpoint_ref:string)->array { [ -{sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM plugin_graph_refs WHERE graph_id=? AND owner_kind='checkpoint' AND owner_id=? AND ref_id=?) OR NOT EXISTS(SELECT 1 FROM plugin_graph_locks WHERE graph_id=?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[sql_text(graph_id.copy()),sql_text(checkpoint_id.copy()),sql_text(checkpoint_ref.copy()),sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())],query:false,limits:plugin_limits()}, -{sql:"INSERT INTO plugin_graph_refs(graph_id,owner_kind,owner_id,ref_id,retained_at_ms) SELECT ?,'run',?,?,? WHERE (EXISTS(SELECT 1 FROM runs WHERE id=? AND session_id=?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[sql_text(graph_id.copy()),sql_text(run_id.copy()),sql_text(ref_id.copy()),sql_int(now_ms.copy()),sql_text(run_id.copy()),sql_text(session_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())],query:false,limits:plugin_limits()}, -{sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM plugin_graph_refs WHERE graph_id=? AND owner_kind='run' AND owner_id=? AND ref_id=?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params:[sql_text(graph_id.copy()),sql_text(run_id.copy()),sql_text(ref_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())],query:false,limits:plugin_limits()}, +{sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM plugin_graph_refs WHERE graph_id=? AND owner_kind='checkpoint' AND owner_id=? AND ref_id=?) OR NOT EXISTS(SELECT 1 FROM plugin_graph_locks WHERE graph_id=?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([sql_text(graph_id.copy()),sql_text(checkpoint_id.copy()),sql_text(checkpoint_ref.copy()),sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())]),query: Some(false),limits: Some(plugin_limits())}, +{sql:"INSERT INTO plugin_graph_refs(graph_id,owner_kind,owner_id,ref_id,retained_at_ms) SELECT ?,'run',?,?,? WHERE (EXISTS(SELECT 1 FROM runs WHERE id=? AND session_id=?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([sql_text(graph_id.copy()),sql_text(run_id.copy()),sql_text(ref_id.copy()),sql_int(now_ms.copy()),sql_text(run_id.copy()),sql_text(session_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())]),query: Some(false),limits: Some(plugin_limits())}, +{sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE (NOT EXISTS(SELECT 1 FROM plugin_graph_refs WHERE graph_id=? AND owner_kind='run' AND owner_id=? AND ref_id=?)) AND (? = '' OR (NOT EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) AND NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q WHERE q.run_id = ? AND (q.graph_id <> ? OR q.ref_id <> ?)))) ",params: Some([sql_text(graph_id.copy()),sql_text(run_id.copy()),sql_text(ref_id.copy()), sql_text(graph_id.copy()), sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())]),query: Some(false),limits: Some(plugin_limits())}, storage_pin_requirement_statement(graph_id.copy(), ref_id.copy(), run_id.copy(), session_id.copy()), -{sql:"SELECT graph_id,ref_id,? AS generation FROM plugin_graph_refs WHERE owner_kind='run' AND owner_id=? AND ref_id=?",params:[sql_int(generation.copy()),sql_text(run_id.copy()),sql_text(ref_id.copy())],query:true,limits:plugin_limits()} +{sql:"SELECT graph_id,ref_id,? AS generation FROM plugin_graph_refs WHERE owner_kind='run' AND owner_id=? AND ref_id=?",params: Some([sql_int(generation.copy()),sql_text(run_id.copy()),sql_text(ref_id.copy())]),query: Some(true),limits: Some(plugin_limits())} ] } pub fn storage_admission_pin_classification(graph_id: string, ref_id: string, run_id: string) -> SqliteStatement { { sql: "SELECT CASE WHEN EXISTS (SELECT 1 FROM plugin_graph_revocations WHERE graph_id = ?) THEN 'plugin_graph_revoked' WHEN NOT EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN plugin_graph_refs g ON g.graph_id=q.graph_id AND g.ref_id=q.ref_id AND g.owner_kind='run' AND g.owner_id=q.run_id WHERE q.run_id=? AND q.graph_id=? AND q.ref_id=?) THEN 'plugin_graph_pin_required' ELSE 'ok' END", - params: [sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(graph_id.copy()), sql_text(run_id.copy()), sql_text(graph_id.copy()), sql_text(ref_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } } @@ -509,14 +522,27 @@ pub fn storage_pin_int(entry: SqliteTransactionResult, column: int) -> int { plugin_query_int(entry, column) } +struct PinHarnessPolicy {package_id:string,artifact_id:string,entry_path:string,prompt_export:string,context_export:string,guardian_export:string} +struct PinHarnessSelection {mode:string,tool_package:Option,policy:Option} + fn decode_pin_selection(encoded:string)->map { let mut selection:map={}; - if encoded != "null" {selection=json::decode(encoded);} + if encoded != "null" { + let decoded:PinHarnessSelection=json::decode::(encoded); + let mode:string=decoded.mode.copy(); + if mode == "tool_only" { + selection={mode:mode.copy(),tool_package:match decoded.tool_package {Some(value)=>value,None=>""}}; + } else if mode == "context_prompt" { + let policy:PinHarnessPolicy=match decoded.policy {Some(value)=>value,None=>{package_id:"",artifact_id:"",entry_path:"",prompt_export:"",context_export:"",guardian_export:""}}; + selection={mode:mode.copy(),tool_package:match decoded.tool_package {Some(value)=>value,None=>""},policy:{package_id:policy.package_id,artifact_id:policy.artifact_id,entry_path:policy.entry_path,prompt_export:policy.prompt_export,context_export:policy.context_export,guardian_export:policy.guardian_export}}; + } + } selection } pub fn storage_pin_by_owner(db_id: resource, owner_id: string) -> map { - let queried: SqliteQueryResult = sqlite::query(&db_id, "SELECT COALESCE(json_extract(idem.response_json, '$.tool_graph_id'), ''), COALESCE(json_extract(idem.response_json, '$.tool_ref_id'), ''), COALESCE(CAST(json_extract(idem.response_json, '$.tool_generation') AS INTEGER), 0), idem.resource_id, COALESCE((SELECT session_id FROM runs WHERE id = idem.resource_id), ''), COALESCE(json_extract(idem.response_json, '$.registry_identity'), ''), COALESCE(json_extract(idem.response_json, '$.workspace'), ''), COALESCE(json_extract(idem.response_json, '$.profile'), ''), COALESCE(json_extract(idem.response_json, '$.harness_selection'), 'null') FROM idempotency_records idem JOIN runs original ON original.id = idem.resource_id WHERE idem.resource_id = ? AND ((original.idempotency_key <> '' AND idem.scope = original.idempotency_scope AND idem.key = original.idempotency_key) OR (original.idempotency_key = '' AND idem.scope = 'native.tool-pin' AND idem.key = original.id)) AND EXISTS (SELECT 1 FROM plugin_graph_refs refs WHERE refs.graph_id = json_extract(idem.response_json, '$.tool_graph_id') AND refs.owner_kind = 'run' AND refs.owner_id = idem.resource_id AND refs.ref_id = json_extract(idem.response_json, '$.tool_ref_id'))", [sql_text(owner_id.copy())], plugin_limits()); + let params: array = [sql_text(owner_id.copy())]; + let queried: SqliteQueryResult = sqlite::query(&db_id, "SELECT COALESCE(json_extract(idem.response_json, '$.tool_graph_id'), ''), COALESCE(json_extract(idem.response_json, '$.tool_ref_id'), ''), COALESCE(CAST(json_extract(idem.response_json, '$.tool_generation') AS INTEGER), 0), idem.resource_id, COALESCE((SELECT session_id FROM runs WHERE id = idem.resource_id), ''), COALESCE(json_extract(idem.response_json, '$.registry_identity'), ''), COALESCE(json_extract(idem.response_json, '$.workspace'), ''), COALESCE(json_extract(idem.response_json, '$.profile'), ''), COALESCE(json_extract(idem.response_json, '$.harness_selection'), 'null') FROM idempotency_records idem JOIN runs original ON original.id = idem.resource_id WHERE idem.resource_id = ? AND ((original.idempotency_key <> '' AND idem.scope = original.idempotency_scope AND idem.key = original.idempotency_key) OR (original.idempotency_key = '' AND idem.scope = 'native.tool-pin' AND idem.key = original.id)) AND EXISTS (SELECT 1 FROM plugin_graph_refs refs WHERE refs.graph_id = json_extract(idem.response_json, '$.tool_graph_id') AND refs.owner_kind = 'run' AND refs.owner_id = idem.resource_id AND refs.ref_id = json_extract(idem.response_json, '$.tool_ref_id'))", params, plugin_limits()); let mut graph_id: string = ""; let mut ref_id: string = ""; let mut generation: int = 0; @@ -531,15 +557,15 @@ pub fn storage_pin_by_owner(db_id: resource, owner_id: string if rows.length == 1 { let row: SqliteRow = rows[0].copy(); let cells: array = row.cells.copy(); - graph_id = cells[0].text_value; - ref_id = cells[1].text_value; - generation = cells[2].int_value; - run_id = cells[3].text_value; - session_id = cells[4].text_value; - registry_identity = cells[5].text_value; - workspace = cells[6].text_value; - profile = cells[7].text_value; - harness_selection_json = cells[8].text_value; + graph_id = cells.copy()[0].text_value.unwrap_or(""); + ref_id = cells.copy()[1].text_value.unwrap_or(""); + generation = cells.copy()[2].int_value.unwrap_or(0); + run_id = cells.copy()[3].text_value.unwrap_or(""); + session_id = cells.copy()[4].text_value.unwrap_or(""); + registry_identity = cells.copy()[5].text_value.unwrap_or(""); + workspace = cells.copy()[6].text_value.unwrap_or(""); + profile = cells.copy()[7].text_value.unwrap_or(""); + harness_selection_json = cells.copy()[8].text_value.unwrap_or(""); pinned = graph_id.copy() != "" && ref_id.copy() != ""; } { @@ -602,8 +628,6 @@ struct PluginStateBindingInput { owner_kind: string, owner_id: string, graph_id: string, - generation: int, - ref_id: string, epoch: int, schema_version: int, revision: int, @@ -683,15 +707,15 @@ pub fn storage_plugin_state_object_put(db_id: resource, paylo let statements: array = [ { sql: "INSERT INTO plugin_state_objects(object_id,package_id,artifact_id,schema_version,document_json,document_bytes,created_at_ms) SELECT ?,?,?,?,?,?,? WHERE NOT EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=?)", - params: [sql_text(input.object_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy()), sql_text(input.document_json.copy()), sql_int(input.document_bytes.copy()), sql_int(input.now_ms.copy()), sql_text(input.object_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.object_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy()), sql_text(input.document_json.copy()), sql_int(input.document_bytes.copy()), sql_int(input.now_ms.copy()), sql_text(input.object_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT object_id,package_id,artifact_id,schema_version,document_bytes FROM plugin_state_objects WHERE object_id=?", - params: [sql_text(input.object_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.object_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let result: array = sqlite::transaction(&db_id, statements); @@ -748,21 +772,21 @@ pub fn storage_plugin_state_root_put(db_id: resource, payload let statements: array = [ { sql: "INSERT INTO plugin_state_roots(owner_kind,owner_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,schema_version,revision,object_id,updated_at_ms) SELECT ?,?,?,?,?,?,?,?,1,?,? WHERE ?=0 AND NOT EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind=? AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=?) AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=? AND package_id=? AND artifact_id=? AND schema_version=?)", - params: [sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy()), sql_text(input.object_id.copy()), sql_int(input.now_ms.copy()), sql_int(input.expected_revision.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.object_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy()), sql_text(input.object_id.copy()), sql_int(input.now_ms.copy()), sql_int(input.expected_revision.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.object_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "UPDATE plugin_state_roots SET package_id=?,artifact_id=?,schema_version=?,revision=revision+1,object_id=?,updated_at_ms=? WHERE owner_kind=? AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=? AND package_id=? AND artifact_id=? AND schema_version=?)", - params: [sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy()), sql_text(input.object_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.object_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy()), sql_text(input.object_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.object_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT owner_kind,owner_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,schema_version,revision,object_id FROM plugin_state_roots WHERE owner_kind=? AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=?", - params: [sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let result: array = sqlite::transaction(&db_id, statements); @@ -793,7 +817,8 @@ pub fn storage_plugin_state_root_put(db_id: resource, payload pub fn storage_plugin_state_root_get(db_id: resource, payload_json: string) -> map { let input: PluginStateRootQuery = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT owner_kind,owner_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,schema_version,revision,object_id FROM plugin_state_roots WHERE owner_kind=? AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=?", [sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy())], plugin_limits())) + let params: array = [sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT owner_kind,owner_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,schema_version,revision,object_id FROM plugin_state_roots WHERE owner_kind=? AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=?", params, plugin_limits())) } fn plugin_state_binding_matches(entry: SqliteTransactionResult, input: PluginStateBindingInput) -> bool { @@ -828,15 +853,15 @@ pub fn storage_plugin_state_bind_run(db_id: resource, payload let statements: array = [ { sql: "INSERT INTO plugin_state_run_bindings(run_id,session_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,owner_kind,owner_id,graph_id,epoch,schema_version,revision,object_id,checkpoint_id,created_at_ms) SELECT ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? WHERE EXISTS(SELECT 1 FROM runs WHERE id=? AND session_id=?) AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=? AND schema_version=?) AND NOT EXISTS(SELECT 1 FROM plugin_state_run_bindings WHERE run_id=?)", - params: [sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.graph_id.copy()), sql_int(input.epoch.copy()), sql_int(input.schema_version.copy()), sql_int(input.revision.copy()), sql_text(input.object_id.copy()), sql_text(input.checkpoint_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.object_id.copy()), sql_int(input.schema_version.copy()), sql_text(input.run_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.graph_id.copy()), sql_int(input.epoch.copy()), sql_int(input.schema_version.copy()), sql_int(input.revision.copy()), sql_text(input.object_id.copy()), sql_text(input.checkpoint_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.object_id.copy()), sql_int(input.schema_version.copy()), sql_text(input.run_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT run_id,session_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,owner_kind,owner_id,schema_version,revision,object_id FROM plugin_state_run_bindings WHERE run_id=?", - params: [sql_text(input.run_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.run_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let result: array = sqlite::transaction(&db_id, statements); @@ -868,7 +893,8 @@ pub fn storage_plugin_state_bind_run(db_id: resource, payload pub fn storage_plugin_state_binding_get(db_id: resource, payload_json: string) -> map { let input: PluginStateRunInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT b.run_id,b.session_id,b.workspace_id,b.branch_id,b.plugin_id,b.package_id,b.artifact_id,b.owner_kind,b.owner_id,b.graph_id,b.epoch,b.schema_version,b.revision,b.object_id,b.checkpoint_id,o.document_json,o.document_bytes FROM plugin_state_run_bindings b JOIN plugin_state_objects o ON o.object_id=b.object_id WHERE b.run_id=?", [sql_text(input.run_id.copy())], plugin_limits())) + let params: array = [sql_text(input.run_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT b.run_id,b.session_id,b.workspace_id,b.branch_id,b.plugin_id,b.package_id,b.artifact_id,b.owner_kind,b.owner_id,b.graph_id,b.epoch,b.schema_version,b.revision,b.object_id,b.checkpoint_id,o.document_json,o.document_bytes FROM plugin_state_run_bindings b JOIN plugin_state_objects o ON o.object_id=b.object_id WHERE b.run_id=?", params, plugin_limits())) } pub fn storage_plugin_state_binding_advance(db_id: resource, payload_json: string) -> map { @@ -876,15 +902,15 @@ pub fn storage_plugin_state_binding_advance(db_id: resource, let statements: array = [ { sql: "UPDATE plugin_state_run_bindings SET schema_version=?,revision=?,object_id=? WHERE run_id=? AND revision=? AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=? AND schema_version=?)", - params: [sql_int(input.schema_version.copy()), sql_int(input.revision.copy()), sql_text(input.object_id.copy()), sql_text(input.run_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.object_id.copy()), sql_int(input.schema_version.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_int(input.schema_version.copy()), sql_int(input.revision.copy()), sql_text(input.object_id.copy()), sql_text(input.run_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.object_id.copy()), sql_int(input.schema_version.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT schema_version,revision,object_id FROM plugin_state_run_bindings WHERE run_id=?", - params: [sql_text(input.run_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.run_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let result: array = sqlite::transaction(&db_id, statements); @@ -927,11 +953,11 @@ pub fn storage_admission_state_statements(payload_json: string) -> array(payload_json); let next_revision: int = input.expected_revision.copy() + 1; [ - {sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND schema_version=?)",params:[sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_int(input.expected_revision.copy()),sql_text(input.expected_object_id.copy()),sql_int(input.old_schema_version.copy())],query:false,limits:plugin_limits()}, - {sql:"UPDATE plugin_state_roots SET package_id=?,artifact_id=?,schema_version=?,revision=revision+1,object_id=?,updated_at_ms=? WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=? AND package_id=? AND artifact_id=? AND schema_version=?)",params:[sql_text(input.package_id.copy()),sql_text(input.artifact_id.copy()),sql_int(input.schema_version.copy()),sql_text(input.object_id.copy()),sql_int(input.now_ms.copy()),sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_int(input.expected_revision.copy()),sql_text(input.expected_object_id.copy()),sql_text(input.object_id.copy()),sql_text(input.package_id.copy()),sql_text(input.artifact_id.copy()),sql_int(input.schema_version.copy())],query:false,limits:plugin_limits()}, - {sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND schema_version=?)",params:[sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_int(next_revision.copy()),sql_text(input.object_id.copy()),sql_int(input.schema_version.copy())],query:false,limits:plugin_limits()}, - {sql:"INSERT INTO plugin_state_run_bindings(run_id,session_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,owner_kind,owner_id,graph_id,epoch,schema_version,revision,object_id,checkpoint_id,created_at_ms) SELECT ?,?,?,?,?,?,?,'session',?,?,?,?,?,?,'',? WHERE EXISTS(SELECT 1 FROM runs WHERE id=? AND session_id=?) AND EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND schema_version=?) AND NOT EXISTS(SELECT 1 FROM plugin_state_run_bindings WHERE run_id=?)",params:[sql_text(input.run_id.copy()),sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_text(input.package_id.copy()),sql_text(input.artifact_id.copy()),sql_text(input.session_id.copy()),sql_text(input.graph_id.copy()),sql_int(input.epoch.copy()),sql_int(input.schema_version.copy()),sql_int(next_revision.copy()),sql_text(input.object_id.copy()),sql_int(input.now_ms.copy()),sql_text(input.run_id.copy()),sql_text(input.session_id.copy()),sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_int(next_revision.copy()),sql_text(input.object_id.copy()),sql_int(input.schema_version.copy()),sql_text(input.run_id.copy())],query:false,limits:plugin_limits()}, - {sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_state_run_bindings WHERE run_id=? AND session_id=? AND schema_version=? AND revision=? AND object_id=?)",params:[sql_text(input.run_id.copy()),sql_text(input.session_id.copy()),sql_int(input.schema_version.copy()),sql_int(next_revision.copy()),sql_text(input.object_id.copy())],query:false,limits:plugin_limits()} + {sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND schema_version=?)",params: Some([sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_int(input.expected_revision.copy()),sql_text(input.expected_object_id.copy()),sql_int(input.old_schema_version.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"UPDATE plugin_state_roots SET package_id=?,artifact_id=?,schema_version=?,revision=revision+1,object_id=?,updated_at_ms=? WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=? AND package_id=? AND artifact_id=? AND schema_version=?)",params: Some([sql_text(input.package_id.copy()),sql_text(input.artifact_id.copy()),sql_int(input.schema_version.copy()),sql_text(input.object_id.copy()),sql_int(input.now_ms.copy()),sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_int(input.expected_revision.copy()),sql_text(input.expected_object_id.copy()),sql_text(input.object_id.copy()),sql_text(input.package_id.copy()),sql_text(input.artifact_id.copy()),sql_int(input.schema_version.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND schema_version=?)",params: Some([sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_int(next_revision.copy()),sql_text(input.object_id.copy()),sql_int(input.schema_version.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"INSERT INTO plugin_state_run_bindings(run_id,session_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,owner_kind,owner_id,graph_id,epoch,schema_version,revision,object_id,checkpoint_id,created_at_ms) SELECT ?,?,?,?,?,?,?,'session',?,?,?,?,?,?,'',? WHERE EXISTS(SELECT 1 FROM runs WHERE id=? AND session_id=?) AND EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND schema_version=?) AND NOT EXISTS(SELECT 1 FROM plugin_state_run_bindings WHERE run_id=?)",params: Some([sql_text(input.run_id.copy()),sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_text(input.package_id.copy()),sql_text(input.artifact_id.copy()),sql_text(input.session_id.copy()),sql_text(input.graph_id.copy()),sql_int(input.epoch.copy()),sql_int(input.schema_version.copy()),sql_int(next_revision.copy()),sql_text(input.object_id.copy()),sql_int(input.now_ms.copy()),sql_text(input.run_id.copy()),sql_text(input.session_id.copy()),sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_int(next_revision.copy()),sql_text(input.object_id.copy()),sql_int(input.schema_version.copy()),sql_text(input.run_id.copy())]),query: Some(false),limits: Some(plugin_limits())}, + {sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_state_run_bindings WHERE run_id=? AND session_id=? AND schema_version=? AND revision=? AND object_id=?)",params: Some([sql_text(input.run_id.copy()),sql_text(input.session_id.copy()),sql_int(input.schema_version.copy()),sql_int(next_revision.copy()),sql_text(input.object_id.copy())]),query: Some(false),limits: Some(plugin_limits())} ] } @@ -950,23 +976,26 @@ struct PluginStateAdmissionGuardInput { pub fn storage_admission_state_guard_statements(payload_json: string) -> array { let input: PluginStateAdmissionGuardInput = json::decode::(payload_json); [ - {sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND package_id=? AND artifact_id=? AND revision=? AND object_id=? AND schema_version=?)",params:[sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_text(input.package_id.copy()),sql_text(input.artifact_id.copy()),sql_int(input.revision.copy()),sql_text(input.object_id.copy()),sql_int(input.schema_version.copy())],query:false,limits:plugin_limits()} + {sql:"INSERT INTO plugin_control_guard(id) SELECT 2 WHERE NOT EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND package_id=? AND artifact_id=? AND revision=? AND object_id=? AND schema_version=?)",params: Some([sql_text(input.session_id.copy()),sql_text(input.workspace_id.copy()),sql_text(input.branch_id.copy()),sql_text(input.plugin_id.copy()),sql_text(input.package_id.copy()),sql_text(input.artifact_id.copy()),sql_int(input.revision.copy()),sql_text(input.object_id.copy()),sql_int(input.schema_version.copy())]),query: Some(false),limits: Some(plugin_limits())} ] } pub fn storage_plugin_state_roots_for_checkpoint(db_id: resource, payload_json: string) -> map { let input: PluginStateRunInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT owner_kind,owner_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,schema_version,revision,object_id FROM plugin_state_run_bindings WHERE run_id=?", [sql_text(input.run_id.copy())], plugin_limits())) + let params: array = [sql_text(input.run_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT owner_kind,owner_id,workspace_id,branch_id,plugin_id,package_id,artifact_id,schema_version,revision,object_id FROM plugin_state_run_bindings WHERE run_id=?", params, plugin_limits())) } pub fn storage_plugin_state_session_nonterminal(db_id: resource, payload_json: string) -> map { let input: PluginStateSessionInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT COUNT(*) FROM runs WHERE session_id=? AND status IN ('queued','running','waiting_approval','compacting')", [sql_text(input.session_id.copy())], plugin_limits())) + let params: array = [sql_text(input.session_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT COUNT(*) FROM runs WHERE session_id=? AND status IN ('queued','running','waiting_approval','compacting')", params, plugin_limits())) } pub fn storage_plugin_state_session_max_schema(db_id: resource, payload_json: string) -> map { let input: PluginStatePluginInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT COALESCE(MAX(schema_version),0) FROM plugin_state_roots WHERE owner_kind='session' AND plugin_id=?", [sql_text(input.plugin_id.copy())], plugin_limits())) + let params: array = [sql_text(input.plugin_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT COALESCE(MAX(schema_version),0) FROM plugin_state_roots WHERE owner_kind='session' AND plugin_id=?", params, plugin_limits())) } pub fn storage_plugin_state_commit(db_id: resource, payload_json: string) -> map { @@ -974,21 +1003,21 @@ pub fn storage_plugin_state_commit(db_id: resource, payload_j let statements: array = [ { sql: "UPDATE plugin_state_run_bindings SET package_id=?,artifact_id=?,schema_version=?,revision=?,object_id=? WHERE run_id=? AND revision=? AND object_id=? AND owner_kind=? AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND EXISTS(SELECT 1 FROM plugin_state_roots WHERE owner_kind=? AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=?) AND EXISTS(SELECT 1 FROM plugin_state_objects WHERE object_id=? AND package_id=? AND artifact_id=? AND schema_version=?)", - params: [sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy()), sql_int(input.expected_revision.copy() + 1), sql_text(input.object_id.copy()), sql_text(input.run_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.expected_object_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.expected_object_id.copy()), sql_text(input.object_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy()), sql_int(input.expected_revision.copy() + 1), sql_text(input.object_id.copy()), sql_text(input.run_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.expected_object_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.expected_object_id.copy()), sql_text(input.object_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_int(input.schema_version.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "UPDATE plugin_state_roots SET schema_version=?,revision=revision+1,object_id=?,updated_at_ms=? WHERE owner_kind=? AND owner_id=? AND workspace_id=? AND branch_id=? AND plugin_id=? AND revision=? AND object_id=? AND EXISTS(SELECT 1 FROM plugin_state_run_bindings WHERE run_id=? AND revision=? AND object_id=?)", - params: [sql_int(input.schema_version.copy()), sql_text(input.object_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.expected_object_id.copy()), sql_text(input.run_id.copy()), sql_int(input.expected_revision.copy() + 1), sql_text(input.object_id.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_int(input.schema_version.copy()), sql_text(input.object_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.plugin_id.copy()), sql_int(input.expected_revision.copy()), sql_text(input.expected_object_id.copy()), sql_text(input.run_id.copy()), sql_int(input.expected_revision.copy() + 1), sql_text(input.object_id.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "SELECT schema_version,revision,object_id FROM plugin_state_run_bindings WHERE run_id=?", - params: [sql_text(input.run_id.copy())], - query: true, - limits: plugin_limits(), + params: Some([sql_text(input.run_id.copy())]), + query: Some(true), + limits: Some(plugin_limits()), } ]; let result: array = sqlite::transaction(&db_id, statements); @@ -1008,28 +1037,31 @@ pub fn storage_plugin_state_commit(db_id: resource, payload_j response } - pub fn storage_plugin_state_authorize(db_id: resource, payload_json: string) -> map { let input: PluginStateAuthorizeInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT CASE WHEN NOT EXISTS(SELECT 1 FROM plugin_state_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.run_id=? AND b.session_id=? AND b.plugin_id=? AND b.package_id=? AND b.artifact_id=? AND b.owner_kind=? AND b.owner_id=? AND b.graph_id=? AND b.workspace_id=? AND b.branch_id=?) THEN 'plugin_state_binding_mismatch' WHEN NOT EXISTS(SELECT 1 FROM runs r JOIN sessions s ON s.id=r.session_id WHERE r.id=? AND r.session_id=? AND s.profile=? AND r.status IN ('queued','running')) THEN 'plugin_state_run_inactive' WHEN EXISTS(SELECT 1 FROM plugin_graph_revocations WHERE graph_id=?) THEN 'plugin_graph_revoked' WHEN EXISTS(SELECT 1 FROM managed_run_bindings WHERE run_id=?) AND NOT EXISTS(SELECT 1 FROM managed_run_bindings WHERE run_id=? AND workspace_id=? AND branch_id=? AND epoch=? AND original_deadline_ms>?) THEN 'plugin_state_epoch_mismatch' ELSE 'ok' END", [sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.graph_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.profile.copy()), sql_text(input.graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_int(input.epoch.copy()), sql_int(input.now_ms.copy())], plugin_limits())) + let params: array = [sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.package_id.copy()), sql_text(input.artifact_id.copy()), sql_text(input.owner_kind.copy()), sql_text(input.owner_id.copy()), sql_text(input.graph_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_text(input.run_id.copy()), sql_text(input.session_id.copy()), sql_text(input.profile.copy()), sql_text(input.graph_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy()), sql_int(input.epoch.copy()), sql_int(input.now_ms.copy())]; + ok_query(sqlite::query(&db_id, "SELECT CASE WHEN NOT EXISTS(SELECT 1 FROM plugin_state_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.run_id=? AND b.session_id=? AND b.plugin_id=? AND b.package_id=? AND b.artifact_id=? AND b.owner_kind=? AND b.owner_id=? AND b.graph_id=? AND b.workspace_id=? AND b.branch_id=?) THEN 'plugin_state_binding_mismatch' WHEN NOT EXISTS(SELECT 1 FROM runs r JOIN sessions s ON s.id=r.session_id WHERE r.id=? AND r.session_id=? AND s.profile=? AND r.status IN ('queued','running')) THEN 'plugin_state_run_inactive' WHEN EXISTS(SELECT 1 FROM plugin_graph_revocations WHERE graph_id=?) THEN 'plugin_graph_revoked' WHEN EXISTS(SELECT 1 FROM managed_run_bindings WHERE run_id=?) AND NOT EXISTS(SELECT 1 FROM managed_run_bindings WHERE run_id=? AND workspace_id=? AND branch_id=? AND epoch=? AND original_deadline_ms>?) THEN 'plugin_state_epoch_mismatch' ELSE 'ok' END", params, plugin_limits())) } struct PluginStateSessionKeyInput { session_id: string, plugin_id: string } pub fn storage_plugin_state_session_key(db_id: resource, payload_json: string) -> map { let input: PluginStateSessionKeyInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT workspace_id,branch_id,plugin_id FROM plugin_state_run_bindings WHERE session_id=? AND plugin_id=? AND owner_kind='session' ORDER BY created_at_ms DESC LIMIT 1", [sql_text(input.session_id.copy()), sql_text(input.plugin_id.copy())], plugin_limits())) + let params: array = [sql_text(input.session_id.copy()), sql_text(input.plugin_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT workspace_id,branch_id,plugin_id FROM plugin_state_run_bindings WHERE session_id=? AND plugin_id=? AND owner_kind='session' ORDER BY created_at_ms DESC LIMIT 1", params, plugin_limits())) } struct PluginStateSessionKeyExactInput { session_id: string, plugin_id: string, workspace_id: string, branch_id: string } pub fn storage_plugin_state_session_key_for(db_id: resource, payload_json: string) -> map { let input: PluginStateSessionKeyExactInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT workspace_id,branch_id,plugin_id FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND plugin_id=? AND workspace_id=? AND branch_id=? LIMIT 1", [sql_text(input.session_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy())], plugin_limits())) + let params: array = [sql_text(input.session_id.copy()), sql_text(input.plugin_id.copy()), sql_text(input.workspace_id.copy()), sql_text(input.branch_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT workspace_id,branch_id,plugin_id FROM plugin_state_roots WHERE owner_kind='session' AND owner_id=? AND plugin_id=? AND workspace_id=? AND branch_id=? LIMIT 1", params, plugin_limits())) } struct PluginStateObjectKeyInput { object_id: string } pub fn storage_plugin_state_object_get(db_id: resource, payload_json: string) -> map { let input: PluginStateObjectKeyInput = json::decode::(payload_json); - ok_query(sqlite::query(&db_id, "SELECT object_id,schema_version,document_json FROM plugin_state_objects WHERE object_id=?", [sql_text(input.object_id.copy())], plugin_limits())) + let params: array = [sql_text(input.object_id.copy())]; + ok_query(sqlite::query(&db_id, "SELECT object_id,schema_version,document_json FROM plugin_state_objects WHERE object_id=?", params, plugin_limits())) } struct PluginStateCollectInput { failpoint: string, protected_ids_json: string } @@ -1039,15 +1071,15 @@ pub fn storage_plugin_state_collect(db_id: resource, payload_ let statements: array = [ { sql: "DELETE FROM plugin_state_objects WHERE object_id NOT IN (SELECT object_id FROM plugin_state_roots UNION SELECT object_id FROM plugin_state_run_bindings UNION SELECT json_extract(value,'$.immutable_root_id') FROM managed_checkpoints, json_each(json_extract(receipt_json,'$.checkpoint.plugin_state.roots')) UNION SELECT json_extract(prepared.prepared_record_json,'$.plugin_state.object_id') FROM managed_effect_receipts prepared JOIN managed_effect_calls prepared_call ON prepared_call.effect_id=prepared.effect_id WHERE prepared_call.state='prepared' AND json_extract(prepared.prepared_record_json,'$.plugin_state.object_id') IS NOT NULL) AND object_id NOT IN (SELECT value FROM json_each(?)) AND object_id NOT IN (SELECT object_id FROM plugin_state_objects WHERE object_id NOT IN (SELECT object_id FROM plugin_state_roots UNION SELECT object_id FROM plugin_state_run_bindings UNION SELECT json_extract(value,'$.immutable_root_id') FROM managed_checkpoints, json_each(json_extract(receipt_json,'$.checkpoint.plugin_state.roots')) UNION SELECT json_extract(prepared.prepared_record_json,'$.plugin_state.object_id') FROM managed_effect_receipts prepared JOIN managed_effect_calls prepared_call ON prepared_call.effect_id=prepared.effect_id WHERE prepared_call.state='prepared' AND json_extract(prepared.prepared_record_json,'$.plugin_state.object_id') IS NOT NULL) ORDER BY created_at_ms DESC LIMIT 64)", - params: [sql_text(input.protected_ids_json.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.protected_ids_json.copy())]), + query: Some(false), + limits: Some(plugin_limits()), }, { sql: "INSERT INTO plugin_control_guard (id) SELECT 2 WHERE ? = 'after_partial_write'", - params: [sql_text(input.failpoint.copy())], - query: false, - limits: plugin_limits(), + params: Some([sql_text(input.failpoint.copy())]), + query: Some(false), + limits: Some(plugin_limits()), } ]; let result = sqlite::transaction(&db_id, statements); diff --git a/rss/storage/runs.rss b/rss/storage/runs.rss index b3034d3..8ac824f 100644 --- a/rss/storage/runs.rss +++ b/rss/storage/runs.rss @@ -1,27 +1,33 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } +fn no_params() -> Option> { None } + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } @@ -36,11 +42,10 @@ fn sqlite_query_map(result: SqliteQueryResult) -> map { use self::schema as schema; use self::existence as existence; -fn runs_query_limits(max_rows: int, max_bytes: int) { +fn runs_query_limits(max_rows: int, max_bytes: int) -> SqliteLimits { sql_limits(max_rows, max_bytes) } - struct RunCreateInput { id: string, session_id: string, @@ -91,6 +96,7 @@ struct RunTerminalInput { message_content_json: string, message_run_id: string, message_finish_reason: string, + message_ordinal: Option, now_ms: int } @@ -120,6 +126,7 @@ struct RunTerminalPublishedInput { message_content_json: string, message_run_id: string, message_finish_reason: string, + message_ordinal: Option, now_ms: int, publication_json: string } @@ -175,18 +182,22 @@ struct RecoveryBatchResult { pub fn storage_run_create(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> map { let input: RunCreateInput = json::decode::(payload_json); let mut result = { ok: true, code: "ok", message: "", result: { columns: [], rows: [] } }; - if !(sqlite::query(&db_id, existence::session_exists_sql(), [sql_text(input.session_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.session_id.copy())]; + if !(sqlite::query(&db_id, existence::session_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "session_not_found", message: "run create targets an unknown session", result: { columns: [], rows: [] } }; } else { - if input.parent_run_id.copy() != "" && !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.parent_run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.parent_run_id.copy())]; + if input.parent_run_id.copy() != "" && !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "parent_not_found", message: "parent run does not exist", result: { columns: [], rows: [] } }; } else { - sqlite::execute(&db_id, "INSERT OR IGNORE INTO runs (id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, created_at_ms, updated_at_ms) SELECT ?, ?, ?, 'queued', ?, ?, ?, ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM runs existing WHERE existing.id = ? AND existing.session_id <> ?) AND EXISTS (SELECT 1 FROM sessions WHERE id = ?) AND (? = '' OR EXISTS (SELECT 1 FROM runs parent WHERE parent.id = ?))", [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.input_json.copy()), sql_text(input.provider.copy()), sql_text(input.model.copy()), sql_text(input.script_hash.copy()), sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy())]); + let params: array = [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.input_json.copy()), sql_text(input.provider.copy()), sql_text(input.model.copy()), sql_text(input.script_hash.copy()), sql_text(input.idempotency_scope.copy()), sql_text(input.idempotency_key.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.parent_run_id.copy())]; + sqlite::execute(&db_id, "INSERT OR IGNORE INTO runs (id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, created_at_ms, updated_at_ms) SELECT ?, ?, ?, 'queued', ?, ?, ?, ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM runs existing WHERE existing.id = ? AND existing.session_id <> ?) AND EXISTS (SELECT 1 FROM sessions WHERE id = ?) AND (? = '' OR EXISTS (SELECT 1 FROM runs parent WHERE parent.id = ?))", params); + let params: array = [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)]; result = { ok: true, code: "ok", message: "", - result: sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? AND session_id = ? LIMIT ?", [sql_text(input.id.copy()), sql_text(input.session_id.copy()), sql_int(max_rows)], runs_query_limits(max_rows, max_bytes)) + result: sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? AND session_id = ? LIMIT ?", params, runs_query_limits(max_rows, max_bytes)) }; } } @@ -194,11 +205,13 @@ pub fn storage_run_create(db_id: resource, payload_json: stri } pub fn storage_run_get(db_id: resource, run_id: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT ?", [sql_text(run_id.copy()), sql_int(max_rows)], runs_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(run_id.copy()), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT ?", params, runs_query_limits(max_rows, max_bytes)) } pub fn storage_run_list(db_id: resource, session_id: string, status: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE session_id = ? AND (? = '' OR status = ?) ORDER BY created_at_ms DESC, id DESC LIMIT ?", [sql_text(session_id.copy()), sql_text(status), sql_text(status), sql_int(max_rows)], runs_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(session_id.copy()), sql_text(status), sql_text(status), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE session_id = ? AND (? = '' OR status = ?) ORDER BY created_at_ms DESC, id DESC LIMIT ?", params, runs_query_limits(max_rows, max_bytes)) } pub fn storage_run_status_allowed(status: string) -> bool { @@ -211,21 +224,21 @@ pub fn storage_run_transition(db_id: resource, payload_json: let statements: array = [ { sql: "UPDATE runs SET status = ?, error_code = ?, error_message = ?, recovery_reason = ?, started_at_ms = CASE WHEN ? = 'running' AND started_at_ms = 0 THEN ? ELSE started_at_ms END, finished_at_ms = CASE WHEN ? IN ('completed', 'failed', 'cancelled') THEN ? ELSE finished_at_ms END, updated_at_ms = ? WHERE id = ? AND status = ? AND ((status = 'queued' AND ? IN ('running', 'cancelled', 'failed')) OR (status = 'running' AND ? IN ('waiting_approval', 'compacting', 'completed', 'failed', 'cancelled')) OR (status = 'waiting_approval' AND ? IN ('running', 'compacting', 'completed', 'failed', 'cancelled')) OR (status = 'compacting' AND ? IN ('running', 'completed', 'failed', 'cancelled')))", - params: [sql_text(input.to_status.copy()), sql_text(input.error_code.copy()), sql_text(input.error_message.copy()), sql_text(input.recovery_reason.copy()), sql_text(input.to_status.copy()), sql_int(input.now_ms.copy()), sql_text(input.to_status.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.from_status.copy()), sql_text(input.to_status.copy()), sql_text(input.to_status.copy()), sql_text(input.to_status.copy()), sql_text(input.to_status.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.to_status.copy()), sql_text(input.error_code.copy()), sql_text(input.error_message.copy()), sql_text(input.recovery_reason.copy()), sql_text(input.to_status.copy()), sql_int(input.now_ms.copy()), sql_text(input.to_status.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.from_status.copy()), sql_text(input.to_status.copy()), sql_text(input.to_status.copy()), sql_text(input.to_status.copy()), sql_text(input.to_status.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT OR IGNORE INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT id, COALESCE((SELECT MAX(seq) FROM run_events e WHERE e.run_id = runs.id), 0) + 1, 'run-transition:' || id || ':' || ? || ':' || ? || ':' || ? || ':' || (COALESCE((SELECT MAX(seq) FROM run_events e WHERE e.run_id = runs.id), 0) + 1), 'run.status_changed', '{}', ? FROM runs WHERE id = ? AND status = ? AND updated_at_ms = ? AND changes() = 1", - params: [sql_int(input.now_ms.copy()), sql_text(input.from_status.copy()), sql_text(input.to_status.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.to_status.copy()), sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_text(input.from_status.copy()), sql_text(input.to_status.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.to_status.copy()), sql_int(input.now_ms.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT id, COALESCE((SELECT MIN(seq) FROM run_events e WHERE e.run_id = runs.id), 0), COALESCE((SELECT MAX(seq) FROM run_events e WHERE e.run_id = runs.id), 0), ? FROM runs WHERE id = ? AND changes() = 1 ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; sqlite::transaction(&db_id, statements) @@ -234,20 +247,24 @@ pub fn storage_run_transition(db_id: resource, payload_json: pub fn storage_run_link_child(db_id: resource, payload_json: string) -> map { let input: ChildLinkInput = json::decode::(payload_json); let mut result = { ok: true, code: "ok", message: "", result: { columns: [], rows: [] } }; - if !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.parent_run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.parent_run_id.copy())]; + if !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "run_not_found", message: "parent or child run does not exist", result: { columns: [], rows: [] } }; } else { - if !(sqlite::query(&db_id, existence::run_exists_sql(), [sql_text(input.child_run_id.copy())], sql_limits(1, 4096)).rows.length > 0) { + let params: array = [sql_text(input.child_run_id.copy())]; + if !(sqlite::query(&db_id, existence::run_exists_sql(), params, sql_limits(1, 4096)).rows.length > 0) { result = { ok: false, code: "run_not_found", message: "parent or child run does not exist", result: { columns: [], rows: [] } }; } else { // Idempotent: re-linking an existing pair is a no-op success, // never an orphan write or a duplicate error. - sqlite::execute(&db_id, "INSERT OR IGNORE INTO child_run_links (parent_run_id, child_run_id, ordinal, relation, state, created_at_ms) SELECT ?, ?, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ?) AND EXISTS (SELECT 1 FROM runs WHERE id = ?)", [sql_text(input.parent_run_id.copy()), sql_text(input.child_run_id.copy()), sql_int(input.ordinal.copy()), sql_text(input.relation.copy()), sql_text(input.state.copy()), sql_int(input.now_ms.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.child_run_id.copy())]); + let params: array = [sql_text(input.parent_run_id.copy()), sql_text(input.child_run_id.copy()), sql_int(input.ordinal.copy()), sql_text(input.relation.copy()), sql_text(input.state.copy()), sql_int(input.now_ms.copy()), sql_text(input.parent_run_id.copy()), sql_text(input.child_run_id.copy())]; + sqlite::execute(&db_id, "INSERT OR IGNORE INTO child_run_links (parent_run_id, child_run_id, ordinal, relation, state, created_at_ms) SELECT ?, ?, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ?) AND EXISTS (SELECT 1 FROM runs WHERE id = ?)", params); + let params: array = [sql_text(input.parent_run_id.copy()), sql_text(input.child_run_id.copy())]; result = { ok: true, code: "ok", message: "", - result: sqlite::query(&db_id, "SELECT parent_run_id, child_run_id, ordinal, relation, state, created_at_ms FROM child_run_links WHERE parent_run_id = ? AND child_run_id = ? LIMIT 1", [sql_text(input.parent_run_id.copy()), sql_text(input.child_run_id.copy())], sql_limits(1, 4096)) + result: sqlite::query(&db_id, "SELECT parent_run_id, child_run_id, ordinal, relation, state, created_at_ms FROM child_run_links WHERE parent_run_id = ? AND child_run_id = ? LIMIT 1", params, sql_limits(1, 4096)) }; } } @@ -262,7 +279,7 @@ pub fn storage_run_link_child(db_id: resource, payload_json: /// the returned rows let the caller reconcile in-memory sequences. /// Strict extraction of one string member from the frozen publication document. fn json_extract_text(document: string, path: string) -> string { - let parsed: map = json::decode(document.copy()); + let parsed: map = json::decode::(document.copy()); let mut text: string = ""; if path == "$.proof" { let value: string = parsed["proof"].copy(); @@ -278,16 +295,16 @@ fn json_extract_text(document: string, path: string) -> string { /// The single entry owns the Native resource directly; replay, guard abort and /// post-commit readback all happen here so no resource is forwarded onward. pub fn storage_run_terminal_published(db_id: resource, payload_json: string) -> map { - let raw_payload: map = json::decode(payload_json.copy()); let input: RunTerminalPublishedInput = json::decode::(payload_json.copy()); assert(input.to_status.copy() == "completed"); - let mut reserved_ordinal: int = 0; - if raw_payload.has("message_ordinal") { - reserved_ordinal = raw_payload["message_ordinal"].copy(); - } + let reserved_ordinal: int = match input.message_ordinal { + Some(value) => value, + None => 0 + }; + let params: array = [sql_text(input.run_id.copy()), sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy())]; // Read-only replay precheck: an exact receipt for this frozen request with // the run already completed writes nothing and returns the existing rows. - let existing: SqliteQueryResult = sqlite::query(&db_id, "SELECT json_object('proof',original_request_proof,'receipt',json(receipt_json),'run_status',(SELECT status FROM runs WHERE id=json_extract(?,'$.run_id'))) AS record_json FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')", [sql_text(input.run_id.copy()), sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy())], sql_limits(1, 65536)); + let existing: SqliteQueryResult = sqlite::query(&db_id, "SELECT json_object('proof',original_request_proof,'receipt',json(receipt_json),'run_status',(SELECT status FROM runs WHERE id=json_extract(?,'$.run_id'))) AS record_json FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')", params, sql_limits(1, 65536)); let existing_rows = existing.rows; let mut replay: bool = false; let mut conflict: bool = false; @@ -295,8 +312,8 @@ pub fn storage_run_terminal_published(db_id: resource, payloa if existing_rows.length > 0 { replay = true; let saved_row: SqliteRow = existing_rows[0].copy(); - let saved = saved_row.cells[0].text_value; - let saved_decode: map = json::decode(saved.copy()); + let saved = saved_row.cells[0].text_value.unwrap_or(""); + let saved_decode: map = json::decode::(saved.copy()); let saved_proof: string = saved_decode["proof"].copy(); let frozen_proof: string = json_extract_text(input.publication_json.copy(), "$.proof"); if saved_proof != frozen_proof { @@ -311,6 +328,7 @@ pub fn storage_run_terminal_published(db_id: resource, payloa } } let publication = input.publication_json.copy(); + let params: array = [sql_text(input.run_id.copy())]; if !replay && !conflict && !inconsistent { let mut statements: array = []; @@ -320,30 +338,30 @@ pub fn storage_run_terminal_published(db_id: resource, payloa // workspace principal is `managed_workspaces.profile`/binding profile. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id JOIN sessions s ON s.id=b.session_id JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.profile=b.profile JOIN managed_branches h ON h.workspace_id=b.workspace_id AND h.branch_id=b.branch_id AND h.store_id=w.store_id WHERE b.run_id=json_extract(?,'$.run_id') AND b.profile=json_extract(?,'$.profile') AND b.session_id=json_extract(?,'$.session_id') AND b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND b.epoch=json_extract(?,'$.epoch') AND b.schema_version=1 AND r.status='running' AND r.session_id=b.session_id)", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // No other active run on this branch. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_run_bindings b JOIN runs r ON r.id=b.run_id WHERE b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND b.run_id<>json_extract(?,'$.run_id') AND r.status NOT IN ('completed','failed','cancelled'))", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // No unresolved managed effect on this branch. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_effect_calls WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id') AND state IN ('intent','prepared','indeterminate'))", - params: [sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // Exact branch root/generation/epoch/store/schema/lineage. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id AND w.profile=json_extract(?,'$.profile') WHERE b.workspace_id=json_extract(?,'$.expected_head.workspace_id') AND b.branch_id=json_extract(?,'$.expected_head.branch_id') AND b.status='active' AND b.epoch=json_extract(?,'$.expected_head.epoch') AND b.head_object_id=json_extract(?,'$.expected_head.head_object_id') AND b.head_version=json_extract(?,'$.expected_head.head_version') AND b.store_id=json_extract(?,'$.expected_head.store_id') AND b.schema_version=json_extract(?,'$.expected_head.schema_version') AND b.process_lineage_tainted=json_extract(?,'$.expected_head.process_lineage_tainted'))", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // Absolute lineage success predicate: the admitted branch must be // untainted, and the frozen expected head must agree. Equality of two @@ -352,9 +370,9 @@ pub fn storage_run_terminal_published(db_id: resource, payloa // required to be zero. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_branches b WHERE b.workspace_id=json_extract(?,'$.expected_head.workspace_id') AND b.branch_id=json_extract(?,'$.expected_head.branch_id') AND b.process_lineage_tainted=0 AND json_extract(?,'$.expected_head.process_lineage_tainted')=0)", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // Fresh Native source observation against the durable captured source // identity of this admitted workspace. The observation is taken from @@ -363,86 +381,86 @@ pub fn storage_run_terminal_published(db_id: resource, payloa // captured value must agree with both. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_workspaces w WHERE w.workspace_id=json_extract(?,'$.workspace_id') AND w.profile=json_extract(?,'$.profile') AND w.source_device=json_extract(?,'$.fresh_source_identity.device_decimal') AND w.source_inode=json_extract(?,'$.fresh_source_identity.inode_decimal') AND json_extract(?,'$.captured_source_identity.device_decimal')=json_extract(?,'$.fresh_source_identity.device_decimal') AND json_extract(?,'$.captured_source_identity.inode_decimal')=json_extract(?,'$.fresh_source_identity.inode_decimal'))", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // Fresh Native authority generation against the value captured by the // admitted grant. Two equal real reads are the success fact; a stale // capture never publishes through this transaction. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE json_extract(?,'$.authority_generation') IS NOT json_extract(?,'$.observed_authority_generation') OR json_extract(?,'$.authority_generation')<1", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // Cross-action request-id conflict: one request id never names two actions. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof<>json_extract(?,'$.proof')) OR EXISTS (SELECT 1 FROM managed_branch_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_checkpoints WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')) OR EXISTS (SELECT 1 FROM managed_restore_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // Pointer revision CAS. statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE (json_extract(?,'$.expected_revision')=0 AND EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id'))) OR (json_extract(?,'$.expected_revision')<>0 AND NOT EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND revision=json_extract(?,'$.expected_revision')))", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // Exact graph revocation facts for this run's admitted pin. A run with // no admitted graph pin has no revocation constraint (non-anchor stays // Unresolved only through the branch owner, never a fabricated pin). statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS (SELECT 1 FROM plugin_graph_pin_requirements q JOIN plugin_graph_revocations r ON r.graph_id=q.graph_id WHERE q.run_id=json_extract(?,'$.run_id'))", - params: [sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; // Terminal events in message.delta?/workspace.published/run.completed order. if input.event_count.copy() >= 1 { statements[statements.length] = { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = 'running')", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_1_id.copy()), sql_text(input.event_1_type.copy()), sql_text(input.event_1_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_1_id.copy()), sql_text(input.event_1_type.copy()), sql_text(input.event_1_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; } if input.event_count.copy() >= 2 { statements[statements.length] = { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = 'running')", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_2_id.copy()), sql_text(input.event_2_type.copy()), sql_text(input.event_2_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_2_id.copy()), sql_text(input.event_2_type.copy()), sql_text(input.event_2_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; } if input.event_count.copy() >= 3 { statements[statements.length] = { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = 'running')", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_3_id.copy()), sql_text(input.event_3_type.copy()), sql_text(input.event_3_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_3_id.copy()), sql_text(input.event_3_type.copy()), sql_text(input.event_3_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; } // Assistant message: the frozen request's expected cardinality is explicit. if input.message_id.copy() != "" { statements[statements.length] = { sql: "INSERT INTO messages (id, session_id, ordinal, role, content_json, metadata_json, run_id, finish_reason, created_at_ms) SELECT ?, ?, CASE WHEN ? > 0 THEN ? ELSE COALESCE(MAX(ordinal), 0) + 1 END, ?, ?, '{}', ?, ?, ? FROM messages WHERE session_id = ? AND EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = 'running') AND NOT EXISTS (SELECT 1 FROM messages existing WHERE existing.id = ? AND existing.session_id <> ?)", - params: [sql_text(input.message_id.copy()), sql_text(input.message_session_id.copy()), sql_int(reserved_ordinal.copy()), sql_int(reserved_ordinal.copy()), sql_text(input.message_role.copy()), sql_text(input.message_content_json.copy()), sql_text(input.message_run_id.copy()), sql_text(input.message_finish_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_session_id.copy()), sql_text(input.run_id.copy()), sql_text(input.message_id.copy()), sql_text(input.message_session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.message_id.copy()), sql_text(input.message_session_id.copy()), sql_int(reserved_ordinal.copy()), sql_int(reserved_ordinal.copy()), sql_text(input.message_role.copy()), sql_text(input.message_content_json.copy()), sql_text(input.message_run_id.copy()), sql_text(input.message_finish_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_session_id.copy()), sql_text(input.run_id.copy()), sql_text(input.message_id.copy()), sql_text(input.message_session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "UPDATE sessions SET last_message_seq = COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id = ?), 0), updated_at_ms = ? WHERE id = ?", - params: [sql_text(input.message_session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.message_session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; } statements[statements.length] = { // The trailing predicate is a Native one-shot fixture fault only: @@ -451,59 +469,61 @@ pub fn storage_run_terminal_published(db_id: resource, payloa // whole transaction (pointer/receipt/run/messages/events) back. // Normal requests carry no such member and always match one row. sql: "UPDATE runs SET status = ?, error_code = ?, error_message = ?, recovery_reason = '', finished_at_ms = ?, updated_at_ms = ? WHERE id = ? AND status = 'running' AND json_extract(?, '$.managed_zero_row_fault') IS NULL", - params: [sql_text(input.to_status.copy()), sql_text(input.error_code.copy()), sql_text(input.error_message.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.publication_json.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.to_status.copy()), sql_text(input.error_code.copy()), sql_text(input.error_message.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.publication_json.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT ?, COALESCE((SELECT MIN(seq) FROM run_events WHERE run_id = ?), 0), COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id = ?), 0), ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = ?) ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.to_status.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.to_status.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; // Pointer insert or revision update, guarded by the frozen expected revision. statements[statements.length] = { sql: "INSERT INTO managed_workspace_publications(workspace_id,profile,branch_id,epoch,head_object_id,head_version,previous_head_object_id,request_id,published_at_ms,revision,store_id,schema_version,record_json) SELECT b.workspace_id,w.profile,b.branch_id,b.epoch,b.head_object_id,b.head_version,NULL,json_extract(?,'$.request_id'),json_extract(?,'$.published_at_ms'),1,b.store_id,b.schema_version,'{}' FROM managed_branches b JOIN managed_workspaces w ON w.workspace_id=b.workspace_id WHERE json_extract(?,'$.expected_revision')=0 AND b.workspace_id=json_extract(?,'$.workspace_id') AND b.branch_id=json_extract(?,'$.branch_id') AND NOT EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=b.workspace_id) AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "UPDATE managed_workspace_publications SET profile=(SELECT profile FROM managed_workspaces WHERE workspace_id=managed_workspace_publications.workspace_id), branch_id=json_extract(?,'$.branch_id'), epoch=(SELECT epoch FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), head_object_id=(SELECT head_object_id FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), head_version=(SELECT head_version FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), previous_head_object_id=head_object_id, request_id=json_extract(?,'$.request_id'), published_at_ms=json_extract(?,'$.published_at_ms'), revision=revision+1, store_id=(SELECT store_id FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')), schema_version=(SELECT schema_version FROM managed_branches WHERE workspace_id=json_extract(?,'$.workspace_id') AND branch_id=json_extract(?,'$.branch_id')) WHERE workspace_id=json_extract(?,'$.workspace_id') AND revision=json_extract(?,'$.expected_revision') AND json_extract(?,'$.expected_revision')<>0 AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND revision=CASE WHEN json_extract(?,'$.expected_revision')=0 THEN 1 ELSE json_extract(?,'$.expected_revision')+1 END)", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "UPDATE managed_workspace_publications SET record_json=json_object('workspace_id',workspace_id,'profile',profile,'branch_id',branch_id,'epoch',epoch,'head_object_id',head_object_id,'head_version',head_version,'previous_published_head',CASE WHEN previous_head_object_id IS NULL THEN json('null') ELSE json_quote(previous_head_object_id) END,'request_id',request_id,'published_at_ms',published_at_ms,'revision',revision,'store_id',store_id,'schema_version',schema_version) WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id')", - params: [sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "INSERT INTO managed_publish_receipts(workspace_id,request_id,branch_id,original_request_proof,receipt_json,action) SELECT workspace_id,request_id,branch_id,json_extract(?,'$.proof'),json_object('status','committed','workspace_id',workspace_id,'profile',profile,'branch_id',branch_id,'epoch',epoch,'head_object_id',head_object_id,'head_version',head_version,'previous_published_head',CASE WHEN previous_head_object_id IS NULL THEN json('null') ELSE json_quote(previous_head_object_id) END,'request_id',request_id,'published_at_ms',published_at_ms),'publish' FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND NOT EXISTS (SELECT 1 FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))", - params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; - statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: [], query: false, limits: sql_limits(1000, 4194304) }; - statements[statements.length] = { sql: "SELECT json_object('receipt',(SELECT json(receipt_json) FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')),'publication',(SELECT json(record_json) FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))) AS record_json", params: [sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())], query: true, limits: sql_limits(1, 65536) }; + statements[statements.length] = { sql: "INSERT INTO managed_effect_guard(id) SELECT 2 WHERE changes()<>1", params: no_params(), query: Some(false), limits: Some(sql_limits(1000, 4194304)) }; + statements[statements.length] = { sql: "SELECT json_object('receipt',(SELECT json(receipt_json) FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof')),'publication',(SELECT json(record_json) FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))) AS record_json", params: Some([sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy()), sql_text(publication.copy())]), query: Some(true), limits: Some(sql_limits(1, 65536)) }; let results: array = sqlite::transaction(&db_id, statements); let last: SqliteTransactionResult = results[results.length - 1].copy(); - let query: SqliteQueryResult = last.query; + let query: SqliteQueryResult = match last.query { Some(value) => value, None => {columns: [], rows: [], truncated: false, next_cursor: no_int()}, }; } - let run_result: SqliteQueryResult = sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT 1", [sql_text(input.run_id.copy())], sql_limits(1, 65536)); - let receipt_result: SqliteQueryResult = sqlite::query(&db_id, "SELECT json_object('receipt',json(receipt_json),'publication',(SELECT json(record_json) FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))) AS record_json FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof') LIMIT 1", [sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy())], sql_limits(1, 65536)); - let event_result: map = sqlite_query_map(sqlite::query(&db_id, "SELECT seq, run_id, event_id, event_type, payload_json, created_at_ms FROM run_events WHERE run_id = ? ORDER BY seq ASC LIMIT ?", [sql_text(input.run_id.copy()), sql_int(512)], sql_limits(512, 2097152))); + let run_result: SqliteQueryResult = sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT 1", params, sql_limits(1, 65536)); + let params: array = [sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy()), sql_text(input.publication_json.copy())]; + let receipt_result: SqliteQueryResult = sqlite::query(&db_id, "SELECT json_object('receipt',json(receipt_json),'publication',(SELECT json(record_json) FROM managed_workspace_publications WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id'))) AS record_json FROM managed_publish_receipts WHERE workspace_id=json_extract(?,'$.workspace_id') AND request_id=json_extract(?,'$.request_id') AND original_request_proof=json_extract(?,'$.proof') LIMIT 1", params, sql_limits(1, 65536)); + let params: array = [sql_text(input.run_id.copy()), sql_int(512)]; + let event_result: map = sqlite_query_map(sqlite::query(&db_id, "SELECT seq, run_id, event_id, event_type, payload_json, created_at_ms FROM run_events WHERE run_id = ? ORDER BY seq ASC LIMIT ?", params, sql_limits(512, 2097152))); let receipt_map: map = sqlite_query_map(receipt_result); let receipt_rows: array = receipt_map.rows; let receipt_row_count: int = receipt_rows.length; @@ -527,58 +547,58 @@ pub fn storage_run_terminal_published(db_id: resource, payloa } pub fn storage_run_terminal(db_id: resource, payload_json: string) -> map { - let raw_payload: map = json::decode(payload_json); let input: RunTerminalInput = json::decode::(payload_json); assert(storage_run_status_allowed(input.to_status.copy())); - let mut reserved_ordinal: int = 0; - if raw_payload.has("message_ordinal") { - reserved_ordinal = raw_payload["message_ordinal"].copy(); - } + let reserved_ordinal: int = match input.message_ordinal { + Some(value) => value, + None => 0 + }; let mut statements: array = []; if input.event_count.copy() >= 1 { statements[statements.length] = { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = 'running')", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_1_id.copy()), sql_text(input.event_1_type.copy()), sql_text(input.event_1_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_1_id.copy()), sql_text(input.event_1_type.copy()), sql_text(input.event_1_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; } if input.event_count.copy() >= 2 { statements[statements.length] = { sql: "INSERT INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT ?, COALESCE((SELECT MAX(events.seq) FROM run_events events WHERE events.run_id = ?), 0) + 1, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = 'running')", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_2_id.copy()), sql_text(input.event_2_type.copy()), sql_text(input.event_2_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.event_2_id.copy()), sql_text(input.event_2_type.copy()), sql_text(input.event_2_payload.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; } if input.message_id.copy() != "" { statements[statements.length] = { sql: "INSERT INTO messages (id, session_id, ordinal, role, content_json, metadata_json, run_id, finish_reason, created_at_ms) SELECT ?, ?, CASE WHEN ? > 0 THEN ? ELSE COALESCE(MAX(ordinal), 0) + 1 END, ?, ?, '{}', ?, ?, ? FROM messages WHERE session_id = ? AND EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = 'running') AND NOT EXISTS (SELECT 1 FROM messages existing WHERE existing.id = ? AND existing.session_id <> ?)", - params: [sql_text(input.message_id.copy()), sql_text(input.message_session_id.copy()), sql_int(reserved_ordinal.copy()), sql_int(reserved_ordinal.copy()), sql_text(input.message_role.copy()), sql_text(input.message_content_json.copy()), sql_text(input.message_run_id.copy()), sql_text(input.message_finish_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_session_id.copy()), sql_text(input.run_id.copy()), sql_text(input.message_id.copy()), sql_text(input.message_session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.message_id.copy()), sql_text(input.message_session_id.copy()), sql_int(reserved_ordinal.copy()), sql_int(reserved_ordinal.copy()), sql_text(input.message_role.copy()), sql_text(input.message_content_json.copy()), sql_text(input.message_run_id.copy()), sql_text(input.message_finish_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_session_id.copy()), sql_text(input.run_id.copy()), sql_text(input.message_id.copy()), sql_text(input.message_session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "UPDATE sessions SET last_message_seq = COALESCE((SELECT MAX(ordinal) FROM messages WHERE session_id = ?), 0), updated_at_ms = ? WHERE id = ?", - params: [sql_text(input.message_session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.message_session_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.message_session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; } statements[statements.length] = { sql: "UPDATE runs SET status = ?, error_code = ?, error_message = ?, recovery_reason = '', finished_at_ms = ?, updated_at_ms = ? WHERE id = ? AND status = 'running'", - params: [sql_text(input.to_status.copy()), sql_text(input.error_code.copy()), sql_text(input.error_message.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.to_status.copy()), sql_text(input.error_code.copy()), sql_text(input.error_message.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; statements[statements.length] = { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT ?, COALESCE((SELECT MIN(seq) FROM run_events WHERE run_id = ?), 0), COALESCE((SELECT MAX(seq) FROM run_events WHERE run_id = ?), 0), ? WHERE EXISTS (SELECT 1 FROM runs WHERE id = ? AND status = ?) ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.to_status.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy()), sql_text(input.to_status.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }; sqlite::transaction(&db_id, statements); - let run_result = sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT 1", [sql_text(input.run_id.copy())], sql_limits(1, 65536)); + let params: array = [sql_text(input.run_id.copy())]; + let run_result = sqlite::query(&db_id, "SELECT id, session_id, parent_run_id, status, input_json, provider, model, script_hash, idempotency_scope, idempotency_key, turn_count, input_tokens, output_tokens, error_code, error_message, recovery_reason, created_at_ms, started_at_ms, finished_at_ms, updated_at_ms FROM runs WHERE id = ? LIMIT 1", params, sql_limits(1, 65536)); let run_result_copy: map = sqlite_query_map(run_result); let run_rows = run_result.rows; let mut result = { ok: true, code: "ok", message: "", result: { run: run_result_copy, events: { columns: [], rows: [] } } }; @@ -589,11 +609,12 @@ pub fn storage_run_terminal(db_id: resource, payload_json: st // terminal, or never running) is a typed conflict, never a silent // success: the run row's status tells whether the transition landed. let run_row = run_rows[0]; - let run_status: string = run_row.cells[3].text_value; + let run_status: string = run_row.cells[3].text_value.unwrap_or(""); if run_status != input.to_status.copy() { result = { ok: false, code: "transition_conflict", message: "run terminal transition matched no row", result: { run: run_result_copy, events: { columns: [], rows: [] } } }; } else { - let event_result: map = sqlite_query_map(sqlite::query(&db_id, "SELECT seq, run_id, event_id, event_type, payload_json, created_at_ms FROM run_events WHERE run_id = ? ORDER BY seq ASC LIMIT ?", [sql_text(input.run_id.copy()), sql_int(512)], sql_limits(512, 2097152))); + let params: array = [sql_text(input.run_id.copy()), sql_int(512)]; + let event_result: map = sqlite_query_map(sqlite::query(&db_id, "SELECT seq, run_id, event_id, event_type, payload_json, created_at_ms FROM run_events WHERE run_id = ? ORDER BY seq ASC LIMIT ?", params, sql_limits(512, 2097152))); result = { ok: true, code: "ok", message: "", result: { run: run_result_copy, events: event_result } }; } } @@ -601,14 +622,18 @@ pub fn storage_run_terminal(db_id: resource, payload_json: st } pub fn storage_run_list_children(db_id: resource, parent_run_id: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT parent_run_id, child_run_id, ordinal, relation, state, created_at_ms FROM child_run_links WHERE parent_run_id = ? ORDER BY ordinal ASC, child_run_id ASC LIMIT ?", [sql_text(parent_run_id), sql_int(max_rows)], runs_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(parent_run_id), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT parent_run_id, child_run_id, ordinal, relation, state, created_at_ms FROM child_run_links WHERE parent_run_id = ? ORDER BY ordinal ASC, child_run_id ASC LIMIT ?", params, runs_query_limits(max_rows, max_bytes)) } pub fn storage_run_record_usage(db_id: resource, payload_json: string) -> SqliteQueryResult { let input: UsageInput = json::decode::(payload_json); - sqlite::execute(&db_id, "INSERT INTO provider_usage (id, run_id, provider, model, request_count, input_tokens, output_tokens, reasoning_tokens, cached_tokens, created_at_ms, usage_ids) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '|' || ? || '|' WHERE ? >= 0 AND ? >= 0 AND ? >= 0 AND ? >= 0 AND ? >= 0 AND instr(?, '|') = 0 ON CONFLICT (run_id, provider, model) DO UPDATE SET request_count = provider_usage.request_count + excluded.request_count, input_tokens = provider_usage.input_tokens + excluded.input_tokens, output_tokens = provider_usage.output_tokens + excluded.output_tokens, reasoning_tokens = provider_usage.reasoning_tokens + excluded.reasoning_tokens, cached_tokens = provider_usage.cached_tokens + excluded.cached_tokens, usage_ids = provider_usage.usage_ids || excluded.id || '|' WHERE instr(provider_usage.usage_ids, '|' || excluded.id || '|') = 0", [sql_text(input.id.copy()), sql_text(input.run_id.copy()), sql_text(input.provider.copy()), sql_text(input.model.copy()), sql_int(input.request_count.copy()), sql_int(input.input_tokens.copy()), sql_int(input.output_tokens.copy()), sql_int(input.reasoning_tokens.copy()), sql_int(input.cached_tokens.copy()), sql_int(input.now_ms.copy()), sql_text(input.id.copy()), sql_int(input.request_count.copy()), sql_int(input.input_tokens.copy()), sql_int(input.output_tokens.copy()), sql_int(input.reasoning_tokens.copy()), sql_int(input.cached_tokens.copy()), sql_text(input.id.copy())]); - sqlite::execute(&db_id, "UPDATE runs SET input_tokens = COALESCE((SELECT SUM(input_tokens) FROM provider_usage WHERE run_id = ?), 0), output_tokens = COALESCE((SELECT SUM(output_tokens) FROM provider_usage WHERE run_id = ?), 0), updated_at_ms = ? WHERE id = ?", [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]); - sqlite::query(&db_id, "SELECT id, input_tokens, output_tokens FROM runs WHERE id = ? LIMIT 1", [sql_text(input.run_id.copy())], sql_limits(1, 4096)) + let params: array = [sql_text(input.id.copy()), sql_text(input.run_id.copy()), sql_text(input.provider.copy()), sql_text(input.model.copy()), sql_int(input.request_count.copy()), sql_int(input.input_tokens.copy()), sql_int(input.output_tokens.copy()), sql_int(input.reasoning_tokens.copy()), sql_int(input.cached_tokens.copy()), sql_int(input.now_ms.copy()), sql_text(input.id.copy()), sql_int(input.request_count.copy()), sql_int(input.input_tokens.copy()), sql_int(input.output_tokens.copy()), sql_int(input.reasoning_tokens.copy()), sql_int(input.cached_tokens.copy()), sql_text(input.id.copy())]; + sqlite::execute(&db_id, "INSERT INTO provider_usage (id, run_id, provider, model, request_count, input_tokens, output_tokens, reasoning_tokens, cached_tokens, created_at_ms, usage_ids) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '|' || ? || '|' WHERE ? >= 0 AND ? >= 0 AND ? >= 0 AND ? >= 0 AND ? >= 0 AND instr(?, '|') = 0 ON CONFLICT (run_id, provider, model) DO UPDATE SET request_count = provider_usage.request_count + excluded.request_count, input_tokens = provider_usage.input_tokens + excluded.input_tokens, output_tokens = provider_usage.output_tokens + excluded.output_tokens, reasoning_tokens = provider_usage.reasoning_tokens + excluded.reasoning_tokens, cached_tokens = provider_usage.cached_tokens + excluded.cached_tokens, usage_ids = provider_usage.usage_ids || excluded.id || '|' WHERE instr(provider_usage.usage_ids, '|' || excluded.id || '|') = 0", params); + let params: array = [sql_text(input.run_id.copy()), sql_text(input.run_id.copy()), sql_int(input.now_ms.copy()), sql_text(input.run_id.copy())]; + sqlite::execute(&db_id, "UPDATE runs SET input_tokens = COALESCE((SELECT SUM(input_tokens) FROM provider_usage WHERE run_id = ?), 0), output_tokens = COALESCE((SELECT SUM(output_tokens) FROM provider_usage WHERE run_id = ?), 0), updated_at_ms = ? WHERE id = ?", params); + let params: array = [sql_text(input.run_id.copy())]; + sqlite::query(&db_id, "SELECT id, input_tokens, output_tokens FROM runs WHERE id = ? LIMIT 1", params, sql_limits(1, 4096)) } pub fn storage_run_idempotency_begin(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { @@ -616,36 +641,38 @@ pub fn storage_run_idempotency_begin(db_id: resource, payload let statements: array = [ { sql: "DELETE FROM idempotency_records WHERE scope = ? AND key = ? AND ((state = 'claimed' AND expires_at_ms <= ?) OR state = 'expired')", - params: [sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_int(input.now_ms.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT OR IGNORE INTO idempotency_records (scope, key, request_hash, resource_type, resource_id, state, created_at_ms, expires_at_ms) SELECT ?, ?, ?, ?, ?, 'claimed', ?, ? WHERE ? <> ''", - params: [sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.request_hash.copy()), sql_text(input.resource_type.copy()), sql_text(input.resource_id.copy()), sql_int(input.now_ms.copy()), sql_int(input.expires_at_ms.copy()), sql_text(input.claim_token.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.request_hash.copy()), sql_text(input.resource_type.copy()), sql_text(input.resource_id.copy()), sql_int(input.now_ms.copy()), sql_int(input.expires_at_ms.copy()), sql_text(input.claim_token.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO idempotency_claim_history (claim_token, scope, key, claimed_at_ms) SELECT ?, ?, ?, ? WHERE changes() = 1 AND ? <> ''", - params: [sql_text(input.claim_token.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_int(input.now_ms.copy()), sql_text(input.claim_token.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.claim_token.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_int(input.now_ms.copy()), sql_text(input.claim_token.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT OR REPLACE INTO idempotency_claims (scope, key, request_hash, claim_token, claimed_at_ms) SELECT ?, ?, ?, ?, ? WHERE changes() = 1", - params: [sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.request_hash.copy()), sql_text(input.claim_token.copy()), sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.request_hash.copy()), sql_text(input.claim_token.copy()), sql_int(input.now_ms.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; sqlite::transaction(&db_id, statements); - sqlite::query(&db_id, "SELECT scope, key, request_hash, resource_type, resource_id, CASE WHEN request_hash = ? THEN state ELSE 'failed' END AS state, CASE WHEN request_hash = ? THEN response_json ELSE '{\"error_code\":\"idempotency_key_conflict\"}' END AS response_json, CASE WHEN changes() = 1 AND EXISTS (SELECT 1 FROM idempotency_claims WHERE scope = ? AND key = ? AND claim_token = ?) THEN 1 ELSE 0 END AS acquired, CASE WHEN changes() = 1 AND EXISTS (SELECT 1 FROM idempotency_claims WHERE scope = ? AND key = ? AND claim_token = ?) THEN ? ELSE '' END AS claim_token, created_at_ms, expires_at_ms, completed_at_ms FROM idempotency_records WHERE scope = ? AND key = ? LIMIT ?", [sql_text(input.request_hash.copy()), sql_text(input.request_hash.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.claim_token.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.claim_token.copy()), sql_text(input.claim_token.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_int(max_rows)], runs_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(input.request_hash.copy()), sql_text(input.request_hash.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.claim_token.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.claim_token.copy()), sql_text(input.claim_token.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT scope, key, request_hash, resource_type, resource_id, CASE WHEN request_hash = ? THEN state ELSE 'failed' END AS state, CASE WHEN request_hash = ? THEN response_json ELSE '{\"error_code\":\"idempotency_key_conflict\"}' END AS response_json, CASE WHEN changes() = 1 AND EXISTS (SELECT 1 FROM idempotency_claims WHERE scope = ? AND key = ? AND claim_token = ?) THEN 1 ELSE 0 END AS acquired, CASE WHEN changes() = 1 AND EXISTS (SELECT 1 FROM idempotency_claims WHERE scope = ? AND key = ? AND claim_token = ?) THEN ? ELSE '' END AS claim_token, created_at_ms, expires_at_ms, completed_at_ms FROM idempotency_records WHERE scope = ? AND key = ? LIMIT ?", params, runs_query_limits(max_rows, max_bytes)) } pub fn storage_run_idempotency_complete(db_id: resource, payload_json: string) -> SqliteExecuteResult { let input: IdempotencyCompleteInput = json::decode::(payload_json); - sqlite::execute(&db_id, "UPDATE idempotency_records SET state = ?, response_json = ?, completed_at_ms = ? WHERE scope = ? AND key = ? AND request_hash = ? AND state = 'claimed' AND ? IN ('completed', 'failed') AND expires_at_ms > ? AND ? <> '' AND EXISTS (SELECT 1 FROM idempotency_claims WHERE scope = ? AND key = ? AND request_hash = ? AND claim_token = ?)", [sql_text(input.state.copy()), sql_text(input.response_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.request_hash.copy()), sql_text(input.state.copy()), sql_int(input.now_ms.copy()), sql_text(input.claim_token.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.request_hash.copy()), sql_text(input.claim_token.copy())]) + let params: array = [sql_text(input.state.copy()), sql_text(input.response_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.request_hash.copy()), sql_text(input.state.copy()), sql_int(input.now_ms.copy()), sql_text(input.claim_token.copy()), sql_text(input.scope.copy()), sql_text(input.key.copy()), sql_text(input.request_hash.copy()), sql_text(input.claim_token.copy())]; + sqlite::execute(&db_id, "UPDATE idempotency_records SET state = ?, response_json = ?, completed_at_ms = ? WHERE scope = ? AND key = ? AND request_hash = ? AND state = 'claimed' AND ? IN ('completed', 'failed') AND expires_at_ms > ? AND ? <> '' AND EXISTS (SELECT 1 FROM idempotency_claims WHERE scope = ? AND key = ? AND request_hash = ? AND claim_token = ?)", params) } pub fn storage_run_recover_active(db_id: resource, payload_json: string) -> array { @@ -653,27 +680,27 @@ pub fn storage_run_recover_active(db_id: resource, payload_js let statements: array = [ { sql: "INSERT OR IGNORE INTO recovery_records (id, run_id, from_status, to_status, reason, details_json, recovered_at_ms) SELECT 'recovery:' || id || ':' || ?, id, status, 'failed', ?, ?, ? FROM runs WHERE id IN (SELECT id FROM runs WHERE status IN ('queued', 'running', 'waiting_approval', 'compacting') ORDER BY updated_at_ms ASC, id ASC LIMIT CASE WHEN ? <= 0 THEN 64 WHEN ? > 512 THEN 512 ELSE ? END)", - params: [sql_int(input.now_ms.copy()), sql_text(input.reason.copy()), sql_text(input.details_json.copy()), sql_int(input.now_ms.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_text(input.reason.copy()), sql_text(input.details_json.copy()), sql_int(input.now_ms.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT OR IGNORE INTO run_events (run_id, seq, event_id, event_type, payload_json, created_at_ms) SELECT id, COALESCE((SELECT MAX(e.seq) FROM run_events e WHERE e.run_id = runs.id), 0) + ROW_NUMBER() OVER (PARTITION BY runs.id ORDER BY runs.updated_at_ms ASC, runs.id ASC), 'recovery-event:' || id || ':' || ?, 'run.failed', '{\"status\":\"failed\",\"error_code\":\"gateway_restart\",\"recovery_reason\":\"gateway_restart\"}', ? FROM runs WHERE id IN (SELECT id FROM runs WHERE status IN ('queued', 'running', 'waiting_approval', 'compacting') ORDER BY updated_at_ms ASC, id ASC LIMIT CASE WHEN ? <= 0 THEN 64 WHEN ? > 512 THEN 512 ELSE ? END)", - params: [sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE runs SET status = 'failed', error_code = 'gateway_restart', error_message = 'run interrupted during gateway restart', recovery_reason = ?, finished_at_ms = ?, updated_at_ms = ? WHERE id IN (SELECT id FROM runs WHERE status IN ('queued', 'running', 'waiting_approval', 'compacting') ORDER BY updated_at_ms ASC, id ASC LIMIT CASE WHEN ? <= 0 THEN 64 WHEN ? > 512 THEN 512 ELSE ? END)", - params: [sql_text(input.reason.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.reason.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy()), sql_int(input.max_rows.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE approvals SET state = 'expired', resolved_at_ms = ? WHERE state = 'pending' AND run_id IN (SELECT run_id FROM recovery_records WHERE recovered_at_ms = ? AND reason = ?)", - params: [sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.reason.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.reason.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { // After a restart EVERY pending compaction is an interrupted @@ -682,33 +709,33 @@ pub fn storage_run_recover_active(db_id: resource, payload_js // terminal (never in the recovered set), so the fail-sweep is // unconditional instead of being limited to recovered runs. sql: "UPDATE compactions SET state = 'failed', error_message = 'run interrupted during gateway restart', completed_at_ms = ? WHERE state = 'pending'", - params: [sql_int(input.now_ms.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE idempotency_records SET state = 'expired', completed_at_ms = ? WHERE state = 'claimed' AND resource_type = 'run' AND resource_id IN (SELECT run_id FROM recovery_records WHERE recovered_at_ms = ? AND reason = ?)", - params: [sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.reason.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.reason.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM idempotency_claims WHERE EXISTS (SELECT 1 FROM idempotency_records WHERE idempotency_records.scope = idempotency_claims.scope AND idempotency_records.key = idempotency_claims.key AND idempotency_records.state = 'expired')", - params: [], - query: false, - limits: sql_limits(1000, 4194304) + params: no_params(), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM run_events WHERE rowid IN (SELECT rowid FROM (SELECT rowid, ROW_NUMBER() OVER (PARTITION BY run_id ORDER BY seq ASC) AS rn, COUNT(*) OVER (PARTITION BY run_id) AS total FROM run_events WHERE run_id IN (SELECT run_id FROM recovery_records WHERE recovered_at_ms = ? AND reason = ?)) WHERE rn <= total - CASE WHEN ? <= 0 THEN 128 WHEN ? > 256 THEN 256 ELSE ? END)", - params: [sql_int(input.now_ms.copy()), sql_text(input.reason.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_text(input.reason.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy()), sql_int(input.max_events.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "INSERT INTO run_retention (run_id, first_seq, high_water_seq, updated_at_ms) SELECT id, COALESCE((SELECT MIN(seq) FROM run_events e WHERE e.run_id = runs.id), 0), COALESCE((SELECT MAX(seq) FROM run_events e WHERE e.run_id = runs.id), 0), ? FROM runs WHERE id IN (SELECT run_id FROM recovery_records WHERE recovered_at_ms = ? AND reason = ?) ON CONFLICT (run_id) DO UPDATE SET first_seq = excluded.first_seq, high_water_seq = MAX(run_retention.high_water_seq, excluded.high_water_seq), updated_at_ms = excluded.updated_at_ms", - params: [sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.reason.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.reason.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; sqlite::transaction(&db_id, statements) diff --git a/rss/storage/schema.rss b/rss/storage/schema.rss index 890fca7..2d14dd2 100644 --- a/rss/storage/schema.rss +++ b/rss/storage/schema.rss @@ -333,10 +333,10 @@ pub fn max_messages_limit(requested: int) -> int { if value > 256 => { 256 } else => { value } } -pub fn query_limits(requested_rows: int, requested_bytes: int) -> map { +pub fn query_limits(requested_rows: int, requested_bytes: int) -> SqliteLimits { { - max_rows: max_rows_limit(requested_rows), - max_result_bytes: max_bytes_limit(requested_bytes) + max_rows: Some(max_rows_limit(requested_rows)), + max_result_bytes: Some(max_bytes_limit(requested_bytes)) } } diff --git a/rss/storage/sessions.rss b/rss/storage/sessions.rss index 91321be..232da49 100644 --- a/rss/storage/sessions.rss +++ b/rss/storage/sessions.rss @@ -1,37 +1,50 @@ use json; use sqlite; +fn no_int() -> Option { None } +fn no_float() -> Option { None } +fn no_string() -> Option { None } +fn no_bytes() -> Option { None } + +fn empty_params() -> array { + [] +} + +fn empty_limits() -> SqliteLimits { + let empty: SqliteLimits = {}; + empty +} + fn sql_text(value: string) -> SqliteValue { - { kind: "text", int_value: null, float_value: null, text_value: value, blob_value: null } + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Some(value), blob_value: no_bytes() } } fn sql_int(value: int) -> SqliteValue { - { kind: "int", int_value: value, float_value: null, text_value: null, blob_value: null } + { kind: "int", int_value: Some(value), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } } fn sql_limits(max_rows: int, max_bytes: int) -> SqliteLimits { { - max_connections: 16, - max_statements: 128, - max_rows: max_rows, - max_columns: 128, - max_result_bytes: max_bytes, - max_statement_bytes: 1048576, - max_parameters: 128, - max_parameter_bytes: 1048576, - max_pending_operations: 32, - max_transaction_ms: 5000, - busy_timeout_ms: 5000 + max_connections: Some(16), + max_statements: Some(128), + max_rows: Some(max_rows), + max_columns: Some(128), + max_result_bytes: Some(max_bytes), + max_statement_bytes: Some(1048576), + max_parameters: Some(128), + max_parameter_bytes: Some(1048576), + max_pending_operations: Some(32), + max_transaction_ms: Some(5000), + busy_timeout_ms: Some(5000) } } use self::schema as schema; -fn sessions_query_limits(max_rows: int, max_bytes: int) { +fn sessions_query_limits(max_rows: int, max_bytes: int) -> SqliteLimits { sql_limits(max_rows, max_bytes) } - struct SessionCreateInput { id: string, profile: string, @@ -78,7 +91,7 @@ struct SessionWorkspaceBindInput { fn session_tx_rows_affected(entry: SqliteTransactionResult) -> int { let mut affected: int = 0; if entry.kind == "execute" { - let result: SqliteExecuteResult = entry.execute; + let result: SqliteExecuteResult = match entry.execute { Some(value) => value, None => {rows_affected: 0, last_insert_rowid: 0}, }; affected = result.rows_affected; } affected @@ -89,34 +102,36 @@ pub fn storage_session_create(db_id: resource, payload_json: let statements: array = [ { sql: "INSERT OR IGNORE INTO sessions (id, profile, platform, account_id, chat_id, thread_id, user_id, generation, system_prompt, model, provider, toolset_hash, metadata_json, title, end_reason, created_at_ms, updated_at_ms) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM sessions WHERE profile = ? AND platform = ? AND account_id = ? AND chat_id = ? AND thread_id = ? AND id <> ?)", - params: [sql_text(input.id.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.chat_id.copy()), sql_text(input.thread_id.copy()), sql_text(input.user_id.copy()), sql_int(input.generation.copy()), sql_text(input.system_prompt.copy()), sql_text(input.model.copy()), sql_text(input.provider.copy()), sql_text(input.toolset_hash.copy()), sql_text(input.metadata_json.copy()), sql_text(input.title.copy()), sql_text(input.end_reason.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.chat_id.copy()), sql_text(input.thread_id.copy()), sql_text(input.id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.id.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.chat_id.copy()), sql_text(input.thread_id.copy()), sql_text(input.user_id.copy()), sql_int(input.generation.copy()), sql_text(input.system_prompt.copy()), sql_text(input.model.copy()), sql_text(input.provider.copy()), sql_text(input.toolset_hash.copy()), sql_text(input.metadata_json.copy()), sql_text(input.title.copy()), sql_text(input.end_reason.copy()), sql_int(input.now_ms.copy()), sql_int(input.now_ms.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.chat_id.copy()), sql_text(input.thread_id.copy()), sql_text(input.id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "UPDATE sessions SET updated_at_ms = ? WHERE id = ? AND profile = ? AND platform = ? AND account_id = ? AND chat_id = ? AND thread_id = ?", - params: [sql_int(input.now_ms.copy()), sql_text(input.id.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.chat_id.copy()), sql_text(input.thread_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_int(input.now_ms.copy()), sql_text(input.id.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.chat_id.copy()), sql_text(input.thread_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; sqlite::transaction(&db_id, statements); - sqlite::query(&db_id, "SELECT id, profile, platform, account_id, chat_id, thread_id, user_id, generation, status, system_prompt, model, provider, toolset_hash, metadata_json, last_message_seq, created_at_ms, updated_at_ms, title, end_reason FROM sessions WHERE id = ? AND profile = ? AND platform = ? AND account_id = ? AND chat_id = ? AND thread_id = ? LIMIT ?", [sql_text(input.id.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.chat_id.copy()), sql_text(input.thread_id.copy()), sql_int(max_rows)], sessions_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(input.id.copy()), sql_text(input.profile.copy()), sql_text(input.platform.copy()), sql_text(input.account_id.copy()), sql_text(input.chat_id.copy()), sql_text(input.thread_id.copy()), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT id, profile, platform, account_id, chat_id, thread_id, user_id, generation, status, system_prompt, model, provider, toolset_hash, metadata_json, last_message_seq, created_at_ms, updated_at_ms, title, end_reason FROM sessions WHERE id = ? AND profile = ? AND platform = ? AND account_id = ? AND chat_id = ? AND thread_id = ? LIMIT ?", params, sessions_query_limits(max_rows, max_bytes)) } pub fn storage_session_get(db_id: resource, session_id: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { let statement = storage_session_get_statement(session_id, max_rows, max_bytes); - let params: array = statement.params; - let limits: SqliteLimits = statement.limits; + let params: array = statement.params.unwrap_or(empty_params()); + let limits: SqliteLimits = statement.limits.unwrap_or(empty_limits()); sqlite::query(&db_id, statement.sql, params, limits) } pub fn storage_session_get_statement(session_id: string, max_rows: int, max_bytes: int) -> SqliteStatement { - { sql: "SELECT id, profile, platform, account_id, chat_id, thread_id, user_id, generation, status, system_prompt, model, provider, toolset_hash, metadata_json, last_message_seq, created_at_ms, updated_at_ms, title, end_reason FROM sessions WHERE id = ? LIMIT ?", params: [sql_text(session_id.copy()), sql_int(max_rows)], query: true, limits: sql_limits(max_rows, max_bytes) } + { sql: "SELECT id, profile, platform, account_id, chat_id, thread_id, user_id, generation, status, system_prompt, model, provider, toolset_hash, metadata_json, last_message_seq, created_at_ms, updated_at_ms, title, end_reason FROM sessions WHERE id = ? LIMIT ?", params: Some([sql_text(session_id.copy()), sql_int(max_rows)]), query: Some(true), limits: Some(sql_limits(max_rows, max_bytes)) } } pub fn storage_session_list(db_id: resource, profile: string, platform: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { - sqlite::query(&db_id, "SELECT id, profile, platform, account_id, chat_id, thread_id, user_id, generation, status, system_prompt, model, provider, toolset_hash, metadata_json, last_message_seq, created_at_ms, updated_at_ms, title, end_reason FROM sessions WHERE profile = ? AND platform = ? ORDER BY updated_at_ms DESC, id ASC LIMIT ?", [sql_text(profile), sql_text(platform), sql_int(max_rows)], sessions_query_limits(max_rows, max_bytes)) + let params: array = [sql_text(profile), sql_text(platform), sql_int(max_rows)]; + sqlite::query(&db_id, "SELECT id, profile, platform, account_id, chat_id, thread_id, user_id, generation, status, system_prompt, model, provider, toolset_hash, metadata_json, last_message_seq, created_at_ms, updated_at_ms, title, end_reason FROM sessions WHERE profile = ? AND platform = ? ORDER BY updated_at_ms DESC, id ASC LIMIT ?", params, sessions_query_limits(max_rows, max_bytes)) } pub fn storage_session_touch(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> SqliteQueryResult { @@ -126,7 +141,8 @@ pub fn storage_session_touch(db_id: resource, payload_json: s // generation) must never silently no-op. `generation` stays in the // input contract for payload compatibility but does not gate the // update; compaction owns generation bumps. - sqlite::execute(&db_id, "UPDATE sessions SET status = ?, system_prompt = ?, model = ?, provider = ?, toolset_hash = ?, metadata_json = json_patch(CASE WHEN json_valid(metadata_json) THEN CASE WHEN json_type(metadata_json) = 'object' THEN metadata_json ELSE '{}' END ELSE '{}' END, CASE WHEN json_valid(?) THEN CASE WHEN json_type(?) = 'object' THEN json(?) ELSE '{}' END ELSE '{}' END), title = ?, end_reason = ?, updated_at_ms = ? WHERE id = ?", [sql_text(input.status.copy()), sql_text(input.system_prompt.copy()), sql_text(input.model.copy()), sql_text(input.provider.copy()), sql_text(input.toolset_hash.copy()), sql_text(input.metadata_json.copy()), sql_text(input.metadata_json.copy()), sql_text(input.metadata_json.copy()), sql_text(input.title.copy()), sql_text(input.end_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy())]); + let params: array = [sql_text(input.status.copy()), sql_text(input.system_prompt.copy()), sql_text(input.model.copy()), sql_text(input.provider.copy()), sql_text(input.toolset_hash.copy()), sql_text(input.metadata_json.copy()), sql_text(input.metadata_json.copy()), sql_text(input.metadata_json.copy()), sql_text(input.title.copy()), sql_text(input.end_reason.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy())]; + sqlite::execute(&db_id, "UPDATE sessions SET status = ?, system_prompt = ?, model = ?, provider = ?, toolset_hash = ?, metadata_json = json_patch(CASE WHEN json_valid(metadata_json) THEN CASE WHEN json_type(metadata_json) = 'object' THEN metadata_json ELSE '{}' END ELSE '{}' END, CASE WHEN json_valid(?) THEN CASE WHEN json_type(?) = 'object' THEN json(?) ELSE '{}' END ELSE '{}' END), title = ?, end_reason = ?, updated_at_ms = ? WHERE id = ?", params); storage_session_get(db_id, input.session_id, max_rows, max_bytes) } @@ -135,25 +151,29 @@ pub fn storage_session_touch(db_id: resource, payload_json: s /// this command only merges its display metadata and guards active runs. pub fn storage_session_workspace_bind(db_id: resource, payload_json: string, max_rows: int, max_bytes: int) -> map { let input: SessionWorkspaceBindInput = json::decode::(payload_json); - let results: array = sqlite::transaction(&db_id, [ + let bind_params: array = [sql_text(input.workspace_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy())]; + let statements: array = [ { sql: "UPDATE sessions SET metadata_json = json_set(CASE WHEN json_valid(metadata_json) THEN CASE WHEN json_type(metadata_json) = 'object' THEN metadata_json ELSE '{}' END ELSE '{}' END, '$.workspace', json(?)), updated_at_ms = ? WHERE id = ? AND NOT EXISTS (SELECT 1 FROM runs WHERE session_id = ? AND status IN ('queued', 'running', 'started', 'stopping'))", - params: [sql_text(input.workspace_json.copy()), sql_int(input.now_ms.copy()), sql_text(input.session_id.copy()), sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some(bind_params), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } - ]); + ]; + let results: array = sqlite::transaction(&db_id, statements); let mut affected: int = 0; if results.length > 0 { affected = session_tx_rows_affected(results[0]); } let mut result: map = {ok: false, code: "workspace_bind_conflict", message: "workspace binding could not be updated", result: {}}; if affected == 0 { - let session = sqlite::query(&db_id, "SELECT id FROM sessions WHERE id = ? LIMIT 1", [sql_text(input.session_id.copy())], sql_limits(1, 4096)); + let params: array = [sql_text(input.session_id.copy())]; + let session = sqlite::query(&db_id, "SELECT id FROM sessions WHERE id = ? LIMIT 1", params, sql_limits(1, 4096)); if session.rows.length == 0 { result = {ok: false, code: "session_not_found", message: "session does not exist", result: {}}; } else { - let active = sqlite::query(&db_id, "SELECT id FROM runs WHERE session_id = ? AND status IN ('queued', 'running', 'started', 'stopping') LIMIT 1", [sql_text(input.session_id.copy())], sql_limits(1, 4096)); + let params: array = [sql_text(input.session_id.copy())]; + let active = sqlite::query(&db_id, "SELECT id FROM runs WHERE session_id = ? AND status IN ('queued', 'running', 'started', 'stopping') LIMIT 1", params, sql_limits(1, 4096)); if active.rows.length > 0 { result = {ok: false, code: "active_run", message: "workspace cannot change while a run is active", result: {}}; } @@ -181,7 +201,8 @@ pub fn storage_session_workspace_bind(db_id: resource, payloa pub fn storage_session_managed_retained(db_id: resource,payload_json:string)->bool { let input:SessionIdInput=json::decode::(payload_json); - let found:SqliteQueryResult=sqlite::query(&db_id,"SELECT run_id FROM managed_run_bindings WHERE session_id=? LIMIT 1",[sql_text(input.session_id)],sql_limits(1,4096)); + let params: array = [sql_text(input.session_id)]; + let found:SqliteQueryResult=sqlite::query(&db_id,"SELECT run_id FROM managed_run_bindings WHERE session_id=? LIMIT 1",params,sql_limits(1,4096)); found.rows.length > 0 } @@ -190,79 +211,79 @@ pub fn storage_session_delete(db_id: resource, payload_json: let statements: array = [ { sql:"INSERT INTO managed_effect_guard(id) SELECT 2 WHERE EXISTS(SELECT 1 FROM managed_run_bindings WHERE session_id=?)", - params:[sql_text(input.session_id.copy())],query:false,limits:sql_limits(1,4096) + params: Some([sql_text(input.session_id.copy())]),query: Some(false),limits: Some(sql_limits(1,4096)) }, { sql: "DELETE FROM plugin_graph_pin_requirements WHERE run_id IN (SELECT id FROM runs WHERE session_id = ?)", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM plugin_graph_refs WHERE owner_kind = 'run' AND owner_id IN (SELECT id FROM runs WHERE session_id = ?)", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM idempotency_records WHERE resource_type = 'run' AND resource_id IN (SELECT id FROM runs WHERE session_id = ?)", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM sessions WHERE id = ?", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM run_retention WHERE run_id IN (SELECT id FROM runs WHERE session_id = ?)", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM run_events WHERE run_id IN (SELECT id FROM runs WHERE session_id = ?)", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM child_run_links WHERE parent_run_id IN (SELECT id FROM runs WHERE session_id = ?) OR child_run_id IN (SELECT id FROM runs WHERE session_id = ?)", - params: [sql_text(input.session_id.copy()), sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy()), sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM approvals WHERE session_id = ? OR run_id IN (SELECT id FROM runs WHERE session_id = ?)", - params: [sql_text(input.session_id.copy()), sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy()), sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM compactions WHERE session_id = ? OR run_id IN (SELECT id FROM runs WHERE session_id = ?)", - params: [sql_text(input.session_id.copy()), sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy()), sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM runs WHERE session_id = ?", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM messages WHERE session_id = ?", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) }, { sql: "DELETE FROM delivery_cursors WHERE session_id = ?", - params: [sql_text(input.session_id.copy())], - query: false, - limits: sql_limits(1000, 4194304) + params: Some([sql_text(input.session_id.copy())]), + query: Some(false), + limits: Some(sql_limits(1000, 4194304)) } ]; sqlite::transaction(&db_id, statements) diff --git a/rss/tools/dispatch.rss b/rss/tools/dispatch.rss index 50696f9..e2c8cdd 100644 --- a/rss/tools/dispatch.rss +++ b/rss/tools/dispatch.rss @@ -61,54 +61,67 @@ fn call_id(call: map) -> string { id } +/// One tool call's arguments in the two forms this pin can carry. +/// +/// `arguments_json` is authoritative: the provider's own text when it sent one, +/// otherwise the host's serialization of the call's `arguments` map. The guest +/// cannot decode open JSON into a map here (`json::decode::` is rejected by +/// the typed boundary), so a text-only call reports `has_arguments: false` with +/// an empty map and the *host* parses the text on the pinned invocation path. +/// Nothing is invented: a call that carried neither form keeps the legacy empty +/// object spelling of "no arguments". fn parse_call_arguments(call: map) -> map { - let mut out: map = { - ok: true, - arguments: {}, - code: "", - message: "" - }; - if call.has("arguments") { - if type(call.arguments) == "map" { - out.arguments = call.arguments; + let mut ok: bool = true; + let mut arguments: map = {}; + let mut text: string = ""; + let mut has_text: bool = false; + let mut has_arguments: bool = false; + if call.has("arguments_json") { + if type(call.arguments_json.copy()) == "string" { + text = types::map_string(call, "arguments_json", ""); + has_text = true; } else { - out = { - ok: false, - arguments: {}, - code: "malformed_payload", - message: "tool arguments must be a JSON object" - }; + ok = false; } - } else { - if call.has("arguments_json") { - if type(call.arguments_json) == "string" { - let text: string = call.arguments_json; - let parsed: map = agent::parse_json_object(text); - if map_bool(parsed, "ok", false) { - out.arguments = types::map_map(parsed, "value"); + } + if ok && call.has("arguments") { + if type(call.arguments.copy()) == "map" { + arguments = call.arguments; + has_arguments = true; + if has_text == false { + let encoded = agent::json_text(arguments.copy()); + if encoded.ok { + text = encoded.text; + has_text = true; } else { - out = { - ok: false, - arguments: {}, - code: "malformed_payload", - message: "tool arguments must be a JSON object" - }; + ok = false; } - } else { - out = { - ok: false, - arguments: {}, - code: "malformed_payload", - message: "tool arguments must be a JSON object" - }; } + } else { + ok = false; } } - out -} - -fn call_arguments(call: map) -> map { - types::map_map(parse_call_arguments(call), "arguments") + let mut code: string = ""; + let mut message: string = ""; + if ok == false { + arguments = {}; + text = ""; + has_arguments = false; + code = "malformed_payload"; + message = "tool arguments must be a JSON object"; + } else { + if has_text == false { + text = "{}"; + } + } + { + ok: ok, + arguments: arguments, + arguments_json: text, + has_arguments: has_arguments, + code: code, + message: message + } } fn registry_from_input(input: map) -> array { @@ -298,8 +311,18 @@ fn fail_envelope(call: map, code: string, message: string, terminal: bool) -> ma /// Project the Native-witnessed canonical result into the public message /// protocol. The retained child's return wrapper is not execution evidence. -pub fn result_envelope(call: map, result: map) -> map { - let mut projected: map = canonical_envelope(call, result); +pub fn result_envelope(call: map, result: PinnedToolInvokeResult) -> map { + let mut projected: map = canonical_envelope(call, { + ok: result.ok, + content: result.content.copy(), + truncated: result.truncated, + error: error_map(result.code.copy(), result.message.copy()) + }); + // Open canonical JSON stays text in the guest; the native provider + // adapter restores the object before dispatching the request. + let mut block: map = types::map_map(projected, "content_block"); + block.result = result.canonical_result; + projected.content_block = block; let error: map = types::map_map(projected, "error"); let code: string = types::map_string(error, "code", ""); // A committed process-handle observation can report cancellation without @@ -312,8 +335,13 @@ pub fn result_envelope(call: map, result: map) -> map { } fn tool_context(input: map, call: map, descriptor: map) -> map { - let args: map = call_arguments(call); - let digest: string = json::encode(args); + let parsed: map = parse_call_arguments(call); + let args: map = types::map_map(parsed, "arguments"); + // The digest is the canonical arguments text itself, so the pinned + // invocation's `argument_digest == canonical_arguments` check sees exactly + // the bytes the provider sent (or the host's serialization of the map). + let digest: string = types::map_string(parsed, "arguments_json", ""); + let has_arguments: bool = map_bool(parsed, "has_arguments", false); let name: string = call_name(call); let identity: string = types::map_string(input, "registry_identity", ""); let mut config: map = types::map_map(input, "config"); @@ -323,6 +351,8 @@ fn tool_context(input: map, call: map, descriptor: map) -> map { { kind: "execute", arguments: args, + arguments_json: digest, + has_arguments: has_arguments, prepare: { run_id: types::map_string(input, "run_id", ""), call_id: call_id(call), @@ -422,6 +452,10 @@ pub fn dispatch(input: map) -> map { types::map_string(parsed, "message", "tool arguments are malformed"), true ); + } else if call.has("arguments_json") && agent::parse_json_object(types::map_string(parsed, "arguments_json", "")).ok == false { + // The direct dispatch entry has no pinned native invocation + // to validate the authoritative text before routing. + out = fail_envelope(call, "malformed_payload", "tool arguments must be a JSON object", true); } else { let mut routed: map = call; routed.arguments = types::map_map(parsed, "arguments"); diff --git a/rss/tools/patch.rss b/rss/tools/patch.rss index 55409be..2f1be06 100644 --- a/rss/tools/patch.rss +++ b/rss/tools/patch.rss @@ -1,4 +1,5 @@ use self::types as types; + use bytes; use json; @@ -25,7 +26,7 @@ fn map_bool(value: map, key: string, fallback: bool) -> bool { } fn int_to_string(value: int) -> string { - let encoded: string = json::encode(value); + let encoded: string = json::encode::(value); encoded } @@ -71,14 +72,13 @@ fn succeed(content: string, data: map, truncated: bool, artifacts: array) -> map ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: artifacts } } -fn fail_host(envelope: map) -> map { - let error: map = types::map_map(envelope, "error"); +fn fail_host(error: map) -> map { let mut code: string = types::map_string(error, "code", "internal_error"); let mut message: string = types::map_string(error, "message", "capability failed"); if message == "symlinks are not followed" { @@ -352,23 +352,43 @@ fn utf8_is_valid(data: array) -> bool { valid } +/// Serialized prepare metadata. Open JSON leaves the guest as text, so the host +/// parses and validates it; an empty string is refused there, never defaulted. +fn prepare_metadata(context: map, tool_name: string, risk_class: string) -> string { + let prepare: map = types::map_map(context, "prepare"); + let encoded = agent::json_text({ + run_id: types::map_string(prepare, "run_id", ""), + call_id: types::map_string(prepare, "call_id", ""), + name: types::map_string(prepare, "name", tool_name), + argument_digest: types::map_string(prepare, "argument_digest", ""), + registry_identity: types::map_string(prepare, "registry_identity", ""), + risk_class: risk_class, + summary: types::map_string(prepare, "summary", tool_name) + }); + if encoded.ok => { encoded.text } else => { "" } +} + +/// Serialized canonical tool result for `tool_commit`. +fn commit_text(result: map) -> string { + let encoded = agent::json_text(result); + if encoded.ok => { encoded.text } else => { "" } +} + + fn control_failure() -> map { - let control: map = agent::control_check(); - if map_bool(control, "ok", false) => { + let control = agent::control_check(); + if control.ok => { { ok: true, code: "", message: "" } } else => { - let error: map = types::map_map(control, "error"); - { - ok: false, - code: types::map_string(error, "code", "cancelled"), - message: types::map_string(error, "message", "cancelled") - } + match control.error { Some(failure) => { ok: false, code: failure.code, message: failure.message }, None => { ok: false, code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } } } fn encoded_len(result: map) -> int { - let encoded: string = json::encode(result); - utf8_len(encoded) + // Open JSON text is produced on the host: this pin admits no guest-side + // serializer and no dynamic `json::encode` target. + let encoded = agent::json_text(result); + if encoded.ok => { encoded.bytes } else => { 0 } } fn utf8_len(value: string) -> int { @@ -441,10 +461,10 @@ fn shrink_result(result: map, max_output_bytes: int, token: string) -> map { let data: map = types::map_map(current, "data"); if content.length > 0 { let payload: bytes = bytes::from_utf8(content); - let put: map = cap::artifact_put_result(token, payload, {}); - if map_bool(put, "ok", false) { - let id: string = types::map_string(put, "id", ""); - let len: int = map_int(put, "len", utf8_len(content)); + let put = cap::artifact_put_result(token, payload, "{}"); + if put.ok { + let id: string = put.id; + let len: int = put.len; let mut artifacts: array = []; artifacts[0] = id; current = succeed(artifact_summary(id, len, max_output_bytes), data, true, artifacts); @@ -817,7 +837,7 @@ pub fn execute(context: map, arguments: map) -> map { replace_all = arguments.replace_all; } } - let mut result: map = { ok: true, content: "", data: {}, error: null, truncated: false, artifacts: [] }; + let mut result: map = { ok: true, content: "", data: {}, error: (), truncated: false, artifacts: [] }; let checked: map = control_failure(); if map_bool(checked, "ok", false) == false { result = fail(types::map_string(checked, "code", "cancelled"), types::map_string(checked, "message", ""), unpublished()); @@ -833,15 +853,15 @@ pub fn execute(context: map, arguments: map) -> map { let max_patch_bytes: int = map_int(config, "max_patch_bytes", 8388608); let max_patch_preview_bytes: int = map_int(config, "max_patch_preview_bytes", 16384); let max_output_bytes: int = map_int(config, "max_tool_output_bytes", 65536); - let meta: map = cap::fs_metadata(token, path); - if map_bool(meta, "ok", false) == false { - result = fail_host(meta); + let meta = cap::fs_metadata(token, path); + if meta.ok == false { + result = fail_host(types::capability_error(meta.error)); } else { - let file_type: string = types::map_string(meta, "file_type", ""); + let file_type: string = meta.file_type; if file_type == "directory" || file_type == "other" { result = fail("path_denied", "read-only open requires a regular file", unpublished()); } else { - let file_len: int = map_int(meta, "len", 0); + let file_len: int = meta.len; if file_len > max_read_bytes { result = fail("budget_exceeded", "read budget exceeded", unpublished()); } else { @@ -849,16 +869,16 @@ pub fn execute(context: map, arguments: map) -> map { let mut expected_hash: string = ""; let mut decoded: bool = true; if file_len > 0 { - let read: map = cap::fs_read_range(token, path, 0, file_len); - if map_bool(read, "ok", false) == false { - result = fail_host(read); + let read = cap::fs_read_range(token, path, 0, file_len); + if read.ok == false { + result = fail_host(types::capability_error(read.error)); decoded = false; } else { - if map_bool(read, "truncated", false) { + if read.truncated { result = fail("budget_exceeded", "read budget exceeded", unpublished()); decoded = false; } else { - let expected_from_read: string = types::map_string(read, "hash", ""); + let expected_from_read: string = match read.hash { Some(digest) => digest, None => "" }; let payload: bytes = read.bytes; let data: array = bytes::to_array_u8(payload); if data.length > max_patch_bytes { @@ -919,18 +939,18 @@ pub fn execute(context: map, arguments: map) -> map { result = fail(types::map_string(before_publish, "code", "cancelled"), types::map_string(before_publish, "message", ""), unpublished()); } else { let payload: bytes = bytes::from_utf8(updated); - let written: map = cap::fs_write_atomic(token, path, expected_hash, payload); - if map_bool(written, "ok", false) == false { - result = fail_host(written); + let written = cap::fs_write_atomic(token, path, expected_hash, payload); + if written.ok == false { + result = fail_host(types::capability_error(written.error)); } else { let preview: string = bounded_diff(path, source, updated, max_patch_preview_bytes); let mut published: map = succeed( preview, { publication: "published", - durable: map_bool(written, "durable", true), - staging_cleaned: map_bool(written, "staging_cleaned", true), - bytes: map_int(written, "len", utf8_len(updated)), + durable: written.durable, + staging_cleaned: written.staging_cleaned, + bytes: written.len, replacements: replacements }, false, @@ -974,18 +994,18 @@ pub fn invoke(context: map) -> map { } fail(code, message, data) } else => { - let prepared: map = agent_runtime::tool_prepare(types::map_map(context, "prepare")); - if types::map_string(prepared, "kind", "") == "replay" => { - types::map_map(prepared, "result") - } else if map_bool(prepared, "ok", false) == false => { - let error: map = types::map_map(prepared, "error"); + let prepared = agent_runtime::tool_prepare(prepare_metadata(context, "patch", "write")); + if prepared.ok == false => { + let error: map = types::capability_error(prepared.error); fail( types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "capability failed"), {} ) + } else if prepared.kind == "replay" => { + types::file_replay_result(prepared.result) } else => { - let token: string = types::map_string(prepared, "execution_token", ""); + let token: string = prepared.execution_token; let result: map = execute( { execution_token: token, @@ -993,11 +1013,9 @@ pub fn invoke(context: map) -> map { }, arguments ); - let committed: map = agent_runtime::tool_commit(token, result); - if types::map_string(committed, "kind", "") == "committed" => { - types::map_map(committed, "result") - } else if map_bool(committed, "ok", false) == false => { - let error: map = types::map_map(committed, "error"); + let committed = agent_runtime::tool_commit(token, commit_text(result)); + if committed.ok == false => { + let error: map = types::capability_error(committed.error); fail( types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "capability failed"), diff --git a/rss/tools/process.rss b/rss/tools/process.rss index 11a0130..0387403 100644 --- a/rss/tools/process.rss +++ b/rss/tools/process.rss @@ -1,4 +1,21 @@ use self::types as types; + +// The canonical tool result a pinned/managed invocation hands back. Only the +// fields this tool projects are declared; `data` is that result's own open +// payload, so it crosses as serialized text. +struct ToolCapabilityFailure { + code: string, + message: string +} + +struct AgentCanonicalToolResult { + ok: bool, + content: string, + data: string, + truncated: bool, + artifacts: array, + error: Option +} use bytes; use json; @@ -40,8 +57,10 @@ fn utf8_len(text: string) -> int { } fn encoded_len(value: map) -> int { - let encoded: string = json::encode(value); - utf8_len(encoded) + // Open JSON text is produced on the host: this pin admits no guest-side + // serializer and no dynamic `json::encode` target. + let encoded = agent::json_text(value); + if encoded.ok => { encoded.bytes } else => { 0 } } fn unpublished() -> map { @@ -53,7 +72,7 @@ fn succeed(content: string, data: map, truncated: bool) -> map { ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: [] } @@ -258,20 +277,16 @@ fn snapshot_data(envelope: map) -> map { } else { if map_bool(envelope, "signaled", false) { data.status = "signaled"; - if envelope.has("signal") { - if type(envelope["signal"].copy()) == "int" { - data.signal = map_int(envelope, "signal", 0); - } + if envelope.has("signal_number") { + data.signal = map_int(envelope, "signal_number", 0); } } else { if map_bool(envelope, "unknown", false) { data.status = "unknown"; } else { data.status = "exited"; - if envelope.has("exit_code") { - if type(envelope["exit_code"].copy()) == "int" { - data.exit_code = map_int(envelope, "exit_code", 0); - } + if envelope.has("exit_number") { + data.exit_code = map_int(envelope, "exit_number", 0); } } } @@ -414,7 +429,7 @@ fn apply_output_bounds(result: map, token: string, config: map, envelope: map) - data.overflow_stdout_bytes = stdout_raw.length; data.overflow_stderr_bytes = stderr_raw.length; bounded.data = data; - let stored: map = cap::artifact_put_result(token, payload, {}); + let stored: map = cap::artifact_put_result(token, payload, "{}"); let mut stored_artifact: bool = false; if map_bool(stored, "ok", false) { stored_artifact = true; @@ -469,11 +484,11 @@ fn execute(context: map) -> map { let stream_limit: int = config_int(config, "max_stream_bytes", 1048576); let action: string = types::map_string(arguments, "action", ""); let handle: string = handle_id(arguments); - let control: map = agent::control_check(); + let control = agent::control_check(); let mut outcome: map = fail("cancelled", "process was cancelled", unpublished()); let mut last_envelope: map = {}; - if map_bool(control, "ok", false) == false { - let error: map = types::map_map(control, "error"); + if control.ok == false { + let error: map = match control.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } }; outcome = fail(types::map_string(error, "code", "cancelled"), types::map_string(error, "message", "process was cancelled"), unpublished()); } else { if action == "poll" { @@ -583,9 +598,9 @@ fn execute(context: map) -> map { outcome = fail("internal_error", "process wait loop exhausted", unpublished()); while finished == false && iters < 1000000 { iters = iters + 1; - let again: map = agent::control_check(); - if map_bool(again, "ok", false) == false { - let error: map = types::map_map(again, "error"); + let again = agent::control_check(); + if again.ok == false { + let error: map = match again.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } }; outcome = fail(types::map_string(error, "code", "cancelled"), types::map_string(error, "message", "process was cancelled"), unpublished()); finished = true; } else { @@ -646,6 +661,47 @@ fn execute(context: map) -> map { apply_output_bounds(outcome, token, config, last_envelope) } +/// Serialized prepare metadata. Open JSON leaves the guest as text, so the host +/// parses and validates it; an empty string is refused there, never defaulted. +fn prepare_metadata(context: map, tool_name: string, risk_class: string) -> string { + let prepare: map = types::map_map(context, "prepare"); + let encoded = agent::json_text({ + run_id: types::map_string(prepare, "run_id", ""), + call_id: types::map_string(prepare, "call_id", ""), + name: types::map_string(prepare, "name", tool_name), + argument_digest: types::map_string(prepare, "argument_digest", ""), + registry_identity: types::map_string(prepare, "registry_identity", ""), + risk_class: risk_class, + summary: types::map_string(prepare, "summary", tool_name) + }); + if encoded.ok => { encoded.text } else => { "" } +} + +/// Serialized canonical tool result for `tool_commit`. +fn commit_text(result: map) -> string { + let encoded = agent::json_text(result); + if encoded.ok => { encoded.text } else => { "" } +} + +/// A replayed canonical result: the host hands back text, so the declared +/// struct is the only way to read it. `data` is that result's own open payload +/// and therefore its serialized text. +fn replay_result(text: string) -> map { + let mut result: map = fail("internal_error", "replayed result is unavailable", {}); + if text.length > 0 { + let decoded: AgentCanonicalToolResult = json::decode::(text); + result = { + ok: decoded.ok, + content: decoded.content, + data: decoded.data, + truncated: decoded.truncated, + artifacts: decoded.artifacts, + error: match decoded.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "", message: "" } } + }; + } + result +} + pub fn descriptor() -> map { types::process_descriptor() } @@ -660,32 +716,24 @@ pub fn invoke(context: map) -> map { if map_bool(validated, "ok", false) == false { result = fail(types::map_string(validated, "code", "invalid_arguments"), types::map_string(validated, "message", "invalid arguments"), unpublished()); } else { - let prepare: map = types::map_map(context, "prepare"); - let prepared: map = agent_runtime::tool_prepare({ - run_id: types::map_string(prepare, "run_id", ""), - call_id: types::map_string(prepare, "call_id", ""), - name: types::map_string(prepare, "name", "process"), - argument_digest: types::map_string(prepare, "argument_digest", ""), - registry_identity: types::map_string(prepare, "registry_identity", ""), - risk_class: "execute", - summary: types::map_string(prepare, "summary", "process") - }); - if map_bool(prepared, "ok", false) == false { - let error: map = types::map_map(prepared, "error"); - result = fail(types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "prepare failed"), unpublished()); + let prepared = agent_runtime::tool_prepare(prepare_metadata(context, "process", "execute")); + if prepared.ok == false { + let prepare_failure: map = match prepared.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }; + result = fail(types::map_string(prepare_failure, "code", "internal_error"), types::map_string(prepare_failure, "message", "prepare failed"), unpublished()); } else { - if types::map_string(prepared, "kind", "") == "replay" { - result = types::map_map(prepared, "result"); + let token: string = prepared.execution_token; + if prepared.kind == "replay" { + result = replay_result(prepared.result); } else { let mut exec_context: map = context; - exec_context.execution_token = types::map_string(prepared, "execution_token", ""); + exec_context.execution_token = token.copy(); let canonical: map = execute(exec_context); - let committed: map = agent_runtime::tool_commit(types::map_string(prepared, "execution_token", ""), canonical); - if map_bool(committed, "ok", false) == false { - let error: map = types::map_map(committed, "error"); - result = fail(types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "commit failed"), unpublished()); + let committed = agent_runtime::tool_commit(token, commit_text(canonical)); + if committed.ok == false { + let commit_failure: map = match committed.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }; + result = fail(types::map_string(commit_failure, "code", "internal_error"), types::map_string(commit_failure, "message", "commit failed"), unpublished()); } else { - result = types::map_map(committed, "result"); + result = canonical; } } } diff --git a/rss/tools/read_file.rss b/rss/tools/read_file.rss index c43a420..606d80f 100644 --- a/rss/tools/read_file.rss +++ b/rss/tools/read_file.rss @@ -1,4 +1,5 @@ use self::types as types; + use bytes; use json; @@ -25,7 +26,7 @@ fn map_bool(value: map, key: string, fallback: bool) -> bool { } fn int_to_string(value: int) -> string { - let encoded: string = json::encode(value); + let encoded: string = json::encode::(value); encoded } @@ -67,14 +68,37 @@ fn succeed(content: string, data: map, truncated: bool, artifacts: array) -> map ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: artifacts } } -fn fail_host(envelope: map) -> map { - let error: map = types::map_map(envelope, "error"); +/// Serialized prepare metadata. Open JSON leaves the guest as text, so the host +/// parses and validates it; an empty string is refused there, never defaulted. +fn prepare_metadata(context: map, tool_name: string, risk_class: string) -> string { + let prepare: map = types::map_map(context, "prepare"); + let encoded = agent::json_text({ + run_id: types::map_string(prepare, "run_id", ""), + call_id: types::map_string(prepare, "call_id", ""), + name: types::map_string(prepare, "name", tool_name), + argument_digest: types::map_string(prepare, "argument_digest", ""), + registry_identity: types::map_string(prepare, "registry_identity", ""), + risk_class: risk_class, + summary: types::map_string(prepare, "summary", tool_name) + }); + if encoded.ok => { encoded.text } else => { "" } +} + +/// Serialized canonical tool result for `tool_commit`. +fn commit_text(result: map) -> string { + let encoded = agent::json_text(result); + if encoded.ok => { encoded.text } else => { "" } +} + + +// Callers have already unwrapped the native result's Option. +fn fail_host(error: map) -> map { let code: string = types::map_string(error, "code", "internal_error"); let mut message: string = types::map_string(error, "message", "capability failed"); if message == "symlinks are not followed" { @@ -299,22 +323,20 @@ fn utf8_is_valid(data: array) -> bool { } fn control_failure() -> map { - let control: map = agent::control_check(); - if map_bool(control, "ok", false) => { + let control = agent::control_check(); + if control.ok => { { ok: true, code: "", message: "" } } else => { - let error: map = types::map_map(control, "error"); - { - ok: false, - code: types::map_string(error, "code", "cancelled"), - message: types::map_string(error, "message", "cancelled") - } + let error: map = match control.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } }; + { ok: false, code: types::map_string(error, "code", "control_contract_violation"), message: types::map_string(error, "message", "control_check reported a failure without a structured error") } } } fn encoded_len(result: map) -> int { - let encoded: string = json::encode(result); - utf8_len(encoded) + // Open JSON text is produced on the host: this pin admits no guest-side + // serializer and no dynamic `json::encode` target. + let encoded = agent::json_text(result); + if encoded.ok => { encoded.bytes } else => { 0 } } fn utf8_len(value: string) -> int { @@ -372,10 +394,10 @@ fn shrink_result(result: map, max_output_bytes: int, token: string) -> map { let data: map = types::map_map(current, "data"); if content.length > 0 { let payload: bytes = bytes::from_utf8(content); - let put: map = cap::artifact_put_result(token, payload, {}); - if map_bool(put, "ok", false) { - let id: string = types::map_string(put, "id", ""); - let len: int = map_int(put, "len", utf8_len(content)); + let put = cap::artifact_put_result(token, payload, "{}"); + if put.ok { + let id: string = put.id; + let len: int = put.len; let mut artifacts: array = []; artifacts[0] = id; let mut summary: string = "artifact " + id + " (" + int_to_string(len) + " bytes)"; @@ -455,7 +477,7 @@ pub fn execute(context: map, arguments: map) -> map { let token: string = types::map_string(context, "execution_token", ""); let config: map = types::map_map(context, "config"); let path: string = types::map_string(arguments, "path", ""); - let mut result: map = { ok: true, content: "", data: {}, error: null, truncated: false, artifacts: [] }; + let mut result: map = { ok: true, content: "", data: {}, error: (), truncated: false, artifacts: [] }; let checked: map = control_failure(); if map_bool(checked, "ok", false) == false { result = fail(types::map_string(checked, "code", "cancelled"), types::map_string(checked, "message", ""), {}); @@ -478,33 +500,34 @@ pub fn execute(context: map, arguments: map) -> map { limit = max_read_lines; } } - let meta: map = cap::fs_metadata(token, path); - if map_bool(meta, "ok", false) == false { - let error: map = types::map_map(meta, "error"); + let meta = cap::fs_metadata(token, path); + if meta.ok == false { + let error: map = types::capability_error(meta.error); let code: string = types::map_string(error, "code", "internal_error"); if code == "wrong_type" { result = fail("path_denied", types::map_string(error, "message", "path denied"), {}); } else { - result = fail_host(meta); + result = fail_host(error); } } else { - let file_type: string = types::map_string(meta, "file_type", ""); + let file_type: string = meta.file_type; if file_type != "file" { result = fail("wrong_type", "read-only open requires a regular file", {}); } else { - let file_len: int = map_int(meta, "len", 0); + let file_len: int = meta.len; if file_len > max_read_bytes { result = fail("budget_exceeded", "read budget exceeded", {}); } else { let mut text: string = ""; let mut decoded: bool = true; if file_len > 0 { - let read: map = cap::fs_read_range(token, path, 0, file_len); - if map_bool(read, "ok", false) == false { - result = fail_host(read); + let read = cap::fs_read_range(token, path, 0, file_len); + if read.ok == false { + let error: map = types::capability_error(read.error); + result = fail_host(error); decoded = false; } else { - if map_bool(read, "truncated", false) { + if read.truncated { result = fail("budget_exceeded", "read budget exceeded", {}); decoded = false; } else { @@ -579,13 +602,13 @@ pub fn invoke(context: map) -> map { data ) } else => { - let prepared: map = agent_runtime::tool_prepare(types::map_map(context, "prepare")); - if types::map_string(prepared, "kind", "") == "replay" => { - types::map_map(prepared, "result") - } else if map_bool(prepared, "ok", false) == false => { - fail_host(prepared) + let prepared = agent_runtime::tool_prepare(prepare_metadata(context, "read_file", "read")); + if prepared.ok == false => { + fail_host(types::capability_error(prepared.error)) + } else if prepared.kind == "replay" => { + types::file_replay_result(prepared.result) } else => { - let token: string = types::map_string(prepared, "execution_token", ""); + let token: string = prepared.execution_token; let result: map = execute( { execution_token: token, @@ -593,11 +616,9 @@ pub fn invoke(context: map) -> map { }, arguments ); - let committed: map = agent_runtime::tool_commit(token, result); - if types::map_string(committed, "kind", "") == "committed" => { - types::map_map(committed, "result") - } else if map_bool(committed, "ok", false) == false => { - fail_host(committed) + let committed = agent_runtime::tool_commit(token, commit_text(result)); + if committed.ok == false => { + fail_host(types::capability_error(committed.error)) } else => { result } diff --git a/rss/tools/search_files.rss b/rss/tools/search_files.rss index cd66c19..9531c21 100644 --- a/rss/tools/search_files.rss +++ b/rss/tools/search_files.rss @@ -1,4 +1,5 @@ use self::types as types; + use bytes; use json; @@ -584,14 +585,13 @@ fn succeed(content: string, data: map, truncated: bool, artifacts: array) -> map ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: artifacts } } -fn fail_host(envelope: map) -> map { - let error: map = types::map_map(envelope, "error"); +fn fail_host(error: map) -> map { fail( types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "capability failed"), @@ -600,8 +600,10 @@ fn fail_host(envelope: map) -> map { } fn encoded_len(value: map) -> int { - let encoded: string = json::encode(value); - utf8_len(encoded) + // Open JSON text is produced on the host: this pin admits no guest-side + // serializer and no dynamic `json::encode` target. + let encoded = agent::json_text(value); + if encoded.ok => { encoded.bytes } else => { 0 } } fn utf8_len(value: string) -> int { @@ -656,25 +658,43 @@ fn skip_search_code(code: string) -> bool { code == "path_denied" || code == "not_found" || code == "wrong_type" || code == "permission_denied" || code == "budget_exceeded" || code == "invalid_utf8" || code == "invalid_data" } +/// Serialized prepare metadata. Open JSON leaves the guest as text, so the host +/// parses and validates it; an empty string is refused there, never defaulted. +fn prepare_metadata(context: map, tool_name: string, risk_class: string) -> string { + let prepare: map = types::map_map(context, "prepare"); + let encoded = agent::json_text({ + run_id: types::map_string(prepare, "run_id", ""), + call_id: types::map_string(prepare, "call_id", ""), + name: types::map_string(prepare, "name", tool_name), + argument_digest: types::map_string(prepare, "argument_digest", ""), + registry_identity: types::map_string(prepare, "registry_identity", ""), + risk_class: risk_class, + summary: types::map_string(prepare, "summary", tool_name) + }); + if encoded.ok => { encoded.text } else => { "" } +} + +/// Serialized canonical tool result for `tool_commit`. +fn commit_text(result: map) -> string { + let encoded = agent::json_text(result); + if encoded.ok => { encoded.text } else => { "" } +} + + fn control_failure() -> map { - let control: map = agent::control_check(); - if map_bool(control, "ok", false) => { + let control = agent::control_check(); + if control.ok => { { ok: true, code: "", message: "" } } else => { - let error: map = types::map_map(control, "error"); - { - ok: false, - code: types::map_string(error, "code", "cancelled"), - message: types::map_string(error, "message", "cancelled") - } + match control.error { Some(failure) => { ok: false, code: failure.code, message: failure.message }, None => { ok: false, code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } } } fn read_text(token: string, path: string, max_read_bytes: int) -> map { let mut result: map = { ok: false, skip: false, content_skip: false, text: "", code: "", message: "" }; - let read: map = cap::fs_read_range(token, path, 0, max_read_bytes); - if map_bool(read, "ok", false) == false { - let error: map = types::map_map(read, "error"); + let read = cap::fs_read_range(token, path, 0, max_read_bytes); + if read.ok == false { + let error: map = types::capability_error(read.error); let code: string = types::map_string(error, "code", "internal_error"); if skip_search_code(code) { result = { ok: false, skip: true, content_skip: false, text: "", code: code, message: types::map_string(error, "message", "") }; @@ -682,7 +702,7 @@ fn read_text(token: string, path: string, max_read_bytes: int) -> map { result = { ok: false, skip: false, content_skip: false, text: "", code: code, message: types::map_string(error, "message", "") }; } } else { - if map_bool(read, "truncated", false) { + if read.truncated { result = { ok: false, skip: true, content_skip: false, text: "", code: "budget_exceeded", message: "read budget exceeded" }; } else { let payload: bytes = read.bytes; @@ -730,14 +750,15 @@ fn push_match(state: map, line: string) -> map { } fn observe_controls(state: map) -> map { - let tick: map = cap::clock_monotonic_ms(types::map_string(state, "token", "")); - if map_bool(tick, "ok", false) == false { + let tick = cap::clock_monotonic_ms(types::map_string(state, "token", "")); + if tick.ok == false { + let error: map = types::capability_error(tick.error); state.fatal = true; - state.fatal_code = types::map_string(types::map_map(tick, "error"), "code", "internal_error"); - state.fatal_message = types::map_string(types::map_map(tick, "error"), "message", "capability failed"); + state.fatal_code = types::map_string(error, "code", "internal_error"); + state.fatal_message = types::map_string(error, "message", "capability failed"); state.stop = true; } else { - let now: int = map_int(tick, "ms", 0); + let now: int = tick.ms; let start: int = map_int(state, "start_ms", 0); if now < start { state.truncated = true; @@ -785,9 +806,9 @@ fn walk_search(state: map, path: string, depth: int) -> map { if list_limit <= 0 { list_limit = 1; } - let listed: map = cap::fs_list(types::map_string(state, "token", ""), path, 0, list_limit); - if map_bool(listed, "ok", false) == false { - let error: map = types::map_map(listed, "error"); + let listed = cap::fs_list(types::map_string(state, "token", ""), path, 0, list_limit); + if listed.ok == false { + let error: map = types::capability_error(listed.error); let code: string = types::map_string(error, "code", "internal_error"); let mut treat_truncated: bool = false; if code == "budget_exceeded" { @@ -823,20 +844,20 @@ fn walk_search(state: map, path: string, depth: int) -> map { } else { let mut drop_page: bool = false; if remaining <= 2 { - let probe_entries: array = types::map_array(listed, "entries"); - if map_bool(listed, "truncated", false) { + let probe_entries = listed.entries.copy(); + if listed.truncated { drop_page = true; } else { if probe_entries.length > 0 { drop_page = true; } else { - if map_int(listed, "next_cursor", 0) > map_int(listed, "cursor", 0) { + if listed.next_cursor.unwrap_or(0) > listed.cursor { drop_page = true; } } } } else { - if map_bool(listed, "truncated", false) { + if listed.truncated { drop_page = true; } } @@ -844,7 +865,7 @@ fn walk_search(state: map, path: string, depth: int) -> map { state.truncated = true; state.stop = true; } else { - let mut entries: array = types::map_array(listed, "entries"); + let mut entries: array = listed.entries; entries = sort_entries(entries); let mut index: int = 0; while map_bool(state, "stop", false) == false && index < entries.length { @@ -899,7 +920,7 @@ fn walk_search(state: map, path: string, depth: int) -> map { let line: string = lines[line_index].copy(); if string_contains(line, pattern) { let trimmed: string = trim_crlf(line); - let encoded: string = json::encode(line_index + 1); + let encoded: string = json::encode::(line_index + 1); state = push_match(state, child + ":" + encoded + ":" + trimmed); } } @@ -933,13 +954,13 @@ fn shrink_result(result: map, max_output_bytes: int, token: string) -> map { let data: map = types::map_map(current, "data"); if content.length > 0 { let payload: bytes = bytes::from_utf8(content); - let put: map = cap::artifact_put_result(token, payload, {}); + let put: map = cap::artifact_put_result(token, payload, "{}"); if map_bool(put, "ok", false) { let id: string = types::map_string(put, "id", ""); let len: int = map_int(put, "len", utf8_len(content)); let mut artifacts: array = []; artifacts[0] = id; - let mut summary: string = "artifact " + id + " (" + json::encode(len) + " bytes)"; + let mut summary: string = "artifact " + id + " (" + json::encode::(len) + " bytes)"; current = succeed(summary, data, true, artifacts); if encoded_len(current) > max_output_bytes { current = succeed("artifact " + id, data, true, artifacts); @@ -1024,7 +1045,7 @@ pub fn execute(context: map, arguments: map) -> map { let pattern: string = types::map_string(arguments, "pattern", ""); let path: string = types::map_string(arguments, "path", ""); let target: string = types::map_string(arguments, "target", ""); - let mut result: map = { ok: true, content: "", data: {}, error: null, truncated: false, artifacts: [] }; + let mut result: map = { ok: true, content: "", data: {}, error: (), truncated: false, artifacts: [] }; let checked: map = control_failure(); if map_bool(checked, "ok", false) == false { result = fail(types::map_string(checked, "code", "cancelled"), types::map_string(checked, "message", ""), {}); @@ -1074,11 +1095,11 @@ pub fn execute(context: map, arguments: map) -> map { max_search_wall_time_ms: map_int(config, "max_search_wall_time_ms", 2000), max_read_bytes: map_int(config, "max_read_bytes", 1048576) }; - let start_clock: map = cap::clock_monotonic_ms(token); - if map_bool(start_clock, "ok", false) == false { - result = fail_host(start_clock); + let start_clock = cap::clock_monotonic_ms(token); + if start_clock.ok == false { + result = fail_host(types::capability_error(start_clock.error)); } else { - state.start_ms = map_int(start_clock, "ms", 0); + state.start_ms = start_clock.ms; state = observe_controls(state); if map_bool(state, "stop", false) == false { state = walk_search(state, path, 0); @@ -1126,13 +1147,13 @@ pub fn invoke(context: map) -> map { {} ) } else => { - let prepared: map = agent_runtime::tool_prepare(types::map_map(context, "prepare")); - if types::map_string(prepared, "kind", "") == "replay" => { - types::map_map(prepared, "result") - } else if map_bool(prepared, "ok", false) == false => { - fail_host(prepared) + let prepared = agent_runtime::tool_prepare(prepare_metadata(context, "search_files", "read")); + if prepared.ok == false => { + fail_host(types::capability_error(prepared.error)) + } else if prepared.kind == "replay" => { + types::file_replay_result(prepared.result) } else => { - let token: string = types::map_string(prepared, "execution_token", ""); + let token: string = prepared.execution_token; let result: map = execute( { execution_token: token, @@ -1140,11 +1161,9 @@ pub fn invoke(context: map) -> map { }, arguments ); - let committed: map = agent_runtime::tool_commit(token, result); - if types::map_string(committed, "kind", "") == "committed" => { - types::map_map(committed, "result") - } else if map_bool(committed, "ok", false) == false => { - fail_host(committed) + let committed = agent_runtime::tool_commit(token, commit_text(result)); + if committed.ok == false => { + fail_host(types::capability_error(committed.error)) } else => { result } diff --git a/rss/tools/terminal.rss b/rss/tools/terminal.rss index 5649c4a..3652b56 100644 --- a/rss/tools/terminal.rss +++ b/rss/tools/terminal.rss @@ -1,4 +1,21 @@ use self::types as types; + +// The canonical tool result a pinned/managed invocation hands back. Only the +// fields this tool projects are declared; `data` is that result's own open +// payload, so it crosses as serialized text. +struct ToolCapabilityFailure { + code: string, + message: string +} + +struct AgentCanonicalToolResult { + ok: bool, + content: string, + data: string, + truncated: bool, + artifacts: array, + error: Option +} use bytes; use json; @@ -40,8 +57,10 @@ fn utf8_len(text: string) -> int { } fn encoded_len(value: map) -> int { - let encoded: string = json::encode(value); - utf8_len(encoded) + // Open JSON text is produced on the host: this pin admits no guest-side + // serializer and no dynamic `json::encode` target. + let encoded = agent::json_text(value); + if encoded.ok => { encoded.bytes } else => { 0 } } fn unpublished() -> map { @@ -53,7 +72,7 @@ fn succeed(content: string, data: map, truncated: bool) -> map { ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: [] } @@ -249,9 +268,10 @@ fn snapshot_data(envelope: map, include_background: bool, background: bool) -> m } else { if map_bool(envelope, "signaled", false) { data.status = "signaled"; - if envelope.has("signal") { - if type(envelope["signal"].copy()) == "int" { - data.signal = map_int(envelope, "signal", 0); + let signal: map = types::map_map(envelope, "signal"); + if signal.has("payload") { + if type(signal["payload"].copy()) == "int" { + data.signal = map_int(signal, "payload", 0); } } } else { @@ -259,9 +279,10 @@ fn snapshot_data(envelope: map, include_background: bool, background: bool) -> m data.status = "unknown"; } else { data.status = "exited"; - if envelope.has("exit_code") { - if type(envelope["exit_code"].copy()) == "int" { - data.exit_code = map_int(envelope, "exit_code", 0); + let exit_code: map = types::map_map(envelope, "exit_code"); + if exit_code.has("payload") { + if type(exit_code["payload"].copy()) == "int" { + data.exit_code = map_int(exit_code, "payload", 0); } } } @@ -414,7 +435,7 @@ fn apply_output_bounds(result: map, token: string, config: map, envelope: map) - data.overflow_stdout_bytes = stdout_raw.length; data.overflow_stderr_bytes = stderr_raw.length; bounded.data = data; - let stored: map = cap::artifact_put_result(token, payload, {}); + let stored: map = cap::artifact_put_result(token, payload, "{}"); let mut stored_artifact: bool = false; if map_bool(stored, "ok", false) { stored_artifact = true; @@ -437,10 +458,7 @@ fn apply_output_bounds(result: map, token: string, config: map, envelope: map) - bounded } -fn map_spawn_error(envelope: map) -> map { - let error: map = types::map_map(envelope, "error"); - let code: string = types::map_string(error, "code", "spawn_failed"); - let message: string = types::map_string(error, "message", "process spawn failed"); +fn map_spawn_error(code: string, message: string) -> map { let mut mapped: map = fail(code, message, unpublished()); if code == "path_denied" { mapped = fail("invalid_cwd", "cwd is outside the workspace", unpublished()); @@ -490,11 +508,11 @@ fn execute(context: map) -> map { let background: bool = map_bool(arguments, "background", false); let stdin_text: string = types::map_string(arguments, "stdin", ""); let argv: array = types::map_array(arguments, "argv"); - let control: map = agent::control_check(); + let control = agent::control_check(); let mut outcome: map = fail("cancelled", "process was cancelled", unpublished()); let mut last_envelope: map = {}; - if map_bool(control, "ok", false) == false { - let error: map = types::map_map(control, "error"); + if control.ok == false { + let error: map = match control.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } }; outcome = fail(types::map_string(error, "code", "cancelled"), types::map_string(error, "message", "process was cancelled"), unpublished()); } else { let limits: AgentProcessLimits = { @@ -507,11 +525,14 @@ fn execute(context: map) -> map { close_after_initial: background == false }; let stdin_bytes: bytes = bytes::from_utf8(stdin_text); - let spawned: map = cap::process_spawn(token, argv, cwd, [], limits, stdin_bytes); - if map_bool(spawned, "ok", false) == false { - outcome = map_spawn_error(spawned); + let spawned = cap::process_spawn(token, argv, cwd, [], limits, stdin_bytes); + if spawned.ok == false { + outcome = match spawned.error { + Some(failure) => map_spawn_error(failure.code, failure.message), + None => fail("spawn_failed", "process spawn failed", unpublished()), + }; } else { - let handle: string = types::map_string(spawned, "handle", ""); + let handle: string = spawned.handle; let mut spawn_ready: bool = true; if background == false { let closed: map = cap::process_close(token, handle); @@ -545,15 +566,15 @@ fn execute(context: map) -> map { let mut iters: int = 0; while finished == false && iters < 1000000 { iters = iters + 1; - let again: map = agent::control_check(); - if map_bool(again, "ok", false) == false { + let again = agent::control_check(); + if again.ok == false { let killed: map = cap::process_kill(token, handle); if map_bool(killed, "ok", false) == false { let ignored_kill: bool = false; } let snap: map = poll_snapshot(token, handle, stream_limit); last_envelope = snap; - let error: map = types::map_map(again, "error"); + let error: map = match again.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "control_contract_violation", message: "control_check reported a failure without a structured error" } }; outcome = fail_from_snapshot(types::map_string(error, "code", "cancelled"), types::map_string(error, "message", "process was cancelled"), snap); finished = true; } else { @@ -612,6 +633,47 @@ fn execute(context: map) -> map { apply_output_bounds(outcome, token, config, last_envelope) } +/// Serialized prepare metadata. Open JSON leaves the guest as text, so the host +/// parses and validates it; an empty string is refused there, never defaulted. +fn prepare_metadata(context: map, tool_name: string, risk_class: string) -> string { + let prepare: map = types::map_map(context, "prepare"); + let encoded = agent::json_text({ + run_id: types::map_string(prepare, "run_id", ""), + call_id: types::map_string(prepare, "call_id", ""), + name: types::map_string(prepare, "name", tool_name), + argument_digest: types::map_string(prepare, "argument_digest", ""), + registry_identity: types::map_string(prepare, "registry_identity", ""), + risk_class: risk_class, + summary: types::map_string(prepare, "summary", tool_name) + }); + if encoded.ok => { encoded.text } else => { "" } +} + +/// Serialized canonical tool result for `tool_commit`. +fn commit_text(result: map) -> string { + let encoded = agent::json_text(result); + if encoded.ok => { encoded.text } else => { "" } +} + +/// A replayed canonical result: the host hands back text, so the declared +/// struct is the only way to read it. `data` is that result's own open payload +/// and therefore its serialized text. +fn replay_result(text: string) -> map { + let mut result: map = fail("internal_error", "replayed result is unavailable", {}); + if text.length > 0 { + let decoded: AgentCanonicalToolResult = json::decode::(text); + result = { + ok: decoded.ok, + content: decoded.content, + data: decoded.data, + truncated: decoded.truncated, + artifacts: decoded.artifacts, + error: match decoded.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "", message: "" } } + }; + } + result +} + pub fn descriptor() -> map { types::terminal_descriptor() } @@ -626,32 +688,24 @@ pub fn invoke(context: map) -> map { if map_bool(validated, "ok", false) == false { result = fail(types::map_string(validated, "code", "invalid_arguments"), types::map_string(validated, "message", "invalid arguments"), unpublished()); } else { - let prepare: map = types::map_map(context, "prepare"); - let prepared: map = agent_runtime::tool_prepare({ - run_id: types::map_string(prepare, "run_id", ""), - call_id: types::map_string(prepare, "call_id", ""), - name: types::map_string(prepare, "name", "terminal"), - argument_digest: types::map_string(prepare, "argument_digest", ""), - registry_identity: types::map_string(prepare, "registry_identity", ""), - risk_class: "execute", - summary: types::map_string(prepare, "summary", "terminal") - }); - if map_bool(prepared, "ok", false) == false { - let error: map = types::map_map(prepared, "error"); - result = fail(types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "prepare failed"), unpublished()); + let prepared = agent_runtime::tool_prepare(prepare_metadata(context, "terminal", "execute")); + if prepared.ok == false { + let prepare_failure: map = match prepared.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }; + result = fail(types::map_string(prepare_failure, "code", "internal_error"), types::map_string(prepare_failure, "message", "prepare failed"), unpublished()); } else { - if types::map_string(prepared, "kind", "") == "replay" { - result = types::map_map(prepared, "result"); + let token: string = prepared.execution_token; + if prepared.kind == "replay" { + result = replay_result(prepared.result); } else { let mut exec_context: map = context; - exec_context.execution_token = types::map_string(prepared, "execution_token", ""); + exec_context.execution_token = token.copy(); let canonical: map = execute(exec_context); - let committed: map = agent_runtime::tool_commit(types::map_string(prepared, "execution_token", ""), canonical); - if map_bool(committed, "ok", false) == false { - let error: map = types::map_map(committed, "error"); - result = fail(types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "commit failed"), unpublished()); + let committed = agent_runtime::tool_commit(token, commit_text(canonical)); + if committed.ok == false { + let commit_failure: map = match committed.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }; + result = fail(types::map_string(commit_failure, "code", "internal_error"), types::map_string(commit_failure, "message", "commit failed"), unpublished()); } else { - result = types::map_map(committed, "result"); + result = canonical; } } } diff --git a/rss/tools/types.rss b/rss/tools/types.rss index 8081663..072f182 100644 --- a/rss/tools/types.rss +++ b/rss/tools/types.rss @@ -1,6 +1,51 @@ // Canonical model-facing tool descriptor contracts. // Individual tool modules later own execute/validate; this module freezes the // six current public names, descriptions, schemas, risk classes, and toolsets. +use json; + +struct ToolCapabilityFailure { + code: string, + message: string +} + +struct AgentCanonicalToolResult { + ok: bool, + content: string, + data: string, + truncated: bool, + artifacts: array, + error: Option +} + +// The native lifecycle has already validated this stored result. Keep its +// open data as text and mark the replay so the native consumer can rehydrate it. +pub fn file_replay_result(text: string) -> map { + let replayed = json::decode::(text); + let mut result: map = { + ok: replayed.ok, + content: replayed.content, + data: replayed.data, + truncated: replayed.truncated, + artifacts: replayed.artifacts, + error: () + }; + match replayed.error { + Some(failure) => { kind: "replay", result: with_replay_error(result, failure) }, + None => { kind: "replay", result: result } + } +} + +fn with_replay_error(result: map, failure: ToolCapabilityFailure) -> map { + result.error = { code: failure.code, message: failure.message }; + result +} + +pub fn capability_error(error: Option) -> map { + match error { + Some(failure) => { code: failure.code, message: failure.message }, + None => { code: "internal_error", message: "capability reported a failure without a structured error" } + } +} pub fn map_string(value: map, key: string, fallback: string) -> string { let mut result: string = fallback; diff --git a/rss/tools/validate.rss b/rss/tools/validate.rss index 85b6489..c1a21f1 100644 --- a/rss/tools/validate.rss +++ b/rss/tools/validate.rss @@ -1,5 +1,5 @@ // Bounded structural validation for an RSS tool descriptor snapshot. -use json; +use agent; use self::types as types; pub fn max_registry_entries() -> int { @@ -49,11 +49,6 @@ fn error_result(code: string, message: string, name: string, limit: int) -> map } } -fn encoded_schema_length(schema: map) -> int { - let encoded: string = json::encode(schema); - encoded.length -} - fn map_ok(value: map) -> bool { let mut result = false; if value.has("ok") { @@ -110,9 +105,11 @@ fn validate_entry(descriptor: map, seen: array) -> map { error_result("unsupported_risk_class", "unsupported risk class", name, 0) } else => { let schema: map = types::map_map(descriptor, "schema"); - let schema_len: int = encoded_schema_length(schema); - if schema_len > max_schema_bytes() => { + let encoded = agent::json_text(schema); + if encoded.code == "payload_too_large" || encoded.bytes > max_schema_bytes() => { error_result("schema_too_large", "tool schema exceeds the byte limit", name, max_schema_bytes()) + } else if encoded.ok == false => { + error_result("invalid_schema", "tool schema is not JSON representable", name, 0) } else => { ok_result() } diff --git a/rss/tools/write_file.rss b/rss/tools/write_file.rss index 3f2148c..d607d71 100644 --- a/rss/tools/write_file.rss +++ b/rss/tools/write_file.rss @@ -1,4 +1,5 @@ use self::types as types; + use bytes; use json; @@ -25,7 +26,7 @@ fn map_bool(value: map, key: string, fallback: bool) -> bool { } fn int_to_string(value: int) -> string { - let encoded: string = json::encode(value); + let encoded: string = json::encode::(value); encoded } @@ -71,14 +72,13 @@ fn succeed(content: string, data: map, truncated: bool, artifacts: array) -> map ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: artifacts } } -fn fail_host(envelope: map) -> map { - let error: map = types::map_map(envelope, "error"); +fn fail_host(error: map) -> map { let mut code: string = types::map_string(error, "code", "internal_error"); let mut message: string = types::map_string(error, "message", "capability failed"); if message == "symlinks are not followed" { @@ -236,23 +236,43 @@ fn validate_tool_path(path: string, allow_empty: bool) -> map { result } +/// Serialized prepare metadata. Open JSON leaves the guest as text, so the host +/// parses and validates it; an empty string is refused there, never defaulted. +fn prepare_metadata(context: map, tool_name: string, risk_class: string) -> string { + let prepare: map = types::map_map(context, "prepare"); + let encoded = agent::json_text({ + run_id: types::map_string(prepare, "run_id", ""), + call_id: types::map_string(prepare, "call_id", ""), + name: types::map_string(prepare, "name", tool_name), + argument_digest: types::map_string(prepare, "argument_digest", ""), + registry_identity: types::map_string(prepare, "registry_identity", ""), + risk_class: risk_class, + summary: types::map_string(prepare, "summary", tool_name) + }); + if encoded.ok => { encoded.text } else => { "" } +} + +/// Serialized canonical tool result for `tool_commit`. +fn commit_text(result: map) -> string { + let encoded = agent::json_text(result); + if encoded.ok => { encoded.text } else => { "" } +} + + fn control_failure() -> map { - let control: map = agent::control_check(); - if map_bool(control, "ok", false) => { + let control = agent::control_check(); + if control.ok => { { ok: true, code: "", message: "" } } else => { - let error: map = types::map_map(control, "error"); - { - ok: false, - code: types::map_string(error, "code", "cancelled"), - message: types::map_string(error, "message", "cancelled") - } + match control.error { Some(failure) => { ok: false, code: failure.code, message: failure.message }, None => { ok: false, code: "control_contract_violation", message: "control_check reported a failure without a structured error" } } } } fn encoded_len(result: map) -> int { - let encoded: string = json::encode(result); - utf8_len(encoded) + // Open JSON text is produced on the host: this pin admits no guest-side + // serializer and no dynamic `json::encode` target. + let encoded = agent::json_text(result); + if encoded.ok => { encoded.bytes } else => { 0 } } fn utf8_len(value: string) -> int { @@ -325,10 +345,10 @@ fn shrink_result(result: map, max_output_bytes: int, token: string) -> map { let data: map = types::map_map(current, "data"); if content.length > 0 { let payload: bytes = bytes::from_utf8(content); - let put: map = cap::artifact_put_result(token, payload, {}); - if map_bool(put, "ok", false) { - let id: string = types::map_string(put, "id", ""); - let len: int = map_int(put, "len", utf8_len(content)); + let put = cap::artifact_put_result(token, payload, "{}"); + if put.ok { + let id: string = put.id; + let len: int = put.len; let mut artifacts: array = []; artifacts[0] = id; current = succeed(artifact_summary(id, len, max_output_bytes), data, true, artifacts); @@ -399,7 +419,7 @@ pub fn execute(context: map, arguments: map) -> map { let config: map = types::map_map(context, "config"); let path: string = types::map_string(arguments, "path", ""); let content: string = types::map_string(arguments, "content", ""); - let mut result: map = { ok: true, content: "", data: {}, error: null, truncated: false, artifacts: [] }; + let mut result: map = { ok: true, content: "", data: {}, error: (), truncated: false, artifacts: [] }; let checked: map = control_failure(); if map_bool(checked, "ok", false) == false { result = fail(types::map_string(checked, "code", "cancelled"), types::map_string(checked, "message", ""), unpublished()); @@ -419,16 +439,16 @@ pub fn execute(context: map, arguments: map) -> map { result = fail(types::map_string(before_publish, "code", "cancelled"), types::map_string(before_publish, "message", ""), unpublished()); } else { let payload: bytes = bytes::from_utf8(content); - let written: map = cap::fs_write_atomic(token, path, "*", payload); - if map_bool(written, "ok", false) == false { - result = fail_host(written); + let written = cap::fs_write_atomic(token, path, "*", payload); + if written.ok == false { + result = fail_host(types::capability_error(written.error)); } else { result = shrink_result( published_result( "wrote " + int_to_string(content_len) + " bytes", - map_bool(written, "durable", true), - map_bool(written, "staging_cleaned", true), - map_int(written, "len", content_len) + written.durable, + written.staging_cleaned, + written.len ), max_output_bytes, token @@ -462,18 +482,18 @@ pub fn invoke(context: map) -> map { data ) } else => { - let prepared: map = agent_runtime::tool_prepare(types::map_map(context, "prepare")); - if types::map_string(prepared, "kind", "") == "replay" => { - types::map_map(prepared, "result") - } else if map_bool(prepared, "ok", false) == false => { - let error: map = types::map_map(prepared, "error"); + let prepared = agent_runtime::tool_prepare(prepare_metadata(context, "write_file", "write")); + if prepared.ok == false => { + let error: map = types::capability_error(prepared.error); fail( types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "capability failed"), {} ) + } else if prepared.kind == "replay" => { + types::file_replay_result(prepared.result) } else => { - let token: string = types::map_string(prepared, "execution_token", ""); + let token: string = prepared.execution_token; let result: map = execute( { execution_token: token, @@ -481,11 +501,9 @@ pub fn invoke(context: map) -> map { }, arguments ); - let committed: map = agent_runtime::tool_commit(token, result); - if types::map_string(committed, "kind", "") == "committed" => { - types::map_map(committed, "result") - } else if map_bool(committed, "ok", false) == false => { - let error: map = types::map_map(committed, "error"); + let committed = agent_runtime::tool_commit(token, commit_text(result)); + if committed.ok == false => { + let error: map = types::capability_error(committed.error); fail( types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "capability failed"), diff --git a/src/auth/host.rs b/src/auth/host.rs index cd851a3..01e9178 100644 --- a/src/auth/host.rs +++ b/src/auth/host.rs @@ -16,7 +16,7 @@ use rustscript_vm::{ CallOutcome, CallReturn, HostApiCatalog, HostFunctionDescriptor, HostFunctionRegistry, HostFunctionSchema, HostModuleDescriptor, HostParamPassing, HostParamSchema, HostResourceType, HostResourceTypeMeta, HostStructField, HostTypeSchema, ResourceTypeKey, Value, Vm, VmError, - VmResult, + VmResult, option, }; use serde_json::{Value as Json, json}; use url::Url; @@ -35,6 +35,7 @@ use crate::auth::store::{ use crate::auth::token::CredentialId; use crate::config_file::{AgentPaths, ConfigFile}; use crate::domain::json_to_vm_value; +use crate::runtime::host_shapes; use crate::runtime::rss_runner::RunCancellation; const LIMIT: usize = 64; @@ -374,6 +375,22 @@ impl AuthHost { /// Install a fresh VM owner and return the only RSS entry context. pub fn bind_vm(&self, vm: &mut Vm) -> VmResult { + self.bind_vm_with_json_policy(vm, false) + } + + pub(crate) fn bind_vm_with_json_policy( + &self, + vm: &mut Vm, + allow_json_encode: bool, + ) -> VmResult { + if !allow_json_encode { + // Core lowers typed encode to an internal builtin, which is not + // screened by the registry allowlist. Core honors the public + // encoder override for that lowered call as well. + vm.bind_builtin_static_override("json::encode", |_, _| { + Err(error("auth_json_builtin_denied")) + })?; + } let snapshot = self .published .lock() @@ -1033,6 +1050,12 @@ fn key(name: &str) -> HostTypeSchema { fn opt(ty: HostTypeSchema) -> HostTypeSchema { HostTypeSchema::Optional(Box::new(ty)) } +/// A logical guest `Option` slot. Its carrier is the canonical +/// `{tag, payload}` map, built by the adapter; it is never inferred from a +/// physical Null. +fn logical(ty: HostTypeSchema) -> HostTypeSchema { + HostTypeSchema::Option(Box::new(ty)) +} fn array(ty: HostTypeSchema) -> HostTypeSchema { HostTypeSchema::Array(Box::new(ty)) } @@ -1056,10 +1079,10 @@ fn metadata_type() -> HostTypeSchema { ("token_type", HostTypeSchema::String), ("expires_at_ms", HostTypeSchema::Int), ("scopes", array(HostTypeSchema::String)), - ("account_id", opt(HostTypeSchema::String)), + ("account_id", logical(HostTypeSchema::String)), ("generation", HostTypeSchema::Int), ("status", HostTypeSchema::String), - ("last_refresh_at_ms", opt(HostTypeSchema::Int)), + ("last_refresh_at_ms", logical(HostTypeSchema::Int)), ("has_refresh_token", HostTypeSchema::Bool), ], ) @@ -1074,10 +1097,10 @@ fn credential_type() -> HostTypeSchema { ("token_type", HostTypeSchema::String), ("expires_at_ms", HostTypeSchema::Int), ("scopes", array(HostTypeSchema::String)), - ("account_id", opt(HostTypeSchema::String)), + ("account_id", logical(HostTypeSchema::String)), ("generation", HostTypeSchema::Int), ("status", HostTypeSchema::String), - ("last_refresh_at_ms", opt(HostTypeSchema::Int)), + ("last_refresh_at_ms", logical(HostTypeSchema::Int)), ("has_refresh_token", HostTypeSchema::Bool), ], ) @@ -1088,19 +1111,19 @@ fn config_type() -> HostTypeSchema { let oauth = named( "OAuthConfig", vec![ - ("flow", opt(s.clone())), - ("issuer", opt(s.clone())), - ("client_id", opt(s.clone())), - ("device_user_code_path", opt(s.clone())), - ("device_poll_path", opt(s.clone())), - ("authorization_path", opt(s.clone())), - ("token_endpoint", opt(s.clone())), - ("redirect_uri", opt(s.clone())), + ("flow", logical(s.clone())), + ("issuer", logical(s.clone())), + ("client_id", logical(s.clone())), + ("device_user_code_path", logical(s.clone())), + ("device_poll_path", logical(s.clone())), + ("authorization_path", logical(s.clone())), + ("token_endpoint", logical(s.clone())), + ("redirect_uri", logical(s.clone())), ("refresh_skew_seconds", i.clone()), - ("scopes", opt(array(s.clone()))), + ("scopes", logical(array(s.clone()))), ( "authorization_parameters", - opt(array(flow::public_query_entry_type())), + logical(array(flow::public_query_entry_type())), ), ], ); @@ -1135,8 +1158,8 @@ fn config_type() -> HostTypeSchema { ("name", s.clone()), ("protocol", s.clone()), ("base_url", s.clone()), - ("auth", opt(s.clone())), - ("oauth", opt(oauth)), + ("auth", logical(s.clone())), + ("oauth", logical(oauth)), ], )), ), @@ -1146,7 +1169,7 @@ fn config_type() -> HostTypeSchema { "WorkspaceConfig", vec![ ("allowed_roots", array(s.clone())), - ("default", opt(s.clone())), + ("default", logical(s.clone())), ( "named", array(named( @@ -1183,16 +1206,20 @@ fn config_type() -> HostTypeSchema { ], ) } -fn envelope(name: &str, fields: Vec<(&str, HostTypeSchema)>) -> HostTypeSchema { +/// The converted envelope shell: a fixed `ok` flag plus a logical +/// `Option` error slot. `None` is the success case; a failure is +/// reported by the adapter as a transport error, never as `ok: true` with a +/// message. +fn fixed_envelope(name: &str, fields: Vec<(&str, HostTypeSchema)>) -> HostTypeSchema { let mut all = vec![ ("ok", HostTypeSchema::Bool), - ("error", opt(HostTypeSchema::String)), + ("error", logical(HostTypeSchema::String)), ]; all.extend(fields); named(name, all) } fn config_envelope() -> HostTypeSchema { - envelope( + fixed_envelope( "ConfigSnapshotEnvelope", vec![ ("public_config", config_type()), @@ -1210,7 +1237,7 @@ fn config_envelope() -> HostTypeSchema { ) } fn metadata_envelope() -> HostTypeSchema { - envelope( + fixed_envelope( "AuthMetadataEnvelope", vec![ ("metadata", metadata_type()), @@ -1219,10 +1246,10 @@ fn metadata_envelope() -> HostTypeSchema { ) } fn handle_envelope(name: &str, ty: &str) -> HostTypeSchema { - envelope(name, vec![("handle", key(ty))]) + fixed_envelope(name, vec![("handle", key(ty))]) } fn mutation_envelope() -> HostTypeSchema { - envelope( + fixed_envelope( "RedactedMutationResult", vec![("deleted", HostTypeSchema::Bool)], ) @@ -1237,7 +1264,7 @@ fn save_type() -> HostTypeSchema { ("metadata", credential_type()), ("access_slot", key(SecretSlot::KEY)), ("refresh_action", HostTypeSchema::String), - ("refresh_slot", opt(key(SecretSlot::KEY))), + ("refresh_slot", logical(key(SecretSlot::KEY))), ], ) } @@ -1268,25 +1295,30 @@ fn transport_use_type() -> HostTypeSchema { "TransportCredentialUse", vec![ ("kind", HostTypeSchema::String), - ("access_handle", opt(key(Access::KEY))), - ("refresh_handle", opt(key(Refresh::KEY))), - ("code_handle", opt(key("oauth.authorization_code"))), - ("verifier_handle", opt(key("oauth.verifier"))), - ("device_handle", opt(key(device::DEVICE_KEY))), + ("access_handle", logical(key(Access::KEY))), + ("refresh_handle", logical(key(Refresh::KEY))), + ("code_handle", logical(key("oauth.authorization_code"))), + ("verifier_handle", logical(key("oauth.verifier"))), + ("device_handle", logical(key(device::DEVICE_KEY))), ], ) } fn transport_response_type() -> HostTypeSchema { - envelope( + // The response shell is fixed: a concrete `status`/`public_headers`/body + // text, plus the four genuinely optional leaves as logical `Option` + // carriers. The resource slots keep their live `Value::Int` handles inside + // the carrier — nothing here is serialized, so a handle never turns into + // text and an absent slot is tagged `None` instead of being a null. + fixed_envelope( "TransportResponseEnvelope", vec![ ("status", HostTypeSchema::Int), ("public_headers", array(public_header_type())), ("body_without_secret_fields", HostTypeSchema::String), - ("retry_after_ms", opt(HostTypeSchema::Int)), - ("access_slot", opt(key(SecretSlot::KEY))), - ("refresh_slot", opt(key(SecretSlot::KEY))), - ("device_handle", opt(key(device::DEVICE_KEY))), + ("retry_after_ms", logical(HostTypeSchema::Int)), + ("access_slot", logical(key(SecretSlot::KEY))), + ("refresh_slot", logical(key(SecretSlot::KEY))), + ("device_handle", logical(key(device::DEVICE_KEY))), ], ) } @@ -1414,7 +1446,7 @@ fn close_access_desc() -> HostFunctionDescriptor { key(Access::KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_access, ) } @@ -1426,7 +1458,7 @@ fn close_refresh_desc() -> HostFunctionDescriptor { key(Refresh::KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_refresh, ) } @@ -1438,7 +1470,7 @@ fn close_slot_desc() -> HostFunctionDescriptor { key(SecretSlot::KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_slot, ) } @@ -1576,7 +1608,7 @@ fn config_call(vm: &mut Vm, args: &[Value]) -> VmResult { }), ); } - result(object(vec![ + let legacy = object(vec![ ("ok", Value::Bool(true)), ("error", Value::Null), ("public_config", json_to_vm_value(&public)), @@ -1596,16 +1628,51 @@ fn config_call(vm: &mut Vm, args: &[Value]) -> VmResult { Value::Int(owner.snapshot.allowed.len() as i64), )]), ), - ])) -} -fn optional_field<'a>(value: &'a Value, name: &str) -> VmResult> { - let Value::Map(entries) = value else { + ]); + // The envelope keeps its `ConfigSnapshotEnvelope` shell and the live + // `policy_handle` resource; only the nullable leaves become logical + // `Option` carriers, and the whole outer shape is checked before it leaves + // the adapter. + let schema = config_envelope(); + let shaped = host_shapes::logical_from_legacy(&schema, &legacy)?; + host_shapes::shaped(vm, &schema, shaped) +} +/// One `TransportCredentialUse` handle slot. +/// +/// The declared shape is a logical `Option>`: an RSS caller can only +/// pass the canonical `{tag, payload}` carrier (the compiler checks it), where +/// `None` selects "this use supplies no such handle" and a `Some` carries a live +/// `Value::Int` handle. +/// +/// Two pre-conversion native spellings are still read, because they come from +/// Rust call sites that predate the carrier and describe the same thing: +/// an absent field or a physical null is `None`, and a bare handle is that +/// handle. The bare form is not a guess about +/// meaning — the caller's match arm still runs `borrowed::` and the owner +/// check on it, so a value that is not a live handle of this exact resource type +/// fails closed. A canonical `Some` without a payload is refused, never read as +/// "no handle". +fn transport_use_handle<'a>( + usage: &'a Value, + name: &str, + resource_key: &'static str, +) -> VmResult> { + let Value::Map(entries) = usage else { return Err(error("invalid_request")); }; - Ok(entries.iter().find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name && !matches!(value, Value::Null) => Some(value), + let Some(raw) = entries.iter().find_map(|(key, value)| match key { + Value::String(key) if key.as_ref() == name => Some(value), _ => None, - })) + }) else { + return Ok(None); + }; + if matches!(raw, Value::Null) { + return Ok(None); + } + if matches!(raw, Value::Int(_)) { + return Ok(Some(raw)); + } + host_shapes::option_payload(&key(resource_key), raw).map_err(|_| error("invalid_request")) } fn canonical_json_content_type(value: &str) -> bool { @@ -1741,11 +1808,11 @@ fn transport_credential( credential_id: &str, ) -> VmResult { validate_transport_use_keys(usage)?; - let access = optional_field(usage, "access_handle")?; - let refresh = optional_field(usage, "refresh_handle")?; - let code = optional_field(usage, "code_handle")?; - let verifier = optional_field(usage, "verifier_handle")?; - let device = optional_field(usage, "device_handle")?; + let access = transport_use_handle(usage, "access_handle", Access::KEY)?; + let refresh = transport_use_handle(usage, "refresh_handle", Refresh::KEY)?; + let code = transport_use_handle(usage, "code_handle", "oauth.authorization_code")?; + let verifier = transport_use_handle(usage, "verifier_handle", "oauth.verifier")?; + let device = transport_use_handle(usage, "device_handle", device::DEVICE_KEY)?; match ( string(field(usage, "kind")?)?, access, @@ -2087,6 +2154,16 @@ fn transport_call_shared( device_handle.clone().unwrap_or(Value::Null), ), ]); + // The declared shape is checked *here*, inside the issuing scope, so a + // failure is observed by the rollback below instead of by the + // post-return pass — which would leave the handles this call just + // issued live in the arena. `logical_from_legacy` is what turns the + // four nullable leaves (and the error slot) into canonical `Option` + // carriers; the resource slots keep the `Value::Int` handles issued + // above, never their JSON spelling. + let response_schema = transport_response_type(); + let public_response = host_shapes::logical_from_legacy(&response_schema, &public_response)?; + crate::runtime::host_shapes::ensure_shaped(vm, &response_schema, &public_response)?; if capture_value.is_none() { return result(public_response); } @@ -2096,18 +2173,23 @@ fn transport_call_shared( let code = published.code_handle.clone(); let verifier = published.verifier_handle.clone(); published_pair = Some(published); - (code, verifier) + (option::some_carrier(code), option::some_carrier(verifier)) } else { - (Value::Null, Value::Null) + (option::none_carrier(), option::none_carrier()) }; owner.check()?; let envelope = object(vec![ ("ok", Value::Bool(true)), - ("error", Value::Null), + ("error", option::none_carrier()), ("response", public_response), ("code_handle", code_handle), ("verifier_handle", verifier_handle), ]); + // The capture envelope is checked in the same scope: its own two + // resource leaves are issued here, so a shape failure after publication + // must roll the pair and the slots back rather than surface later. + let capture_schema = capture::envelope_type(); + crate::runtime::host_shapes::ensure_shaped(vm, &capture_schema, &envelope)?; owner.check()?; result(envelope) })(); @@ -2159,13 +2241,16 @@ fn metadata_value(meta: &AuthMetadata) -> Value { &json!({"credential_id":meta.credential_id,"provider":meta.provider,"kind":meta.kind,"source":meta.source,"token_type":meta.token_type,"expires_at_ms":meta.expires_at_ms,"scopes":meta.scopes,"account_id":meta.account_id,"generation":meta.generation,"status":meta.status,"last_refresh_at_ms":meta.last_refresh_at_ms,"has_refresh_token":meta.has_refresh_token}), ) } -fn meta_result(meta: &AuthMetadata, outcome: &str) -> VmResult { - result(object(vec![ +fn meta_result(vm: &mut Vm, meta: &AuthMetadata, outcome: &str) -> VmResult { + let legacy = object(vec![ ("ok", Value::Bool(true)), ("error", Value::Null), ("metadata", metadata_value(meta)), ("outcome", Value::string(outcome)), - ])) + ]); + let schema = metadata_envelope(); + let shaped = host_shapes::logical_from_legacy(&schema, &legacy)?; + host_shapes::shaped(vm, &schema, shaped) } fn policy_owner(vm: &mut Vm, value: &Value, id: &str) -> VmResult> { let owner = borrowed::(vm, value)?; @@ -2202,19 +2287,23 @@ fn cleanup_inactive_code_pair(vm: &mut Vm, request: &Value, usage: &Value, id: & if string(kind).ok() != Some("code_verifier") { return; } - let Ok(access) = optional_field(usage, "access_handle") else { + let Ok(access) = transport_use_handle(usage, "access_handle", Access::KEY) else { return; }; - let Ok(refresh) = optional_field(usage, "refresh_handle") else { + let Ok(refresh) = transport_use_handle(usage, "refresh_handle", Refresh::KEY) else { return; }; let (Some(code), Some(verifier)) = ( - optional_field(usage, "code_handle").ok().flatten(), - optional_field(usage, "verifier_handle").ok().flatten(), + transport_use_handle(usage, "code_handle", "oauth.authorization_code") + .ok() + .flatten(), + transport_use_handle(usage, "verifier_handle", "oauth.verifier") + .ok() + .flatten(), ) else { return; }; - let Ok(device) = optional_field(usage, "device_handle") else { + let Ok(device) = transport_use_handle(usage, "device_handle", device::DEVICE_KEY) else { return; }; if access.is_some() @@ -2250,7 +2339,7 @@ fn metadata_call(vm: &mut Vm, args: &[Value]) -> VmResult { .load_metadata(id) .map_err(|_| error("credential_unavailable"))?; owner.check()?; - meta_result(&meta, "loaded") + meta_result(vm, &meta, "loaded") } fn metadata_result_call(vm: &mut Vm, args: &[Value]) -> VmResult { @@ -2267,7 +2356,7 @@ fn metadata_result_call(vm: &mut Vm, args: &[Value]) -> VmResult { ) })?; owner.check()?; - meta_result(&metadata, "loaded") + meta_result(vm, &metadata, "loaded") } fn access_call(vm: &mut Vm, args: &[Value]) -> VmResult { @@ -2296,11 +2385,29 @@ fn access_call(vm: &mut Vm, args: &[Value]) -> VmResult { }, ) .inspect_err(|_| h.force_expire())?; - result(object(vec![ + let schema = handle_envelope("AccessHandleEnvelope", Access::KEY); + let legacy = object(vec![ ("ok", Value::Bool(true)), ("error", Value::Null), - ("handle", value), - ])) + ("handle", value.clone()), + ]); + let checked = host_shapes::logical_from_legacy(&schema, &legacy) + .map_err(VmError::from) + .and_then(|shaped| host_shapes::ensure_shaped(vm, &schema, &shaped).map(|()| shaped)); + match checked { + Ok(shaped) => result(shaped), + // The handle was already issued: a failed outer shape check must close + // it inside this rollback scope instead of leaking a live resource. + Err(failure) => { + if let Ok(handle) = handle(&value) { + let _ = vm + .host_context() + .close_resource::(handle, ResourceCloseReason::Requested); + } + h.force_expire(); + Err(failure) + } + } } fn refresh_call(vm: &mut Vm, args: &[Value]) -> VmResult { let id = string(&args[0])?; @@ -2328,11 +2435,27 @@ fn refresh_call(vm: &mut Vm, args: &[Value]) -> VmResult { }, ) .inspect_err(|_| h.force_expire())?; - result(object(vec![ + let schema = handle_envelope("RefreshHandleEnvelope", Refresh::KEY); + let legacy = object(vec![ ("ok", Value::Bool(true)), ("error", Value::Null), - ("handle", value), - ])) + ("handle", value.clone()), + ]); + let checked = host_shapes::logical_from_legacy(&schema, &legacy) + .map_err(VmError::from) + .and_then(|shaped| host_shapes::ensure_shaped(vm, &schema, &shaped).map(|()| shaped)); + match checked { + Ok(shaped) => result(shaped), + Err(failure) => { + if let Ok(handle) = handle(&value) { + let _ = vm + .host_context() + .close_resource::(handle, ResourceCloseReason::Requested); + } + h.force_expire(); + Err(failure) + } + } } fn slot(vm: &mut Vm, value: &Value, owner: &Arc) -> VmResult { borrowed::(vm, value)?; @@ -2353,20 +2476,38 @@ fn save_call(vm: &mut Vm, args: &[Value]) -> VmResult { let id = string(field(req, "credential_id")?)?; let owner = policy_owner(vm, field(req, "policy_handle")?, id)?; let expected = natural(field(req, "expected_generation")?)?; - let metadata = crate::domain::vm_value_to_json(field(req, "metadata")?); + // The guest sends the declared `CredentialMetadata` shape, where the two + // nullable leaves are logical Options; the durable record keeps the old + // nullable JSON shape, so a `None` carrier is read back as an absent field. + let declared_metadata = + host_shapes::legacy_from_logical(&credential_type(), field(req, "metadata")?) + .map_err(|_| error("metadata_invalid"))?; + let metadata = crate::domain::vm_value_to_json(&declared_metadata); let metadata: CredentialConfig = serde_json::from_value(metadata).map_err(|_| error("metadata_invalid"))?; let access = slot(vm, field(req, "access_slot")?, &owner)?; + // `refresh_slot` is a logical `Option>`: only the + // selected `Some` payload is a live handle, and a `Some` that lost its + // payload fails closed instead of becoming "no replacement". + let refresh_slot = + host_shapes::option_payload(&key(SecretSlot::KEY), field(req, "refresh_slot")?) + .map_err(|_| error("invalid_request"))? + .cloned(); let refresh = match string(field(req, "refresh_action")?)? { "preserve" => RefreshSecretAction::Preserve, "clear" => RefreshSecretAction::Clear, - "replace" => RefreshSecretAction::Replace(slot(vm, field(req, "refresh_slot")?, &owner)?), + "replace" => match refresh_slot.as_ref() { + Some(value) => RefreshSecretAction::Replace(slot(vm, value, &owner)?), + None => return Err(error("slot_invalid")), + }, _ => return Err(error("refresh_action_invalid")), }; owner.check()?; let mut candidates = vec![(handle(field(req, "access_slot")?)?, access.clone())]; - if let RefreshSecretAction::Replace(ref replacement) = refresh { - candidates.push((handle(field(req, "refresh_slot")?)?, replacement.clone())); + if let (RefreshSecretAction::Replace(replacement), Some(value)) = + (&refresh, refresh_slot.as_ref()) + { + candidates.push((handle(value)?, replacement.clone())); } let id = CredentialId::new(id.to_owned()).map_err(|_| error("invalid_credential_id"))?; let request = StoreSaveRequest::new( @@ -2395,6 +2536,7 @@ fn save_call(vm: &mut Vm, args: &[Value]) -> VmResult { } let saved = saved.map_err(|_| error("save_rejected"))?; meta_result( + vm, saved.metadata(), if saved.is_adopted() { "adopted" @@ -2412,11 +2554,14 @@ fn delete_call(vm: &mut Vm, args: &[Value]) -> VmResult { owner.check().map_err(|_| AuthStoreError::MutationInactive) }) .map_err(|_| error("delete_rejected"))?; - result(object(vec![ + let schema = mutation_envelope(); + let legacy = object(vec![ ("ok", Value::Bool(true)), ("error", Value::Null), ("deleted", Value::Bool(true)), - ])) + ]); + let shaped = host_shapes::logical_from_legacy(&schema, &legacy)?; + host_shapes::shaped(vm, &schema, shaped) } fn close_access(vm: &mut Vm, args: &[Value]) -> VmResult { borrowed::(vm, &args[0])?; @@ -2440,6 +2585,12 @@ fn close_slot(vm: &mut Vm, args: &[Value]) -> VmResult { result(Value::Null) } +#[cfg(test)] +mod option_carrier_tests; + +#[cfg(test)] +mod transport_rollback_tests; + #[cfg(test)] mod tests { use super::*; @@ -2586,7 +2737,7 @@ mod tests { auth.fixture_inject_matching_callback(vm, &callback, "SC01_SYNTHETIC_CALLBACK_CODE") .expect("inject synthetic callback"); let waited = call_rss(vm, "wait", std::slice::from_ref(&callback)); - let code = test_field(&waited, "code_handle"); + let code = present_handle(&waited, "code_handle"); (callback, code, verifier) } @@ -2601,11 +2752,36 @@ mod tests { ]) } + /// Reads a handle that the migrated contract publishes as a logical + /// `Option>`: only the selected `Some` payload is a live handle. + fn present_handle(value: &Value, name: &str) -> Value { + let carrier = test_field(value, name); + let (present, payload) = rustscript_vm::option::payload(&carrier) + .unwrap_or_else(|error| panic!("{name} is not an Option carrier: {error}")); + assert!(present, "{name} must be the selected Some carrier"); + payload + .cloned() + .unwrap_or_else(|| panic!("{name} Some carrier without a payload")) + } + + /// The published `TransportCredentialUse` declares `kind` plus five handle + /// slots, each a logical `Option>`; an omitted or bare-handle + /// slot fails the callable's argument schema. fn transport_usage(code: &Value, verifier: &Value) -> Value { + let absent = rustscript_vm::option::none_carrier; test_record(vec![ ("kind", Value::string("code_verifier")), - ("code_handle", code.clone()), - ("verifier_handle", verifier.clone()), + ("access_handle", absent()), + ("refresh_handle", absent()), + ( + "code_handle", + rustscript_vm::option::some_carrier(code.clone()), + ), + ( + "verifier_handle", + rustscript_vm::option::some_carrier(verifier.clone()), + ), + ("device_handle", absent()), ]) } @@ -2835,3 +3011,117 @@ mod tests { assert!(!failure.to_string().contains("resource_invalid")); } } + +/// The fixed-shape auth hosts carry live resources. These tests are the +/// regression trap for the "just serialize the whole envelope" shortcut: the +/// declared shapes are asserted to keep their resource carriers, and the +/// adapter-side shape guard used by `oauth::transport` is asserted to reject a +/// value whose resource slot was turned into text. +#[cfg(test)] +mod host_carrier_tests { + use super::*; + use crate::runtime::host_shapes; + use rustscript_vm::{ + CompileSourceFileOptions, SourceFlavor, compile_source_with_flavor_and_options, + }; + + fn field(schema: &HostTypeSchema, name: &str) -> HostTypeSchema { + let HostTypeSchema::Named { fields, .. } = schema else { + panic!("expected a named envelope, got {schema:?}"); + }; + fields + .iter() + .find(|field| field.name == name) + .unwrap_or_else(|| panic!("missing field {name}")) + .ty + .clone() + } + + fn resource_slot(ty: &HostTypeSchema) -> bool { + match ty { + HostTypeSchema::Resource(_) => true, + HostTypeSchema::Optional(inner) | HostTypeSchema::Option(inner) => { + matches!(inner.as_ref(), HostTypeSchema::Resource(_)) + } + _ => false, + } + } + + #[test] + fn fixed_shape_hosts_keep_their_native_resource_carriers() { + let config = config_envelope(); + assert!( + resource_slot(&field(&config, "policy_handle")), + "config::load_snapshot must keep policy_handle as a resource handle: {:?}", + field(&config, "policy_handle") + ); + let transport = transport_response_type(); + for slot in ["access_slot", "refresh_slot", "device_handle"] { + assert!( + resource_slot(&field(&transport, slot)), + "oauth::transport must keep {slot} as a resource handle: {:?}", + field(&transport, slot) + ); + } + assert!(matches!( + field(&transport, "body_without_secret_fields"), + HostTypeSchema::String + )); + } + + fn bare_vm() -> Vm { + let catalog = auth_host_catalog(); + let compiled = compile_source_with_flavor_and_options( + "pub fn run() -> int { 1 }", + SourceFlavor::RustScript, + CompileSourceFileOptions::default().with_host_api_catalog(catalog), + ) + .expect("compile probe"); + Vm::new_shared(Arc::new(compiled.program)) + } + + /// The converted response shell: every optional leaf is one canonical + /// `{tag, payload}` carrier, never a physical null. + fn response_with(slot: Value) -> Value { + object(vec![ + ("ok", Value::Bool(true)), + ("error", rustscript_vm::option::none_carrier()), + ("status", Value::Int(200)), + ("public_headers", Value::array(Vec::new())), + ( + "body_without_secret_fields", + Value::string("{\"access_token\":\"x\"}"), + ), + ("retry_after_ms", rustscript_vm::option::none_carrier()), + ("access_slot", slot), + ("refresh_slot", rustscript_vm::option::none_carrier()), + ("device_handle", rustscript_vm::option::none_carrier()), + ]) + } + + #[test] + fn the_transport_shape_guard_rejects_a_textualised_resource_slot() { + let vm = bare_vm(); + let schema = transport_response_type(); + // An absent slot must be the tagged `None` carrier; a physical null is + // no longer the declared shape either. + host_shapes::ensure_shaped( + &vm, + &schema, + &response_with(rustscript_vm::option::none_carrier()), + ) + .expect("an absent optional resource slot is the declared None carrier"); + host_shapes::ensure_shaped(&vm, &schema, &response_with(Value::Null)) + .expect_err("a physical null is not a logical Option carrier"); + let failure = host_shapes::ensure_shaped( + &vm, + &schema, + &response_with(rustscript_vm::option::some_carrier(Value::string("7"))), + ) + .expect_err("a resource slot carrying text is not the declared shape"); + assert!( + !failure.to_string().is_empty(), + "the guard reports which slot failed" + ); + } +} diff --git a/src/auth/host/capture.rs b/src/auth/host/capture.rs index e109691..ffc284d 100644 --- a/src/auth/host/capture.rs +++ b/src/auth/host/capture.rs @@ -2,7 +2,9 @@ use rustscript_vm::{HostTypeSchema, Value, VmResult}; use crate::auth::oauth::{ExchangeCodeField, OpaqueExchangePair, OpaqueExchangeResponseIntent}; -use super::{TransportCredential, envelope, error, key, named, opt, transport_response_type}; +use super::{ + TransportCredential, error, fixed_envelope, key, logical, named, transport_response_type, +}; const MAX_SELECTOR_BYTES: usize = 64; const MAX_REDIRECT_URI_BYTES: usize = 2048; @@ -17,13 +19,17 @@ pub(super) fn intent_type() -> HostTypeSchema { ) } +/// The capture shell is fixed: the nested transport response, plus the two +/// published exchange handles as logical `Option>` carriers. A pair +/// that was never published reports `None` for both; a published one carries +/// live `Value::Int` handles, never their JSON spelling. pub(super) fn envelope_type() -> HostTypeSchema { - envelope( + fixed_envelope( "TransportCaptureEnvelope", vec![ ("response", transport_response_type()), - ("code_handle", opt(key("oauth.authorization_code"))), - ("verifier_handle", opt(key("oauth.verifier"))), + ("code_handle", logical(key("oauth.authorization_code"))), + ("verifier_handle", logical(key("oauth.verifier"))), ], ) } diff --git a/src/auth/host/device.rs b/src/auth/host/device.rs index e433999..96e1dc2 100644 --- a/src/auth/host/device.rs +++ b/src/auth/host/device.rs @@ -11,8 +11,8 @@ use rustscript_vm::{ use crate::auth::oauth::OpaqueDeviceSessionHandle; use super::{ - Bound, LIMIT, Owner, OwnerResource, desc, error, field, handle, key, optional_field, result, - string, validate_transport_use_keys, + Bound, LIMIT, Owner, OwnerResource, desc, error, field, handle, key, result, string, + validate_transport_use_keys, }; pub(super) const DEVICE_KEY: &str = "oauth.device_session"; @@ -117,7 +117,7 @@ pub(super) fn close_desc() -> HostFunctionDescriptor { key(DEVICE_KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_call, ) } @@ -239,18 +239,17 @@ pub(super) fn cleanup_inactive(vm: &mut Vm, request: &Value, usage: &Value, cred return; } let (Ok(None), Ok(None), Ok(None), Ok(Some(device))) = ( - optional_field(usage, "access_handle"), - optional_field(usage, "refresh_handle"), - optional_field(usage, "code_handle"), - optional_field(usage, "device_handle"), + super::transport_use_handle(usage, "access_handle", super::Access::KEY), + super::transport_use_handle(usage, "refresh_handle", super::Refresh::KEY), + super::transport_use_handle(usage, "code_handle", "oauth.authorization_code"), + super::transport_use_handle(usage, "device_handle", DEVICE_KEY), ) else { return; }; - if optional_field(usage, "verifier_handle") - .ok() - .flatten() - .is_some() - { + if !matches!( + super::transport_use_handle(usage, "verifier_handle", "oauth.verifier"), + Ok(None) + ) { return; } let Ok(policy_value) = field(request, "policy_handle") else { diff --git a/src/auth/host/fallible.rs b/src/auth/host/fallible.rs index 87d4fe0..29eb65e 100644 --- a/src/auth/host/fallible.rs +++ b/src/auth/host/fallible.rs @@ -1,11 +1,12 @@ use rustscript_vm::{ CallOutcome, CallReturn, HostFunctionDescriptor, HostResourceType, HostTypeSchema, Value, Vm, - VmError, VmResult, + VmError, VmResult, option, }; use std::time::Instant; use super::{ - Owner, borrow, desc, error, field, key, named, object, opt, policy_owner_for_cleanup, result, + Owner, borrow, desc, error, field, key, logical, named, object, policy_owner_for_cleanup, + result, }; use crate::auth::transport_failure::TransportFailureDetail; @@ -116,8 +117,8 @@ pub(super) fn failure_type() -> HostTypeSchema { ("deadline_exceeded", HostTypeSchema::Bool), ("revoked", HostTypeSchema::Bool), // Bounded stream transport facts; absent for every other failure. - ("transport_phase", opt(HostTypeSchema::String)), - ("transport_category", opt(HostTypeSchema::String)), + ("transport_phase", logical(HostTypeSchema::String)), + ("transport_category", logical(HostTypeSchema::String)), ], ) } @@ -127,8 +128,8 @@ pub(super) fn result_type(name: &str, value_type: HostTypeSchema) -> HostTypeSch name, vec![ ("ok", HostTypeSchema::Bool), - ("error", opt(failure_type())), - ("value", opt(value_type)), + ("error", logical(failure_type())), + ("value", logical(value_type)), ], ) } @@ -318,7 +319,10 @@ pub(super) fn wait_ms_result_desc() -> HostFunctionDescriptor { ], named( "OAuthWaitResultEnvelope", - vec![("ok", HostTypeSchema::Bool), ("error", opt(failure_type()))], + vec![ + ("ok", HostTypeSchema::Bool), + ("error", logical(failure_type())), + ], ), wait_ms_result_call, ) @@ -342,8 +346,8 @@ pub(super) fn emit_device_instruction_result_desc() -> HostFunctionDescriptor { "DeviceInstructionResultEnvelope", vec![ ("ok", HostTypeSchema::Bool), - ("value", opt(HostTypeSchema::Bool)), - ("error", opt(failure_type())), + ("value", logical(HostTypeSchema::Bool)), + ("error", logical(failure_type())), ], ), emit_device_instruction_result_call, @@ -359,8 +363,8 @@ fn emit_device_instruction_result_call(vm: &mut Vm, args: &[Value]) -> VmResult< match super::cli_io::emit_device_instruction_call(vm, args, &owner) { Ok(CallOutcome::Return(CallReturn::One(Value::Bool(true)))) => result(object(vec![ ("ok", Value::Bool(true)), - ("value", Value::Bool(true)), - ("error", Value::Null), + ("value", option::some_carrier(Value::Bool(true))), + ("error", option::none_carrier()), ])), Ok(_) => Err(VmError::InvalidFrameState( "device instruction adapter returned an unexpected outcome", @@ -380,12 +384,14 @@ fn device_instruction_failure(owner: Option<&Owner>, code: &str) -> VmResult VmResult { result(object(vec![ ("ok", Value::Bool(false)), - ("error", failure_value_with_detail(owner, code, detail)), - ("value", Value::Null), + ( + "error", + option::some_carrier(failure_value_with_detail(owner, code, detail)), + ), + ("value", option::none_carrier()), ])) } @@ -514,16 +532,20 @@ pub(super) fn wrap_value( owner: std::sync::Arc, outcome: VmResult, ) -> VmResult { + // The bridge envelope is `{ok, error: Option, + // value: Option}`. Both slots are logical Options, so the selected + // branch is an explicit carrier and the unselected one is `None` — a + // physical null never stands in for either. match outcome { Ok(CallOutcome::Return(CallReturn::One(value))) => result(object(vec![ ("ok", Value::Bool(true)), - ("error", Value::Null), - ("value", value), + ("error", option::none_carrier()), + ("value", option::some_carrier(value)), ])), Err(VmError::HostError(code)) => result(object(vec![ ("ok", Value::Bool(false)), - ("error", failure_value(&owner, &code)), - ("value", Value::Null), + ("error", option::some_carrier(failure_value(&owner, &code))), + ("value", option::none_carrier()), ])), Err(failure) => Err(failure), Ok(_) => Err(VmError::InvalidFrameState( @@ -537,16 +559,18 @@ fn wrap_without_value( outcome: VmResult, ) -> VmResult { match outcome { + // `error` is a logical `Option`: `None` on success, + // an explicit carrier on failure. Neither branch is a physical null. Ok(CallOutcome::Return(CallReturn::One(Value::Null))) => result(object(vec![ ("ok", Value::Bool(true)), - ("error", Value::Null), + ("error", option::none_carrier()), ])), Ok(CallOutcome::Return(CallReturn::One(_))) | Ok(_) => Err(VmError::InvalidFrameState( "auth wait adapter returned an unexpected value", )), Err(VmError::HostError(code)) => result(object(vec![ ("ok", Value::Bool(false)), - ("error", failure_value(&owner, &code)), + ("error", option::some_carrier(failure_value(&owner, &code))), ])), Err(failure) => Err(failure), } @@ -621,6 +645,19 @@ mod tests { } } + /// Reads a field that the migrated contract publishes as a logical + /// `Option`: the selected `Some` carrier is unwrapped so the assertions keep + /// addressing the failure record itself rather than its carrier. + fn present_field(value: &Value, name: &str) -> Value { + let carrier = field(value, name); + let (present, payload) = rustscript_vm::option::payload(&carrier) + .unwrap_or_else(|error| panic!("{name} is not an Option carrier: {error}")); + assert!(present, "{name} must be the selected Some carrier"); + payload + .cloned() + .unwrap_or_else(|| panic!("{name} Some carrier without a payload")) + } + fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected typed RSS map, got {value:?}"); @@ -760,7 +797,7 @@ mod tests { assert_eq!(field(&started, "ok"), Value::Bool(true)); let waited = waited.expect("RSS wait result").expect("typed wait result"); assert_eq!(field(&waited, "ok"), Value::Bool(false)); - let failure = field(&waited, "error"); + let failure = present_field(&waited, "error"); assert_eq!(field(&failure, "code"), Value::string("policy_inactive")); assert_eq!(field(&failure, "cancelled"), Value::Bool(true)); assert_eq!(field(&failure, "deadline_exceeded"), Value::Bool(false)); @@ -809,7 +846,7 @@ mod tests { ) .expect("immediate wait-deadline result"); assert_eq!(field(&too_long, "ok"), Value::Bool(false)); - let wait_error = field(&too_long, "error"); + let wait_error = present_field(&too_long, "error"); assert_eq!( field(&wait_error, "code"), Value::string("wait_deadline_exceeded") @@ -825,7 +862,7 @@ mod tests { let expired = invoke(&mut vm, "now_result", std::slice::from_ref(&policy)) .expect("elapsed owner deadline is a readable result"); assert_eq!(field(&expired, "ok"), Value::Bool(false)); - let expired_error = field(&expired, "error"); + let expired_error = present_field(&expired, "error"); assert_eq!( field(&expired_error, "code"), Value::string("policy_inactive") @@ -865,7 +902,7 @@ mod tests { let revoked = invoke(&mut vm, "wait_result", &[policy.clone(), Value::Int(0)]) .expect("revoked wait result"); assert_eq!(field(&revoked, "ok"), Value::Bool(false)); - let failure = field(&revoked, "error"); + let failure = present_field(&revoked, "error"); assert_eq!(field(&failure, "code"), Value::string("policy_inactive")); assert_eq!(field(&failure, "revoked"), Value::Bool(true)); assert_eq!(field(&failure, "cancelled"), Value::Bool(false)); diff --git a/src/auth/host/flow.rs b/src/auth/host/flow.rs index 00dad81..abf8bf2 100644 --- a/src/auth/host/flow.rs +++ b/src/auth/host/flow.rs @@ -6,7 +6,7 @@ use rustscript_vm::resource::{ }; use rustscript_vm::{ CallOutcome, HostFunctionDescriptor, HostParamPassing, HostParamSchema, HostResourceType, - HostResourceTypeMeta, HostTypeSchema, ResourceTypeKey, Value, Vm, VmResult, + HostResourceTypeMeta, HostTypeSchema, ResourceTypeKey, Value, Vm, VmResult, option, }; use crate::auth::oauth::{ @@ -17,8 +17,8 @@ use crate::auth::oauth::{ use super::{ Bound, LIMIT, Owner, OwnerResource, PublicAuthorizationInstruction, TrustedTransportPolicy, - borrow, desc, envelope, error, field, handle, issue, key, named, object, opt, policy_owner, - result, string, transport_policy, + borrow, desc, error, field, fixed_envelope, handle, issue, key, logical, named, object, + policy_owner, result, string, transport_policy, }; const CALLBACK_KEY: &str = "oauth.callback"; @@ -480,7 +480,9 @@ fn public_intent_type() -> HostTypeSchema { } fn pkce_begin_envelope() -> HostTypeSchema { - envelope( + // Fixed shell, concrete resource handles: the begin call always publishes + // both the callback and the verifier handle, so neither is an `Option`. + fixed_envelope( "PkceBeginEnvelope", vec![ ("authorization_url", HostTypeSchema::String), @@ -494,11 +496,14 @@ fn pkce_begin_envelope() -> HostTypeSchema { } fn callback_envelope() -> HostTypeSchema { - envelope( + // A callback can legitimately deliver no code (a denial, a cancellation), so + // the published code handle is a logical `Option>` carrier: a + // live handle when present, a tagged `None` when not — never a null. + fixed_envelope( "CallbackEnvelope", vec![ ("outcome", HostTypeSchema::String), - ("code_handle", opt(resource(CODE_KEY))), + ("code_handle", logical(resource(CODE_KEY))), ], ) } @@ -569,7 +574,7 @@ fn close_desc( resource(resource_key), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, ); let effects = rustscript_vm::vm::host_extension::guest_resource_effects(&schema); let mut descriptor = crate::runtime::host_compose::static_stack_descriptor(schema, adapter); @@ -679,7 +684,7 @@ fn pkce_begin_call(vm: &mut Vm, args: &[Value]) -> VmResult { result(object(vec![ ("ok", Value::Bool(true)), - ("error", Value::Null), + ("error", option::none_carrier()), ("authorization_url", Value::string(begun.authorization_url)), ("redirect_uri", Value::string(begun.redirect_uri)), ("code_challenge", Value::string(begun.code_challenge)), @@ -856,9 +861,9 @@ fn callback_wait_call(vm: &mut Vm, args: &[Value]) -> VmResult { result(object(vec![ ("ok", Value::Bool(true)), - ("error", Value::Null), + ("error", option::none_carrier()), ("outcome", Value::string("success")), - ("code_handle", code_value), + ("code_handle", option::some_carrier(code_value)), ])) } diff --git a/src/auth/host/option_carrier_tests.rs b/src/auth/host/option_carrier_tests.rs new file mode 100644 index 0000000..2757daa --- /dev/null +++ b/src/auth/host/option_carrier_tests.rs @@ -0,0 +1,585 @@ +//! Round-6 carrier tests for the converted auth/oauth/config hosts. +//! +//! Every case compiles a real RSS probe against the published +//! `auth_host_catalog`, binds it through a real restricted registry, runs it in +//! a real VM, and then reads the guest-visible value back. The negative cases +//! assert the same functions fail closed (stale resource, missing credential, +//! wrong generation, malformed parameter carrier, invalid pointer) instead of +//! publishing a half-shaped envelope. + +use super::{ + AuthHost, Bound, HostHome, auth_host_catalog, config_envelope, handle, issue_owner_slot, + object, save_call, +}; +use crate::auth::oauth::{ + OAuthHost, ScriptedBrowser, ScriptedCallback, ScriptedCancel, ScriptedClock, + ScriptedHttpsTransport, +}; +use crate::auth::store::{OpaqueSecretSlot, SecretSlotKind}; +use crate::runtime::RunCancellation; +use crate::runtime::host_shapes; +use rustscript_vm::resource::ResourceCloseReason; +use rustscript_vm::{CallOutcome, CallReturn, HostTypeSchema, Value, Vm, VmError, VmResult}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +const PROBE: &str = r#" +use config; +use auth; +use oauth; + +pub fn snapshot_probe(home: resource) -> map { + let snapshot: ConfigSnapshotEnvelope = config::load_snapshot(&home); + let public_config: BoundedPublicConfig = snapshot.public_config; + let error_present: bool = match snapshot.error { Some(_) => true, None => false }; + let providers: array = public_config.providers.copy(); + let provider: ProviderEntry = providers[0].copy(); + let auth_present: bool = match provider.auth { Some(_) => true, None => false }; + let oauth_present: bool = match provider.oauth { Some(_) => true, None => false }; + let default_present: bool = match public_config.workspaces.default { Some(_) => true, None => false }; + { + ok: snapshot.ok && !error_present, + generation: snapshot.policy_generation, + credentials: snapshot.credential_refs.length, + providers: providers.length, + provider_name: provider.name, + auth_present: auth_present, + oauth_present: oauth_present, + default_present: default_present + } +} + +pub fn snapshot_policy(home: resource) -> resource { + config::load_snapshot(&home).policy_handle +} + +pub fn metadata_probe(policy: resource, credential_id: string) -> map { + let loaded: AuthMetadataEnvelope = auth::load_metadata(credential_id, &policy); + let metadata: AuthMetadata = loaded.metadata.copy(); + let error_present: bool = match loaded.error { Some(_) => true, None => false }; + let account_present: bool = match metadata.account_id { Some(_) => true, None => false }; + let refreshed_present: bool = match metadata.last_refresh_at_ms.copy() { Some(_) => true, None => false }; + { + ok: loaded.ok, + error_present: error_present, + account_present: account_present, + refreshed_present: refreshed_present, + generation: metadata.generation, + outcome: loaded.outcome + } +} + +pub fn access_probe(policy: resource, generation: int) -> map { + let issued: AccessHandleEnvelope = auth::access_handle("fixture-codex", generation, &policy); + let error_present: bool = match issued.error { Some(_) => true, None => false }; + let handle: resource = issued.handle; + auth::close_access(handle); + {ok: issued.ok, error_present: error_present} +} + +pub fn refresh_probe(policy: resource, generation: int) -> map { + let issued: RefreshHandleEnvelope = auth::refresh_handle("fixture-codex", generation, &policy); + let error_present: bool = match issued.error { Some(_) => true, None => false }; + auth::close_refresh(issued.handle); + {ok: issued.ok, error_present: error_present} +} + +pub fn now_probe(policy: resource) -> map { + let clock: OAuthNowResultEnvelope = oauth::now_ms_result(&policy); + let error_present: bool = match clock.error { Some(_) => true, None => false }; + let now: Option = clock.value; + {ok: clock.ok, error_present: error_present, now: now.unwrap_or(-1)} +} + +pub fn field_probe(policy: resource, text: string, pointer: string) -> PublicJsonFieldEnvelope { + oauth::public_json_field(&policy, text, pointer) +} + +pub fn field_result_probe(policy: resource, text: string, pointer: string) -> PublicJsonFieldResultEnvelope { + oauth::public_json_field_result(&policy, text, pointer) +} + +pub fn delete_probe(policy: resource) -> map { + let deleted: RedactedMutationResult = auth::delete("fixture-codex", &policy); + let error_present: bool = match deleted.error { Some(_) => true, None => false }; + {ok: deleted.ok, deleted: deleted.deleted, error_present: error_present} +} +"#; + +struct TestHome(PathBuf); + +impl TestHome { + fn new(label: &str) -> Self { + let base = + PathBuf::from(std::env::var("RUSTSCRIPT_AGENT_TEST_TMP").expect("assigned test temp")); + let path = base.join(format!( + "auth-option-carrier-{label}-{}-{}", + std::process::id(), + uuid::Uuid::new_v4() + )); + fs::create_dir_all(&path).expect("create private auth home"); + let fixtures = + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/config_auth_rss"); + fs::copy(fixtures.join("config.yaml"), path.join("config.yaml")) + .expect("copy config fixture"); + let auth = fs::read_to_string(fixtures.join("auth.yaml")) + .expect("read auth fixture") + .replace("1788440000000", "1900000000000"); + fs::write(path.join("auth.yaml"), auth).expect("write auth fixture"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).expect("home mode"); + for name in ["config.yaml", "auth.yaml"] { + fs::set_permissions(path.join(name), fs::Permissions::from_mode(0o600)) + .expect("fixture file mode"); + } + } + Self(path) + } + + fn path(&self) -> &Path { + &self.0 + } +} + +impl Drop for TestHome { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +fn oauth_host() -> Arc { + Arc::new(OAuthHost::new( + Arc::new(ScriptedHttpsTransport::new()), + Arc::new(ScriptedCallback::new()), + Arc::new(ScriptedBrowser::new()), + Arc::new(ScriptedClock::new(1_900_000_000_000)), + Arc::new(ScriptedCancel::new()), + )) +} + +fn vm_for(host: &AuthHost) -> Vm { + let catalog = auth_host_catalog(); + let compiled = rustscript_vm::compile_source_with_flavor_and_options( + PROBE, + rustscript_vm::SourceFlavor::RustScript, + rustscript_vm::CompileSourceFileOptions::default() + .with_host_api_catalog(Arc::clone(&catalog)), + ) + .expect("compile carrier probe against the published catalog"); + let mut vm = Vm::new_shared(Arc::new(compiled.program)); + let mut registry = rustscript_vm::HostFunctionRegistry::restricted(); + host.register(&mut registry, catalog.as_ref()) + .expect("register the published auth catalog"); + registry + .bind_vm_cached(&mut vm) + .expect("bind the restricted registry"); + assert!(matches!( + vm.run().expect("probe root frame"), + rustscript_vm::VmStatus::Halted + )); + vm +} + +fn caller(host: &AuthHost, vm: &mut Vm) -> (Value, Value) { + let bound = host.bind_vm(vm).expect("bind run owner"); + let home = field(&bound, "host_home"); + let callable = vm + .resolve_exported_callable("snapshot_policy") + .expect("snapshot export"); + let policy = vm + .invoke_callable(callable, std::slice::from_ref(&home)) + .expect("load owner policy"); + (home, policy) +} + +fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> VmResult { + let callable = vm + .resolve_exported_callable(name) + .unwrap_or_else(|_| panic!("export {name}")); + vm.invoke_callable(callable, args) +} + +fn field(value: &Value, name: &str) -> Value { + let Value::Map(entries) = value else { + panic!("expected a typed result map, got {value:?}"); + }; + entries + .iter() + .find_map(|(key, value)| match key { + Value::String(key) if key.as_ref() == name => Some(value.clone()), + _ => None, + }) + .unwrap_or_else(|| panic!("missing {name} in {value:?}")) +} + +#[test] +fn config_snapshot_publishes_logical_option_leaves_over_the_resource_shell() { + let home = TestHome::new("snapshot"); + let host = AuthHost::new( + home.path().to_path_buf(), + RunCancellation::with_timeout(Duration::from_secs(30)), + ) + .expect("auth host"); + let mut vm = vm_for(&host); + let (home_value, _) = caller(&host, &mut vm); + let snapshot = invoke(&mut vm, "snapshot_probe", &[home_value]).expect("snapshot probe"); + + assert_eq!(field(&snapshot, "ok"), Value::Bool(true)); + assert_eq!(field(&snapshot, "generation"), Value::Int(1)); + assert_eq!(field(&snapshot, "credentials"), Value::Int(1)); + assert_eq!(field(&snapshot, "providers"), Value::Int(1)); + assert_eq!( + field(&snapshot, "provider_name"), + Value::string("openai-codex") + ); + // Every nested nullable config leaf is a live Some payload, not a Null. + assert_eq!(field(&snapshot, "auth_present"), Value::Bool(true)); + assert_eq!(field(&snapshot, "oauth_present"), Value::Bool(true)); + assert_eq!(field(&snapshot, "default_present"), Value::Bool(true)); +} + +#[test] +fn config_snapshot_rejects_a_stale_home_resource() { + let home = TestHome::new("snapshot-stale"); + let host = AuthHost::new( + home.path().to_path_buf(), + RunCancellation::with_timeout(Duration::from_secs(30)), + ) + .expect("auth host"); + let mut vm = vm_for(&host); + let (home_value, _) = caller(&host, &mut vm); + let handle = handle(&home_value).expect("home handle"); + vm.host_context() + .close_resource::(handle, ResourceCloseReason::Requested) + .expect("close the bound home"); + let failure = invoke(&mut vm, "snapshot_probe", &[home_value]).expect_err("closed home"); + assert!( + matches!(&failure, VmError::HostError(code) if code == "resource_invalid" || code.contains("resource")), + "unexpected failure: {failure:?}" + ); +} + +#[test] +fn metadata_publishes_option_leaves_and_rejects_an_unknown_credential() { + let home = TestHome::new("metadata"); + let host = AuthHost::new( + home.path().to_path_buf(), + RunCancellation::with_timeout(Duration::from_secs(30)), + ) + .expect("auth host"); + let mut vm = vm_for(&host); + let (_, policy) = caller(&host, &mut vm); + let loaded = invoke( + &mut vm, + "metadata_probe", + &[policy.clone(), Value::string("fixture-codex")], + ) + .expect("metadata probe"); + assert_eq!(field(&loaded, "ok"), Value::Bool(true)); + assert_eq!(field(&loaded, "error_present"), Value::Bool(false)); + assert_eq!(field(&loaded, "account_present"), Value::Bool(true)); + assert_eq!(field(&loaded, "refreshed_present"), Value::Bool(true)); + assert_eq!(field(&loaded, "generation"), Value::Int(4)); + assert_eq!(field(&loaded, "outcome"), Value::string("loaded")); + + let failure = invoke( + &mut vm, + "metadata_probe", + &[policy, Value::string("absent-credential")], + ) + .expect_err("unknown credential"); + assert!( + matches!(&failure, VmError::HostError(code) if code == "credential_not_admitted"), + "an id that is not in the admission snapshot is refused before the store is read: {failure:?}" + ); +} + +#[test] +fn access_and_refresh_handles_publish_none_error_carriers_and_keep_live_handles() { + let home = TestHome::new("handles"); + let host = AuthHost::new( + home.path().to_path_buf(), + RunCancellation::with_timeout(Duration::from_secs(30)), + ) + .expect("auth host"); + let mut vm = vm_for(&host); + let (_, policy) = caller(&host, &mut vm); + let access = + invoke(&mut vm, "access_probe", &[policy.clone(), Value::Int(4)]).expect("access probe"); + assert_eq!(field(&access, "ok"), Value::Bool(true)); + assert_eq!(field(&access, "error_present"), Value::Bool(false)); + let refresh = + invoke(&mut vm, "refresh_probe", &[policy.clone(), Value::Int(4)]).expect("refresh probe"); + assert_eq!(field(&refresh, "ok"), Value::Bool(true)); + assert_eq!(field(&refresh, "error_present"), Value::Bool(false)); + + let stale = + invoke(&mut vm, "access_probe", &[policy, Value::Int(3)]).expect_err("wrong generation"); + assert!( + matches!(&stale, VmError::HostError(code) if code == "access_unavailable"), + "unexpected failure: {stale:?}" + ); +} + +#[test] +fn now_result_publishes_an_option_value_and_rejects_a_forged_policy() { + let home = TestHome::new("now"); + let host = AuthHost::new( + home.path().to_path_buf(), + RunCancellation::with_timeout(Duration::from_secs(30)), + ) + .expect("auth host") + .with_oauth_host(oauth_host()); + let mut vm = vm_for(&host); + let (_, policy) = caller(&host, &mut vm); + let now = invoke(&mut vm, "now_probe", &[policy]).expect("now probe"); + assert_eq!(field(&now, "ok"), Value::Bool(true)); + assert_eq!(field(&now, "error_present"), Value::Bool(false)); + assert_eq!(field(&now, "now"), Value::Int(1_900_000_000_000)); + + let forged = Value::Int(987_654); + let failure = invoke(&mut vm, "now_probe", &[forged]).expect_err("forged policy"); + assert!( + matches!( + &failure, + VmError::HostError(_) | VmError::TypeMismatch(_) | VmError::InvalidFrameState(_) + ), + "unexpected failure: {failure:?}" + ); +} + +#[test] +fn public_json_field_and_its_result_shell_expose_one_selected_option_slot() { + let home = TestHome::new("public-json"); + let host = AuthHost::new( + home.path().to_path_buf(), + RunCancellation::with_timeout(Duration::from_secs(30)), + ) + .expect("auth host"); + let mut vm = vm_for(&host); + let (_, policy) = caller(&host, &mut vm); + let body = Value::string(r#"{"expires_in":3600,"token_type":"Bearer"}"#); + let field_value = invoke( + &mut vm, + "field_probe", + &[policy.clone(), body.clone(), Value::string("/expires_in")], + ) + .expect("field probe"); + assert_eq!(field(&field_value, "kind"), Value::string("int")); + assert_eq!( + host_shapes::option_payload(&HostTypeSchema::Int, &field(&field_value, "int_value")) + .expect("int carrier"), + Some(&Value::Int(3600)) + ); + + let missing = invoke( + &mut vm, + "field_probe", + &[policy.clone(), body.clone(), Value::string("/absent")], + ) + .expect("missing pointer"); + assert_eq!(field(&missing, "kind"), Value::string("missing")); + + let result = invoke( + &mut vm, + "field_result_probe", + &[policy.clone(), body, Value::string("/token_type")], + ) + .expect("result probe"); + assert_eq!(field(&result, "ok"), Value::Bool(true)); + assert_eq!( + host_shapes::option_payload(&HostTypeSchema::String, &field(&result, "error")) + .expect("error carrier"), + None, + "a successful public_json_field_result carries Option::None for error" + ); + let payload = rustscript_vm::option::payload(&field(&result, "value")) + .expect("value carrier") + .1 + .cloned() + .expect("a selected field is Some"); + assert_eq!(field(&payload, "kind"), Value::string("string")); + assert_eq!( + host_shapes::option_payload(&HostTypeSchema::String, &field(&payload, "string_value")) + .expect("string carrier"), + Some(&Value::string("Bearer")) + ); + assert_eq!( + host_shapes::option_payload(&HostTypeSchema::Int, &field(&payload, "int_value")) + .expect("int carrier"), + None, + "an unselected slot is None, not a Null" + ); + + let failure = invoke( + &mut vm, + "field_probe", + &[policy, Value::string("{}"), Value::string("not-a-pointer")], + ) + .expect_err("invalid pointer"); + assert!( + matches!(&failure, VmError::HostError(code) if code == "public_json_pointer_invalid"), + "unexpected failure: {failure:?}" + ); +} + +#[test] +fn save_if_generation_reads_refresh_slot_as_a_logical_option() { + let home = TestHome::new("save"); + let host = AuthHost::new_for_commands( + home.path().to_path_buf(), + RunCancellation::with_timeout(Duration::from_secs(30)), + ) + .expect("commands host"); + let mut vm = vm_for(&host); + let (_, policy) = caller(&host, &mut vm); + let owner = vm + .host_context() + .scope_state::() + .map(|bound| Arc::clone(&bound.0)) + .expect("bound owner"); + let slot_handle = OpaqueSecretSlot::from_host_secret_until( + SecretSlotKind::Access, + "fixture-codex", + 4, + owner.snapshot.generation, + &owner.identity, + zeroize::Zeroizing::new(b"synthetic-access".to_vec()), + Instant::now() + Duration::from_secs(60), + ) + .expect("host slot"); + let access_slot = issue_owner_slot( + &mut vm, + &owner, + "fixture-codex", + 4, + SecretSlotKind::Access, + slot_handle, + ) + .expect("issue access slot"); + let metadata = object(vec![ + ("provider", Value::string("openai-codex")), + ("kind", Value::string("oauth")), + ("source", Value::string("carrier-test")), + ("token_type", Value::string("Bearer")), + ("expires_at_ms", Value::Int(1_900_000_000_000)), + ("scopes", Value::array(Vec::new())), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string("acct")), + ), + ("generation", Value::Int(4)), + ("status", Value::string("active")), + ( + "last_refresh_at_ms", + rustscript_vm::option::some_carrier(Value::Int(1_790_000_000_000)), + ), + ("has_refresh_token", Value::Bool(true)), + ]); + let request = |refresh_action: &str, refresh_slot: Value| { + object(vec![ + ("credential_id", Value::string("fixture-codex")), + ("expected_generation", Value::Int(4)), + ("policy_handle", policy.clone()), + ("metadata", metadata.clone()), + ("access_slot", access_slot.clone()), + ("refresh_action", Value::string(refresh_action)), + ("refresh_slot", refresh_slot), + ]) + }; + + // `replace` with a None carrier cannot name a slot: fail closed. + let failure = save_call( + &mut vm, + &[request("replace", rustscript_vm::option::none_carrier())], + ) + .expect_err("replace without a slot"); + assert!( + matches!(&failure, VmError::HostError(code) if code == "slot_invalid"), + "unexpected failure: {failure:?}" + ); + + // A physical null in the Option slot is not a carrier at all. + let failure = + save_call(&mut vm, &[request("replace", Value::Null)]).expect_err("null is not a carrier"); + assert!( + matches!(&failure, VmError::HostError(code) if code == "invalid_request"), + "unexpected failure: {failure:?}" + ); + + // A `None` carrier with `preserve` is the normal path. + let saved = save_call( + &mut vm, + &[request("preserve", rustscript_vm::option::none_carrier())], + ) + .expect("preserve save"); + let CallOutcome::Return(CallReturn::One(saved)) = saved else { + panic!("save must return one envelope"); + }; + assert_eq!(field(&saved, "ok"), Value::Bool(true)); + let envelope_error = field(&saved, "error"); + assert_eq!( + host_shapes::option_payload(&HostTypeSchema::String, &envelope_error) + .expect("error carrier"), + None, + "a successful save carries Option::None for error" + ); +} + +#[test] +fn delete_publishes_a_none_error_and_a_true_deleted_flag() { + let home = TestHome::new("delete"); + let host = AuthHost::new_for_commands( + home.path().to_path_buf(), + RunCancellation::with_timeout(Duration::from_secs(30)), + ) + .expect("commands host"); + let mut vm = vm_for(&host); + let (_, policy) = caller(&host, &mut vm); + let deleted = + invoke(&mut vm, "delete_probe", std::slice::from_ref(&policy)).expect("delete probe"); + assert_eq!(field(&deleted, "ok"), Value::Bool(true)); + assert_eq!(field(&deleted, "deleted"), Value::Bool(true)); + assert_eq!(field(&deleted, "error_present"), Value::Bool(false)); + + let failure = invoke( + &mut vm, + "metadata_probe", + &[policy, Value::string("fixture-codex")], + ) + .expect_err("deleted credential is gone"); + assert!( + matches!(&failure, VmError::HostError(code) if code == "credential_unavailable"), + "unexpected failure: {failure:?}" + ); +} + +#[test] +fn legacy_null_config_value_is_converted_field_by_field() { + // The walker is driven by the declared schema, not by the data: a null + // in a declared Option slot becomes None, a nested Option keeps its + // carrier, and an undecidable null in a plain leaf is refused. + let schema = config_envelope(); + let HostTypeSchema::Named { fields, .. } = &schema else { + panic!("config envelope is a named shape"); + }; + assert!( + fields + .iter() + .all(|field| !matches!(field.ty, HostTypeSchema::Optional(_))), + "the converted envelope keeps no legacy nullable slot" + ); + let error_slot = fields + .iter() + .find(|field| field.name == "error") + .expect("error field"); + assert!(matches!(error_slot.ty, HostTypeSchema::Option(_))); + assert!( + host_shapes::logical_from_legacy(&HostTypeSchema::String, &Value::Null).is_err(), + "a physical null cannot decide a non-Option leaf" + ); +} diff --git a/src/auth/host/paths.rs b/src/auth/host/paths.rs index 87b34f2..82c78f1 100644 --- a/src/auth/host/paths.rs +++ b/src/auth/host/paths.rs @@ -174,6 +174,15 @@ pub(super) fn home_meta() -> HostResourceTypeMeta { HostResourceTypeMeta::of::() } +/// Fixed `{ok, error: Option, value: Option}` shell. +/// The descriptor and the adapter share it so they cannot drift apart. +fn config_path_result_type() -> rustscript_vm::HostTypeSchema { + super::fallible::result_type( + "ConfigPathResultEnvelope", + rustscript_vm::HostTypeSchema::String, + ) +} + pub(super) fn resolved_path_result_desc() -> rustscript_vm::HostFunctionDescriptor { desc( PATH_IMPORT_NAME, @@ -182,10 +191,7 @@ pub(super) fn resolved_path_result_desc() -> rustscript_vm::HostFunctionDescript key(RESOURCE_KEY), HostParamPassing::Borrow, )], - super::fallible::result_type( - "ConfigPathResultEnvelope", - rustscript_vm::HostTypeSchema::String, - ), + config_path_result_type(), resolved_path_result_call, ) } @@ -240,11 +246,14 @@ fn resolved_path_result_call(vm: &mut Vm, args: &[Value]) -> VmResult Result { @@ -255,17 +264,21 @@ fn resource_handle(value: &Value) -> Result { } fn failure(code: &str, flags: OwnerFlags) -> VmResult { + // The envelope's `error`/`value` slots are logical Options; a failure is + // `Some(failure)` with no value, and `ok: false` never accompanies `None`. result(object(vec![ ("ok", Value::Bool(false)), ( "error", - object(vec![ + rustscript_vm::option::some_carrier(object(vec![ ("code", Value::string(code)), ("cancelled", Value::Bool(flags.cancelled)), ("deadline_exceeded", Value::Bool(flags.deadline_exceeded)), ("revoked", Value::Bool(flags.revoked)), - ]), + ("transport_phase", rustscript_vm::option::none_carrier()), + ("transport_category", rustscript_vm::option::none_carrier()), + ])), ), - ("value", Value::Null), + ("value", rustscript_vm::option::none_carrier()), ])) } diff --git a/src/auth/host/payload.rs b/src/auth/host/payload.rs index 0ff18e5..fe48436 100644 --- a/src/auth/host/payload.rs +++ b/src/auth/host/payload.rs @@ -38,6 +38,9 @@ fn selector(value: Option<&Value>) -> VmResult<&str> { fn optional_selector(value: Option<&Value>) -> VmResult> { match value { None | Some(Value::Null) => Ok(None), + // Legacy guests spell "no selector" as an empty string; treat it the + // same way as an absent one instead of rejecting the whole intent. + Some(Value::String(text)) if text.is_empty() => Ok(None), Some(value) => selector(Some(value)).map(Some), } } diff --git a/src/auth/host/public_json.rs b/src/auth/host/public_json.rs index 0958311..0b63bc6 100644 --- a/src/auth/host/public_json.rs +++ b/src/auth/host/public_json.rs @@ -1,5 +1,6 @@ use rustscript_vm::{ CallOutcome, HostFunctionDescriptor, HostResourceType, HostTypeSchema, Value, Vm, VmResult, + option, }; use serde::Deserializer; use serde::de::{self, DeserializeSeed, MapAccess, SeqAccess, Visitor}; @@ -8,7 +9,8 @@ use std::cell::Cell; use std::collections::BTreeSet; use std::fmt; -use super::{Policy, borrow, desc, error, key, named, object, opt, param, result}; +use super::{Policy, borrow, desc, error, key, logical, named, object, param}; +use crate::runtime::host_shapes; const MAX_JSON_BYTES: usize = 65_536; const MAX_JSON_DEPTH: usize = 64; @@ -440,15 +442,17 @@ fn resolve_pointer<'a>(document: &'a Json, tokens: &[String]) -> Option<&'a Json } pub(super) fn public_json_field_type() -> HostTypeSchema { + // Exactly one value slot is selected by `kind`; the others are logical + // `Option` carriers built by the adapter, never a physical null. named( "PublicJsonFieldEnvelope", vec![ ("kind", HostTypeSchema::String), - ("string_value", opt(HostTypeSchema::String)), - ("int_value", opt(HostTypeSchema::Int)), - ("float_value", opt(HostTypeSchema::Float)), - ("bool_value", opt(HostTypeSchema::Bool)), - ("length", opt(HostTypeSchema::Int)), + ("string_value", logical(HostTypeSchema::String)), + ("int_value", logical(HostTypeSchema::Int)), + ("float_value", logical(HostTypeSchema::Float)), + ("bool_value", logical(HostTypeSchema::Bool)), + ("length", logical(HostTypeSchema::Int)), ], ) } @@ -468,31 +472,32 @@ pub(super) fn public_json_field_desc() -> HostFunctionDescriptor { fn envelope( kind: &str, - string_value: Value, - int_value: Value, - float_value: Value, - bool_value: Value, - length: Value, + string_value: Option, + int_value: Option, + float_value: Option, + bool_value: Option, + length: Option, ) -> Value { object(vec![ ("kind", Value::string(kind)), - ("string_value", string_value), - ("int_value", int_value), - ("float_value", float_value), - ("bool_value", bool_value), - ("length", length), + ("string_value", carrier(string_value)), + ("int_value", carrier(int_value)), + ("float_value", carrier(float_value)), + ("bool_value", carrier(bool_value)), + ("length", carrier(length)), ]) } +/// The selected field value becomes `Some`; every unselected field is `None`. +fn carrier(value: Option) -> Value { + match value { + Some(value) => option::some_carrier(value), + None => option::none_carrier(), + } +} + fn empty_envelope(kind: &str) -> Value { - envelope( - kind, - Value::Null, - Value::Null, - Value::Null, - Value::Null, - Value::Null, - ) + envelope(kind, None, None, None, None, None) } pub(super) fn public_json_field_call(vm: &mut Vm, args: &[Value]) -> VmResult { @@ -519,33 +524,14 @@ pub(super) fn public_json_field_call(vm: &mut Vm, args: &[Value]) -> VmResult MAX_JSON_BYTES { return Err(error("public_json_too_large")); } - envelope( - "string", - Value::string(value), - Value::Null, - Value::Null, - Value::Null, - Value::Null, - ) + envelope("string", Some(Value::string(value)), None, None, None, None) + } + Json::Bool(value) => { + envelope("bool", None, None, None, Some(Value::Bool(*value)), None) } - Json::Bool(value) => envelope( - "bool", - Value::Null, - Value::Null, - Value::Null, - Value::Bool(*value), - Value::Null, - ), Json::Number(value) => { if let Some(number) = value.as_i64() { - envelope( - "int", - Value::Null, - Value::Int(number), - Value::Null, - Value::Null, - Value::Null, - ) + envelope("int", None, Some(Value::Int(number)), None, None, None) } else if value.is_u64() { return Err(error("public_json_number_out_of_range")); } else { @@ -553,45 +539,25 @@ pub(super) fn public_json_field_call(vm: &mut Vm, args: &[Value]) -> VmResult { let length = i64::try_from(fields.len()) .map_err(|_| error("public_json_number_out_of_range"))?; - envelope( - "object", - Value::Null, - Value::Null, - Value::Null, - Value::Null, - Value::Int(length), - ) + envelope("object", None, None, None, None, Some(Value::Int(length))) } Json::Array(values) => { let length = i64::try_from(values.len()) .map_err(|_| error("public_json_number_out_of_range"))?; - envelope( - "array", - Value::Null, - Value::Null, - Value::Null, - Value::Null, - Value::Int(length), - ) + envelope("array", None, None, None, None, Some(Value::Int(length))) } } } }; owner.check()?; - result(output) + let schema = public_json_field_type(); + host_shapes::shaped(vm, &schema, output) } #[cfg(test)] diff --git a/src/auth/host/stream.rs b/src/auth/host/stream.rs index 48eccfb..fe6319a 100644 --- a/src/auth/host/stream.rs +++ b/src/auth/host/stream.rs @@ -13,7 +13,7 @@ use std::sync::{Arc, Mutex}; use rustscript_vm::resource::{CloseProgress, HostResource, ResourceCloseReason, ResourceResult}; use rustscript_vm::{ CallOutcome, HostFunctionDescriptor, HostParamPassing, HostParamSchema, HostResourceType, - HostResourceTypeMeta, HostTypeSchema, ResourceTypeKey, Value, Vm, VmError, VmResult, + HostResourceTypeMeta, HostTypeSchema, ResourceTypeKey, Value, Vm, VmError, VmResult, option, }; use crate::auth::authenticated_stream::{SanitizedAccessSseStream, SanitizedSseRecord}; @@ -22,8 +22,9 @@ use crate::auth::transport_failure::{PHASE_READ, PHASE_START, TransportFailureDe use super::flow; use super::{ Bound, Owner, OwnerResource, TransportCredential, admit_transport, array, borrow, desc, error, - field, handle, issue, key, named, object, opt, param, policy_owner, policy_owner_for_cleanup, - result, string, transport_credential, transport_request_type, transport_use_type, + field, handle, issue, key, logical, named, object, param, policy_owner, + policy_owner_for_cleanup, result, string, transport_credential, transport_request_type, + transport_use_type, }; /// Success envelope for a stream host call. @@ -141,7 +142,7 @@ fn head_type() -> HostTypeSchema { vec![ ("status", HostTypeSchema::Int), ("content_type", HostTypeSchema::String), - ("retry_after_ms", opt(HostTypeSchema::Int)), + ("retry_after_ms", logical(HostTypeSchema::Int)), ], ) } @@ -159,7 +160,7 @@ fn record_type() -> HostTypeSchema { vec![ ("kind", HostTypeSchema::String), ("json", HostTypeSchema::String), - ("literal_index", opt(HostTypeSchema::Int)), + ("literal_index", logical(HostTypeSchema::Int)), ], ) } @@ -194,7 +195,7 @@ pub(super) fn stream_close_desc() -> HostFunctionDescriptor { key(Stream::KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, stream_close_call, ) } @@ -301,10 +302,7 @@ fn stream_start_call(vm: &mut Vm, args: &[Value]) -> Result Result` leaf: a `Some` carrier for a present number, the +/// tagged `None` carrier when the wire carried no value. Never a physical null. +fn optional_int(value: Option) -> Value { + match value { + Some(number) => option::some_carrier(Value::Int(number)), + None => option::none_carrier(), + } +} + fn record_value(record: StreamRecord) -> Value { match record { StreamRecord::Json(json) => object(vec![ ("kind", Value::string("json")), ("json", Value::string(json)), - ("literal_index", Value::Null), + ("literal_index", option::none_carrier()), ]), StreamRecord::Literal(index) => object(vec![ ("kind", Value::string("literal")), ("json", Value::string("")), - ( - "literal_index", - i64::try_from(index).map(Value::Int).unwrap_or(Value::Null), - ), + ("literal_index", optional_int(i64::try_from(index).ok())), ]), StreamRecord::Eof => object(vec![ ("kind", Value::string("eof")), ("json", Value::string("")), - ("literal_index", Value::Null), + ("literal_index", option::none_carrier()), ]), } } diff --git a/src/auth/host/timing.rs b/src/auth/host/timing.rs index 495220d..abd4bf8 100644 --- a/src/auth/host/timing.rs +++ b/src/auth/host/timing.rs @@ -31,7 +31,7 @@ pub(super) fn wait_ms_desc() -> HostFunctionDescriptor { borrow("policy_handle", key(Policy::KEY)), param("millis", HostTypeSchema::Int), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, wait_ms_call, ) } diff --git a/src/auth/host/transients.rs b/src/auth/host/transients.rs index a951bee..0c6afa3 100644 --- a/src/auth/host/transients.rs +++ b/src/auth/host/transients.rs @@ -201,7 +201,7 @@ pub(super) fn cleanup_desc() -> HostFunctionDescriptor { desc( "auth::cleanup_transients", vec![borrow("policy_handle", key(Policy::KEY))], - HostTypeSchema::Null, + HostTypeSchema::Unit, cleanup_call, ) } diff --git a/src/auth/host/transport_rollback_tests.rs b/src/auth/host/transport_rollback_tests.rs new file mode 100644 index 0000000..0e23d1c --- /dev/null +++ b/src/auth/host/transport_rollback_tests.rs @@ -0,0 +1,343 @@ +//! Round-8 regression tests for the converted `oauth::transport` family. +//! +//! Two things are proven here, both through a real compiled RSS source, the +//! published `auth_host_catalog`, a real restricted registry and a real VM: +//! +//! * the response slots are logical `Option>` carriers whose `Some` +//! payload is still a **live** handle (the guest closes it, which only works +//! for a live resource of that exact type) and whose `None` is a tag, not a +//! null; +//! * when the adapter issues handles and then fails afterwards — here the +//! exchange pair cannot be published because the resource table is full — the +//! rollback inside the issuing scope closes what it already issued and the +//! typed error code survives. + +use super::{AuthHost, LIMIT, auth_host_catalog}; +use crate::auth::oauth::{ + OAuthHost, ScriptedBrowser, ScriptedCallback, ScriptedCancel, ScriptedClock, + ScriptedHttpsTransport, +}; +use crate::runtime::RunCancellation; +use rustscript_vm::{Value, Vm, VmError, VmResult}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::time::Duration; + +const PROBE: &str = r#" +use config; +use auth; +use oauth; + +pub fn policy(home: resource) -> resource { + config::load_snapshot(&home).policy_handle +} + +/// Issue one live access handle and hand it back, so the resource table fills +/// up deterministically from real host calls and the caller keeps it live. +pub fn pin_access(policy: resource) -> resource { + auth::access_handle("fixture-codex", 4, &policy).handle +} + +pub fn start_device(policy: resource) -> TransportResponseEnvelope { + let request: TransportPublicRequest = { + policy_handle: policy, + credential_id: "fixture-codex", + method: "POST", + path: "/api/accounts/deviceauth/usercode", + public_headers: [{name: "content-type", value: "application/json"}], + public_body: "{\"client_id\":\"public-client-id\"}" + }; + let usage: TransportCredentialUse = {kind: "none"}; + oauth::transport(&request, &usage) +} + +/// A capture that publishes an exchange pair, i.e. it issues the two secret +/// slots first and only then has to publish the pair. +pub fn capture(policy: resource, device: resource) -> TransportCaptureEnvelope { + let request: TransportPublicRequest = { + policy_handle: policy, + credential_id: "fixture-codex", + method: "POST", + path: "/oauth/token", + public_headers: [{name: "content-type", value: "application/json"}], + public_body: "{\"grant_type\":\"device_code\"}" + }; + let usage: TransportCredentialUse = {kind: "device_session", device_handle: Some(device)}; + let payload: TransportPayloadIntent = { + encoding: "json_object", + secret_fields: "device", + device_field: "device_id" + }; + let capture: TransportCaptureIntent = { + code_field: "authorization_code", + redirect_uri: "https://auth.openai.com/deviceauth/callback" + }; + oauth::transport_capture(&request, &usage, payload, capture) +} + +/// One plain token transport that issues both secret slots (access and refresh) +/// and returns only the status, so the caller never touches the handles. +pub fn transport_status(policy: resource) -> int { + let request: TransportPublicRequest = { + policy_handle: policy, + credential_id: "fixture-codex", + method: "POST", + path: "/oauth/token", + public_headers: [{name: "content-type", value: "application/json"}], + public_body: "{\"grant_type\":\"authorization_code\"}" + }; + let usage: TransportCredentialUse = {kind: "none"}; + let response: TransportResponseEnvelope = oauth::transport(&request, &usage); + response.status +} + +/// Closes one published secret slot. The close only succeeds for a live +/// resource of this exact type, so it is the proof that the `Some` payload is +/// still a handle rather than text or a bare number. +fn close_one_slot(slot: resource) -> bool { + auth::close_slot(slot); + true +} + +fn close_one_device(device: resource) -> bool { + oauth::close_device(device); + true +} + +/// Reads the converted response the way the guest has to: an exhaustive Option +/// match per slot. The envelope carries live resources, so it is move-only — +/// every field is consumed exactly once and nothing is copied. +pub fn transport_frame(home: resource) -> map { + let policy: resource = config::load_snapshot(&home).policy_handle; + let request: TransportPublicRequest = { + policy_handle: policy, + credential_id: "fixture-codex", + method: "POST", + path: "/api/accounts/deviceauth/usercode", + public_headers: [{name: "content-type", value: "application/json"}], + public_body: "{\"client_id\":\"public-client-id\"}" + }; + let usage: TransportCredentialUse = {kind: "none"}; + let response: TransportResponseEnvelope = oauth::transport(&request, &usage); + let error_present: bool = match response.error { Some(_) => true, None => false }; + let retry_present: bool = match response.retry_after_ms { Some(_) => true, None => false }; + let access_closed: bool = match response.access_slot { + Some(slot) => close_one_slot(slot), + None => false + }; + let device_closed: bool = match response.device_handle { + Some(device) => close_one_device(device), + None => false + }; + { + ok: response.ok, + error_present: error_present, + retry_present: retry_present, + status: response.status, + body: response.body_without_secret_fields, + access_closed: access_closed, + device_closed: device_closed + } +} +"#; + +struct TestHome(PathBuf); + +impl TestHome { + fn new(label: &str) -> Self { + let base = + PathBuf::from(std::env::var("RUSTSCRIPT_AGENT_TEST_TMP").expect("assigned test temp")); + let path = base.join(format!( + "auth-transport-rollback-{label}-{}-{}", + std::process::id(), + uuid::Uuid::new_v4() + )); + fs::create_dir_all(&path).expect("create private auth home"); + let fixtures = + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/config_auth_rss"); + fs::copy(fixtures.join("config.yaml"), path.join("config.yaml")) + .expect("copy config fixture"); + let auth = fs::read_to_string(fixtures.join("auth.yaml")) + .expect("read auth fixture") + .replace("1788440000000", "1900000000000"); + fs::write(path.join("auth.yaml"), auth).expect("write auth fixture"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).expect("home mode"); + for name in ["config.yaml", "auth.yaml"] { + fs::set_permissions(path.join(name), fs::Permissions::from_mode(0o600)) + .expect("fixture file mode"); + } + } + Self(path) + } + + fn path(&self) -> &Path { + &self.0 + } +} + +impl Drop for TestHome { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +fn vm_for(host: &AuthHost) -> Vm { + let catalog = auth_host_catalog(); + let compiled = rustscript_vm::compile_source_with_flavor_and_options( + PROBE, + rustscript_vm::SourceFlavor::RustScript, + rustscript_vm::CompileSourceFileOptions::default() + .with_host_api_catalog(Arc::clone(&catalog)), + ) + .expect("compile the transport probe against the published catalog"); + let mut vm = Vm::new_shared(Arc::new(compiled.program)); + let mut registry = rustscript_vm::HostFunctionRegistry::restricted(); + host.register(&mut registry, catalog.as_ref()) + .expect("register the published auth catalog"); + registry + .bind_vm_cached(&mut vm) + .expect("bind the restricted registry"); + assert!(matches!( + vm.run().expect("probe root frame"), + rustscript_vm::VmStatus::Halted + )); + vm +} + +fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> VmResult { + let callable = vm + .resolve_exported_callable(name) + .unwrap_or_else(|_| panic!("export {name}")); + vm.invoke_callable(callable, args) +} + +fn field(value: &Value, name: &str) -> Value { + let Value::Map(entries) = value else { + panic!("expected a typed result map, got {value:?}"); + }; + entries + .iter() + .find_map(|(key, value)| match key { + Value::String(key) if key.as_ref() == name => Some(value.clone()), + _ => None, + }) + .unwrap_or_else(|| panic!("missing {name} in {value:?}")) +} + +/// The device-start wire body the fixture's `codex-device` flow expects. +const DEVICE_BODY: &str = "{\"device_code\":\"DEVICE-PRIVATE\",\"user_code\":\"TEST-USER\",\"verification_uri\":\"https://auth.openai.com/device\",\"expires_in\":900,\"interval\":5}"; + +/// A token response that carries one fresh access and one fresh refresh secret, +/// i.e. two slots the adapter has to issue in the same call. +const TOKEN_BODY: &str = "{\"access_token\":\"ACCESS-PRIVATE\",\"refresh_token\":\"REFRESH-PRIVATE\",\"token_type\":\"Bearer\",\"public\":\"refreshed\"}"; + +fn scripted_host( + home: &TestHome, + cancellation: RunCancellation, + transport: Arc, +) -> Arc { + let oauth = Arc::new(OAuthHost::new( + transport, + Arc::new(ScriptedCallback::new()), + Arc::new(ScriptedBrowser::new()), + Arc::new(ScriptedClock::new(1_900_000_000_000)), + Arc::new(ScriptedCancel::new()), + )); + Arc::new( + AuthHost::new(home.path().to_path_buf(), cancellation) + .expect("auth host") + .with_oauth_host(oauth), + ) +} + +fn bind_policy(host: &AuthHost, vm: &mut Vm) -> Value { + let bound = host.bind_vm(vm).expect("bind run owner"); + let home = field(&bound, "host_home"); + invoke(vm, "policy", &[home]).expect("owner policy") +} + +#[test] +fn transport_response_slots_are_live_handles_behind_option_carriers() { + let home = TestHome::new("shape"); + let transport = Arc::new(ScriptedHttpsTransport::new()); + transport.push_raw(200, DEVICE_BODY); + let host = scripted_host( + &home, + RunCancellation::with_timeout(Duration::from_secs(30)), + transport, + ); + let mut vm = vm_for(&host); + let bound = host.bind_vm(&mut vm).expect("bind run owner"); + let home_value = field(&bound, "host_home"); + + let frame = invoke(&mut vm, "transport_frame", &[home_value]).expect("transport frame"); + + assert_eq!(field(&frame, "ok"), Value::Bool(true)); + assert_eq!(field(&frame, "status"), Value::Int(200)); + // The success branch carries `None` carriers, not nulls. + assert_eq!(field(&frame, "error_present"), Value::Bool(false)); + assert_eq!(field(&frame, "retry_present"), Value::Bool(false)); + // The device slot was a `Some`, and the guest closed it as a *live* + // `oauth.device_session`: the payload crossed as a handle, not as text or a + // plain int. A device-only response issues no secret slots, so the access + // slot matched `None`. + assert_eq!( + field(&frame, "device_closed"), + Value::Bool(true), + "frame: {frame:?}" + ); + assert_eq!(field(&frame, "access_closed"), Value::Bool(false)); + // No private value from the provider body leaks into the public frame. + let body = field(&frame, "body"); + let Value::String(body) = body else { + panic!("body is not text: {body:?}"); + }; + assert!(!body.contains("DEVICE-PRIVATE"), "{body}"); +} + +#[test] +fn transport_rollback_closes_the_slot_it_issued_before_the_failure() { + let home = TestHome::new("rollback"); + let transport = Arc::new(ScriptedHttpsTransport::new()); + transport.push_raw(200, TOKEN_BODY); + let host = scripted_host( + &home, + RunCancellation::with_timeout(Duration::from_secs(30)), + transport, + ); + let mut vm = vm_for(&host); + let policy = bind_policy(&host, &mut vm); + + // Fill the resource table with real, live access handles until exactly one + // more handle fits. The response issues its access slot into that last slot + // and then fails issuing the refresh slot, so a handle really was issued + // before the failure — and the rollback scope has to close it. + let mut pinned = Vec::new(); + while vm.host_context().resource_count() < LIMIT - 1 { + pinned.push( + invoke(&mut vm, "pin_access", std::slice::from_ref(&policy)) + .expect("pin an access handle"), + ); + } + let before = vm.host_context().resource_count(); + assert_eq!(before, LIMIT - 1, "exactly one handle still fits"); + + let failure = invoke(&mut vm, "transport_status", &[policy]).expect_err( + "an issue that runs out of resource table must fail the call, not publish a half response", + ); + assert!( + matches!(&failure, VmError::HostError(code) if code == "resource_limit"), + "unexpected failure: {failure:?}" + ); + // The access slot issued inside this call was closed by the rollback scope, + // so the table is exactly where it was before the call. + assert_eq!( + vm.host_context().resource_count(), + before, + "the handle issued before the failure must be closed, not leaked" + ); +} diff --git a/src/auth/host/workspace.rs b/src/auth/host/workspace.rs index 6a6b79c..48342b8 100644 --- a/src/auth/host/workspace.rs +++ b/src/auth/host/workspace.rs @@ -20,7 +20,7 @@ use std::sync::Arc; use rustscript_vm::resource::{CloseProgress, HostResource, ResourceCloseReason, ResourceResult}; use rustscript_vm::{ CallOutcome, HostFunctionDescriptor, HostParamPassing, HostParamSchema, HostResourceType, - HostResourceTypeMeta, HostTypeSchema, Value, Vm, VmResult, + HostResourceTypeMeta, HostTypeSchema, Value, Vm, VmResult, option, }; use super::{Owner, Policy, error, field, handle, natural, object, string}; @@ -341,20 +341,26 @@ fn selection_type() -> HostTypeSchema { } fn result_type() -> HostTypeSchema { + // Logical `Option` carriers: an absent leaf is the tagged `None` carrier, + // never a physical null. The directory slot keeps its live handle inside the + // carrier, so nothing here is serialized. super::named( "WorkspaceOpenResult", vec![ ("ok", HostTypeSchema::Bool), ( "error", - super::opt(super::named( + super::logical(super::named( "WorkspaceHostError", vec![("code", HostTypeSchema::String)], )), ), - ("directory", super::opt(super::key(WorkspaceDirectory::KEY))), - ("canonical_path", super::opt(HostTypeSchema::String)), - ("policy_generation", super::opt(HostTypeSchema::Int)), + ( + "directory", + super::logical(super::key(WorkspaceDirectory::KEY)), + ), + ("canonical_path", super::logical(HostTypeSchema::String)), + ("policy_generation", super::logical(HostTypeSchema::Int)), ], ) } @@ -366,10 +372,10 @@ fn workspace_error(code: &str) -> Value { fn denied(code: &str) -> Value { object(vec![ ("ok", Value::Bool(false)), - ("directory", Value::Null), - ("canonical_path", Value::Null), - ("policy_generation", Value::Null), - ("error", workspace_error(code)), + ("directory", option::none_carrier()), + ("canonical_path", option::none_carrier()), + ("policy_generation", option::none_carrier()), + ("error", option::some_carrier(workspace_error(code))), ]) } @@ -465,13 +471,16 @@ fn open_shared( Ok(( object(vec![ ("ok", Value::Bool(true)), - ("directory", issued), - ("canonical_path", Value::string(canonical)), + ("directory", option::some_carrier(issued)), + ( + "canonical_path", + option::some_carrier(Value::string(canonical)), + ), ( "policy_generation", - Value::Int(owner.snapshot.generation as i64), + option::some_carrier(Value::Int(owner.snapshot.generation as i64)), ), - ("error", Value::Null), + ("error", option::none_carrier()), ]), None, )) @@ -515,7 +524,7 @@ pub fn close_descriptor() -> HostFunctionDescriptor { super::key(WorkspaceDirectory::KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_call, ) } @@ -710,7 +719,7 @@ mod tests { } } - fn typed_open_vm(host: &AuthHost) -> (Vm, Value) { + fn compile_open_fixture() -> Arc { let source_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/agent/workspace.rss"); let catalog = auth_host_catalog(); let compiled = compile_source_at_path_with_flavor_and_options( @@ -720,7 +729,18 @@ mod tests { CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()), ) .expect("compile restricted native workspace call"); - let mut vm = Vm::new_shared(Arc::new(compiled.program)); + Arc::new(compiled.program) + } + + fn typed_open_vm(host: &AuthHost) -> (Vm, Value) { + let mut vm = prepare_open_vm(host); + let home = bind_open_owner(host, &mut vm); + (vm, home) + } + + fn prepare_open_vm(host: &AuthHost) -> Vm { + let mut vm = Vm::new_shared(compile_open_fixture()); + let catalog = auth_host_catalog(); let mut registry = HostFunctionRegistry::restricted(); host.register(&mut registry, catalog.as_ref()) .expect("install production auth/workspace host"); @@ -728,11 +748,14 @@ mod tests { .bind_vm_cached(&mut vm) .expect("bind restricted host"); assert!(matches!(vm.run().expect("RSS root"), VmStatus::Halted)); - let context = host.bind_vm(&mut vm).expect("bind native owner"); - let home = map_field(&context, "host_home") + vm + } + + fn bind_open_owner(host: &AuthHost, vm: &mut Vm) -> Value { + let context = host.bind_vm(vm).expect("bind native owner"); + map_field(&context, "host_home") .expect("typed host_home resource") - .clone(); - (vm, home) + .clone() } fn map_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { @@ -762,7 +785,13 @@ mod tests { ) -> Arc { let result = invoke_open(vm, home, path); assert!(matches!(map_field(&result, "ok"), Some(Value::Bool(true)))); - let directory = map_field(&result, "directory").expect("typed directory resource"); + // `directory` is a logical `Option>`: only the selected + // `Some` payload is the live handle the arena can adopt. + let carrier = map_field(&result, "directory").expect("typed directory carrier"); + let (present, payload) = rustscript_vm::option::payload(carrier) + .expect("directory is a canonical Option carrier"); + assert!(present, "admitted open must select the directory"); + let directory = payload.expect("Some directory carrier without a payload"); host.take_workspace_directory(vm, directory) .expect("adopt native resource while VM arena is live") } @@ -948,8 +977,15 @@ mod tests { let result = invoke_open(&mut vm, &home, &fixture.workspace.join("escape")); assert!(matches!(map_field(&result, "ok"), Some(Value::Bool(false)))); - let code = map_field(map_field(&result, "error").expect("typed denial"), "code") - .expect("error code"); + let failure_carrier = map_field(&result, "error").expect("typed denial carrier"); + let (failure_present, failure_payload) = + rustscript_vm::option::payload(failure_carrier).expect("failure Option carrier"); + assert!(failure_present, "a denied open must select its failure"); + let code = map_field( + failure_payload.expect("Some failure carrier without a payload"), + "code", + ) + .expect("error code"); assert!(matches!(code, Value::String(code) if code.as_ref() == "workspace_denied")); assert_eq!( std::fs::read(outside.join("sentinel.txt")).expect("outside sentinel"), @@ -972,7 +1008,11 @@ mod tests { let (mut origin, home) = typed_open_vm(&host); let (mut foreign, _) = typed_open_vm(&host); let result = invoke_open(&mut origin, &home, &fixture.workspace); - let directory = map_field(&result, "directory").expect("typed directory resource"); + let carrier = map_field(&result, "directory").expect("typed directory carrier"); + let (present, payload) = + rustscript_vm::option::payload(carrier).expect("directory Option carrier"); + assert!(present, "admitted open must select the directory"); + let directory = payload.expect("Some directory carrier without a payload"); assert!( host.take_workspace_directory(&mut origin, directory) @@ -991,14 +1031,19 @@ mod tests { #[test] fn workspace_grant_expires_with_its_finite_owner_deadline() { let fixture = Fixture::new("deadline"); + let setup = RunCancellation::with_timeout(Duration::from_secs(30)); + let mut host = AuthHost::new(fixture.home.clone(), setup).expect("empty-credential host"); + let mut vm = prepare_open_vm(&host); + + // Start the lease at owner binding, which captures the immutable deadline. let cancellation = RunCancellation::with_timeout(Duration::from_millis(500)); - let host = - AuthHost::new(fixture.home.clone(), cancellation).expect("empty-credential host"); - let (mut vm, home) = typed_open_vm(&host); + host.cancellation = cancellation.clone(); + let home = bind_open_owner(&host, &mut vm); let grant = admitted_grant(&host, &mut vm, &home, &fixture.workspace); assert!(grant.check().is_ok()); - std::thread::sleep(Duration::from_millis(600)); + // Let the same original deadline pass without requesting cancellation. + std::thread::sleep(cancellation.remaining_deadline().unwrap()); assert_eq!( grant.check().expect_err("expired workspace grant"), "workspace_unauthorized" diff --git a/src/auth/native_https.rs b/src/auth/native_https.rs index 2b4f745..7cf4a4b 100644 --- a/src/auth/native_https.rs +++ b/src/auth/native_https.rs @@ -859,14 +859,23 @@ mod tests { } } - fn scripted_server( - reply: Reply, - ) -> ( + type ScriptedServer = ( String, rustls::RootCertStore, mpsc::Receiver, std::thread::JoinHandle<()>, - ) { + ); + + fn scripted_server(reply: Reply) -> ScriptedServer { + scripted_server_with_options(reply, None) + } + + // The body-admission fixture needs time to consume the real upload before + // responding. Other fixtures retain their original protocol deadlines. + fn scripted_server_with_options( + reply: Reply, + upload: Option<(usize, Duration)>, + ) -> ScriptedServer { let certified = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap(); let mut roots = rustls::RootCertStore::empty(); roots.add(certified.cert.der().clone()).unwrap(); @@ -890,7 +899,8 @@ mod tests { .build() .unwrap(); runtime.block_on(async move { - let accept_deadline = Instant::now() + Duration::from_secs(2); + let accept_deadline = Instant::now() + + upload.map_or(Duration::from_secs(2), |(_, watchdog)| watchdog); let tcp = loop { match listener.accept() { Ok((tcp, _)) => break tcp, @@ -915,8 +925,11 @@ mod tests { return; }; let acceptor = tokio_rustls::TlsAcceptor::from(Arc::new(tls)); - let Ok(Ok(tls)) = - tokio::time::timeout(Duration::from_secs(2), acceptor.accept(stream)).await + let Ok(Ok(tls)) = tokio::time::timeout( + upload.map_or(Duration::from_secs(2), |(_, watchdog)| watchdog), + acceptor.accept(stream), + ) + .await else { return; }; @@ -955,6 +968,10 @@ mod tests { Reply::StallHandshake(_) => unreachable!(), }; async move { + if let Some((expected, _)) = upload { + let body = req.into_body().collect().await.expect("provider upload"); + assert_eq!(body.to_bytes().len(), expected); + } let response: Response> = match reply { Reply::Fixed { status, @@ -998,7 +1015,7 @@ mod tests { } }); let completed = tokio::time::timeout( - Duration::from_secs(4), + upload.map_or(Duration::from_secs(4), |(_, watchdog)| watchdog), http1::Builder::new().serve_connection(TokioIo::new(tls), service), ) .await; @@ -2193,18 +2210,22 @@ mod tests { #[test] fn provider_request_body_is_admitted_past_the_credential_flow_bound() { let _admission = read_native_request_admission(); - let (url, roots, _events, server) = scripted_server(Reply::Fixed { - status: 200, - headers: Vec::new(), - body: b"ok".to_vec(), - }); + let body = "x".repeat(64 * 1024 + 1); + let (url, roots, _events, server) = scripted_server_with_options( + Reply::Fixed { + status: 200, + headers: Vec::new(), + body: b"ok".to_vec(), + }, + Some((body.len(), Duration::from_secs(60))), + ); let transport = NativeHttpsTransport::with_roots(roots); - // The module's heavier loopback exchange tests use a 6s client budget. - let (io_context, _) = context(Duration::from_secs(6)); + let mut provider_request = request(url); + provider_request.body = body; + // This test exercises a body limit, not a transport-latency guarantee. + let (io_context, _) = context(Duration::from_secs(30)); let io_context = io_context.for_provider_request(); assert_eq!(io_context.max_request_body_bytes(), 4 * 1024 * 1024); - let mut provider_request = request(url); - provider_request.body = "x".repeat(64 * 1024 + 1); let response = transport .send(&provider_request, &io_context) .expect("the native transport admits a provider body above the credential bound"); diff --git a/src/auth/oauth_host.rs b/src/auth/oauth_host.rs index a06e5d0..63bf16d 100644 --- a/src/auth/oauth_host.rs +++ b/src/auth/oauth_host.rs @@ -361,7 +361,7 @@ pub fn oauth_fixture_module() -> HostModuleDescriptor { } fn fixture_map() -> HostTypeSchema { - HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)) + HostTypeSchema::named_struct("OAuthFixtureEnvelope", vec![]) } fn stack_desc( diff --git a/src/auth/store_host.rs b/src/auth/store_host.rs index bb6118b..852a07e 100644 --- a/src/auth/store_host.rs +++ b/src/auth/store_host.rs @@ -316,7 +316,7 @@ pub fn auth_store_fixture_module() -> HostModuleDescriptor { } fn fixture_map() -> HostTypeSchema { - HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)) + HostTypeSchema::named_struct("AuthStoreFixtureEnvelope", vec![]) } fn stack_desc( diff --git a/src/bin/rustscript-agent.rs b/src/bin/rustscript-agent.rs index 013b4b1..ea2dff4 100644 --- a/src/bin/rustscript-agent.rs +++ b/src/bin/rustscript-agent.rs @@ -330,7 +330,7 @@ fn execute_legacy_run( Err(_) => return code_output("agent_compile_failed", 1), }; let mut sink = DiscardEvents; - match runner.run_with_context_and_events(Value::Null, &mut sink, cancellation) { + match runner.run_with_context_and_events(Value::map(Vec::new()), &mut sink, cancellation) { Ok(value) => run_success_output(&value), Err(error) => run_failure_output(error, cancellation), } @@ -458,11 +458,11 @@ mod tests { impl GuestScript { fn successful() -> (Self, CliInvocation) { - Self::with_source("pub fn run(input: string?) -> string { \"real-driver-success\"; }") + Self::with_source("pub fn run(context: map) -> string { \"real-driver-success\" }") } fn looping() -> (Self, CliInvocation) { - Self::with_source("pub fn run(input: string?) -> int { while true { 1; } 0; }") + Self::with_source("pub fn run(context: map) -> int { while true { 1; } 0 }") } fn with_source(source: &str) -> (Self, CliInvocation) { diff --git a/src/config.rs b/src/config.rs index cb6119d..dfd67e4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2595,6 +2595,9 @@ mod tests { ], "the operation-owned receipt must retain all nine cells in decoder order" ); + // The guest snapshots a row once per read, so `cells.copy()[n]` is the + // current idiom; the column order and kind stay exactly pinned. + let normalized = source.replace(".copy()", ""); for (index, (field, kind)) in [ ("graph_id", "text"), ("ref_id", "text"), @@ -2609,7 +2612,7 @@ mod tests { .into_iter() .enumerate() { - assert!(source.contains(&format!("{field} = cells[{index}].{kind}_value;"))); + assert!(normalized.contains(&format!("{field} = cells[{index}].{kind}_value"))); } for predicate in [ "idem.scope = ? AND idem.key = ? AND idem.resource_id = ?", diff --git a/src/config_host.rs b/src/config_host.rs index a9799ea..abc84dc 100644 --- a/src/config_host.rs +++ b/src/config_host.rs @@ -9,10 +9,12 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, OnceLock}; use std::time::{Duration, Instant}; +use rustscript_vm::resource::{CloseProgress, HostResource, ResourceCloseReason, ResourceResult}; use rustscript_vm::{ CallOutcome, CallReturn, CompileSourceFileOptions, HostApiCatalog, HostFunctionDescriptor, HostFunctionRegistry, HostFunctionSchema, HostModuleDescriptor, HostParamSchema, - HostTypeSchema, Program, SourceFlavor, Value, Vm, VmResult, VmStatus, + HostResourceType, HostResourceTypeMeta, HostStructField, HostTypeSchema, Program, + ResourceTypeKey, SourceFlavor, Value, Vm, VmResult, VmStatus, compile_source_at_path_with_flavor_and_options, }; use serde_json::{Value as JsonValue, json}; @@ -27,6 +29,7 @@ use crate::host_opaque::{OpaqueError, OpaqueRegistry}; const CONFIG_LOAD_SNAPSHOT: &str = "config::load_snapshot"; const CONFIG_CHECK_POLICY: &str = "config::check_policy"; const HOST_HOME_CLASS: &str = "HostHome"; +const POLICY_RESOURCE_KEY: &str = "config_fixture.policy_handle"; const FIXTURE_RUN_DEADLINE: Duration = Duration::from_secs(60); #[derive(Clone, Debug)] @@ -34,6 +37,113 @@ struct BoundHostHome { path: PathBuf, } +struct FixturePolicyResource { + handle: Option, +} + +impl HostResource for FixturePolicyResource { + fn resource_type_key() -> Option { + Some(ResourceTypeKey::new(POLICY_RESOURCE_KEY).expect("static policy key")) + } + + fn begin_close(&mut self, _: ResourceCloseReason) -> ResourceResult { + Ok(CloseProgress::Ready) + } +} + +impl HostResourceType for FixturePolicyResource { + const KEY: &'static str = POLICY_RESOURCE_KEY; + const DESCRIPTION: &'static str = "Fixture policy handle bound to its native owner"; +} + +fn policy_resource_meta() -> HostResourceTypeMeta { + HostResourceTypeMeta::of::() +} + +fn policy_resource() -> HostTypeSchema { + HostTypeSchema::Resource(ResourceTypeKey::new(POLICY_RESOURCE_KEY).expect("static policy key")) +} + +fn named(name: &str, fields: Vec<(&str, HostTypeSchema)>) -> HostTypeSchema { + HostTypeSchema::named_struct( + name, + fields + .into_iter() + .map(|(name, ty)| HostStructField::new(name, ty)) + .collect(), + ) +} + +fn error_type() -> HostTypeSchema { + named( + "ConfigFixtureError", + vec![ + ("code", HostTypeSchema::String), + ("message", HostTypeSchema::String), + ("path", HostTypeSchema::String), + ], + ) +} + +fn snapshot_type() -> HostTypeSchema { + named( + "ConfigSnapshotEnvelope", + vec![ + ("ok", HostTypeSchema::Bool), + ("error", error_type()), + ( + "public_config", + named( + "FixturePublicConfig", + vec![ + ( + "model", + named( + "FixtureModel", + vec![ + ("provider", HostTypeSchema::String), + ("model", HostTypeSchema::String), + ], + ), + ), + ( + "agent", + named("FixtureAgent", vec![("source", HostTypeSchema::String)]), + ), + ], + ), + ), + ( + "credential_refs", + HostTypeSchema::Array(Box::new(HostTypeSchema::String)), + ), + ( + "policy_summary", + named( + "FixturePolicySummary", + vec![ + ( + "providers", + HostTypeSchema::Array(Box::new(HostTypeSchema::String)), + ), + ("policy_generation", HostTypeSchema::Int), + ], + ), + ), + ("policy_handle", policy_resource()), + ("policy_handle_class", HostTypeSchema::String), + ("policy_generation", HostTypeSchema::Int), + ], + ) +} + +fn probe_type() -> HostTypeSchema { + named( + "ConfigPolicyProbe", + vec![("ok", HostTypeSchema::Bool), ("error", error_type())], + ) +} + #[derive(Clone)] struct ConfigFixtureState { home: PathBuf, @@ -57,29 +167,28 @@ pub fn config_fixture_module() -> HostModuleDescriptor { HostModuleDescriptor { name: "config_fixture", functions: CONFIG_FIXTURE_FUNCTIONS, - resources: &[], + resources: &[policy_resource_meta], } } -fn fixture_map() -> HostTypeSchema { - HostTypeSchema::Map(Box::new(HostTypeSchema::Unknown)) -} - fn stack_desc( name: &'static str, params: Vec, + output: HostTypeSchema, adapter: fn(&mut Vm, &[Value]) -> VmResult, ) -> HostFunctionDescriptor { - crate::runtime::host_compose::static_stack_descriptor( - HostFunctionSchema::with_return(name, params, fixture_map()), - adapter, - ) + let schema = HostFunctionSchema::with_return(name, params, output); + let effects = rustscript_vm::vm::host_extension::guest_resource_effects(&schema); + let mut descriptor = crate::runtime::host_compose::static_stack_descriptor(schema, adapter); + descriptor.effects = effects; + descriptor } fn config_load_snapshot_descriptor() -> HostFunctionDescriptor { stack_desc( CONFIG_LOAD_SNAPSHOT, vec![HostParamSchema::value("host_home", HostTypeSchema::Unknown)], + snapshot_type(), load_snapshot_adapter, ) } @@ -91,6 +200,7 @@ fn config_check_policy_descriptor() -> HostFunctionDescriptor { HostParamSchema::value("policy_handle", HostTypeSchema::Unknown), HostParamSchema::value("intent", HostTypeSchema::Unknown), ], + probe_type(), check_policy_adapter, ) } @@ -236,18 +346,24 @@ fn load_snapshot_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { match context.module_state::() { Some(state) => (state.home.clone(), Arc::clone(&state.policies)), None => { - return return_json(error_envelope(&ConfigFileError::HomeInvalid { - reason: "config fixture host home is not bound".to_string(), - path: None, - })); + return snapshot_error( + vm, + &ConfigFileError::HomeInvalid { + reason: "config fixture host home is not bound".to_string(), + path: None, + }, + ); } } }; if matches!(args.first(), Some(Value::String(_))) { - return return_json(error_envelope(&ConfigFileError::HomeInvalid { - reason: "RSS cannot supply or override host_home".to_string(), - path: Some(bound_home), - })); + return snapshot_error( + vm, + &ConfigFileError::HomeInvalid { + reason: "RSS cannot supply or override host_home".to_string(), + path: Some(bound_home), + }, + ); } let Some(bound) = args .first() @@ -256,20 +372,26 @@ fn load_snapshot_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { .and_then(|value| value.downcast_arc::()) .map(|home| (*home).clone()) else { - return return_json(error_envelope(&ConfigFileError::HomeInvalid { - reason: "host_home must be the host-bound HostHome".to_string(), - path: Some(bound_home), - })); + return snapshot_error( + vm, + &ConfigFileError::HomeInvalid { + reason: "host_home must be the host-bound HostHome".to_string(), + path: Some(bound_home), + }, + ); }; if bound.path != bound_home { - return return_json(error_envelope(&ConfigFileError::HomeInvalid { - reason: "host_home does not match the bound home".to_string(), - path: Some(bound_home), - })); + return snapshot_error( + vm, + &ConfigFileError::HomeInvalid { + reason: "host_home does not match the bound home".to_string(), + path: Some(bound_home), + }, + ); } match policies.load_snapshot(&bound_home) { - Ok(snapshot) => return_value(snapshot_to_vm_value(&snapshot)), - Err(error) => return_json(error_envelope(&error)), + Ok(snapshot) => snapshot_to_vm_value(vm, &snapshot).and_then(return_value), + Err(error) => snapshot_error(vm, &error), } } @@ -283,25 +405,83 @@ fn check_policy_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { } } }; - let handle = match args - .first() - .and_then(|value| OpaquePolicyHandle::from_vm_value(policies.opaques(), value)) - { - Some(handle) => handle, - None => { - return return_json(error_envelope(&ConfigFileError::PolicyHandleInvalid)); - } + let handle = args.first().and_then(|value| { + let Value::Int(raw) = value else { + return None; + }; + let raw = u64::try_from(*raw).ok()?; + let token = rustscript_vm::resource::ResourceHandle::from_raw(raw).ok()?; + let context = vm.host_context(); + context + .borrow_resource_with_key::( + token, + &ResourceTypeKey::new(POLICY_RESOURCE_KEY).expect("static policy key"), + ) + .ok()? + .handle + .clone() + }); + let Some(handle) = handle else { + return return_json(error_envelope(&ConfigFileError::PolicyHandleInvalid)); }; let intent = parse_intent(args.get(1)); match policies.check_policy(&handle, &intent) { - Ok(probe) => return_json(json!({ "ok": probe.ok })), + Ok(probe) => return_json( + json!({ "ok": probe.ok, "error": { "code": "", "message": "", "path": "" } }), + ), Err(error) => return_json(error_envelope(&error)), } } -fn snapshot_to_vm_value(snapshot: &ConfigSnapshotEnvelope) -> Value { - Value::map(vec![ +fn issue_policy_resource(vm: &mut Vm, handle: Option) -> VmResult { + let resource = vm + .host_context() + .push_resource(FixturePolicyResource { handle }) + .map_err(|error| rustscript_vm::VmError::HostError(error.to_string()))?; + Ok(Value::Int(resource.handle().raw() as i64)) +} + +fn snapshot_error(vm: &mut Vm, error: &ConfigFileError) -> VmResult { + let policy_handle = issue_policy_resource(vm, None)?; + let mut fields = vec![ + ( + Value::string("public_config"), + json_to_vm_value(&json!({ + "model": { "provider": "", "model": "" }, + "agent": { "source": "" } + })), + ), + (Value::string("credential_refs"), Value::array(vec![])), + ( + Value::string("policy_summary"), + json_to_vm_value(&json!({ "providers": [], "policy_generation": 0 })), + ), + (Value::string("policy_handle"), policy_handle), + ( + Value::string("policy_handle_class"), + Value::string("OpaquePolicyHandle"), + ), + (Value::string("policy_generation"), Value::Int(0)), + ]; + let envelope = json_to_vm_value(&error_envelope(error)); + if let Value::Map(values) = envelope { + fields.extend( + values + .iter() + .map(|(key, value)| (key.clone(), value.clone())), + ); + } + return_value(Value::map(fields)) +} + +fn snapshot_to_vm_value(vm: &mut Vm, snapshot: &ConfigSnapshotEnvelope) -> VmResult { + let handle = issue_policy_resource(vm, Some(snapshot.policy_handle.clone()))?; + Ok(Value::map(vec![ (Value::string("ok"), Value::Bool(true)), + ( + Value::string("error"), + json_to_vm_value(&json!({ "code": "", "message": "", "path": "" })), + ), ( Value::string("public_config"), json_to_vm_value(&json!(snapshot.public_config)), @@ -314,10 +494,7 @@ fn snapshot_to_vm_value(snapshot: &ConfigSnapshotEnvelope) -> Value { Value::string("policy_summary"), json_to_vm_value(&json!(snapshot.policy_summary)), ), - ( - Value::string("policy_handle"), - snapshot.policy_handle.to_vm_value(), - ), + (Value::string("policy_handle"), handle), ( Value::string("policy_handle_class"), Value::string(snapshot.policy_handle.class()), @@ -326,7 +503,7 @@ fn snapshot_to_vm_value(snapshot: &ConfigSnapshotEnvelope) -> Value { Value::string("policy_generation"), Value::Int(i64::try_from(snapshot.policy_generation).unwrap_or(i64::MAX)), ), - ]) + ])) } fn parse_intent(value: Option<&Value>) -> PolicyIntent { @@ -361,7 +538,7 @@ fn error_envelope(error: &ConfigFileError) -> JsonValue { "error": { "code": error.code(), "message": error.to_string(), - "path": error.path(), + "path": error.path().unwrap_or_default(), } }) } diff --git a/src/effects/broker.rs b/src/effects/broker.rs index d3561c2..85e2970 100644 --- a/src/effects/broker.rs +++ b/src/effects/broker.rs @@ -190,6 +190,8 @@ pub(crate) struct ManagedEffectRuntime { /// while the real bridge arrives. #[cfg(test)] pub(crate) child_gate_observer: Mutex>, + #[cfg(all(test, feature = "workspace-io-fault"))] + pub(crate) checkpoint_pause_observer: Mutex>>, #[cfg(test)] restore_closed_observer: Mutex>, #[cfg(test)] @@ -1407,21 +1409,23 @@ impl ManagedEffectRuntime { impl ManagedEffectRuntime { /// The later same-owner read-only reconciliation of a retained unknown /// process-group terminal commit. It reuses the original call's exact - /// durable identities and the existing `repository.get` path under the - /// existing metadata control budget; the run's business clock is neither - /// extended nor refreshed. It never commits again, recovers, publishes, + /// durable identities under the original call's cancellation/deadline. + /// An expired root cannot acquire a fresh metadata clock during terminal + /// cleanup; a later independent read can inspect the durable receipt. + /// It never commits again, recovers, publishes, /// reattaches a PID or synthesizes a second terminal: a staged/null hit is /// returned unchanged so the caller keeps the original unknown disposition. pub(crate) fn read_unknown_group_effect( &self, call: &ManagedCallContext, ) -> Result, String> { - let cancellation = RunCancellation::with_timeout(std::time::Duration::from_secs(5)); + let binding = call.binding()?; + binding.cancel.ensure_open().map_err(str::to_owned)?; let request = ManagedEffectGetRequest { owner: call.identity.owner.clone(), call_id: call.identity.call_id.clone(), }; - let record = self.repository.get(&request, &cancellation)?; + let record = self.repository.get(&request, &binding.cancel)?; #[cfg(test)] self.unknown_group_reads .fetch_add(1, std::sync::atomic::Ordering::SeqCst); @@ -1469,10 +1473,18 @@ pub struct ManagedPrepareGateGuard { #[cfg(feature = "workspace-io-fault")] impl ManagedEffectRuntime { pub(crate) fn arm_prepare_gate(&self, call_id: Option<&str>) -> ManagedPrepareGateGuard { + self.arm_prepare_gate_with_watchdog(call_id, std::time::Duration::from_secs(20)) + } + + pub(crate) fn arm_prepare_gate_with_watchdog( + &self, + call_id: Option<&str>, + watchdog: std::time::Duration, + ) -> ManagedPrepareGateGuard { let gate = Arc::new(ManagedPrepareGate { state: std::sync::Mutex::new((0, false, false)), cv: std::sync::Condvar::new(), - deadline: std::time::Instant::now() + std::time::Duration::from_secs(20), + deadline: std::time::Instant::now() + watchdog, call_id: call_id.map(str::to_owned), }); *self.prepare_gate.lock() = Some(Arc::clone(&gate)); @@ -1911,6 +1923,8 @@ impl ManagedEffectRuntime { child_gate_observer: Mutex::new(None), #[cfg(test)] restore_closed_observer: Mutex::new(None), + #[cfg(all(test, feature = "workspace-io-fault"))] + checkpoint_pause_observer: Mutex::new(None), #[cfg(test)] settled_call_observer: Mutex::new(None), #[cfg(test)] diff --git a/src/effects/managed_authority_tests.rs b/src/effects/managed_authority_tests.rs index d7f7460..785c3b6 100644 --- a/src/effects/managed_authority_tests.rs +++ b/src/effects/managed_authority_tests.rs @@ -363,13 +363,13 @@ async fn fresh_host_rejects_wrong_risk_name_arguments_and_registry_then_same_vm_ ); let source = r#"pub fn run(context:map)->map { let run:string=context["run"];let registry:string=context["registry"];let args:string=context["arguments"];let input:string=context["input"]; - let attest:map=agent_runtime::approval_attest({run_id:run,call_id:"second",tool_name:"write_file",argument_digest:args,registry_identity:registry,risk_class:"read",summary:"original"}); + let attest_payload=agent::json_text({run_id:run,call_id:"second",tool_name:"write_file",argument_digest:args,registry_identity:registry,risk_class:"read",summary:"original"});let mut attest_text:string=""; if attest_payload.ok { attest_text=attest_payload.text; } else { attest_text=""; }let attest:map=agent_runtime::approval_attest(attest_text); let risk:map=agent_runtime::invoke_pinned_tool({tool_name:"write_file",call_id:"second",canonical_arguments:args,canonical_input:input,run_id:run,registry_identity:registry,argument_digest:args,risk_class:"read",summary:"original",untrusted_graph_id:""}); let execute:map=agent_runtime::invoke_pinned_tool({tool_name:"write_file",call_id:"second",canonical_arguments:args,canonical_input:input,run_id:run,registry_identity:registry,argument_digest:args,risk_class:"execute",summary:"original",untrusted_graph_id:""}); let name:map=agent_runtime::invoke_pinned_tool({tool_name:"read_file",call_id:"second",canonical_arguments:args,canonical_input:input,run_id:run,registry_identity:registry,argument_digest:args,risk_class:"write",summary:"original",untrusted_graph_id:""}); let arguments:map=agent_runtime::invoke_pinned_tool({tool_name:"write_file",call_id:"second",canonical_arguments:"{}",canonical_input:input,run_id:run,registry_identity:registry,argument_digest:"{}",risk_class:"write",summary:"original",untrusted_graph_id:""}); let changed_registry:map=agent_runtime::invoke_pinned_tool({tool_name:"write_file",call_id:"second",canonical_arguments:args,canonical_input:input,run_id:run,registry_identity:"changed",argument_digest:args,risk_class:"write",summary:"original",untrusted_graph_id:""}); - let approve:map=agent_runtime::approval_attest({run_id:run,call_id:"second",tool_name:"write_file",argument_digest:args,registry_identity:registry,risk_class:"write",summary:"original"}); + let approve_payload=agent::json_text({run_id:run,call_id:"second",tool_name:"write_file",argument_digest:args,registry_identity:registry,risk_class:"write",summary:"original"});let mut approve_text:string=""; if approve_payload.ok { approve_text=approve_payload.text; } else { approve_text=""; }let approve:map=agent_runtime::approval_attest(approve_text); let original:map=agent_runtime::invoke_pinned_tool({tool_name:"write_file",call_id:"second",canonical_arguments:args,canonical_input:input,run_id:run,registry_identity:registry,argument_digest:args,risk_class:"write",summary:"original",untrusted_graph_id:""}); {attest:attest,risk:risk,execute:execute,name:name,arguments:arguments,changed_registry:changed_registry,approve:approve,original:original} }"#; @@ -871,6 +871,11 @@ fn find_process_handle(value: &Value) -> Option { fields.values().find_map(find_process_handle) } Value::Array(items) => items.iter().find_map(find_process_handle), + // A tool result now travels as canonical JSON text, so a producer's + // `process_id` is only reachable by reading that text as a document. + Value::String(text) => serde_json::from_str::(text) + .ok() + .and_then(|decoded| find_process_handle(&decoded)), _ => None, } } @@ -1602,7 +1607,8 @@ async fn managed_undrained_output_rejects_completion_proof_and_retains_owner() { handle .close_stdin() .expect("close the real child's stdin after arming the drain failure"); - let _ = handle.wait_until(std::time::Instant::now() + std::time::Duration::from_secs(3)); + let watchdog = std::time::Duration::from_secs(15); + let _ = handle.wait_until(std::time::Instant::now() + watchdog); assert!( handle.terminal_status().is_some(), "the real child reached its terminal state" @@ -1610,7 +1616,7 @@ async fn managed_undrained_output_rejects_completion_proof_and_retains_owner() { // The completion observation propagates the drain failure instead of // treating an exited-but-undrained child as proof. let error = binding - .probe_completion_group(std::time::Instant::now() + std::time::Duration::from_secs(1)) + .probe_completion_group(std::time::Instant::now() + watchdog) .expect_err("undrained output must not prove quiescence"); assert!( error.contains("drain failed"), @@ -1635,7 +1641,7 @@ async fn managed_undrained_output_rejects_completion_proof_and_retains_owner() { })); let run = run_turn_script(&service, |turn, handle| match turn { 1 => json!({"id":"producer","name":"terminal","arguments":{ - "argv":["/bin/sh","-c","read _; exit 0"],"background":true,"timeout_ms":3000}}), + "argv":["/bin/sh","-c","read _; exit 0"],"background":true,"timeout_ms":30000}}), 2 => json!({"id":"poll","name":"process","arguments":{ "action":"poll","process_id":handle}}), _ => Value::Null, @@ -2407,7 +2413,7 @@ async fn managed_unknown_process_group_finalized_hit_applies_existing_dispositio })); let run = run_turn_script(&service, |turn, handle| match turn { 1 => json!({"id":"producer","name":"terminal","arguments":{ - "argv":["/bin/sh","-c","sleep 1"],"background":true,"timeout_ms":5000}}), + "argv":["/bin/sh","-c","sleep 1"],"background":true,"timeout_ms":30000}}), 2 => json!({"id":"close","name":"process","arguments":{ "action":"close","process_id":handle}}), 3 => json!({"id":"process-wait","name":"process","arguments":{ @@ -3833,6 +3839,12 @@ fn request_process_ids(value: &Value) -> Vec { walk(item, out); } } + // Tool results travel as canonical JSON text at this pin. + Value::String(text) => { + if let Ok(decoded) = serde_json::from_str::(text) { + walk(&decoded, out); + } + } _ => {} } } @@ -4192,11 +4204,11 @@ impl crate::AgentProviderHost for SharedLifecycleProvider { // both. dispatch_serial executes them in order before the next // provider turn, and each still gets its own managed receipt. 1 => reply(vec![ - spawn("spawn-p1", "printf 'p1' > p1.txt", 4000), + spawn("spawn-p1", "printf 'p1' > p1.txt", 180000), spawn( "spawn-p2", "read line; printf 'p2:%s' \"$line\" > p2.txt", - 8000, + 240000, ), ]), // Both real handles are known here. Keep the original operation @@ -4205,7 +4217,7 @@ impl crate::AgentProviderHost for SharedLifecycleProvider { // file admission stays last in this batch so its result remains // after wait(P1), as in the one-call-per-turn fixture. 2 => { - let mut calls = vec![wait("wait-p1", &first, 3000)]; + let mut calls = vec![wait("wait-p1", &first, 180000)]; if self.file_gate_and_repeat { calls.push(json!({"id":"file-gate","name":"write_file","arguments":{ "path":"gate.txt","content":"gate"}})); @@ -4215,8 +4227,8 @@ impl crate::AgentProviderHost for SharedLifecycleProvider { 3 => reply(vec![ write(), close(), - wait("wait-p2", &second, 5000), - wait("wait-again", &second, 1000), + wait("wait-p2", &second, 240000), + wait("wait-again", &second, 30000), ]), _ => json!({"text":"done","tool_calls":[], "usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}, @@ -4228,7 +4240,11 @@ impl crate::AgentProviderHost for SharedLifecycleProvider { #[tokio::test(flavor = "multi_thread")] async fn managed_live_sibling_control_receipt_and_single_group_publication() { - let (root, state) = native_fixture(); + // Process deadlines only guard the fixture; the durable receipts below + // establish the actual ordering even when other tests occupy the workers. + let (root, state) = native_fixture_config(|config| { + config.run_timeout = std::time::Duration::from_secs(300); + }); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let persistence = state.persistence().unwrap(); diff --git a/src/effects/managed_restore_authority_tests.rs b/src/effects/managed_restore_authority_tests.rs index 5dff6dd..073cc82 100644 --- a/src/effects/managed_restore_authority_tests.rs +++ b/src/effects/managed_restore_authority_tests.rs @@ -6,6 +6,8 @@ use std::time::Duration; fn fixture(timeout: Duration) -> (PathBuf, AgentGatewayState) { super::managed_authority_tests::native_fixture_config(|config| { config.run_timeout = timeout; + // These restores require the terminal source handle to remain retained. + config.terminal_run_ttl = Duration::from_secs(600); config.bearer_token = Some("synthetic-maintenance-fixture".into()); let policy = &mut config .managed_workspace @@ -65,7 +67,7 @@ fn cas(head: &ManagedWorkspaceHead) -> ManagedSourceCas { // Always release it and join the owned worker before propagating assertions. #[tokio::test(flavor = "multi_thread")] async fn restore_drain_obeys_original_source_owner_deadline() { - let (_root, state) = fixture(Duration::from_secs(4)); + let (_root, state) = fixture(Duration::from_secs(25)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let (entered, received) = std::sync::mpsc::sync_channel(1); @@ -76,7 +78,7 @@ async fn restore_drain_obeys_original_source_owner_deadline() { entered.send(call).unwrap(); waiting .lock() - .recv_timeout(Duration::from_secs(12)) + .recv_timeout(Duration::from_secs(60)) .unwrap(); })); let run = admit(&service, "entered-deadline").await; @@ -85,7 +87,7 @@ async fn restore_drain_obeys_original_source_owner_deadline() { .clone() .run_worker(run.run_id.clone(), String::new()), ); - let call = received.recv_timeout(Duration::from_secs(3)).unwrap(); + let call = received.recv_timeout(Duration::from_secs(20)).unwrap(); let binding = call.binding().unwrap(); let head = runtime .repository @@ -113,12 +115,12 @@ async fn restore_drain_obeys_original_source_owner_deadline() { ) .unwrap(); let original = binding.cancel.deadline_instant().unwrap(); - let management = RunCancellation::with_timeout(Duration::from_secs(9)); + let management = RunCancellation::with_timeout(Duration::from_secs(90)); let rt = runtime.clone(); let mut drain = tokio::task::spawn_blocking(move || rt.settle_restore_source(&prepared, &management)); let result = tokio::time::timeout_at( - tokio::time::Instant::from_std(original + Duration::from_millis(400)), + tokio::time::Instant::from_std(original + Duration::from_secs(5)), &mut drain, ) .await; @@ -138,7 +140,7 @@ async fn restore_drain_obeys_original_source_owner_deadline() { #[tokio::test(flavor = "multi_thread")] async fn late_copy_cannot_install_adapter_after_restore_closes_source() { - let (_root, state) = fixture(Duration::from_secs(60)); + let (_root, state) = fixture(Duration::from_secs(180)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let (entered, received) = std::sync::mpsc::sync_channel(1); @@ -149,7 +151,7 @@ async fn late_copy_cannot_install_adapter_after_restore_closes_source() { entered.send(call).unwrap(); waiting .lock() - .recv_timeout(Duration::from_secs(10)) + .recv_timeout(Duration::from_secs(60)) .unwrap(); })); let run = admit(&service, "late-copy").await; @@ -158,7 +160,7 @@ async fn late_copy_cannot_install_adapter_after_restore_closes_source() { .clone() .run_worker(run.run_id.clone(), String::new()), ); - let call = received.recv_timeout(Duration::from_secs(4)).unwrap(); + let call = received.recv_timeout(Duration::from_secs(30)).unwrap(); let binding = call.binding().unwrap(); let head = runtime .repository @@ -279,7 +281,7 @@ struct ReleaseGate { impl ReleaseGate { fn wait(&self) { let mut state = self.released.lock(); - let end = std::time::Instant::now() + Duration::from_secs(12); + let end = std::time::Instant::now() + Duration::from_secs(60); while !*state { assert!( !self.changed.wait_until(&mut state, end).timed_out(), @@ -398,7 +400,7 @@ fn reject_old_native( #[tokio::test(flavor = "multi_thread")] async fn restore_closes_two_live_runs_and_evicted_issued_owner_then_new_branch_performs_io() { - let (_root, state) = fixture(Duration::from_secs(90)); + let (_root, state) = fixture(Duration::from_secs(180)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let (_source, _request, checkpoint) = checkpoint_source(&service).await; @@ -430,14 +432,14 @@ async fn restore_closes_two_live_runs_and_evicted_issued_owner_then_new_branch_p .clone() .run_worker(first.run_id.clone(), String::new()), ); - let one = received.recv_timeout(Duration::from_secs(3)).unwrap(); + let one = received.recv_timeout(Duration::from_secs(30)).unwrap(); let second = admit_read(&service, "before-enter-two").await; let second_worker = tokio::spawn( service .clone() .run_worker(second.run_id.clone(), String::new()), ); - let two = received.recv_timeout(Duration::from_secs(3)).unwrap(); + let two = received.recv_timeout(Duration::from_secs(30)).unwrap(); assert!(Arc::ptr_eq(&one.binding.branch, &two.binding.branch)); assert!(Arc::ptr_eq( &one.binding.branch, @@ -455,11 +457,11 @@ async fn restore_closes_two_live_runs_and_evicted_issued_owner_then_new_branch_p // The exact gate precedes Native enter, so no old effect can start. let checks = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { assert_eq!( - closed.recv_timeout(Duration::from_secs(3)).unwrap(), + closed.recv_timeout(Duration::from_secs(30)).unwrap(), "closed" ); assert_eq!( - closed.recv_timeout(Duration::from_secs(3)).unwrap(), + closed.recv_timeout(Duration::from_secs(30)).unwrap(), "cancelled" ); for owner in [&one, &two, &evicted_owner] { @@ -681,7 +683,7 @@ async fn process_lineage_case(after_intent: bool) { #[tokio::test(flavor = "multi_thread")] async fn closure_between_authorization_and_operation_raii_denies_actual_direct_write() { - let (_root, state) = fixture(Duration::from_secs(60)); + let (_root, state) = fixture(Duration::from_secs(120)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let ready = Arc::new(ReleaseGate { @@ -700,7 +702,7 @@ async fn closure_between_authorization_and_operation_raii_denies_actual_direct_w .clone() .run_worker(run.run_id.clone(), String::new()), ); - let owner = received.recv_timeout(Duration::from_secs(3)).unwrap(); + let owner = received.recv_timeout(Duration::from_secs(30)).unwrap(); let gate = Arc::new(ReleaseGate { released: Mutex::new(false), changed: parking_lot::Condvar::new(), @@ -718,7 +720,7 @@ async fn closure_between_authorization_and_operation_raii_denies_actual_direct_w .filesystem .write_atomic(&adapter.token, "must-not-start.txt", "*", b"forbidden") }); - received.recv_timeout(Duration::from_secs(3)).unwrap(); + received.recv_timeout(Duration::from_secs(30)).unwrap(); let head = runtime .repository .head(&owner.binding.admitted_branch, &owner.binding.cancel) @@ -766,7 +768,7 @@ async fn closure_between_authorization_and_operation_raii_denies_actual_direct_w #[tokio::test(flavor = "multi_thread")] async fn entered_write_drains_before_restore_outcome_and_never_publishes_old_candidate() { - let (_root, state) = fixture(Duration::from_secs(90)); + let (_root, state) = fixture(Duration::from_secs(180)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let (_base, _request, checkpoint) = checkpoint_source(&service).await; @@ -787,7 +789,7 @@ async fn entered_write_drains_before_restore_outcome_and_never_publishes_old_can .clone() .run_worker(run.run_id.clone(), String::new()), ); - let owner = received.recv_timeout(Duration::from_secs(3)).unwrap(); + let owner = received.recv_timeout(Duration::from_secs(30)).unwrap(); let request = restore_request(&runtime, &checkpoint); let original = request.clone(); let (sent, closed) = std::sync::mpsc::channel(); @@ -800,7 +802,7 @@ async fn entered_write_drains_before_restore_outcome_and_never_publishes_old_can }); let checks = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { assert_eq!( - closed.recv_timeout(Duration::from_secs(3)).unwrap(), + closed.recv_timeout(Duration::from_secs(30)).unwrap(), "closed" ); assert_eq!(owner.call.active_native_operations(), 1); @@ -864,7 +866,7 @@ async fn entered_write_drains_before_restore_outcome_and_never_publishes_old_can #[tokio::test(flavor = "multi_thread")] async fn prepared_and_readonly_witnesses_cannot_commit_across_source_closure() { for readonly in [false, true] { - let (_root, state) = fixture(Duration::from_secs(90)); + let (_root, state) = fixture(Duration::from_secs(180)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let (_base, _request, checkpoint) = checkpoint_source(&service).await; @@ -884,7 +886,7 @@ async fn prepared_and_readonly_witnesses_cannot_commit_across_source_closure() { .clone() .run_worker(run.run_id.clone(), String::new()), ); - let owner = received.recv_timeout(Duration::from_secs(3)).unwrap(); + let owner = received.recv_timeout(Duration::from_secs(30)).unwrap(); gate.wait_arrived(1).unwrap(); let witness = owner.call.witness().unwrap(); assert!(owner.call.outcome.lock().is_none()); @@ -908,7 +910,7 @@ async fn prepared_and_readonly_witnesses_cannot_commit_across_source_closure() { }); let checks = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { assert_eq!( - closed.recv_timeout(Duration::from_secs(3)).unwrap(), + closed.recv_timeout(Duration::from_secs(30)).unwrap(), "closed" ); assert!(!task.is_finished(), "prepared RAII must drain"); @@ -1134,7 +1136,7 @@ async fn identity_rejections_leave_source_and_receipts_unchanged_with_same_owner #[tokio::test(flavor = "multi_thread")] async fn prepared_copy_cannot_install_witness_after_restore_source_closes() { - let (_root, state) = fixture(Duration::from_secs(90)); + let (_root, state) = fixture(Duration::from_secs(180)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let gate = Arc::new(ReleaseGate { @@ -1154,7 +1156,7 @@ async fn prepared_copy_cannot_install_witness_after_restore_source_closes() { .clone() .run_worker(run.run_id.clone(), String::new()), ); - let owner = received.recv_timeout(Duration::from_secs(4)).unwrap(); + let owner = received.recv_timeout(Duration::from_secs(30)).unwrap(); let read = RunCancellation::with_timeout(Duration::from_secs(5)); let head = runtime .repository @@ -1347,7 +1349,7 @@ impl crate::AgentProviderHost for PausedProvider { } #[tokio::test(flavor = "multi_thread")] async fn provider_worker_drain_cannot_borrow_later_management_deadline() { - let (_root, state) = fixture(Duration::from_secs(4)); + let (_root, state) = fixture(Duration::from_secs(25)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let (_base, _create, checkpoint) = checkpoint_source(&service).await; @@ -1372,14 +1374,16 @@ async fn provider_worker_drain_cannot_borrow_later_management_deadline() { .clone() .run_worker(run.run_id.clone(), String::new()), ); - received.recv_timeout(Duration::from_secs(3)).unwrap(); + received.recv_timeout(Duration::from_secs(20)).unwrap(); let request = restore_request(&runtime, &checkpoint); let original = request.clone(); let mut task = tokio::spawn({ let service = service.clone(); async move { service.restore_managed_checkpoint_and_admit(request).await } }); - let limit = binding.cancel.deadline_instant().unwrap() + Duration::from_millis(400); + // The five-second overrun tolerates scheduling of the result future, not a + // borrowed management clock; the original deadline remains the bound. + let limit = binding.cancel.deadline_instant().unwrap() + Duration::from_secs(5); let bounded = tokio::time::timeout_at(tokio::time::Instant::from_std(limit), &mut task).await; let within_original = bounded.is_ok(); gate.release(); @@ -1404,7 +1408,7 @@ async fn provider_worker_drain_cannot_borrow_later_management_deadline() { #[tokio::test(flavor = "multi_thread")] async fn already_entered_direct_write_finishes_before_successful_restore_publication() { - let (_root, state) = fixture(Duration::from_secs(90)); + let (_root, state) = fixture(Duration::from_secs(180)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let (_base, _create, checkpoint) = checkpoint_source(&service).await; @@ -1435,14 +1439,14 @@ async fn already_entered_direct_write_finishes_before_successful_restore_publica .clone() .run_worker(run.run_id.clone(), String::new()), ); - let owner = received.recv_timeout(Duration::from_secs(3)).unwrap(); + let owner = received.recv_timeout(Duration::from_secs(30)).unwrap(); let adapter = owner.adapter.clone(); let direct = tokio::task::spawn_blocking(move || { adapter .filesystem .write_atomic(&adapter.token, "entered-direct.txt", "*", b"B") }); - entered.recv_timeout(Duration::from_secs(3)).unwrap(); + entered.recv_timeout(Duration::from_secs(30)).unwrap(); let original = restore_request(&runtime, &checkpoint); let request = original.clone(); let (sent, closed) = std::sync::mpsc::channel(); @@ -1455,7 +1459,7 @@ async fn already_entered_direct_write_finishes_before_successful_restore_publica }); let before = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { assert_eq!( - closed.recv_timeout(Duration::from_secs(3)).unwrap(), + closed.recv_timeout(Duration::from_secs(30)).unwrap(), "closed" ); assert!(!task.is_finished()); @@ -1483,7 +1487,7 @@ async fn already_entered_direct_write_finishes_before_successful_restore_publica let written = direct.await.unwrap(); let drained = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { assert_eq!( - closed.recv_timeout(Duration::from_secs(3)).unwrap(), + closed.recv_timeout(Duration::from_secs(30)).unwrap(), "cancelled" ); assert!(written.is_ok()); @@ -1535,7 +1539,7 @@ async fn already_entered_direct_write_finishes_before_successful_restore_publica #[tokio::test(flavor = "multi_thread")] async fn ready_prepared_tree_and_readonly_receipts_are_not_committed_after_successful_restore() { for readonly in [false, true] { - let (_root, state) = fixture(Duration::from_secs(90)); + let (_root, state) = fixture(Duration::from_secs(180)); let service = state.service(); let runtime = service.managed_runtime_for_test().unwrap(); let (_base, _create, checkpoint) = checkpoint_source(&service).await; @@ -1560,7 +1564,7 @@ async fn ready_prepared_tree_and_readonly_receipts_are_not_committed_after_succe .clone() .run_worker(run.run_id.clone(), String::new()), ); - let owner = received.recv_timeout(Duration::from_secs(4)).unwrap(); + let owner = received.recv_timeout(Duration::from_secs(30)).unwrap(); let witness = owner.call.witness().unwrap(); assert_eq!( owner.binding.branch.state.lock().active, @@ -1579,11 +1583,11 @@ async fn ready_prepared_tree_and_readonly_receipts_are_not_committed_after_succe }); let checks = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { assert_eq!( - closed.recv_timeout(Duration::from_secs(3)).unwrap(), + closed.recv_timeout(Duration::from_secs(30)).unwrap(), "closed" ); assert_eq!( - closed.recv_timeout(Duration::from_secs(3)).unwrap(), + closed.recv_timeout(Duration::from_secs(30)).unwrap(), "cancelled" ); assert!(!task.is_finished()); diff --git a/src/gateway/managed_storage_budget_tests.rs b/src/gateway/managed_storage_budget_tests.rs index 4bbef45..f0b47c7 100644 --- a/src/gateway/managed_storage_budget_tests.rs +++ b/src/gateway/managed_storage_budget_tests.rs @@ -149,6 +149,8 @@ fn composed_restore_fixed_statement_parameter_census_matches_native_weights() { actual, declared, "fresh-session statement parameter weights drifted" ); + // The restore pin guard binds + // params: Some([sql_text(input.tool_graph_id.copy()),sql_text(restore_json.copy())]). assert_eq!( derived, [ @@ -156,7 +158,7 @@ fn composed_restore_fixed_statement_parameter_census_matches_native_weights() { ("receipt_key".into(), 2), ("receipt_scope".into(), 1), ("registry_identity".into(), 1), - ("restore_harness_json".into(), 1), + ("restore_json".into(), 1), ("workspace".into(), 1) ] .into_iter() @@ -410,13 +412,22 @@ fn graph_fixed_statement_arrays_and_guard_bindings_match_complete_post_edit_cens "/// Returns the persisted retention", ) .split("let statements:") - .nth(1) + .next() .unwrap(); - let mut actual = census(pair, "sql_text(input."); - actual.insert( - "failpoint".into(), - pair.matches("sql_text(failpoint)").count(), - ); + let mut actual = std::collections::BTreeMap::new(); + for line in pair + .lines() + .filter(|line| line.contains("let stmt_params_") && line.contains("array")) + { + let params = line.split("= [").nth(1).unwrap().split(']').next().unwrap(); + for tail in params.split("sql_text(input.").skip(1) { + *actual + .entry(tail.split(['.', ')']).next().unwrap().to_string()) + .or_insert(0) += 1; + } + *actual.entry("failpoint".into()).or_insert(0) += + params.matches("sql_text(failpoint)").count(); + } assert_eq!( actual, EVENT_PAIR_BINDINGS diff --git a/src/gateway/mod.rs b/src/gateway/mod.rs index 8686025..e97c501 100644 --- a/src/gateway/mod.rs +++ b/src/gateway/mod.rs @@ -840,13 +840,21 @@ mod headless_provider_transport_tests { let Value::Map(entries) = value else { panic!("expected RSS record, got {value:?}"); }; - entries + let value = entries .iter() .find_map(|(key, value)| match key { Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing RSS field {name}")) + .unwrap_or_else(|| panic!("missing RSS field {name}")); + if let Value::Map(carrier) = value + && carrier + .iter() + .any(|(key, tag)| key == &Value::string("tag") && tag == &Value::Bool(false)) + { + return &Value::Null; + } + value } fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> Value { @@ -923,11 +931,11 @@ mod headless_provider_transport_tests { ]); let usage = record(vec![ ("kind", Value::string("access")), - ("access_handle", access), - ("refresh_handle", Value::Null), - ("code_handle", Value::Null), - ("verifier_handle", Value::Null), - ("device_handle", Value::Null), + ("access_handle", rustscript_vm::option::some_carrier(access)), + ("refresh_handle", rustscript_vm::option::none_carrier()), + ("code_handle", rustscript_vm::option::none_carrier()), + ("verifier_handle", rustscript_vm::option::none_carrier()), + ("device_handle", rustscript_vm::option::none_carrier()), ]); let result = invoke( &mut vm, diff --git a/src/gateway/store.rs b/src/gateway/store.rs index 75176b4..3614402 100644 --- a/src/gateway/store.rs +++ b/src/gateway/store.rs @@ -924,8 +924,11 @@ impl GatewayPersistence { "maintenance_json": serde_json::to_string(&document) .map_err(|_| crate::effects::journal::storage_error("managed metadata invalid"))?, }); - let result = - self.command_data_with_cancellation("effect.publication_get", &payload, cancellation)?; + let result = self.managed_effect_command_with_cancellation( + "effect.publication_get", + &payload, + cancellation, + )?; crate::effects::journal::decode_record(&result).map_err(|message| StorageError { code: "storage_unavailable".to_string(), message, @@ -1347,6 +1350,17 @@ impl StorageRunner { } #[cfg(any(test, feature = "workspace-io-fault"))] self.executed_commands.fetch_add(1, Ordering::Relaxed); + // The context snapshot takes exactly one data selector. The guest + // dialect admits no view of an open JSON object, so the exactness check + // lives here, before the bundle sees the command. + if op == "compaction.context" { + let selector_only = payload + .as_object() + .is_some_and(|object| object.len() == 1 && object.contains_key("session_id")); + if !selector_only { + return Err("invalid_payload: context snapshot accepts only session_id".into()); + } + } let input = VmValue::map(vec![ (VmValue::string("op"), VmValue::string(op)), ( @@ -1775,7 +1789,20 @@ struct CompactionContextQuery { columns: Vec, rows: Vec, truncated: bool, - next_cursor: Option, + #[allow(dead_code)] // carried for wire validation, not read after decode + next_cursor: Option>, +} + +/// Accepts both encodings of an optional value on the storage wire: the legacy +/// `null`/bare form and the tagged `Option` carrier the strict guest dialect +/// emits. The guest cannot produce a null literal, so a carrier is what an +/// absent cursor actually arrives as. +#[derive(Deserialize, Serialize)] +#[serde(untagged)] +enum Carried { + Plain(T), + Null(()), + Tagged { tag: bool, payload: Option }, } #[derive(Deserialize)] diff --git a/src/plugins/prepare.rs b/src/plugins/prepare.rs index c844d76..ac8f086 100644 --- a/src/plugins/prepare.rs +++ b/src/plugins/prepare.rs @@ -3128,6 +3128,7 @@ fn is_pure(name: &str) -> bool { | "agent::utf8_truncate" | "agent::parse_json_object" | "agent::control_check" + | "agent::json_text" ) } @@ -3299,9 +3300,9 @@ mod policy_tests { PackageTrust::EmbeddingOwnedBuiltin, ] { for source in [ - "pub fn invoke(token: string) -> map { cap::artifact_put_result(token, bytes::from_utf8(\"result\"), {}) }\n", - "pub fn invoke(value: map) -> map { agent_runtime::tool_prepare(value) }\n", - "pub fn invoke(token: string) -> map { agent_runtime::tool_commit(token, {}) }\n", + "pub fn invoke(token: string) -> map { cap::artifact_put_result(token, bytes::from_utf8(\"result\"), \"{}\") }\n", + "pub fn invoke(value: string) -> map { agent_runtime::tool_prepare(value) }\n", + "pub fn invoke(token: string) -> map { agent_runtime::tool_commit(token, \"{}\") }\n", "pub fn invoke(value: int) -> int { agent::sleep_ms(value) }\n", ] { let mut package = InstalledPackage::new( @@ -3526,11 +3527,12 @@ mod policy_tests { const PURE_MIGRATION_SOURCE: &str = r#" use json; +struct MigrationDocument { version: int } pub fn migrate(input: map) -> map { let document: string = input["document"]; - let mut decoded: map = json::decode(document); + let mut decoded: MigrationDocument = json::decode::(document); decoded.version = 2; - { schema: 2, document: json::encode(decoded) } + { schema: 2, document: json::encode::(decoded) } } "#; @@ -4066,8 +4068,8 @@ pub fn migrate(input: map) -> map { const S2_FOCUSED_CALLER_SOURCE: &str = "pub fn run(context: map) -> map { context }\n"; const S2_FOCUSED_CALLEE_SOURCE: &str = r#"pub fn run(context: map) -> map { - let meta: map = cap::fs_metadata("", "s2.txt"); - { ok: true, content: "", data: { meta_ok: meta["ok"] }, error: null, truncated: false, artifacts: [] } + let meta = cap::fs_metadata("", "s2.txt"); + { ok: true, content: "", data: { meta_ok: meta.ok }, truncated: false, artifacts: [] } } "#; diff --git a/src/runtime/agent_host.rs b/src/runtime/agent_host.rs index 6984251..0cba395 100644 --- a/src/runtime/agent_host.rs +++ b/src/runtime/agent_host.rs @@ -12,7 +12,7 @@ use std::time::{Duration, Instant}; use rustscript_vm::{ CallOutcome, CallReturn, HostApiCatalog, HostFunctionDescriptor, HostFunctionRegistry, HostFunctionSchema, HostModuleDescriptor, HostNamedStruct, HostParamSchema, HostTypeSchema, - Value, Vm, VmError, VmResult, + Value, Vm, VmError, VmResult, option, }; use super::cancellation::CancellationReason; @@ -63,7 +63,16 @@ const CAP_ARTIFACT_GET: &str = "cap::artifact_get"; const CAP_ARTIFACT_REFERENCE: &str = "cap::artifact_reference"; const CAP_CLOCK_MONOTONIC_MS: &str = "cap::clock_monotonic_ms"; const PARSE_JSON_OBJECT: &str = "agent::parse_json_object"; +const PREVIEW_ARGUMENTS: &str = "agent::preview_arguments"; const PARSE_JSON_OBJECT_MAX_BYTES: usize = 64 * 1024; +const JSON_TEXT: &str = "agent::json_text"; +/// Bound applied to one serialized open payload leaving the guest. +const JSON_TEXT_MAX_BYTES: usize = 4 * 1024 * 1024; +/// Bound applied to one open payload entering a host adapter (tool metadata, +/// artifact metadata, a tool result). The same ceiling the provider/tool +/// bodies already use. +const JSON_INPUT_MAX_BYTES: usize = 4 * 1024 * 1024; +const JSON_INPUT_MAX_DEPTH: usize = 64; const STATE_READ: &str = "state::read"; const STATE_WRITE: &str = "state::write"; @@ -106,6 +115,8 @@ const AGENT_HOST_FUNCTIONS: &[fn() -> HostFunctionDescriptor] = &[ cap_artifact_reference_descriptor, cap_clock_monotonic_ms_descriptor, parse_json_object_descriptor, + preview_arguments_descriptor, + json_text_descriptor, state_read_descriptor, state_write_descriptor, ]; @@ -147,6 +158,7 @@ pub(crate) fn retained_tool_host_module() -> HostModuleDescriptor { cap_artifact_reference_descriptor, cap_clock_monotonic_ms_descriptor, parse_json_object_descriptor, + json_text_descriptor, state_read_descriptor, state_write_descriptor, ], @@ -173,8 +185,8 @@ fn stack_desc( fn provider_call_descriptor() -> HostFunctionDescriptor { stack_desc( PROVIDER_CALL, - vec![HostParamSchema::value("request", HostTypeSchema::Unknown)], - HostTypeSchema::Unknown, + vec![HostParamSchema::value("request", HostTypeSchema::String)], + super::host_types::AgentProviderEnvelope::host_type_schema(), provider_call_adapter, ) } @@ -182,8 +194,8 @@ fn provider_call_descriptor() -> HostFunctionDescriptor { fn refresh_context_descriptor() -> HostFunctionDescriptor { stack_desc( REFRESH_CONTEXT, - vec![HostParamSchema::value("context", HostTypeSchema::Unknown)], - HostTypeSchema::Unknown, + vec![HostParamSchema::value("context", HostTypeSchema::String)], + super::host_types::AgentRefreshEnvelope::host_type_schema(), refresh_context_adapter, ) } @@ -230,7 +242,7 @@ fn control_check_descriptor() -> HostFunctionDescriptor { fn tool_replay_descriptor() -> HostFunctionDescriptor { stack_desc( TOOL_REPLAY, - vec![HostParamSchema::value("metadata", HostTypeSchema::Unknown)], + vec![HostParamSchema::value("metadata", HostTypeSchema::String)], super::host_types::AgentToolEnvelope::host_type_schema(), tool_replay_adapter, ) @@ -239,7 +251,7 @@ fn tool_replay_descriptor() -> HostFunctionDescriptor { fn approval_attest_descriptor() -> HostFunctionDescriptor { stack_desc( APPROVAL_ATTEST, - vec![HostParamSchema::value("metadata", HostTypeSchema::Unknown)], + vec![HostParamSchema::value("metadata", HostTypeSchema::String)], super::host_types::AgentToolEnvelope::host_type_schema(), approval_attest_adapter, ) @@ -250,9 +262,9 @@ fn approval_storage_descriptor() -> HostFunctionDescriptor { APPROVAL_STORAGE, vec![ HostParamSchema::value("operation", HostTypeSchema::String), - HostParamSchema::value("payload", HostTypeSchema::Unknown), + HostParamSchema::value("payload", HostTypeSchema::String), ], - HostTypeSchema::Unknown, + super::host_types::AgentApprovalStorageEnvelope::host_type_schema(), approval_storage_adapter, ) } @@ -260,7 +272,7 @@ fn approval_storage_descriptor() -> HostFunctionDescriptor { fn tool_prepare_descriptor() -> HostFunctionDescriptor { stack_desc( TOOL_PREPARE, - vec![HostParamSchema::value("metadata", HostTypeSchema::Unknown)], + vec![HostParamSchema::value("metadata", HostTypeSchema::String)], super::host_types::AgentToolEnvelope::host_type_schema(), tool_prepare_adapter, ) @@ -271,7 +283,7 @@ fn tool_commit_descriptor() -> HostFunctionDescriptor { TOOL_COMMIT, vec![ token_param(), - HostParamSchema::value("result", HostTypeSchema::Unknown), + HostParamSchema::value("result", HostTypeSchema::String), ], super::host_types::AgentToolEnvelope::host_type_schema(), tool_commit_adapter, @@ -485,9 +497,9 @@ fn cap_artifact_put_descriptor() -> HostFunctionDescriptor { vec![ token_param(), HostParamSchema::value("bytes", HostTypeSchema::Bytes), - HostParamSchema::value("metadata", HostTypeSchema::Unknown), + HostParamSchema::value("metadata", HostTypeSchema::String), ], - HostTypeSchema::Unknown, + super::host_types::AgentArtifactRef::host_type_schema(), cap_artifact_put_adapter, ) } @@ -498,9 +510,9 @@ fn cap_artifact_put_result_descriptor() -> HostFunctionDescriptor { vec![ token_param(), HostParamSchema::value("bytes", HostTypeSchema::Bytes), - HostParamSchema::value("metadata", HostTypeSchema::Unknown), + HostParamSchema::value("metadata", HostTypeSchema::String), ], - HostTypeSchema::Unknown, + super::host_types::AgentArtifactRef::host_type_schema(), cap_artifact_put_result_adapter, ) } @@ -512,7 +524,7 @@ fn cap_artifact_get_descriptor() -> HostFunctionDescriptor { token_param(), HostParamSchema::value("id", HostTypeSchema::String), ], - HostTypeSchema::Unknown, + super::host_types::AgentArtifactGetResult::host_type_schema(), cap_artifact_get_adapter, ) } @@ -524,7 +536,7 @@ fn cap_artifact_reference_descriptor() -> HostFunctionDescriptor { token_param(), HostParamSchema::value("id", HostTypeSchema::String), ], - HostTypeSchema::Unknown, + super::host_types::AgentArtifactRef::host_type_schema(), cap_artifact_reference_adapter, ) } @@ -542,11 +554,47 @@ fn parse_json_object_descriptor() -> HostFunctionDescriptor { stack_desc( PARSE_JSON_OBJECT, vec![HostParamSchema::value("text", HostTypeSchema::String)], - HostTypeSchema::Unknown, + super::host_types::AgentParsedJsonObject::host_type_schema(), parse_json_object_adapter, ) } +/// `agent::preview_arguments`: parses one tool call's open arguments text into +/// the closed `AgentPreviewArguments` projection the owner-facing preview +/// renders. +/// +/// This exists because the round-7 text carriers left `guardian::action_preview` +/// reading a map that dispatch no longer fills (a text-only tool call reports +/// `has_arguments: false`), so every write/execute preview failed closed with +/// `approval_preview_unavailable`. The preview needs a *closed* set of fields, +/// so the openness stops here: the host parses the text, refuses anything whose +/// known field has the wrong type, and publishes logical `Option` carriers. +/// Unknown keys are ignored on purpose — tool argument validation stays with +/// the tool adapter; this projection only decides what the preview can honestly +/// say. A parse failure is an explicit refusal (`ok: false` + `code`), never an +/// empty projection that pretends the action had no arguments. +fn preview_arguments_descriptor() -> HostFunctionDescriptor { + stack_desc( + PREVIEW_ARGUMENTS, + vec![HostParamSchema::value("arguments", HostTypeSchema::String)], + super::host_types::AgentPreviewArguments::host_type_schema(), + preview_arguments_adapter, + ) +} + +/// The inverse of `agent::parse_json_object`: canonical JSON text for a guest +/// value. This pin has no guest-side serializer and admits no `Map(Unknown)` +/// host return, so an open payload can only leave the guest as text. The +/// parameter is the single documented open input in the published catalog. +fn json_text_descriptor() -> HostFunctionDescriptor { + stack_desc( + JSON_TEXT, + vec![HostParamSchema::value("value", HostTypeSchema::Unknown)], + super::host_types::AgentJsonText::host_type_schema(), + json_text_adapter, + ) +} + fn state_read_descriptor() -> HostFunctionDescriptor { stack_desc( STATE_READ, @@ -924,11 +972,23 @@ const FS_METADATA_BOUND: u64 = if FS_METADATA_FRAME_BOUND > FS_METADATA_ERROR_BO } else { FS_METADATA_ERROR_BOUND }; -/// Exact `fs_read_value` frame: seven keys plus `true`/`false`, `"fs_read"`, -/// two u64 decimal widths and the 64-character hash. The raw payload is -/// charged separately at its own `Vec` length. -const FS_READ_KEYS: [&str; 7] = ["ok", "kind", "offset", "truncated", "len", "bytes", "hash"]; -const FS_READ_FRAME_VALUES: u64 = 4 + 7 + 20 + 5 + 20 + 64; +/// Exact `fs_read_value` frame: eight keys plus `true`/`false`, `"fs_read"`, +/// two u64 decimal widths, the present hash Option carrier and absent error +/// Option carrier. The raw payload is charged separately at its own length. +const FS_READ_KEYS: [&str; 8] = [ + "ok", + "kind", + "offset", + "truncated", + "len", + "bytes", + "hash", + "error", +]; +const FS_READ_HASH_CARRIER: u64 = json_object_frame(&["tag", "payload"], 4 + 71); +const FS_READ_ERROR_CARRIER: u64 = json_object_frame(&["tag"], 5); +const FS_READ_FRAME_VALUES: u64 = + 4 + 7 + 20 + 5 + 20 + FS_READ_HASH_CARRIER + FS_READ_ERROR_CARRIER; const FS_READ_FRAME_BOUND: u64 = json_object_frame(&FS_READ_KEYS, FS_READ_FRAME_VALUES); /// Frame size of one typed map with the given keys: braces, one colon per key, @@ -2219,20 +2279,18 @@ impl AgentHostState { .unwrap_or("") .to_string(), ); - let arguments = call - .get("arguments") - .filter(|value| value.is_object()) - .cloned() - .or_else(|| { - let text = call.get("arguments_json")?.as_str()?; - if json_object_rejected(text) { - return None; - } - serde_json::from_str::(text) - .ok() - .filter(JsonValue::is_object) - }) - .unwrap_or(JsonValue::Null); + let arguments = if let Some(encoded) = call.get("arguments_json") { + encoded + .as_str() + .filter(|text| !json_object_rejected(text)) + .and_then(|text| serde_json::from_str::(text).ok()) + } else { + Some(call.get("arguments").cloned().unwrap_or_else(|| json!({}))) + }; + let Some(arguments) = arguments.filter(JsonValue::is_object) else { + parents.clear(); + return typed_fail("malformed_payload", "tool arguments must be a JSON object"); + }; if let std::collections::hash_map::Entry::Vacant(entry) = parents.entry(key.clone()) { entry.insert(arguments); @@ -3047,7 +3105,16 @@ impl AgentHostState { .cloned() .unwrap_or(JsonValue::Null); let call = input.get("call").unwrap_or(&JsonValue::Null); - if call_arguments != arguments + // A provider call carries its arguments as JSON text, so the model's + // own text stays the canonical form and the child keeps no second + // parsed copy. An empty object here means "arguments were carried as + // text"; the identity checks below still bind the call, and the text + // already passed the digest and object checks above. + let arguments_carried_as_text = call_arguments.is_null() + || call_arguments + .as_object() + .is_some_and(|object| object.is_empty()); + if (call_arguments != arguments && !arguments_carried_as_text) || call .get("id") .and_then(JsonValue::as_str) @@ -3058,6 +3125,12 @@ impl AgentHostState { { return pinned_call_mismatch(); } + let mut call = call.clone(); + if call_arguments != arguments + && let Some(object) = call.as_object_mut() + { + object.insert("arguments".to_string(), arguments.clone()); + } json!({ "call": call, "prepared_context": { @@ -3743,7 +3816,10 @@ impl AgentHostState { }; let value = match fs.read_range(&token, &path, offset, limit) { Ok(read) => fs_read_value(read), - Err(error) => json_to_vm_value(&capability_error_envelope(&error)), + Err(error) => declared_cap_value( + &super::host_types::AgentFsReadResult::host_type_schema(), + &capability_error_envelope(&error), + ), }; if let (Some(meter), Some(charge)) = (&meter, charge.as_mut()) && let Err(message) = meter.settle_exact(charge, vm_value_typed_bytes(&value)) @@ -3842,7 +3918,10 @@ impl AgentHostState { }; match processes.poll(&token, &handle, cursor, limit) { Ok(snapshot) => process_snapshot_value("process_poll", &snapshot), - Err(error) => json_to_vm_value(&capability_error_envelope(&error)), + Err(error) => declared_cap_value( + &super::host_types::AgentProcessSnapshot::host_type_schema(), + &capability_error_envelope(&error), + ), } } @@ -3852,7 +3931,10 @@ impl AgentHostState { }; match processes.wait(&token, &handle, timeout_ms) { Ok(snapshot) => process_snapshot_value("process_wait", &snapshot), - Err(error) => json_to_vm_value(&capability_error_envelope(&error)), + Err(error) => declared_cap_value( + &super::host_types::AgentProcessSnapshot::host_type_schema(), + &capability_error_envelope(&error), + ), } } @@ -3862,7 +3944,10 @@ impl AgentHostState { }; match processes.log(&token, &handle, cursor, limit) { Ok(snapshot) => process_snapshot_value("process_log", &snapshot), - Err(error) => json_to_vm_value(&capability_error_envelope(&error)), + Err(error) => declared_cap_value( + &super::host_types::AgentProcessSnapshot::host_type_schema(), + &capability_error_envelope(&error), + ), } } @@ -3904,38 +3989,27 @@ impl AgentHostState { } } - fn cap_artifact_put(&self, token: String, bytes: Vec, metadata: Value) -> JsonValue { + fn cap_artifact_put(&self, token: String, bytes: Vec, metadata: &JsonValue) -> JsonValue { let Some(artifacts) = self.artifacts.as_ref() else { return Self::missing_capability("artifact"); }; - let json_meta = vm_value_to_json(&metadata); - match artifacts.put(&token, &bytes, &json_meta) { - Ok(refer) => json!({ - "ok": true, - "kind": "artifact_put", - "id": refer.id, - "len": refer.len, - "hash": refer.hash, - "metadata": refer.metadata, - }), + match artifacts.put(&token, &bytes, metadata) { + Ok(refer) => artifact_ref_envelope("artifact_put", &refer), Err(error) => capability_error_envelope(&error), } } - fn cap_artifact_put_result(&self, token: String, bytes: Vec, metadata: Value) -> JsonValue { + fn cap_artifact_put_result( + &self, + token: String, + bytes: Vec, + metadata: &JsonValue, + ) -> JsonValue { let Some(artifacts) = self.artifacts.as_ref() else { return Self::missing_capability("artifact"); }; - let json_meta = vm_value_to_json(&metadata); - match artifacts.put_result(&token, &bytes, &json_meta) { - Ok(refer) => json!({ - "ok": true, - "kind": "artifact_put_result", - "id": refer.id, - "len": refer.len, - "hash": refer.hash, - "metadata": refer.metadata, - }), + match artifacts.put_result(&token, &bytes, metadata) { + Ok(refer) => artifact_ref_envelope("artifact_put_result", &refer), Err(error) => capability_error_envelope(&error), } } @@ -3953,6 +4027,8 @@ impl AgentHostState { "ok": true, "kind": "clock_monotonic", "ms": ms, + "code": "", + "message": "", }), _ => capability_error_envelope(&CapabilityError::new( "internal_error", @@ -3977,7 +4053,8 @@ impl AgentHostState { fn cap_artifact_get(&self, token: String, id: String) -> Value { let Some(artifacts) = self.artifacts.as_ref() else { - return json_to_vm_value(&Self::missing_capability("artifact")); + return declared_artifact_get(&Self::missing_capability("artifact")) + .unwrap_or(Value::Null); }; match artifacts.get(&token, &id) { Ok(bytes) => Value::map(vec![ @@ -3988,8 +4065,11 @@ impl AgentHostState { Value::Int(i64::try_from(bytes.len()).unwrap_or(i64::MAX)), ), (Value::string("bytes"), Value::bytes(bytes)), + (Value::string("error"), option::none_carrier()), ]), - Err(error) => json_to_vm_value(&capability_error_envelope(&error)), + Err(error) => { + declared_artifact_get(&capability_error_envelope(&error)).unwrap_or(Value::Null) + } } } @@ -3998,14 +4078,7 @@ impl AgentHostState { return Self::missing_capability("artifact"); }; match artifacts.reference(&token, &id) { - Ok(refer) => json!({ - "ok": true, - "kind": "artifact_reference", - "id": refer.id, - "len": refer.len, - "hash": refer.hash, - "metadata": refer.metadata, - }), + Ok(refer) => artifact_ref_envelope("artifact_reference", &refer), Err(error) => capability_error_envelope(&error), } } @@ -4177,18 +4250,277 @@ pub fn register_agent_host_functions( } fn provider_call_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { - let request = args.first().cloned().unwrap_or(Value::Null); - validate_provider_json_resources(vm, &request)?; + let text = match args.first() { + Some(Value::String(value)) => value.as_str(), + Some(other) => { + return provider_failure( + vm, + "invalid_request", + &format!( + "provider request must cross as serialized JSON text, got {}", + value_kind(other) + ), + ); + } + None => { + return provider_failure(vm, "invalid_request", "provider request text is missing"); + } + }; + let mut request = match parse_open_payload_text(text) { + Ok(request) => request, + Err((code, message)) => return provider_failure(vm, code, message), + }; + let fields = request.as_object_mut().expect("validated request object"); + if let Some(encoded) = fields.remove("_refreshed_messages_json") { + let messages = encoded + .as_str() + .and_then(|text| serde_json::from_str::(text).ok()); + let Some(messages @ JsonValue::Array(_)) = messages else { + return provider_failure( + vm, + "invalid_request", + "refreshed messages must be a JSON array", + ); + }; + fields.insert("messages".to_string(), messages); + } + // The harness carries open tool results as JSON text. Reconstitute only + // tool-result blocks here, leaving canonical durable JSON untouched. + if let Some(messages) = fields.get_mut("messages").and_then(JsonValue::as_array_mut) { + for message in messages { + let Some(blocks) = message.get_mut("content").and_then(JsonValue::as_array_mut) else { + continue; + }; + for block in blocks { + if block.get("type").and_then(JsonValue::as_str) == Some("tool_result") + && let Some(text) = block.get("result").and_then(JsonValue::as_str) + { + block["result"] = match parse_open_payload_text(text) { + Ok(result) => result, + Err((code, message)) => return provider_failure(vm, code, message), + }; + } + } + } + } + if value_has_unrepresentable(&request) { + return provider_failure( + vm, + "invalid_request", + "JSON payload exceeds the configured depth", + ); + } + // Text cannot smuggle a live resource handle past the guest, but the parsed + // payload is still checked exactly as the native parameter used to be: a + // resource-backed int is refused before the provider sees it. + validate_provider_json_resources(vm, &json_to_vm_value(&request))?; let state = installed_state(vm)?; - let json = vm_value_to_json(&request); - return_json(state.provider_call(&json)) + provider_envelope(vm, state.provider_call(&request)) +} + +/// A typed provider failure for a request that never reached the provider. +fn provider_failure(vm: &mut Vm, code: &str, message: &str) -> VmResult { + provider_envelope(vm, typed_fail(code, message)) +} + +/// Parses one open payload that crossed the boundary as text. +/// +/// The guest can only send text, so the host owns the whole check: the 4 MiB +/// byte bound, JSON validity, depth, and "is a JSON object". The codes are the +/// same class the capability adapters already publish, so a refusal is a typed +/// terminal failure rather than a VM crash. +fn parse_open_payload_text(text: &str) -> Result { + if text.len() > JSON_INPUT_MAX_BYTES { + return Err(( + "payload_too_large", + "JSON payload exceeds the configured bound", + )); + } + let parsed = serde_json::from_str::(text) + .map_err(|_| ("invalid_request", "JSON payload is not valid JSON"))?; + if !parsed.is_object() { + return Err(("invalid_request", "JSON payload must be a JSON object")); + } + if value_has_unrepresentable(&parsed) { + return Err(( + "invalid_request", + "JSON payload exceeds the configured depth", + )); + } + Ok(parsed) +} + +fn value_kind(value: &Value) -> &'static str { + match value { + Value::Null => "null", + Value::Int(_) => "int", + Value::Float(_) => "float", + Value::Bool(_) => "bool", + Value::String(_) => "string", + Value::Bytes(_) => "bytes", + Value::Array(_) => "array", + Value::Map(_) => "map", + Value::Callable(_) => "callable", + } +} + +/// Builds the declared `AgentProviderEnvelope` natively. +/// +/// Only the two bodies are provider-defined, so they cross as JSON text (the +/// open-payload rule); the envelope itself is fixed and is validated inside the +/// adapter. A non-object body is reported as a typed failure instead of being +/// handed to the guest as an empty body. +fn provider_envelope(vm: &mut Vm, envelope: JsonValue) -> VmResult { + let ok = envelope.get("ok").and_then(JsonValue::as_bool) == Some(true); + let body = if ok { "response" } else { "error" }; + let (ok, mut body) = match envelope.get(body) { + Some(object @ JsonValue::Object(_)) => (ok, object.clone()), + _ if ok => ( + false, + json!({"code": "malformed_payload", "message": "provider response is not an object"}), + ), + _ => ( + false, + json!({"code": "malformed_payload", "message": "provider error is not an object"}), + ), + }; + if ok { + // A guest has no dynamic map to put an open `arguments` object into, so + // the existing `arguments_json` spelling is filled in for every tool + // call. The original key is left untouched. + if let Some(calls) = body.get_mut("tool_calls").and_then(JsonValue::as_array_mut) { + for call in calls.iter_mut() { + let Some(call) = call.as_object_mut() else { + continue; + }; + if call.contains_key("arguments_json") { + continue; + } + let Some(arguments) = call.get("arguments").filter(|value| value.is_object()) + else { + continue; + }; + match serde_json::to_string(arguments) { + Ok(text) => { + call.insert("arguments_json".to_string(), JsonValue::String(text)); + } + Err(_) => { + call.remove("arguments_json"); + } + } + } + } + } + let text = serde_json::to_string(&body).map_err(|_| { + VmError::HostError("provider envelope body is not serializable".to_string()) + })?; + // The provider body owns this bound: the guest decodes whatever the host + // hands it, so a body past the 4 MiB open-payload bound is refused here + // with its own code instead of being published as an oversized text. + let (ok, text) = if text.len() > JSON_INPUT_MAX_BYTES { + let refusal = typed_fail( + "provider_body_too_large", + "provider envelope body exceeds the 4 MiB bound", + ); + let refusal = serde_json::to_string(refusal.get("error").unwrap_or(&JsonValue::Null)) + .map_err(|_| { + VmError::HostError("provider refusal body is not serializable".to_string()) + })?; + (false, refusal) + } else { + (ok, text) + }; + let (error, response) = if ok { + (String::new(), text) + } else { + (text, String::new()) + }; + let value = Value::map(vec![ + (Value::string("ok"), Value::Bool(ok)), + (Value::string("error"), Value::string(error)), + (Value::string("response"), Value::string(response)), + ]); + super::host_shapes::shaped( + vm, + &super::host_types::AgentProviderEnvelope::host_type_schema(), + value, + ) } fn refresh_context_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { - let context = args.first().cloned().unwrap_or(Value::Null); - validate_provider_json_resources(vm, &context)?; + let text = match args.first() { + Some(Value::String(value)) => value.as_str(), + _ => { + return refresh_envelope( + vm, + &typed_fail( + "invalid_request", + "refresh context must cross as serialized JSON text", + ), + ); + } + }; + let context = match parse_open_payload_text(text) { + Ok(context) => context, + Err((code, message)) => return refresh_envelope(vm, &typed_fail(code, message)), + }; + validate_provider_json_resources(vm, &json_to_vm_value(&context))?; let state = installed_state(vm)?; - return_json(state.refresh_context(&vm_value_to_json(&context))) + refresh_envelope(vm, &state.refresh_context(&context)) +} + +/// Carries successful durable history as JSON text so the guest need not +/// decode an open message array. Refusals never publish a transcript. +fn refresh_envelope(vm: &mut Vm, answer: &JsonValue) -> VmResult { + let storage_available = answer + .get("storage_available") + .and_then(JsonValue::as_bool) + .unwrap_or(false); + let ok = answer.get("ok") == Some(&JsonValue::Bool(true)); + let messages = answer.get("messages").filter(|value| value.is_array()); + let messages_json = if ok && storage_available { + messages.map(JsonValue::to_string) + } else { + Some(String::new()) + }; + let ok = ok && messages_json.is_some(); + let (code, message) = match answer.get("error") { + Some(error) if !error.is_null() => ( + error + .get("code") + .and_then(JsonValue::as_str) + .unwrap_or("context_refresh_failed") + .to_string(), + error + .get("message") + .and_then(JsonValue::as_str) + .unwrap_or("") + .to_string(), + ), + _ if ok => (String::new(), String::new()), + _ => ( + "context_refresh_failed".to_string(), + "context refresh failed without a structured error".to_string(), + ), + }; + let value = Value::map(vec![ + (Value::string("ok"), Value::Bool(ok)), + ( + Value::string("storage_available"), + Value::Bool(storage_available), + ), + (Value::string("code"), Value::string(code)), + (Value::string("message"), Value::string(message)), + ( + Value::string("messages_json"), + Value::string(messages_json.unwrap_or_default()), + ), + ]); + super::host_shapes::shaped( + vm, + &super::host_types::AgentRefreshEnvelope::host_type_schema(), + value, + ) } fn utf8_byte_len_adapter(_vm: &mut Vm, args: &[Value]) -> VmResult { @@ -4229,16 +4561,227 @@ fn sleep_ms_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { fn control_check_adapter(vm: &mut Vm, _args: &[Value]) -> VmResult { let state = installed_state(vm)?; - let result = state.control_error().unwrap_or_else(|| json!({"ok": true})); - return_json(result) + let error_schema = super::host_types::AgentCapabilityError::host_type_schema(); + // `control_error` carries the whole failure envelope; the declared result + // field is only its structured error object, so it is unwrapped and its + // code/message are checked explicitly. A failure envelope without that + // object is a host contract violation and is rejected as one, never + // replaced by a default code. + let (ok, error) = match state.control_error() { + None => (true, option::none_carrier()), + Some(envelope) => { + let failure = envelope.get("error").and_then(JsonValue::as_object); + let code = failure + .and_then(|failure| failure.get("code")) + .and_then(JsonValue::as_str); + let message = failure + .and_then(|failure| failure.get("message")) + .and_then(JsonValue::as_str); + let (Some(code), Some(message)) = (code, message) else { + return Err(VmError::HostError(format!( + "control_check: control failure has no structured error object ({envelope})" + ))); + }; + let payload = Value::map(vec![ + (Value::string("code"), Value::string(code)), + (Value::string("message"), Value::string(message)), + ]); + let carrier = super::host_shapes::logical_option(&error_schema, Some(&payload))?; + (false, carrier) + } + }; + let value = Value::map(vec![ + (Value::string("ok"), Value::Bool(ok)), + (Value::string("error"), error), + ]); + super::host_shapes::shaped( + vm, + &super::host_types::AgentControlResult::host_type_schema(), + value, + ) +} + +fn parse_json_object_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { + let parsed = match args.first() { + Some(Value::String(value)) => parse_json_object(value.as_str()), + _ => malformed_json_object(), + }; + super::host_shapes::shaped( + vm, + &super::host_types::AgentParsedJsonObject::host_type_schema(), + parsed_json_object_value(parsed), + ) +} + +/// `AgentParsedJsonObject.value` carries the parsed object as serialized text. +fn parsed_json_object_value(parsed: JsonValue) -> Value { + let value = match parsed.get("value") { + Some(JsonValue::Object(object)) => { + serde_json::to_string(object).unwrap_or_else(|_| "{}".to_string()) + } + _ => String::new(), + }; + let text = |name: &str| match parsed.get(name) { + Some(JsonValue::String(value)) => value.clone(), + _ => String::new(), + }; + Value::map(vec![ + ( + Value::string("ok"), + Value::Bool( + parsed + .get("ok") + .and_then(JsonValue::as_bool) + .unwrap_or(false), + ), + ), + (Value::string("code"), Value::string(text("code"))), + (Value::string("message"), Value::string(text("message"))), + (Value::string("value"), Value::string(value)), + ]) +} + +/// Reads one declared field of the tool arguments object as a logical `Option`. +/// +/// * absent or JSON `null` — `None` (the provider did not send the field); +/// * the declared JSON type — `Some(value)`; +/// * anything else — a typed refusal naming the offending field. +/// +/// The refusal is the point: a `path: 7` must not silently become "no path", +/// which would turn a real write request into an unreviewable action and make +/// the approval fail closed for the wrong reason. +fn preview_arguments_field( + object: &JsonValue, + name: &'static str, + kind: PreviewFieldKind, +) -> Result { + let Some(raw) = object.get(name) else { + return Ok(JsonValue::Null); + }; + if raw.is_null() { + return Ok(JsonValue::Null); + } + let expected = kind.expected(); + match (kind, raw) { + (PreviewFieldKind::Text, JsonValue::String(value)) => Ok(JsonValue::String(value.clone())), + (PreviewFieldKind::Bool, JsonValue::Bool(value)) => Ok(JsonValue::Bool(*value)), + (PreviewFieldKind::Int, JsonValue::Number(value)) => match value.as_i64() { + Some(value) => Ok(JsonValue::from(value)), + None => Err(( + "preview_arguments_invalid", + format!("tool argument '{name}' must be an integer"), + )), + }, + (PreviewFieldKind::Strings, JsonValue::Array(items)) => { + let mut out = Vec::with_capacity(items.len()); + for item in items { + match item { + JsonValue::String(value) => out.push(JsonValue::String(value.clone())), + _ => { + return Err(( + "preview_arguments_invalid", + format!("tool argument '{name}' must be an array of strings"), + )); + } + } + } + Ok(JsonValue::Array(out)) + } + _ => Err(( + "preview_arguments_invalid", + format!("tool argument '{name}' must be {expected}"), + )), + } +} + +#[derive(Clone, Copy)] +enum PreviewFieldKind { + Text, + Bool, + Int, + Strings, +} + +impl PreviewFieldKind { + fn expected(self) -> &'static str { + match self { + PreviewFieldKind::Text => "a string", + PreviewFieldKind::Bool => "a boolean", + PreviewFieldKind::Int => "an integer", + PreviewFieldKind::Strings => "an array of strings", + } + } } -fn parse_json_object_adapter(_vm: &mut Vm, args: &[Value]) -> VmResult { +fn preview_arguments_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { + let refusal = + |code: &str, message: &str| json!({ "ok": false, "code": code, "message": message }); let text = match args.first() { Some(Value::String(value)) => value.as_str(), - _ => return return_json(malformed_json_object()), + _ => { + return return_declared( + vm, + &super::host_types::AgentPreviewArguments::host_type_schema(), + &refusal( + "invalid_request", + "tool arguments must cross as serialized JSON text", + ), + ); + } + }; + // Same bounded parser every other open payload uses: 4 MiB, valid JSON, + // bounded depth, and a JSON object. + let parsed = match parse_open_payload_text(text) { + Ok(parsed) => parsed, + Err((code, message)) => { + return return_declared( + vm, + &super::host_types::AgentPreviewArguments::host_type_schema(), + &refusal(code, message), + ); + } }; - return_json(parse_json_object(text)) + let mut envelope = serde_json::Map::new(); + envelope.insert("ok".to_string(), JsonValue::Bool(true)); + envelope.insert("code".to_string(), JsonValue::from("")); + envelope.insert("message".to_string(), JsonValue::from("")); + macro_rules! field { + ($name:literal, $kind:ident) => { + match preview_arguments_field(&parsed, $name, PreviewFieldKind::$kind) { + Ok(value) => { + envelope.insert($name.to_string(), value); + } + Err((code, message)) => { + return return_declared( + vm, + &super::host_types::AgentPreviewArguments::host_type_schema(), + &refusal(code, &message), + ); + } + } + }; + } + field!("path", Text); + field!("content", Text); + field!("old_string", Text); + field!("new_string", Text); + field!("replace_all", Bool); + field!("argv", Strings); + field!("stdin", Text); + field!("cwd", Text); + field!("timeout_ms", Int); + field!("max_output_bytes", Int); + field!("background", Bool); + field!("action", Text); + field!("process_id", Text); + field!("data", Text); + field!("offset", Int); + field!("limit", Int); + return_declared( + vm, + &super::host_types::AgentPreviewArguments::host_type_schema(), + &JsonValue::Object(envelope), + ) } fn state_read_adapter(vm: &mut Vm, _args: &[Value]) -> VmResult { @@ -4296,6 +4839,134 @@ fn malformed_json_object() -> JsonValue { }) } +/// Serializes one guest value to canonical JSON text. +/// +/// Refuses anything the JSON boundary cannot represent (bytes, callables, NaN) +/// and anything past the payload bound. The caller gets a typed envelope, not a +/// VM error, because every consumer reports these as `malformed_payload`. +fn json_text_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { + let text = match args.first() { + Some(value) => match vm_value_to_json(value) { + value if value_has_unrepresentable(&value) => Err("value is not JSON representable"), + JsonValue::Null => Ok("null".to_string()), + other => serde_json::to_string(&other).map_err(|_| "value is not JSON representable"), + }, + None => Err("value is missing"), + }; + let envelope = match text { + Ok(text) if text.len() <= JSON_TEXT_MAX_BYTES => json!({ + "ok": true, + "code": "", + "message": "", + "text": text, + "bytes": i64::try_from(text.len()).unwrap_or(i64::MAX), + }), + Ok(_) => json!({ + "ok": false, + "code": "payload_too_large", + "message": "serialized JSON payload exceeds the configured bound", + "text": "", + "bytes": 0, + }), + Err(message) => json!({ + "ok": false, + "code": "malformed_payload", + "message": message, + "text": "", + "bytes": 0, + }), + }; + return_declared( + vm, + &super::host_types::AgentJsonText::host_type_schema(), + &envelope, + ) +} + +/// `vm_value_to_json` renders bytes as an empty array; a payload the guest +/// cannot round-trip is refused instead of silently corrupted. +fn value_has_unrepresentable(value: &JsonValue) -> bool { + fn walk(value: &JsonValue, depth: usize) -> bool { + if depth > JSON_INPUT_MAX_DEPTH { + return true; + } + match value { + JsonValue::Array(items) => items.iter().any(|item| walk(item, depth + 1)), + JsonValue::Object(fields) => fields.values().any(|item| walk(item, depth + 1)), + _ => false, + } + } + walk(value, 0) +} + +/// Parses one open payload that crossed the boundary as text. +/// +/// The guest can only send text here, so the host owns the whole check: byte +/// bound, UTF-8, depth, and "is a JSON object". Every failure keeps the +/// existing capability error code so callers observe the same typed refusals. +fn parse_metadata_text(text: &str) -> Result { + if text.len() > JSON_INPUT_MAX_BYTES { + return Err(capability_error_envelope(&CapabilityError::new( + "payload_too_large", + "JSON payload exceeds the configured bound", + ))); + } + let parsed = serde_json::from_str::(text).map_err(|_| { + capability_error_envelope(&CapabilityError::new( + "invalid_metadata", + "JSON payload is not valid JSON", + )) + })?; + let JsonValue::Object(map) = parsed else { + return Err(capability_error_envelope(&CapabilityError::new( + "invalid_metadata", + "JSON payload must be an object", + ))); + }; + if value_has_unrepresentable(&JsonValue::Object(map.clone())) { + return Err(capability_error_envelope(&CapabilityError::new( + "invalid_metadata", + "JSON payload exceeds the configured depth", + ))); + } + Ok(JsonValue::Object(map)) +} + +/// One metadata argument, accepted either as the legacy native map (existing +/// internal callers) or, on the RSS boundary, as serialized text. +fn metadata_argument(args: &[Value], index: usize) -> Result { + match args.get(index) { + Some(Value::String(text)) => parse_metadata_text(text.as_str()), + Some(value) => { + let json = vm_value_to_json(value); + if json.is_object() { + Ok(json) + } else { + Err(capability_error_envelope(&CapabilityError::new( + "invalid_metadata", + "JSON payload must be an object", + ))) + } + } + None => Err(capability_error_envelope(&CapabilityError::new( + "invalid_metadata", + "JSON payload is missing", + ))), + } +} + +/// `decode_then` for adapters whose value path answers with a JSON envelope +/// (capability errors are envelopes, not VM errors). +fn decode_then_value( + decode: impl FnOnce() -> Result, + then: impl FnOnce(T) -> JsonValue, +) -> JsonValue { + match decode() { + Ok(value) => then(value), + Err(error) => error, + } +} + fn parse_json_object(text: &str) -> JsonValue { if text.len() > PARSE_JSON_OBJECT_MAX_BYTES || !text.is_char_boundary(text.len()) { return malformed_json_object(); @@ -4312,15 +4983,29 @@ fn parse_json_object(text: &str) -> JsonValue { } fn tool_replay_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { - let metadata = args.first().cloned().unwrap_or(Value::Null); let state = installed_state(vm)?; - return_json(state.capability_replay(&vm_value_to_json(&metadata))) + let envelope = match metadata_argument(args, 0) { + Ok(metadata) => state.capability_replay(&metadata), + Err(error) => error, + }; + return_declared( + vm, + &super::host_types::AgentToolEnvelope::host_type_schema(), + &tool_envelope(&envelope), + ) } fn approval_attest_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { - let metadata = args.first().cloned().unwrap_or(Value::Null); let state = installed_state(vm)?; - return_json(state.capability_approval_attest(&vm_value_to_json(&metadata))) + let envelope = match metadata_argument(args, 0) { + Ok(metadata) => state.capability_approval_attest(&metadata), + Err(error) => error, + }; + return_declared( + vm, + &super::host_types::AgentToolEnvelope::host_type_schema(), + &tool_envelope(&envelope), + ) } fn approval_storage_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { @@ -4328,21 +5013,151 @@ fn approval_storage_adapter(vm: &mut Vm, args: &[Value]) -> VmResult value.as_str(), _ => "", }; - let payload = args.get(1).cloned().unwrap_or(Value::Null); - validate_provider_json_resources(vm, &payload)?; + let text = match args.get(1) { + Some(Value::String(value)) => value.as_str(), + _ => { + return approval_envelope( + vm, + &typed_fail( + "invalid_request", + "approval payload must cross as serialized JSON text", + ), + ); + } + }; + let payload = match parse_open_payload_text(text) { + Ok(payload) => payload, + Err((code, message)) => return approval_envelope(vm, &typed_fail(code, message)), + }; + validate_provider_json_resources(vm, &json_to_vm_value(&payload))?; let state = installed_state(vm)?; - return_json(state.approval_storage(operation, &vm_value_to_json(&payload))) + approval_envelope(vm, &state.approval_storage(operation, &payload)) +} + +/// Projects one storage answer onto the fixed `AgentApprovalStorageEnvelope`. +/// +/// The storage command answers with `{ok, result:{columns, rows}|{rows_affected}} +/// or `{ok:false, error:{code,message}}`. Rows are positional SQL tuples, so the +/// projection names the twelve columns the agent loop reads; a row that is +/// shorter than that is a host bug and fails closed instead of inventing zeroes. +fn approval_envelope(vm: &mut Vm, answer: &JsonValue) -> VmResult { + let ok = answer.get("ok") == Some(&JsonValue::Bool(true)); + let (code, message) = match answer.get("error") { + Some(error) if !error.is_null() => ( + error + .get("code") + .and_then(JsonValue::as_str) + .unwrap_or("approval_storage_unavailable") + .to_string(), + error + .get("message") + .and_then(JsonValue::as_str) + .unwrap_or("approval storage is unavailable") + .to_string(), + ), + _ if ok => (String::new(), String::new()), + _ => ( + "approval_storage_unavailable".to_string(), + "approval storage failed without a structured error".to_string(), + ), + }; + let result = answer.get("result").unwrap_or(&JsonValue::Null); + let mut rows = Vec::new(); + if let Some(items) = result.get("rows").and_then(JsonValue::as_array) { + for row in items { + let JsonValue::Array(cells) = row else { + return Err(VmError::HostError( + "approval storage row is not a positional tuple".to_string(), + )); + }; + if cells.len() < AGENT_APPROVAL_ROW_COLUMNS { + return Err(VmError::HostError(format!( + "approval storage row has {} columns, expected {AGENT_APPROVAL_ROW_COLUMNS}", + cells.len() + ))); + } + let text = |index: usize| Value::string(json_cell_text(&cells[index])); + let int = |index: usize| Value::Int(json_cell_int(&cells[index])); + rows.push(Value::map(vec![ + (Value::string("id"), text(0)), + (Value::string("run_id"), text(1)), + (Value::string("session_id"), text(2)), + (Value::string("tool_call_id"), text(3)), + (Value::string("tool_name"), text(4)), + (Value::string("arguments_json"), text(5)), + (Value::string("risk_class"), text(6)), + (Value::string("state"), text(7)), + (Value::string("decision_scope"), text(8)), + (Value::string("one_time"), int(9)), + (Value::string("requested_at_ms"), int(10)), + (Value::string("expires_at_ms"), int(11)), + ])); + } + } + let rows_affected = result + .get("rows_affected") + .and_then(JsonValue::as_i64) + .unwrap_or(0); + let created = result + .get("created") + .and_then(JsonValue::as_i64) + .unwrap_or(0); + let value = Value::map(vec![ + (Value::string("ok"), Value::Bool(ok)), + (Value::string("code"), Value::string(code)), + (Value::string("message"), Value::string(message)), + (Value::string("rows"), Value::Array(rows.into())), + (Value::string("rows_affected"), Value::Int(rows_affected)), + (Value::string("created"), Value::Int(created)), + ]); + super::host_shapes::shaped( + vm, + &super::host_types::AgentApprovalStorageEnvelope::host_type_schema(), + value, + ) +} + +/// The columns the agent loop reads from one `approvals` row. +const AGENT_APPROVAL_ROW_COLUMNS: usize = 12; + +fn json_cell_text(cell: &JsonValue) -> String { + match cell { + JsonValue::String(value) => value.clone(), + JsonValue::Null => String::new(), + other => other.to_string(), + } +} + +fn json_cell_int(cell: &JsonValue) -> i64 { + match cell { + JsonValue::Number(value) => value.as_i64().unwrap_or(0), + JsonValue::String(value) => value.parse::().unwrap_or(0), + _ => 0, + } } fn tool_prepare_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { - let metadata = args.first().cloned().unwrap_or(Value::Null); let state = installed_state(vm)?; + let metadata = match metadata_argument(args, 0) { + Ok(metadata) => metadata, + Err(error) => { + return return_declared( + vm, + &super::host_types::AgentToolEnvelope::host_type_schema(), + &tool_envelope(&error), + ); + } + }; // The injected AfterIntent cut is a Native crash, not a tool refusal: it // traps the caller VM so the run fails without a tool terminal. let envelope = state - .capability_prepare(&vm_value_to_json(&metadata)) + .capability_prepare(&metadata) .map_err(VmError::HostError)?; - return_json(envelope) + return_declared( + vm, + &super::host_types::AgentToolEnvelope::host_type_schema(), + &tool_envelope(&envelope), + ) } fn tool_commit_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { @@ -4350,19 +5165,76 @@ fn tool_commit_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { Some(Value::String(value)) => value.to_string(), _ => String::new(), }; - let result = args.get(1).cloned().unwrap_or(Value::Null); let state = installed_state(vm)?; - return_json(state.capability_commit_value(&token, &result)) -} - -/// The exact typed bridge request, used only to count its full encoding with a -/// bounded counting writer before any Native-owned copy or parse. -#[derive(serde::Serialize)] -struct TypedChildRequest<'a> { - canonical_arguments: &'a str, + let envelope = match metadata_argument(args, 1) { + // The lifecycle value path keeps its borrowed-value precharge: the + // parsed text is re-materialized as one native value and metered there. + Ok(result) => state.capability_commit_value(&token, &json_to_vm_value(&result)), + Err(error) => error, + }; + return_declared( + vm, + &super::host_types::AgentToolEnvelope::host_type_schema(), + &tool_envelope(&envelope), + ) } -struct CountingWriter(u64); +/// Projects one capability lifecycle envelope onto the declared tool shape. +/// The lifecycle still owns the bytes/budget accounting, so this projection +/// happens after it: it only renames `execution_token`, keeps `deadline_ms` and +/// `call_id`, and serializes the replayed/committed canonical result to text. +fn tool_envelope(envelope: &JsonValue) -> JsonValue { + let text = |name: &str| match envelope.get(name) { + Some(JsonValue::String(value)) => JsonValue::String(value.clone()), + Some(value) if !value.is_null() => { + JsonValue::String(serde_json::to_string(value).unwrap_or_default()) + } + _ => JsonValue::String(String::new()), + }; + let number = |name: &str| match envelope.get(name) { + Some(JsonValue::Number(value)) => JsonValue::Number(value.clone()), + _ => JsonValue::from(0), + }; + let ok = envelope.get("ok") == Some(&JsonValue::Bool(true)); + let mut out = serde_json::Map::new(); + out.insert("ok".into(), JsonValue::Bool(ok)); + out.insert("kind".into(), text("kind")); + out.insert("execution_token".into(), text("execution_token")); + out.insert("deadline_ms".into(), number("deadline_ms")); + out.insert("call_id".into(), text("call_id")); + let result = if envelope["kind"] == "replay" + && let Some(mut replayed) = envelope.get("result").cloned() + && let Some(fields) = replayed.as_object_mut() + { + // Child tools decode a typed result view with its open data as text. + // The pinned outcome witness still owns the original canonical object. + let data = fields.remove("data").unwrap_or_else(|| json!({})); + fields.insert("data".into(), JsonValue::String(data.to_string())); + fields.entry("content").or_insert_with(|| json!("")); + fields.entry("truncated").or_insert_with(|| json!(false)); + fields.entry("artifacts").or_insert_with(|| json!([])); + JsonValue::String(replayed.to_string()) + } else { + text("result") + }; + out.insert("result".into(), result); + match envelope.get("error") { + Some(error) if !error.is_null() => { + out.insert("error".into(), error.clone()); + } + _ => {} + } + JsonValue::Object(out) +} + +/// The exact typed bridge request, used only to count its full encoding with a +/// bounded counting writer before any Native-owned copy or parse. +#[derive(serde::Serialize)] +struct TypedChildRequest<'a> { + canonical_arguments: &'a str, +} + +struct CountingWriter(u64); impl std::io::Write for CountingWriter { fn write(&mut self, buffer: &[u8]) -> std::io::Result { @@ -4431,14 +5303,26 @@ fn canonical_child_result( fn invoke_pinned_tool_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { let request = args.first().cloned().unwrap_or(Value::Null); let state = installed_state(vm)?; - return_json(state.invoke_pinned_tool(&vm_value_to_json(&request))) + let outcome = state.invoke_pinned_tool(&vm_value_to_json(&request)); + // The declared shape carries one logical `Option` slot for the managed + // publication: the adapter decides presence, never the guest. + return_declared( + vm, + &super::host_types::PinnedToolInvokeResult::host_type_schema(), + &outcome, + ) } fn invoke_pinned_plugin_child_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { // decode the named request directly from the borrowed VM value: the // canonical string is never copied before it is bounded and charged. let state = installed_state(vm)?; - return_json(state.invoke_pinned_plugin_child(args.first().unwrap_or(&Value::Null))) + let outcome = state.invoke_pinned_plugin_child(args.first().unwrap_or(&Value::Null)); + return_declared( + vm, + &super::host_types::PinnedToolInvokeResult::host_type_schema(), + &outcome, + ) } fn pinned_outcome( @@ -4449,6 +5333,10 @@ fn pinned_outcome( message: &str, canonical_result: &str, ) -> JsonValue { + let truncated = serde_json::from_str::(canonical_result) + .ok() + .and_then(|result| result.get("truncated").and_then(JsonValue::as_bool)) + .unwrap_or(false); json!({ "ok": ok, "text": content, @@ -4457,6 +5345,7 @@ fn pinned_outcome( "code": code, "message": message, "canonical_result": canonical_result, + "truncated": truncated, }) } @@ -4511,6 +5400,7 @@ fn pinned_tool_result_with_limit(value: &JsonValue, max_bytes: usize) -> JsonVal .or_else(|| { value .get("error") + .and_then(|error| error.get("payload").or(Some(error))) .and_then(|error| error.get("code")) .and_then(JsonValue::as_str) }) @@ -4521,12 +5411,35 @@ fn pinned_tool_result_with_limit(value: &JsonValue, max_bytes: usize) -> JsonVal .or_else(|| { value .get("error") + .and_then(|error| error.get("payload").or(Some(error))) .and_then(|error| error.get("message")) .and_then(JsonValue::as_str) }) .unwrap_or(""); let kind = value.get("kind").and_then(JsonValue::as_str).unwrap_or(""); - let canonical = match serde_json::to_string(value) { + // Preserve the exact canonical data shape for native tool replay. The + // delegated child result has its own JSON-text projection at its boundary. + let mut canonical_value = value + .get("content_block") + .and_then(|block| block.get("result")) + .filter(|result| result.is_object()) + .cloned() + .unwrap_or_else(|| value.clone()); + if !canonical_value + .get("content") + .is_some_and(JsonValue::is_string) + && canonical_value.get("kind").is_none() + { + canonical_value = json!({ + "ok": explicit_ok.unwrap_or(false), + "content": content, + "data": {}, + "error": { "code": code, "message": message }, + "truncated": false, + "artifacts": [], + }); + } + let canonical = match serde_json::to_string(&canonical_value) { Ok(encoded) if encoded.len() <= max_bytes => encoded, Ok(_) => { return pinned_outcome( @@ -4706,7 +5619,13 @@ fn cap_fs_metadata_adapter(vm: &mut Vm, args: &[Value]) -> VmResult arg_string(args, 1, "path")?, )) }, - |(token, path)| return_json(state.cap_fs_metadata(token, path)), + |(token, path)| { + return_declared( + vm, + &super::host_types::AgentFsMetadataResult::host_type_schema(), + &state.cap_fs_metadata(token, path), + ) + }, ) } @@ -4722,7 +5641,25 @@ fn cap_fs_read_range_adapter(vm: &mut Vm, args: &[Value]) -> VmResult VmResult { arg_positive_usize(args, 3, "limit")?, )) }, - |(token, path, cursor, limit)| return_json(state.cap_fs_list(token, path, cursor, limit)), + |(token, path, cursor, limit)| { + return_declared( + vm, + &super::host_types::AgentFsListResult::host_type_schema(), + &state.cap_fs_list(token, path, cursor, limit), + ) + }, ) } @@ -4754,7 +5697,11 @@ fn cap_fs_write_atomic_adapter(vm: &mut Vm, args: &[Value]) -> VmResult VmResult VmResult VmResult VmResult )) }, |(token, handle, cursor, limit)| { - return_value(state.cap_process_log(token, handle, cursor, limit)) + super::host_shapes::shaped( + vm, + &super::host_types::AgentProcessSnapshot::host_type_schema(), + state.cap_process_log(token, handle, cursor, limit), + ) }, ) } @@ -4840,7 +5803,11 @@ fn cap_process_write_adapter(vm: &mut Vm, args: &[Value]) -> VmResult VmResult VmResult VmResult { let state = installed_state(vm)?; - decode_then( + let envelope = decode_then_value( || { Ok(( arg_string(args, 0, "execution_token")?, arg_bytes(args, 1, "bytes")?, - args.get(2).cloned().unwrap_or(Value::Null), + metadata_argument(args, 2)?, )) }, - |(token, bytes, metadata)| return_json(state.cap_artifact_put(token, bytes, metadata)), + |(token, bytes, metadata)| state.cap_artifact_put(token, bytes, &metadata), + ); + return_declared( + vm, + &super::host_types::AgentArtifactRef::host_type_schema(), + &envelope, ) } fn cap_artifact_put_result_adapter(vm: &mut Vm, args: &[Value]) -> VmResult { let state = installed_state(vm)?; - decode_then( + let envelope = decode_then_value( || { Ok(( arg_string(args, 0, "execution_token")?, arg_bytes(args, 1, "bytes")?, - args.get(2).cloned().unwrap_or(Value::Null), + metadata_argument(args, 2)?, )) }, - |(token, bytes, metadata)| { - return_json(state.cap_artifact_put_result(token, bytes, metadata)) - }, + |(token, bytes, metadata)| state.cap_artifact_put_result(token, bytes, &metadata), + ); + return_declared( + vm, + &super::host_types::AgentArtifactRef::host_type_schema(), + &envelope, ) } @@ -4905,7 +5892,13 @@ fn cap_clock_monotonic_ms_adapter(vm: &mut Vm, args: &[Value]) -> VmResult VmResult VmResult { let state = installed_state(vm)?; - decode_then( + let envelope = decode_then_value( || { Ok(( arg_string(args, 0, "execution_token")?, arg_string(args, 1, "id")?, )) }, - |(token, id)| return_json(state.cap_artifact_reference(token, id)), + |(token, id)| state.cap_artifact_reference(token, id), + ); + return_declared( + vm, + &super::host_types::AgentArtifactRef::host_type_schema(), + &envelope, ) } @@ -4950,6 +5954,143 @@ fn return_value(value: Value) -> VmResult { Ok(CallOutcome::Return(CallReturn::One(value))) } +/// Projects one host JSON envelope onto the declared native shape, then +/// validates it with the VM's own validator before it leaves the adapter. +/// +/// Presence is decided by the adapter, never guessed by the guest: +/// +/// * an `Option` field is `None` when the envelope omits the field or puts a +/// physical null there, and `Some(inner)` only when a value is present; +/// * a concrete field must be present in a success envelope — a missing +/// success field is a host bug and fails closed; +/// * a concrete field may be absent in a failure envelope, where it takes its +/// declared zero (`""`/`0`/`false`/empty bytes/empty array). The guest only +/// reads those fields behind `ok`, so this keeps the fixed shape total +/// without inventing presence. +fn return_declared( + vm: &mut Vm, + schema: &HostTypeSchema, + envelope: &JsonValue, +) -> VmResult { + let failed = envelope.get("ok") != Some(&JsonValue::Bool(true)); + let value = declared_value(schema, envelope, failed)?; + super::host_shapes::shaped(vm, schema, value) +} + +fn declared_value(schema: &HostTypeSchema, raw: &JsonValue, failed: bool) -> VmResult { + match schema { + HostTypeSchema::Named { fields, .. } => { + let JsonValue::Object(map) = raw else { + return Err(VmError::HostError( + "host return envelope is not a JSON object".to_string(), + )); + }; + let mut out = Vec::with_capacity(fields.len()); + for field in fields { + let value = match map.get(&field.name) { + Some(raw) if !matches!(raw, JsonValue::Null) => declared_field(&field.ty, raw)?, + _ if matches!(field.ty, HostTypeSchema::Option(_)) => { + declared_field(&field.ty, &JsonValue::Null)? + } + _ if failed => declared_zero(&field.ty), + _ => { + return Err(VmError::HostError(format!( + "host return envelope is missing declared field '{}'", + field.name + ))); + } + }; + out.push((Value::string(&field.name), value)); + } + Ok(Value::map(out)) + } + _ => Err(VmError::HostError( + "declared host returns are named structs".to_string(), + )), + } +} + +fn declared_field(schema: &HostTypeSchema, raw: &JsonValue) -> VmResult { + match schema { + HostTypeSchema::Option(inner) => { + if matches!(raw, JsonValue::Null) { + return Ok(option::none_carrier()); + } + let inner_value = declared_field(inner, raw)?; + Ok(option::some_carrier(inner_value)) + } + HostTypeSchema::Bytes => match raw { + // Bytes never pass through JSON: the adapter that owns them builds + // the native value directly (`Value::bytes`). + JsonValue::Array(_) => Err(VmError::HostError( + "bytes fields must be built natively".to_string(), + )), + _ => Err(VmError::HostError( + "bytes fields must be built natively".to_string(), + )), + }, + HostTypeSchema::Named { .. } => declared_value(schema, raw, false), + HostTypeSchema::Array(inner) => { + let JsonValue::Array(items) = raw else { + return Err(VmError::HostError( + "host return array field is not an array".to_string(), + )); + }; + let mut out = Vec::with_capacity(items.len()); + for item in items { + out.push(declared_field(inner, item)?); + } + Ok(Value::array(out)) + } + _ => Ok(json_to_vm_value(raw)), + } +} + +/// The declared zero for a concrete field that a failure envelope omits. +fn declared_zero(schema: &HostTypeSchema) -> Value { + match schema { + HostTypeSchema::String => Value::string(""), + HostTypeSchema::Int | HostTypeSchema::Number => Value::Int(0), + HostTypeSchema::Float => Value::Float(0.0), + HostTypeSchema::Bool => Value::Bool(false), + HostTypeSchema::Bytes => Value::bytes(Vec::new()), + HostTypeSchema::Array(_) => Value::array(Vec::new()), + HostTypeSchema::Unit => Value::Null, + HostTypeSchema::Option(_) => option::none_carrier(), + _ => Value::Null, + } +} + +/// One artifact reference envelope: identity plus the durable metadata object +/// serialized to text. +fn artifact_ref_envelope(kind: &str, refer: &crate::capabilities::ArtifactRef) -> JsonValue { + json!({ + "ok": true, + "kind": kind, + "id": refer.id, + "len": i64::try_from(refer.len).unwrap_or(i64::MAX), + "hash": refer.hash, + "metadata": serde_json::to_string(&refer.metadata).unwrap_or_else(|_| "{}".to_string()), + }) +} + +/// One declared native shape for a capability failure envelope, so a value +/// path that answers with `Value` still carries every declared field. +fn declared_cap_value(schema: &HostTypeSchema, envelope: &JsonValue) -> Value { + declared_value(schema, envelope, true).unwrap_or(Value::Null) +} + +/// The artifact-get envelope as the declared native shape. `bytes` never goes +/// through JSON: the caller hands the native `Value` straight to `shaped`. +fn declared_artifact_get(envelope: &JsonValue) -> Option { + declared_value( + &super::host_types::AgentArtifactGetResult::host_type_schema(), + envelope, + true, + ) + .ok() +} + fn decode_then( decode: impl FnOnce() -> Result, then: impl FnOnce(T) -> VmResult, @@ -4979,9 +6120,14 @@ fn fs_read_value(read: FsRead) -> Value { ), (Value::string("bytes"), Value::bytes(read.bytes)), ]; - if let Some(hash) = read.hash { - fields.push((Value::string("hash"), Value::string(hash))); - } + fields.push(( + Value::string("hash"), + match read.hash { + Some(hash) => option::some_carrier(Value::string(hash)), + None => option::none_carrier(), + }, + )); + fields.push((Value::string("error"), option::none_carrier())); Value::map(fields) } @@ -5129,19 +6275,27 @@ fn process_snapshot_value(kind: &str, snapshot: &ProcessSnapshot) -> Value { (Value::string("running"), Value::Bool(snapshot.running)), ( Value::string("exit_code"), - snapshot - .exit_code - .map(i64::from) - .map(Value::Int) - .unwrap_or(Value::Null), + match snapshot.exit_code { + Some(code) => option::some_carrier(Value::Int(i64::from(code))), + None => option::none_carrier(), + }, ), ( Value::string("signal"), - snapshot - .signal - .map(i64::from) - .map(Value::Int) - .unwrap_or(Value::Null), + match snapshot.signal { + Some(signal) => option::some_carrier(Value::Int(i64::from(signal))), + None => option::none_carrier(), + }, + ), + // The nullable scalar beside its carrier: the guest reads a concrete + // number here, and presence is decided by the host, never inferred. + ( + Value::string("signal_number"), + Value::Int(snapshot.signal.map(i64::from).unwrap_or(0)), + ), + ( + Value::string("exit_number"), + Value::Int(snapshot.exit_code.map(i64::from).unwrap_or(0)), ), (Value::string("stdout"), Value::string(&snapshot.stdout)), (Value::string("stderr"), Value::string(&snapshot.stderr)), @@ -5201,6 +6355,7 @@ fn process_snapshot_value(kind: &str, snapshot: &ProcessSnapshot) -> Value { Value::Bool(snapshot.deadline_elapsed), ), (Value::string("cancelled"), Value::Bool(snapshot.cancelled)), + (Value::string("error"), option::none_carrier()), ]) } @@ -5317,6 +6472,14 @@ fn normalize_provider_envelope(result: JsonValue) -> JsonValue { result } +#[cfg(test)] +#[path = "agent_host/option_text_tests.rs"] +mod option_text_tests; + +#[cfg(test)] +#[path = "agent_host/preview_tests.rs"] +mod preview_tests; + #[cfg(all(feature = "config-fixture", test))] mod b1_json_guard_native_auth_reuse_tests { use super::*; @@ -5438,11 +6601,7 @@ pub fn b1_send( }; let usage: TransportCredentialUse = { kind: "access", - access_handle: access, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null + access_handle: Some(access) }; oauth::transport_result(&request, &usage) } @@ -5536,12 +6695,22 @@ pub fn b1_send( ), ])]), )]); - let guard_failure = provider_call_adapter(&mut vm, &[nested_request]) - .expect_err("nested resource-bearing provider JSON rejected"); - assert!(matches!( - guard_failure, - VmError::HostError(message) if message == "opaque_nonserializable" - )); + let refusal = provider_call_adapter(&mut vm, &[nested_request]) + .expect("map-shaped provider request returns a typed refusal"); + let CallOutcome::Return(CallReturn::One(refusal)) = refusal else { + panic!("provider refusal returned an unexpected outcome") + }; + assert_eq!(field(&refusal, "ok"), &Value::Bool(false)); + assert_eq!(field(&refusal, "response"), &Value::string("")); + let Value::String(error_text) = field(&refusal, "error") else { + panic!("provider refusal error must be JSON text") + }; + let error: JsonValue = serde_json::from_str(error_text).expect("typed provider error"); + assert_eq!(error["code"], "invalid_request"); + assert_eq!( + error["message"], + "provider request must cross as serialized JSON text, got map" + ); assert_eq!(provider.call_count(), 0); let send = vm @@ -5551,11 +6720,11 @@ pub fn b1_send( .invoke_callable(send, &[policy, access]) .expect("native transport result envelope"); assert_eq!(field(&envelope, "ok"), &Value::Bool(true)); - let response = field(&envelope, "value"); - assert!( - !matches!(response, Value::Null), - "canonical transport response absent" - ); + let response_carrier = field(&envelope, "value"); + let (present, response) = rustscript_vm::option::payload(response_carrier) + .expect("native transport value is an Option carrier"); + assert!(present, "canonical transport response absent"); + let response = response.expect("Some transport response has a payload"); assert_eq!(field(response, "status"), &Value::Int(200)); assert_eq!( field(response, "body_without_secret_fields"), @@ -5578,11 +6747,13 @@ pub fn b1_send( ); provider.push_ok(json!({})); - let control_request = - Value::map(vec![(Value::string("messages"), Value::array(Vec::new()))]); + let control_request = Value::string(r#"{"messages":[]}"#); let control = provider_call_adapter(&mut vm, &[control_request]) - .expect("ordinary provider callback with resource-free JSON"); - assert!(matches!(control, CallOutcome::Return(_))); + .expect("ordinary provider callback with resource-free JSON text"); + let CallOutcome::Return(CallReturn::One(control)) = control else { + panic!("provider callback returned an unexpected outcome") + }; + assert_eq!(field(&control, "ok"), &Value::Bool(true)); assert_eq!(provider.call_count(), 1); } } @@ -5625,7 +6796,15 @@ mod t07_measurement_tests { assert_eq!(keys, expected_keys); // One-digit offset/len and no payload: the frame is exactly the key set // plus the fixed value widths. - let expected = json_object_frame(&FS_READ_KEYS, 4 + 7 + 1 + 5 + 1 + 64); + let expected = json_object_frame( + &FS_READ_KEYS, + 4 + 7 + + 1 + + 5 + + 1 + + json_object_frame(&["tag", "payload"], 4 + 64) + + FS_READ_ERROR_CARRIER, + ); assert_eq!(vm_value_typed_bytes(&value), expected); assert!(expected <= FS_READ_FRAME_BOUND); } @@ -6221,3 +7400,424 @@ mod s2_child_state_handoff_tests { let _ = std::fs::remove_dir_all(&root); } } + +#[cfg(test)] +mod logical_option_boundary_tests { + use super::*; + use crate::runtime::cancellation::CancellationReason; + use rustscript_vm::{ + CompileSourceFileOptions, SourceFlavor, VmStatus, compile_source_with_flavor_and_options, + }; + + const SOURCE: &str = r#" +pub fn control_ok() -> bool { + let control = agent::control_check(); + control.ok +} + +pub fn control_code() -> string { + let control = agent::control_check(); + match control.error { + Some(failure) => failure.code, + None => "none", + } +} + +pub fn control_message() -> string { + let control = agent::control_check(); + match control.error { + Some(failure) => failure.message, + None => "none", + } +} + +pub fn parsed_text(text: string) -> string { + let parsed = agent::parse_json_object(text); + if parsed.ok => { + parsed.value + } else => { + parsed.code + } +} + +pub fn run() -> map { + { ok: true } +} +"#; + + fn bound_vm(cancellation: RunCancellation) -> Vm { + let catalog = agent_host_catalog(); + let options = CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()); + let compiled = + compile_source_with_flavor_and_options(SOURCE, SourceFlavor::RustScript, options) + .expect("compile agent boundary probe"); + let mut vm = Vm::new_shared(Arc::new(compiled.program)); + let mut registry = HostFunctionRegistry::restricted(); + register_agent_host_functions(&mut registry, catalog.as_ref()) + .expect("register agent descriptors"); + registry + .bind_vm_cached(&mut vm) + .expect("bind restricted catalog"); + vm.host_context().set_module_state(AgentHostState { + child_call: None, + child_read: None, + provider: Arc::new(ScriptedProvider::new()), + cancellation, + sleeps: Arc::new(Mutex::new(SleepLog::default())), + skip_sleep: true, + metrics: None, + lifecycle: None, + capability_owner: None, + filesystem: None, + processes: None, + artifacts: None, + leases: Arc::new(Mutex::new(HashMap::new())), + control_hook: None, + pinned_tool: None, + allow_stateless_pin: false, + expected_call: None, + plugin_state_call: None, + provider_arguments: Default::default(), + }); + assert!(matches!(vm.run().expect("RSS root"), VmStatus::Halted)); + vm + } + + fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> Value { + let callable = vm.resolve_exported_callable(name).expect("RSS export"); + vm.invoke_callable(callable, args).expect("RSS call") + } + + #[test] + fn control_check_reports_a_logical_option_error_carrier() { + let mut vm = bound_vm(RunCancellation::with_timeout(Duration::from_secs(30))); + assert_eq!(invoke(&mut vm, "control_ok", &[]), Value::Bool(true)); + assert_eq!( + invoke(&mut vm, "control_code", &[]), + Value::string("none"), + "an active run reports the logical None variant" + ); + + let mut vm = bound_vm(RunCancellation::with_timeout(Duration::from_secs(30))); + vm.host_context() + .module_state::() + .expect("installed state") + .cancellation + .request(CancellationReason::Requested); + assert_eq!(invoke(&mut vm, "control_ok", &[]), Value::Bool(false)); + assert_eq!( + invoke(&mut vm, "control_code", &[]), + Value::string("cancelled"), + "a cancelled run reports Some(failure.code) through the carrier" + ); + assert_eq!( + invoke(&mut vm, "control_message", &[]), + Value::string("run was cancelled"), + "the carried failure keeps the host message, not a guest default" + ); + } + + #[test] + fn control_check_distinguishes_a_deadline_from_a_cancellation() { + let mut vm = bound_vm(RunCancellation::with_timeout(Duration::ZERO)); + assert_eq!(invoke(&mut vm, "control_ok", &[]), Value::Bool(false)); + assert_eq!( + invoke(&mut vm, "control_code", &[]), + Value::string("deadline_elapsed") + ); + assert_eq!( + invoke(&mut vm, "control_message", &[]), + Value::string("run deadline elapsed") + ); + } + + #[test] + fn parse_json_object_carries_the_object_as_text() { + let mut vm = bound_vm(RunCancellation::with_timeout(Duration::from_secs(30))); + let text = invoke( + &mut vm, + "parsed_text", + &[Value::string(r#"{"name":"alpha","count":2}"#)], + ); + let Value::String(text) = text else { + panic!("parsed value must be text, got {text:?}"); + }; + let decoded: JsonValue = serde_json::from_str(text.as_str()).expect("serialized object"); + assert_eq!(decoded, json!({"name": "alpha", "count": 2})); + + let rejected = invoke(&mut vm, "parsed_text", &[Value::string("[1,2,3]")]); + assert_eq!(rejected, Value::string("malformed_payload")); + } +} + +/// Guardian is the real consumer of `agent::parse_json_object`: the assessment +/// text it receives from the provider goes through the declared +/// `AgentParsedJsonObject` contract and is decoded back into a decision map. +/// These tests compile the bundled `rss/agent/guardian.rss` itself against the +/// production catalog, bind the production restricted registry, and drive +/// `review(context, action)` through the real adapters. +#[cfg(test)] +mod guardian_real_entry_tests { + use super::*; + use crate::runtime::cancellation::CancellationReason; + use rustscript_vm::{ + CompileSourceFileOptions, HostFunctionRegistry, SourceFlavor, VmStatus, + compile_source_at_path_with_flavor_and_options, + }; + + fn field<'a>(value: &'a Value, name: &str) -> &'a Value { + let Value::Map(entries) = value else { + panic!("expected a map, got {value:?}"); + }; + entries + .iter() + .find_map(|(key, value)| match key { + Value::String(key) if key.as_ref() == name => Some(value), + _ => None, + }) + .unwrap_or_else(|| panic!("missing field {name} in {value:?}")) + } + + fn text(value: &Value, name: &str) -> String { + match field(value, name) { + Value::String(text) => text.to_string(), + other => panic!("{name} is not text: {other:?}"), + } + } + + fn guardian_vm(provider: &Arc) -> Vm { + let path = + std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/agent/guardian.rss"); + let source = std::fs::read_to_string(&path).expect("bundled Guardian source"); + let catalog = agent_host_catalog(); + let compiled = compile_source_at_path_with_flavor_and_options( + &path, + &source, + SourceFlavor::RustScript, + CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()), + ) + .expect("the real Guardian entry must compile against the production catalog"); + // The pinned core's trace tier-up needs the `cranelift-jit` feature, + // which this build does not enable. Guardian loops over its prompts, so + // it would trip the tier-up path; the interpreter is the configuration + // under test here. + let mut vm = Vm::new_shared_with_jit_config( + Arc::new(compiled.program), + rustscript_vm::jit::JitConfig { + enabled: false, + ..rustscript_vm::jit::JitConfig::default() + }, + ); + let mut registry = HostFunctionRegistry::restricted(); + register_agent_host_functions(&mut registry, catalog.as_ref()) + .expect("register agent descriptors"); + registry + .bind_vm_cached(&mut vm) + .expect("bind restricted catalog"); + vm.host_context().set_module_state(AgentHostState { + child_call: None, + child_read: None, + provider: provider.clone(), + // Compile and bind before starting the review fixture clock. + cancellation: RunCancellation::with_timeout(Duration::from_secs(30)), + sleeps: Arc::new(Mutex::new(SleepLog::default())), + skip_sleep: true, + metrics: None, + lifecycle: None, + capability_owner: None, + filesystem: None, + processes: None, + artifacts: None, + leases: Arc::new(Mutex::new(HashMap::new())), + control_hook: None, + pinned_tool: None, + allow_stateless_pin: false, + expected_call: None, + plugin_state_call: None, + provider_arguments: Default::default(), + }); + assert!(matches!(vm.run().expect("RSS root"), VmStatus::Halted)); + vm + } + + fn context() -> Value { + Value::map(vec![ + ( + Value::string("input"), + Value::string("please refresh the README introduction"), + ), + ( + Value::string("system_prompt"), + Value::string("stay in scope"), + ), + ( + Value::string("coding_system_prompt"), + Value::string("project guidance"), + ), + (Value::string("model"), Value::string("guardian-test-model")), + ( + Value::string("limits"), + Value::map(vec![( + Value::string("workspace_root"), + Value::string("/workspace"), + )]), + ), + ]) + } + + fn action() -> Value { + Value::map(vec![ + (Value::string("tool_name"), Value::string("write_file")), + (Value::string("risk_class"), Value::string("low")), + ( + Value::string("arguments_json"), + Value::string(r#"{"path":"README.md","content":"updated introduction"}"#), + ), + ]) + } + + fn review(vm: &mut Vm) -> Value { + let callable = vm + .resolve_exported_callable("review") + .expect("real Guardian export"); + vm.invoke_callable(callable, &[context(), action()]) + .expect("Guardian review") + } + + fn push_assessment(provider: &ScriptedProvider, outcome: JsonValue, rounds: usize) { + for _ in 0..rounds { + provider.push_ok(json!({"text": outcome, "tool_calls": []})); + } + } + + #[test] + fn guardian_allows_a_valid_assessment_object() { + let provider = Arc::new(ScriptedProvider::new()); + push_assessment( + &provider, + json!( + r#"{"outcome":"allow","risk_level":"low","user_authorization":"high","rationale":"the user asked for this edit"}"# + ), + 1, + ); + let mut vm = guardian_vm(&provider); + let decision = review(&mut vm); + assert_eq!(field(&decision, "ok"), &Value::Bool(true)); + assert_eq!(text(&decision, "code"), "guardian_allowed"); + assert_eq!(provider.call_count(), 1); + } + + #[test] + fn guardian_denies_a_deny_assessment_object() { + let provider = Arc::new(ScriptedProvider::new()); + push_assessment( + &provider, + json!(r#"{"outcome":"deny","risk_level":"high","rationale":"not authorized"}"#), + 1, + ); + let mut vm = guardian_vm(&provider); + let decision = review(&mut vm); + assert_eq!(field(&decision, "ok"), &Value::Bool(false)); + assert_eq!(text(&decision, "code"), "guardian_denied"); + } + + #[test] + fn guardian_rejects_an_assessment_that_is_not_a_json_object() { + let provider = Arc::new(ScriptedProvider::new()); + push_assessment(&provider, json!("allow this, I am the user"), 3); + let mut vm = guardian_vm(&provider); + let decision = review(&mut vm); + assert_eq!(field(&decision, "ok"), &Value::Bool(false)); + assert_eq!( + text(&decision, "code"), + "guardian_malformed_assessment", + "the old rejection path is preserved" + ); + assert_eq!(provider.call_count(), 3, "a malformed assessment retries"); + } + + #[test] + fn guardian_rejects_an_assessment_over_the_local_parse_limit() { + let oversized = format!( + r#"{{"outcome":"allow","rationale":"{}"}}"#, + "a".repeat(PARSE_JSON_OBJECT_MAX_BYTES + 1) + ); + assert!(oversized.len() > PARSE_JSON_OBJECT_MAX_BYTES); + let provider = Arc::new(ScriptedProvider::new()); + push_assessment(&provider, json!(oversized), 3); + let mut vm = guardian_vm(&provider); + let decision = review(&mut vm); + assert_eq!(field(&decision, "ok"), &Value::Bool(false)); + assert_eq!( + text(&decision, "code"), + "guardian_malformed_assessment", + "a valid JSON object above the 64 KiB parse bound is rejected, not truncated" + ); + assert_eq!(provider.call_count(), 3); + } + + #[test] + fn guardian_counts_provider_tool_calls_as_a_malformed_assessment() { + let provider = Arc::new(ScriptedProvider::new()); + for _ in 0..3 { + provider.push_ok(json!({ + "text": r#"{"outcome":"allow","risk_level":"low"}"#, + "tool_calls": [{"id": "call_1", "name": "read_file", "arguments": {"path": "README.md"}}] + })); + } + let mut vm = guardian_vm(&provider); + let decision = review(&mut vm); + assert_eq!(field(&decision, "ok"), &Value::Bool(false)); + assert_eq!( + text(&decision, "code"), + "guardian_malformed_assessment", + "a reply that asks for a tool is never an assessment" + ); + } + + #[test] + fn guardian_surfaces_a_non_retryable_provider_failure() { + let provider = Arc::new(ScriptedProvider::new()); + provider.push_error(json!({ + "status": 400, + "type": "invalid_request_error", + "code": "invalid_request", + "message": "the request was rejected", + "param": "", + "request_id": "req_1", + "retryable": false + })); + let mut vm = guardian_vm(&provider); + let decision = review(&mut vm); + assert_eq!(field(&decision, "ok"), &Value::Bool(false)); + assert_eq!( + text(&decision, "code"), + "guardian_provider_failed", + "the decoded error body feeds the existing retry policy" + ); + assert_eq!( + provider.call_count(), + 1, + "a non-retryable failure is not retried" + ); + } + + #[test] + fn guardian_reports_the_real_control_code_instead_of_a_guest_default() { + let provider = Arc::new(ScriptedProvider::new()); + let mut vm = guardian_vm(&provider); + vm.host_context() + .module_state::() + .expect("installed state") + .cancellation + .request(CancellationReason::Deadline); + let decision = review(&mut vm); + assert_eq!(field(&decision, "ok"), &Value::Bool(false)); + assert_eq!( + text(&decision, "code"), + "deadline_elapsed", + "the guest must surface the host failure code, not its own fallback" + ); + assert_eq!(provider.call_count(), 0, "no provider call after the cut"); + } +} diff --git a/src/runtime/agent_host/option_text_tests.rs b/src/runtime/agent_host/option_text_tests.rs new file mode 100644 index 0000000..daaf8aa --- /dev/null +++ b/src/runtime/agent_host/option_text_tests.rs @@ -0,0 +1,641 @@ +//! Round-7 text-carrier tests for the provider, refresh and approval slots. +//! +//! Every case compiles a real RSS probe against the published agent catalog, +//! registers the real restricted agent module, binds a fresh VM, installs the +//! real `AgentHostState` with a scripted provider host, and then drives the +//! exported guest functions. Nothing is asserted against the adapter in +//! isolation: the text bound, the refusal codes and the field fidelity are read +//! back out of the guest-visible value. + +use super::*; +use rustscript_vm::{ + CompileSourceFileOptions, HostFunctionRegistry, SourceFlavor, VmStatus, + compile_source_at_path_with_flavor_and_options, compile_source_with_flavor_and_options, +}; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +const CANCEL: Duration = Duration::from_secs(30); +const MIB: usize = 1024 * 1024; + +const PROBE: &str = r#" +use agent; +use json; + +/// The same projection `rss/agent/provider_body.rss` performs, declared here so +/// the test reads the boundary exactly as the agent loop does. +struct Failure { + status: Option, + type: Option, + code: Option, + message: Option, + param: Option, + request_id: Option, + retryable: Option +} + +struct Call { + id: Option, + name: Option, + arguments_json: Option +} + +struct Response { + text: Option, + tool_calls: Option> +} + +fn text_of(value: Option) -> string { + match value { + Some(inner) => inner, + None => "", + } +} + +fn int_of(value: Option) -> int { + match value { + Some(inner) => inner, + None => -1, + } +} + +fn bool_of(value: Option) -> bool { + match value { + Some(inner) => inner, + None => false, + } +} + +pub fn call(request: string) -> AgentProviderEnvelope { + agent::provider_call(request) +} + +pub fn failure_body(body: string) -> map { + let mut out: map = { code: "", type: "", message: "", param: "", request_id: "", status: -1, retryable: false }; + if body != "" { + let decoded: Failure = json::decode::(body); + out.code = text_of(decoded.code.copy()); + out.type = text_of(decoded.type.copy()); + out.message = text_of(decoded.message.copy()); + out.param = text_of(decoded.param.copy()); + out.request_id = text_of(decoded.request_id.copy()); + out.status = int_of(decoded.status.copy()); + out.retryable = bool_of(decoded.retryable.copy()); + } + out +} + +pub fn response_body(body: string) -> map { + let mut out: map = { text: "", calls: 0, first_id: "", first_name: "", first_arguments: "" }; + if body != "" { + let decoded: Response = json::decode::(body); + out.text = text_of(decoded.text.copy()); + let list: array = match decoded.tool_calls { + Some(items) => items, + None => [] + }; + out.calls = list.length; + if list.length > 0 { + let first: Call = list[0].copy(); + out.first_id = text_of(first.id.copy()); + out.first_name = text_of(first.name.copy()); + out.first_arguments = text_of(first.arguments_json.copy()); + } + } + out +} + +pub fn refresh(context: string) -> AgentRefreshEnvelope { + agent::refresh_context(context) +} + +pub fn approvals(operation: string, payload: string) -> AgentApprovalStorageEnvelope { + agent::approval_storage(operation, payload) +} + +pub fn envelope_text(text: string) -> string { + let encoded = agent::json_text({ ok: text }); + encoded.text +} + +pub fn run() -> map { + { ok: true } +} +"#; + +fn probe_vm(provider: Arc) -> Vm { + let catalog = agent_host_catalog(); + let options = CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()); + let compiled = compile_source_with_flavor_and_options(PROBE, SourceFlavor::RustScript, options) + .expect("compile the text-carrier probe against the published catalog"); + let mut vm = Vm::new_shared(Arc::new(compiled.program)); + let mut registry = HostFunctionRegistry::restricted(); + register_agent_host_functions(&mut registry, catalog.as_ref()) + .expect("register the published agent descriptors"); + registry + .bind_vm_cached(&mut vm) + .expect("bind the restricted registry"); + vm.host_context().set_module_state(AgentHostState { + child_call: None, + child_read: None, + provider, + cancellation: RunCancellation::with_timeout(CANCEL), + sleeps: Arc::new(Mutex::new(SleepLog::default())), + skip_sleep: true, + metrics: None, + lifecycle: None, + capability_owner: None, + filesystem: None, + processes: None, + artifacts: None, + leases: Arc::new(Mutex::new(HashMap::new())), + control_hook: None, + pinned_tool: None, + allow_stateless_pin: false, + expected_call: None, + plugin_state_call: None, + provider_arguments: Default::default(), + }); + assert!(matches!( + vm.run().expect("probe root frame"), + VmStatus::Halted + )); + vm +} + +fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> Value { + let callable = vm + .resolve_exported_callable(name) + .unwrap_or_else(|_| panic!("export {name}")); + vm.invoke_callable(callable, args) + .unwrap_or_else(|error| panic!("invoke {name}: {error}")) +} + +fn field(value: &Value, name: &str) -> Value { + let Value::Map(entries) = value else { + panic!("expected a map, got {value:?}"); + }; + entries + .iter() + .find_map(|(key, value)| match key { + Value::String(key) if key.as_ref() == name => Some(value.clone()), + _ => None, + }) + .unwrap_or_else(|| panic!("missing {name} in {value:?}")) +} + +fn text_field(value: &Value, name: &str) -> String { + match field(value, name) { + Value::String(text) => text.as_str().to_string(), + other => panic!("{name} is not a string: {other:?}"), + } +} + +fn parse(text: &str) -> JsonValue { + serde_json::from_str(text).expect("body is JSON text") +} + +fn provider_with(envelope: JsonValue) -> Arc { + let provider = Arc::new(ScriptedProvider::new()); + provider.push_envelope(envelope); + provider +} + +/// An approval host that answers with a real storage shape, so the adapter's +/// positional row projection is exercised end to end. +struct ApprovalProvider { + answer: JsonValue, + seen: Mutex>, +} + +impl AgentProviderHost for ApprovalProvider { + fn call(&self, _request: &JsonValue, _cancellation: &RunCancellation) -> JsonValue { + typed_fail("scripted_exhausted", "no provider outcome") + } + + fn approval_storage( + &self, + operation: &str, + payload: &JsonValue, + _cancellation: &RunCancellation, + ) -> JsonValue { + self.seen + .lock() + .expect("approval log") + .push((operation.to_string(), payload.clone())); + self.answer.clone() + } +} + +#[test] +fn provider_call_carries_the_request_text_and_returns_body_text() { + let provider = provider_with(json!({ + "ok": true, + "response": { + "text": "calling a tool", + "tool_calls": [ + {"id": "call-1", "name": "read_file", "arguments": {"path": "note.txt"}} + ] + } + })); + let mut vm = probe_vm(provider.clone()); + + let request = r#"{"provider":"openai","model":"m","messages":[{"role":"user","content":[{"type":"text","text":"hi"}]}]}"#; + let envelope = invoke(&mut vm, "call", &[Value::string(request)]); + + assert_eq!(field(&envelope, "ok"), Value::Bool(true)); + assert_eq!(field(&envelope, "error"), Value::string("")); + // The provider received the parsed object, not a string wrapper. + let seen = provider.requests(); + assert_eq!(seen.len(), 1); + assert_eq!(seen[0], parse(request)); + + // The guest decodes the body text exactly as the loop does, and the + // adapter's `arguments_json` injection makes the open arguments readable. + let body = text_field(&envelope, "response"); + let response = invoke(&mut vm, "response_body", &[Value::string(body)]); + assert_eq!(field(&response, "text"), Value::string("calling a tool")); + assert_eq!(field(&response, "calls"), Value::Int(1)); + assert_eq!(field(&response, "first_id"), Value::string("call-1")); + assert_eq!(field(&response, "first_name"), Value::string("read_file")); + assert_eq!( + field(&response, "first_arguments"), + Value::string(r#"{"path":"note.txt"}"#) + ); +} + +#[test] +fn provider_call_refuses_a_request_that_is_not_a_json_object() { + let provider = provider_with(json!({"ok": true, "response": {"text": ""}})); + let mut vm = probe_vm(provider.clone()); + + for bad in ["not json", "[1,2]", "null"] { + let envelope = invoke(&mut vm, "call", &[Value::string(bad)]); + assert_eq!(field(&envelope, "ok"), Value::Bool(false), "refused: {bad}"); + assert_eq!(field(&envelope, "response"), Value::string("")); + let error = parse(&text_field(&envelope, "error")); + assert_eq!(error["code"], json!("invalid_request"), "refused: {bad}"); + } + assert!( + provider.requests().is_empty(), + "a refused request never reaches the provider" + ); +} + +#[test] +fn provider_call_refuses_a_request_over_the_four_mib_bound() { + let provider = provider_with(json!({"ok": true, "response": {"text": ""}})); + let mut vm = probe_vm(provider.clone()); + + let oversized = format!("{{\"blob\":\"{}\"}}", "a".repeat(4 * MIB + 1)); + let envelope = invoke(&mut vm, "call", &[Value::string(oversized)]); + + assert_eq!(field(&envelope, "ok"), Value::Bool(false)); + let error = parse(&text_field(&envelope, "error")); + assert_eq!(error["code"], json!("payload_too_large")); + assert!( + provider.requests().is_empty(), + "an oversized request never reaches the provider" + ); +} + +#[test] +fn provider_call_refuses_a_response_body_over_the_four_mib_bound() { + let provider = provider_with(json!({ + "ok": true, + "response": {"text": "x".repeat(4 * MIB + 64)} + })); + let mut vm = probe_vm(provider); + + let envelope = invoke(&mut vm, "call", &[Value::string("{}")]); + + assert_eq!( + field(&envelope, "ok"), + Value::Bool(false), + "an over-bound body is refused, not published" + ); + assert_eq!(field(&envelope, "response"), Value::string("")); + let error = parse(&text_field(&envelope, "error")); + assert_eq!(error["code"], json!("provider_body_too_large")); + assert_eq!(error["status"], json!(0)); + assert_eq!(error["retryable"], json!(false)); +} + +#[test] +fn provider_failure_body_keeps_the_retry_decision_fields() { + let provider = provider_with(json!({ + "ok": false, + "error": { + "status": 429, + "type": "rate_limit_error", + "code": "rate_limit_exceeded", + "message": "slow down", + "param": "model", + "request_id": "req_123", + "retryable": true + } + })); + let mut vm = probe_vm(provider); + + let envelope = invoke(&mut vm, "call", &[Value::string("{}")]); + assert_eq!(field(&envelope, "ok"), Value::Bool(false)); + assert_eq!(field(&envelope, "response"), Value::string("")); + + let body = text_field(&envelope, "error"); + let failure = invoke(&mut vm, "failure_body", &[Value::string(body)]); + // The three fields the round-4 review found missing, plus the ones the + // retry classifier already read. + assert_eq!(field(&failure, "type"), Value::string("rate_limit_error")); + assert_eq!(field(&failure, "param"), Value::string("model")); + assert_eq!(field(&failure, "request_id"), Value::string("req_123")); + assert_eq!( + field(&failure, "code"), + Value::string("rate_limit_exceeded") + ); + assert_eq!(field(&failure, "message"), Value::string("slow down")); + assert_eq!(field(&failure, "status"), Value::Int(429)); + assert_eq!(field(&failure, "retryable"), Value::Bool(true)); +} + +#[test] +fn refresh_context_carries_text_and_reports_the_typed_refusal() { + let mut vm = probe_vm(Arc::new(ScriptedProvider::new())); + + // The scripted host has no durable refresh, so the fixed envelope reports + // `ok` with `storage_available: false` — never a dynamic body. + let refreshed = invoke( + &mut vm, + "refresh", + &[Value::string(r#"{"run_id":"run-1","messages":[]}"#)], + ); + assert_eq!(field(&refreshed, "ok"), Value::Bool(true)); + assert_eq!(field(&refreshed, "storage_available"), Value::Bool(false)); + assert_eq!(field(&refreshed, "code"), Value::string("")); + + let refused = invoke(&mut vm, "refresh", &[Value::string("[1]")]); + assert_eq!(field(&refused, "ok"), Value::Bool(false)); + assert_eq!(field(&refused, "code"), Value::string("invalid_request")); + + let oversized = format!("{{\"blob\":\"{}\"}}", "a".repeat(4 * MIB + 1)); + let refused = invoke(&mut vm, "refresh", &[Value::string(oversized)]); + assert_eq!(field(&refused, "ok"), Value::Bool(false)); + assert_eq!(field(&refused, "code"), Value::string("payload_too_large")); +} + +#[test] +fn approval_storage_parses_the_payload_text_and_projects_rows_by_name() { + let provider = Arc::new(ApprovalProvider { + answer: json!({ + "ok": true, + "result": { + "columns": ["id", "run_id", "session_id", "tool_call_id", "tool_name", + "arguments_json", "risk_class", "state", "decision_scope", + "one_time", "requested_at_ms", "expires_at_ms", + "resolved_at_ms", "resolver", "decision_reason"], + "rows": [[ + "approval-1", "run-1", "session-1", "call-1", "write_file", + "{\"path\":\"a.txt\"}", "write", "pending", "single_call", + 1, 10, 20, null, null, null + ]] + } + }), + seen: Mutex::new(Vec::new()), + }); + let mut vm = probe_vm(provider.clone()); + + let payload = r#"{"run_id":"run-1","id":"approval-1","session_id":"session-1"}"#; + let stored = invoke( + &mut vm, + "approvals", + &[Value::string("request"), Value::string(payload)], + ); + + // The host parsed the text and forwarded a real object to the provider. + let seen = provider.seen.lock().expect("approval log"); + assert_eq!(seen.len(), 1); + assert_eq!(seen[0].0, "request"); + assert_eq!(seen[0].1, parse(payload)); + drop(seen); + + assert_eq!(field(&stored, "ok"), Value::Bool(true)); + assert_eq!(field(&stored, "code"), Value::string("")); + assert_eq!(field(&stored, "rows_affected"), Value::Int(0)); + let Value::Array(rows) = field(&stored, "rows") else { + panic!("rows must be an array"); + }; + assert_eq!(rows.len(), 1); + let row = &rows[0]; + assert_eq!(field(row, "id"), Value::string("approval-1")); + assert_eq!(field(row, "run_id"), Value::string("run-1")); + assert_eq!(field(row, "session_id"), Value::string("session-1")); + assert_eq!(field(row, "tool_call_id"), Value::string("call-1")); + assert_eq!(field(row, "tool_name"), Value::string("write_file")); + assert_eq!( + field(row, "arguments_json"), + Value::string(r#"{"path":"a.txt"}"#) + ); + assert_eq!(field(row, "risk_class"), Value::string("write")); + assert_eq!(field(row, "state"), Value::string("pending")); + assert_eq!(field(row, "decision_scope"), Value::string("single_call")); + assert_eq!(field(row, "one_time"), Value::Int(1)); + assert_eq!(field(row, "requested_at_ms"), Value::Int(10)); + assert_eq!(field(row, "expires_at_ms"), Value::Int(20)); +} + +#[test] +fn approval_storage_reports_rows_affected_and_refuses_malformed_text() { + let provider = Arc::new(ApprovalProvider { + answer: json!({"ok": true, "result": {"rows_affected": 1}}), + seen: Mutex::new(Vec::new()), + }); + let mut vm = probe_vm(provider); + + let resolved = invoke( + &mut vm, + "approvals", + &[ + Value::string("resolve"), + Value::string(r#"{"id":"approval-1","state":"approved"}"#), + ], + ); + assert_eq!(field(&resolved, "ok"), Value::Bool(true)); + assert_eq!(field(&resolved, "rows_affected"), Value::Int(1)); + match field(&resolved, "rows") { + Value::Array(rows) => assert!(rows.is_empty()), + other => panic!("rows must be an array: {other:?}"), + } + + let refused = invoke( + &mut vm, + "approvals", + &[Value::string("get"), Value::string("nope")], + ); + assert_eq!(field(&refused, "ok"), Value::Bool(false)); + assert_eq!(field(&refused, "code"), Value::string("invalid_request")); + assert_eq!(field(&refused, "rows_affected"), Value::Int(0)); + + let oversized = format!("{{\"blob\":\"{}\"}}", "a".repeat(4 * MIB + 1)); + let refused = invoke( + &mut vm, + "approvals", + &[Value::string("get"), Value::string(oversized)], + ); + assert_eq!(field(&refused, "ok"), Value::Bool(false)); + assert_eq!(field(&refused, "code"), Value::string("payload_too_large")); +} + +/// The real agent loop, compiled from `rss/agent/main.rss` against the +/// published catalog and driven through the real host state: a text provider +/// round trip must complete, and a provider tool call must reach the tool +/// boundary (its arguments crossed as text, so parsing them cannot crash the +/// loop). +fn main_loop_vm(provider: Arc) -> Vm { + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/agent/main.rss"); + let source = std::fs::read_to_string(&path).expect("read the real agent main"); + let catalog = agent_host_catalog(); + let options = CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()); + let compiled = compile_source_at_path_with_flavor_and_options( + &path, + &source, + SourceFlavor::RustScript, + options, + ) + .expect("compile the real agent main against the published catalog"); + let mut vm = Vm::new_shared(Arc::new(compiled.program)); + let mut registry = HostFunctionRegistry::restricted(); + for builtin in crate::runtime::host_compose::RESTRICTED_STANDARD_BUILTINS { + registry.allow_builtin(builtin).expect("admit builtin"); + } + register_agent_host_functions(&mut registry, catalog.as_ref()) + .expect("register the published agent descriptors"); + registry + .bind_vm_cached(&mut vm) + .expect("bind the restricted registry"); + vm.host_context().set_module_state(AgentHostState { + child_call: None, + child_read: None, + provider, + cancellation: RunCancellation::with_timeout(CANCEL), + sleeps: Arc::new(Mutex::new(SleepLog::default())), + skip_sleep: true, + metrics: None, + lifecycle: None, + capability_owner: None, + filesystem: None, + processes: None, + artifacts: None, + leases: Arc::new(Mutex::new(HashMap::new())), + control_hook: None, + pinned_tool: None, + allow_stateless_pin: true, + expected_call: None, + plugin_state_call: None, + provider_arguments: Default::default(), + }); + assert!(matches!( + vm.run().expect("main root frame"), + VmStatus::Halted + )); + vm +} + +fn loop_context() -> Value { + super::json_to_vm_value(&json!({ + "profile_id": "profile-1", + "session_id": "session-1", + "run_id": "run-1", + "provider": "openai", + "model": "request-model", + "input": {"message": "say hi"}, + "config": {"max_retries": 0}, + "limits": {"max_turns": 2, "max_tool_calls": 2} + })) +} + +#[test] +fn main_loop_completes_a_text_carried_provider_turn() { + let provider = provider_with(json!({ + "ok": true, + "response": {"text": "hello from the provider"} + })); + let mut vm = main_loop_vm(provider.clone()); + + let decision = invoke(&mut vm, "run", &[loop_context()]); + assert_eq!(field(&decision, "kind"), Value::string("run.completed")); + assert_eq!( + field(&decision, "answer"), + Value::string("hello from the provider") + ); + + // The request reached the provider as a parsed object with the canonical + // message array the loop built. + let seen = provider.requests(); + assert_eq!(seen.len(), 1); + let request = &seen[0]; + assert_eq!(request["provider"], json!("openai")); + assert_eq!(request["_provider_step_key"], json!("business:0")); + let messages = request["messages"].as_array().expect("messages array"); + assert!( + messages + .iter() + .any(|message| message["role"] == json!("user")), + "the seeded user message is on the wire: {request}" + ); +} + +#[test] +fn main_loop_reaches_the_tool_boundary_with_text_carried_arguments() { + let provider = provider_with(json!({ + "ok": true, + "response": { + "text": "", + "tool_calls": [{ + "id": "call-1", + "name": "read_file", + "arguments": {"path": "note.txt"}, + "arguments_json": "{\"path\":\"note.txt\"}" + }] + } + })); + let mut vm = main_loop_vm(provider.clone()); + + let decision = invoke(&mut vm, "run", &[loop_context()]); + assert_eq!(field(&decision, "kind"), Value::string("run.failed")); + // The call was dispatched — one tool call consumed, and the loop moved on + // to a second provider turn — so the text-only arguments were accepted by + // the dispatcher instead of crashing a typed decode. The scripted provider + // simply has no second outcome. + assert_eq!(field(&decision, "tool_calls_used"), Value::Int(1)); + assert_eq!( + field(&decision, "reason"), + Value::string("scripted_exhausted"), + "decision: {decision:?}" + ); + + // The follow-up request proves the fidelity of the rebuilt assistant + // tool_call: the provider's own `arguments_json` text is carried verbatim, + // not re-encoded from a missing map. + let seen = provider.requests(); + assert_eq!(seen.len(), 2, "the loop took a second provider turn"); + let followup = &seen[1]; + let mut carried: Option<&JsonValue> = None; + for message in followup["messages"].as_array().expect("messages") { + for block in message["content"].as_array().cloned().unwrap_or_default() { + if block["type"] == json!("tool_call") { + carried = Some(&followup["messages"]); + assert_eq!(block["tool_call_id"], json!("call-1")); + assert_eq!(block["name"], json!("read_file")); + assert_eq!( + block["arguments_json"], + json!("{\"path\":\"note.txt\"}"), + "the provider's arguments text must be carried verbatim" + ); + } + } + } + assert!( + carried.is_some(), + "follow-up carries the tool_call: {followup}" + ); +} diff --git a/src/runtime/agent_host/preview_tests.rs b/src/runtime/agent_host/preview_tests.rs new file mode 100644 index 0000000..fbef17f --- /dev/null +++ b/src/runtime/agent_host/preview_tests.rs @@ -0,0 +1,578 @@ +//! Round-8 Guardian preview tests: the closed `PreviewArguments` projection and +//! the `ask` path that reads it. +//! +//! The round-7 text carriers stopped `dispatch` from filling an `arguments` map, +//! so `guardian::action_preview` had nothing to render and every write/execute +//! approval failed closed with `approval_preview_unavailable`. These tests drive +//! the boundary the way production does: a real RSS source compiled against the +//! published agent catalog, a real restricted registry, a real VM, real host +//! state. Nothing is asserted against the adapter in isolation, and the ask-path +//! case runs the real `rss/agent/main.rss` loop. + +use super::*; +use rustscript_vm::{ + CompileSourceFileOptions, HostFunctionRegistry, SourceFlavor, VmStatus, + compile_source_at_path_with_flavor_and_options, compile_source_with_flavor_and_options, +}; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +const CANCEL: Duration = Duration::from_secs(30); +const MIB: usize = 1024 * 1024; + +/// A clock that starts at 1 s instead of the unix epoch, so the lifecycle's +/// deadline is in the future for the whole test. +struct FixtureClock { + now_ms: Mutex, +} + +impl FixtureClock { + fn new(now_ms: u64) -> Arc { + Arc::new(Self { + now_ms: Mutex::new(now_ms), + }) + } +} + +impl crate::capabilities::LifecycleClock for FixtureClock { + fn now_ms(&self) -> u64 { + *self.now_ms.lock().expect("clock") + } + + fn now(&self) -> Instant { + Instant::now() + } +} + +/// In-memory durable tool lifecycle: this test never needs a real replay store, +/// only a lifecycle that admits a fresh (non-replayed) call. +#[derive(Default)] +struct MemoryDurable { + results: Mutex>, +} + +impl crate::capabilities::DurableToolLifecycle for MemoryDurable { + fn assert_active_run(&self, _run_id: &str) -> Result<(), crate::capabilities::LifecycleError> { + Ok(()) + } + + fn prepare_parent( + &self, + _run_id: &str, + _call_id: &str, + _tool_name: &str, + ) -> Result<(), crate::capabilities::LifecycleError> { + Ok(()) + } + + fn replay_result( + &self, + _run_id: &str, + call_id: &str, + _tool_name: &str, + ) -> Result, crate::capabilities::LifecycleError> { + Ok(self.results.lock().expect("results").get(call_id).cloned()) + } + + fn commit_started( + &self, + _record: &crate::capabilities::DurableStarted, + ) -> Result<(), crate::capabilities::LifecycleError> { + Ok(()) + } + + fn commit_result( + &self, + call_id: &str, + result: &JsonValue, + ) -> Result { + self.results + .lock() + .expect("results") + .insert(call_id.to_string(), result.clone()); + Ok(result.clone()) + } + + fn interrupt(&self, _call_id: &str) -> Result<(), crate::capabilities::LifecycleError> { + Ok(()) + } +} + +/// The real capability lifecycle the ask path needs: `tool_replay` refuses to +/// run without one, so a fixture here is the difference between "the preview +/// works" and "the product path actually reaches the approval request". +struct LifecycleFixture { + lifecycle: crate::capabilities::CapabilityLifecycle, + owner: crate::capabilities::CapabilityOwner, + root: std::path::PathBuf, +} + +impl Drop for LifecycleFixture { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.root); + } +} + +impl LifecycleFixture { + fn new(label: &str) -> Self { + let root = std::env::temp_dir().join(format!( + "preview-approval-{label}-{}-{}", + std::process::id(), + uuid::Uuid::new_v4() + )); + std::fs::create_dir_all(&root).expect("workspace root"); + let owner = crate::capabilities::CapabilityOwner::new("profile-1", "session-1", "run-1") + .expect("capability owner"); + let lifecycle = crate::capabilities::CapabilityLifecycle::builder() + .owner(owner.clone()) + .registry_identity("registry-1") + .workspace(&root) + .limits(crate::capabilities::LifecycleLimits { + max_tool_calls: 8, + max_output_bytes: 64 * 1024, + max_summary_bytes: 256, + }) + .deadline_ms(60_000) + .clock(FixtureClock::new(1_000) as Arc) + .tokens(Arc::new(crate::capabilities::UuidIssuer) + as Arc) + .durable(Arc::new(MemoryDurable::default()) + as Arc) + .approval(Arc::new(crate::capabilities::AllowAllApproval) + as Arc) + .cancellation(Arc::new(crate::capabilities::NeverCancelled) + as Arc) + .generation(1) + .build() + .expect("capability lifecycle"); + Self { + lifecycle, + owner, + root, + } + } +} + +const PROBE: &str = r#" +use agent; + +/// The one call `guardian::action_preview` makes: the tool's arguments text in, +/// the closed projection out. +pub fn arguments(text: string) -> AgentPreviewArguments { + agent::preview_arguments(text) +} + +pub fn run() -> map { + { ok: true } +} +"#; + +fn probe_vm(provider: Arc) -> Vm { + let catalog = agent_host_catalog(); + let options = CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()); + let compiled = compile_source_with_flavor_and_options(PROBE, SourceFlavor::RustScript, options) + .expect("compile the preview-arguments probe against the published catalog"); + let mut vm = Vm::new_shared(Arc::new(compiled.program)); + let mut registry = HostFunctionRegistry::restricted(); + register_agent_host_functions(&mut registry, catalog.as_ref()) + .expect("register the published agent descriptors"); + registry + .bind_vm_cached(&mut vm) + .expect("bind the restricted registry"); + vm.host_context().set_module_state(AgentHostState { + child_call: None, + child_read: None, + provider, + cancellation: RunCancellation::with_timeout(CANCEL), + sleeps: Arc::new(Mutex::new(SleepLog::default())), + skip_sleep: true, + metrics: None, + lifecycle: None, + capability_owner: None, + filesystem: None, + processes: None, + artifacts: None, + leases: Arc::new(Mutex::new(HashMap::new())), + control_hook: None, + pinned_tool: None, + allow_stateless_pin: false, + expected_call: None, + plugin_state_call: None, + provider_arguments: Default::default(), + }); + assert!(matches!( + vm.run().expect("probe root frame"), + VmStatus::Halted + )); + vm +} + +fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> Value { + let callable = vm + .resolve_exported_callable(name) + .unwrap_or_else(|_| panic!("export {name}")); + vm.invoke_callable(callable, args) + .unwrap_or_else(|error| panic!("invoke {name}: {error}")) +} + +fn field(value: &Value, name: &str) -> Value { + let Value::Map(entries) = value else { + panic!("expected a map, got {value:?}"); + }; + entries + .iter() + .find_map(|(key, value)| match key { + Value::String(key) if key.as_ref() == name => Some(value.clone()), + _ => None, + }) + .unwrap_or_else(|| panic!("missing {name} in {value:?}")) +} + +fn text_field(value: &Value, name: &str) -> String { + match field(value, name) { + Value::String(text) => text.as_str().to_string(), + other => panic!("{name} is not a string: {other:?}"), + } +} + +/// One logical `Option` slot as the guest sees it: `None`, or the payload. +fn option_slot(value: &Value, name: &str) -> Option { + let slot = field(value, name); + let (some, payload) = rustscript_vm::option::payload(&slot) + .unwrap_or_else(|error| panic!("{name} is not one canonical Option carrier: {error}")); + if !some { + return None; + } + Some(payload.cloned().expect("Some carries a payload")) +} + +fn arguments(text: &str) -> Value { + let mut vm = probe_vm(Arc::new(ScriptedProvider::new())); + invoke(&mut vm, "arguments", &[Value::string(text)]) +} + +#[test] +fn preview_arguments_projects_the_write_call_into_named_options() { + let projected = arguments(r#"{"path":"notes.txt","content":"hello","unknown_key":true}"#); + + assert_eq!(field(&projected, "ok"), Value::Bool(true)); + assert_eq!(field(&projected, "code"), Value::string("")); + assert_eq!( + option_slot(&projected, "path"), + Some(Value::string("notes.txt")) + ); + assert_eq!( + option_slot(&projected, "content"), + Some(Value::string("hello")) + ); + // Everything the write call did not send stays a `None` carrier, so the + // preview can distinguish "absent" from "present but empty". + for absent in [ + "old_string", + "new_string", + "replace_all", + "argv", + "stdin", + "cwd", + "timeout_ms", + "max_output_bytes", + "background", + "action", + "process_id", + "data", + "offset", + "limit", + ] { + assert_eq!(option_slot(&projected, absent), None, "{absent}"); + } + // An unknown tool argument is ignored: the projection is closed, it does + // not police the tool's own argument set. + assert!( + !matches!(&projected, Value::Map(entries) if entries + .iter() + .any(|(key, _)| matches!(key, Value::String(name) if name.as_ref() == "unknown_key"))), + "only declared fields cross: {projected:?}" + ); +} + +#[test] +fn preview_arguments_projects_the_terminal_and_process_fields() { + let terminal = arguments( + r#"{"argv":["echo","hi"],"cwd":"/work","timeout_ms":1500,"background":true,"stdin":""}"#, + ); + assert_eq!(field(&terminal, "ok"), Value::Bool(true)); + match option_slot(&terminal, "argv").expect("argv present") { + Value::Array(items) => { + assert_eq!(items.len(), 2); + } + other => panic!("argv is not an array: {other:?}"), + } + assert_eq!(option_slot(&terminal, "cwd"), Some(Value::string("/work"))); + assert_eq!(option_slot(&terminal, "timeout_ms"), Some(Value::Int(1500))); + assert_eq!( + option_slot(&terminal, "background"), + Some(Value::Bool(true)) + ); + + let process = arguments(r#"{"action":"poll","process_id":"p-1","limit":5}"#); + assert_eq!(option_slot(&process, "action"), Some(Value::string("poll"))); + assert_eq!( + option_slot(&process, "process_id"), + Some(Value::string("p-1")) + ); + assert_eq!(option_slot(&process, "limit"), Some(Value::Int(5))); + assert_eq!(option_slot(&process, "offset"), None); +} + +#[test] +fn preview_arguments_refuses_malformed_text_instead_of_publishing_empty_options() { + // A refusal, never an empty projection that would render as "the action had + // no arguments". + for (text, code) in [("", "invalid_request"), ("not json", "invalid_request")] { + let projected = arguments(text); + assert_eq!( + field(&projected, "ok"), + Value::Bool(false), + "refused: {text:?}" + ); + assert_eq!(field(&projected, "code"), Value::string(code), "{text:?}"); + assert_eq!(option_slot(&projected, "path"), None); + } + + // The projected shape is closed: a JSON array is not a tool argument object. + let array = arguments("[1,2]"); + assert_eq!(field(&array, "ok"), Value::Bool(false)); + assert_eq!(field(&array, "code"), Value::string("invalid_request")); + + let oversized = format!("{{\"content\":\"{}\"}}", "a".repeat(4 * MIB + 1)); + let projected = arguments(&oversized); + assert_eq!(field(&projected, "ok"), Value::Bool(false)); + assert_eq!( + field(&projected, "code"), + Value::string("payload_too_large") + ); +} + +#[test] +fn preview_arguments_refuses_a_wrongly_typed_field_by_name() { + for (text, field_name) in [ + (r#"{"path":7}"#, "path"), + (r#"{"content":["a"]}"#, "content"), + (r#"{"replace_all":"yes"}"#, "replace_all"), + (r#"{"timeout_ms":"fast"}"#, "timeout_ms"), + (r#"{"argv":["ok",1]}"#, "argv"), + (r#"{"process_id":true}"#, "process_id"), + ] { + let projected = arguments(text); + assert_eq!(field(&projected, "ok"), Value::Bool(false), "{text}"); + assert_eq!( + field(&projected, "code"), + Value::string("preview_arguments_invalid"), + "{text}" + ); + let message = text_field(&projected, "message"); + assert!( + message.contains(field_name), + "the refusal names the offending field: {message}" + ); + } +} + +/// A provider host that answers provider turns from a script and records the +/// durable approval commands the loop issues, so the ask path can be observed +/// from the host side as well as from the guest's decision. +struct AskPathProvider { + provider: Arc, + request_answer: JsonValue, + resolve_answer: JsonValue, + seen: Mutex>, +} + +impl AgentProviderHost for AskPathProvider { + fn call(&self, request: &JsonValue, cancellation: &RunCancellation) -> JsonValue { + self.provider.call(request, cancellation) + } + + fn approval_storage( + &self, + operation: &str, + payload: &JsonValue, + _cancellation: &RunCancellation, + ) -> JsonValue { + self.seen + .lock() + .expect("approval log") + .push((operation.to_string(), payload.clone())); + match operation { + "request" => self.request_answer.clone(), + _ => self.resolve_answer.clone(), + } + } +} + +fn main_loop_vm(provider: Arc, fixture: &LifecycleFixture) -> Vm { + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/agent/main.rss"); + let source = std::fs::read_to_string(&path).expect("read the real agent main"); + let catalog = agent_host_catalog(); + let options = CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()); + let compiled = compile_source_at_path_with_flavor_and_options( + &path, + &source, + SourceFlavor::RustScript, + options, + ) + .expect("compile the real agent main against the published catalog"); + let mut vm = Vm::new_shared(Arc::new(compiled.program)); + let mut registry = HostFunctionRegistry::restricted(); + for builtin in crate::runtime::host_compose::RESTRICTED_STANDARD_BUILTINS { + registry.allow_builtin(builtin).expect("admit builtin"); + } + register_agent_host_functions(&mut registry, catalog.as_ref()) + .expect("register the published agent descriptors"); + registry + .bind_vm_cached(&mut vm) + .expect("bind the restricted registry"); + vm.host_context().set_module_state(AgentHostState { + child_call: None, + child_read: None, + provider, + cancellation: RunCancellation::with_timeout(CANCEL), + sleeps: Arc::new(Mutex::new(SleepLog::default())), + skip_sleep: true, + metrics: None, + lifecycle: Some(Arc::new(fixture.lifecycle.clone())), + capability_owner: Some(fixture.owner.clone()), + filesystem: None, + processes: None, + artifacts: None, + leases: Arc::new(Mutex::new(HashMap::new())), + control_hook: None, + pinned_tool: None, + allow_stateless_pin: true, + expected_call: None, + plugin_state_call: None, + provider_arguments: Default::default(), + }); + assert!(matches!( + vm.run().expect("main root frame"), + VmStatus::Halted + )); + vm +} + +/// The approval-request row the loop writes, echoed back by the storage host. +fn request_answer() -> JsonValue { + json!({ + "ok": true, + "result": { + "columns": [ + "id", "run_id", "session_id", "tool_call_id", "tool_name", + "arguments_json", "risk_class", "state", "decision_scope", + "one_time", "requested_at_ms", "expires_at_ms", + "resolved_at_ms", "resolver", "decision_reason" + ], + "rows": [[ + "guardian-approval:run-1:call-1", "run-1", "session-1", "call-1", + "write_file", "{}", "write", "pending", "single_call", 1, 0, 0, + null, null, null + ]] + } + }) +} + +#[test] +fn write_call_with_text_arguments_builds_a_preview_and_enters_approval() { + let scripted = Arc::new(ScriptedProvider::new()); + scripted.push_envelope(json!({ + "ok": true, + "response": { + "text": "", + "tool_calls": [{ + "id": "call-1", + "name": "write_file", + "arguments_json": "{\"path\":\"notes.txt\",\"content\":\"APPROVAL_SENTINEL\"}" + }] + } + })); + scripted.push_envelope(json!({ + "ok": true, + "response": { + "text": "{\"outcome\":\"allow\",\"risk_level\":\"low\",\"user_authorization\":\"medium\",\"rationale\":\"the user asked for this file\"}" + } + })); + let provider = Arc::new(AskPathProvider { + provider: scripted.clone(), + request_answer: request_answer(), + resolve_answer: json!({"ok": true, "result": {"rows_affected": 1}}), + seen: Mutex::new(Vec::new()), + }); + let fixture = LifecycleFixture::new("ask"); + let mut vm = main_loop_vm(provider.clone(), &fixture); + + let context = super::json_to_vm_value(&json!({ + "profile_id": "profile-1", + "session_id": "session-1", + "run_id": "run-1", + "metadata": {"registry_identity": "registry-1"}, + "provider": "openai", + "model": "request-model", + "input": {"message": "write the file notes.txt"}, + "system_prompt": "run constraints", + "coding_system_prompt": "coding prompt", + "config": {"max_retries": 0}, + "limits": {"max_turns": 3, "max_tool_calls": 2}, + "approval_settings": {"write": "ask", "reviewer": "guardian", "required": true}, + "tools": [{"name": "write_file", "risk_class": "write"}] + })); + let decision = invoke(&mut vm, "run", &[context]); + + // The durable approval request is the observable proof that the action + // entered the approval path instead of failing closed at the preview. + let seen = provider.seen.lock().expect("approval log").clone(); + let request = seen + .iter() + .find(|(operation, _)| operation == "request") + .map(|(_, payload)| payload.clone()) + .unwrap_or_else(|| { + panic!("no approval request was persisted: {seen:?} decision: {decision:?}") + }); + let preview = request["owner_preview"] + .as_str() + .unwrap_or_else(|| panic!("owner_preview is not text: {request}")); + assert!( + preview.starts_with("write_file "), + "preview must describe the write action: {preview:?}" + ); + assert!( + preview.contains("\"path\":\"notes.txt\""), + "preview must name the target path: {preview:?}" + ); + assert!( + preview.contains("content_bytes"), + "preview must report the bounded content size: {preview:?}" + ); + assert!( + !preview.contains("APPROVAL_SENTINEL"), + "file content never reaches the owner preview: {preview:?}" + ); + + // It was not the preview that stopped the action: neither the decision + // reason nor the typed error code is the fail-closed preview refusal. + let reason = text_field(&decision, "reason"); + assert_ne!( + reason, "approval_preview_unavailable", + "the preview must not fail closed for a text-carried write call: {decision:?}" + ); + let error = field(&decision, "error"); + assert_ne!( + text_field(&error, "code"), + "approval_preview_unavailable", + "the preview must not fail closed for a text-carried write call: {decision:?}" + ); + assert_eq!( + field(&decision, "tool_calls_used"), + Value::Int(1), + "the write call reached the dispatch boundary: {decision:?}" + ); +} diff --git a/src/runtime/auth_runner.rs b/src/runtime/auth_runner.rs index 85c6980..4695cbb 100644 --- a/src/runtime/auth_runner.rs +++ b/src/runtime/auth_runner.rs @@ -20,7 +20,7 @@ use rustscript_vm::{ CapabilityProfile, CompileSourceFileOptions, HostApiCatalog, HostFunctionRegistry, InvocationError, InvocationItem, InvocationPoll, ParserDialect, SharedParserOptions, SourceFlavor, UsePathSegment, Value, Vm, VmStatus, VmYieldReason, - compile_source_with_flavor_and_options, parse_source_with_dialect, + compile_source_with_flavor_and_options, option, parse_source_with_dialect, }; use crate::auth::host::{ @@ -39,7 +39,15 @@ const VM_DRAIN_TIMEOUT: Duration = Duration::from_secs(1); struct AuthEntryParserDialect; +/// Parses the auth bundle with the same contract the RustScript frontend +/// enforces: the strict guest dialect enables logical `Option`/`Some`/`None`, +/// `()`, `Result` syntax and closes legacy null literals. A scan under a +/// narrower dialect would reject sources the compiler accepts. impl ParserDialect for AuthEntryParserDialect { + fn is_import_keyword(&self, ident: &str) -> bool { + ident == "import" + } + fn allow_let_mut_binding(&self) -> bool { true } @@ -55,6 +63,22 @@ impl ParserDialect for AuthEntryParserDialect { fn allow_for_in_loop(&self) -> bool { true } + + fn allow_result_syntax(&self) -> bool { + true + } + + fn allow_option_syntax(&self) -> bool { + true + } + + fn allow_result_try(&self) -> bool { + true + } + + fn reject_legacy_null(&self) -> bool { + true + } } static AUTH_ENTRY_PARSER_DIALECT: AuthEntryParserDialect = AuthEntryParserDialect; @@ -293,7 +317,9 @@ impl AuthEntryRunner { let registry = restricted_auth_registry(&auth_host, self.catalog.as_ref(), self.bundle_json_encode) .map_err(|error| setup_failure(cancellation, error))?; - self.run_with_registry(registry, input, cancellation, |vm| auth_host.bind_vm(vm)) + self.run_with_registry(registry, input, cancellation, |vm| { + auth_host.bind_vm_with_json_policy(vm, self.bundle_json_encode) + }) } /// Run the same RSS entry engine with a no-store, path-only native host. @@ -546,20 +572,24 @@ fn drive_root_frame(vm: &mut Vm, cancellation: &RunCancellation) -> Result<(), A } } +/// Builds the guest command map. +/// +/// The two optional selectors are logical `Option` slots in the guest: +/// an absent selector is the tagged `None` carrier, never a physical null, so +/// the callable argument schema matches what `commands::run` declares. fn command_value(input: AuthCommandInput) -> Value { + let provider = match input.provider { + Some(value) => option::some_carrier(Value::string(value)), + None => option::none_carrier(), + }; + let credential_id = match input.credential_id { + Some(value) => option::some_carrier(Value::string(value)), + None => option::none_carrier(), + }; Value::map(vec![ (Value::string("operation"), Value::string(input.operation)), - ( - Value::string("provider"), - input.provider.map(Value::string).unwrap_or(Value::Null), - ), - ( - Value::string("credential_id"), - input - .credential_id - .map(Value::string) - .unwrap_or(Value::Null), - ), + (Value::string("provider"), provider), + (Value::string("credential_id"), credential_id), ]) } @@ -589,7 +619,7 @@ fn invoke_entry( } InvocationPoll::Ready(Some(Ok(InvocationItem::Complete(value)))) => { validate_output(&value)?; - return Ok(value); + return Ok(normalize_entry_output(value)); } InvocationPoll::Ready(Some(Err(InvocationError::Cancelled(reason)))) => { return Err(AuthEntryError::Cancelled(reason)); @@ -607,6 +637,63 @@ fn invoke_entry( } } +/// Reads a guest `Option` field that may arrive either in the legacy +/// encoding (`null` or a bare string) or as the tagged carrier the strict +/// dialect emits (`{tag:false}` / `{tag:true,payload}`). +fn option_string_field(value: &Value, name: &str) -> Result, AuthEntryError> { + match map_field(value, name) { + None | Some(Value::Null) => Ok(None), + Some(Value::String(text)) => Ok(Some(text.as_str().to_owned())), + Some(Value::Map(fields)) => { + let tag = fields.iter().find_map(|(key, value)| match (key, value) { + (Value::String(key), Value::Bool(tag)) if key.as_str() == "tag" => Some(*tag), + _ => None, + }); + match tag { + Some(false) if fields.len() == 1 => Ok(None), + Some(true) if fields.len() == 2 => { + match fields.iter().find_map(|(key, value)| match (key, value) { + (Value::String(key), Value::String(text)) if key.as_str() == "payload" => { + Some(text.as_str().to_owned()) + } + _ => None, + }) { + Some(text) => Ok(Some(text)), + None => Err(AuthEntryError::OutputInvalid), + } + } + _ => Err(AuthEntryError::OutputInvalid), + } + } + Some(_) => Err(AuthEntryError::OutputInvalid), + } +} + +/// Rewrites the logical `Option` fields back to the legacy wire shape +/// (`string` or `null`) so callers that predate the carrier keep working. +fn normalize_entry_output(value: Value) -> Value { + let Value::Map(fields) = &value else { + return value; + }; + let mut rebuilt = Vec::with_capacity(fields.len()); + for (key, field) in fields.iter() { + let name = match key { + Value::String(name) => name.as_str(), + _ => "", + }; + let field = match name { + "credential_id" | "error" => match option_string_field(&value, name) { + Ok(Some(text)) => Value::string(text), + Ok(None) => Value::Null, + Err(_) => field.clone(), + }, + _ => field.clone(), + }; + rebuilt.push((key.clone(), field)); + } + Value::map(rebuilt) +} + fn validate_output(value: &Value) -> Result<(), AuthEntryError> { let Value::Map(fields) = value else { return Err(AuthEntryError::OutputInvalid); @@ -626,17 +713,13 @@ fn validate_output(value: &Value) -> Result<(), AuthEntryError> { let Some(status) = map_field(value, "status") else { return Err(AuthEntryError::OutputInvalid); }; - let credential_id = map_field(value, "credential_id"); - let error = map_field(value, "error"); let scalar_string = |value: &Value| matches!(value, Value::String(_)); + option_string_field(value, "credential_id")?; + option_string_field(value, "error")?; if !matches!(ok, Value::Bool(_)) || !scalar_string(operation) - || credential_id.is_some_and(|value| { - !(matches!(value, Value::Null) || scalar_string(value)) - }) || !matches!(generation, Value::Int(n) if *n >= 0) || !scalar_string(status) - || error.is_some_and(|value| !(matches!(value, Value::Null) || scalar_string(value))) || fields.iter().any(|(key, _)| { !matches!(key, Value::String(name) if ["ok", "operation", "credential_id", "generation", "status", "error"].contains(&name.as_str())) }) diff --git a/src/runtime/host_compose.rs b/src/runtime/host_compose.rs index 09853f7..65f26d7 100644 --- a/src/runtime/host_compose.rs +++ b/src/runtime/host_compose.rs @@ -11,30 +11,100 @@ use rustscript_vm::{ HostTypeSchema, Value, Vm, VmResult, standard_host_catalog, standard_host_modules, }; -/// Genuinely open JSON/tool payloads that remain `HostTypeSchema::Unknown`. -/// Each entry is (`function_name`, `slot`) where slot is a parameter label -/// or `return.` for a named-struct field. -#[allow(dead_code)] -pub const DYNAMIC_HOST_SLOTS: &[(&str, &str)] = &[ +/// Slots that carry one open JSON payload as **serialized text**. +/// +/// Every entry is (`function_name`, `slot`) where slot is a parameter label or +/// `return.` of the declared named struct. The audit resolves the entry +/// to that exact field and requires it to be `String` itself — a struct that +/// merely *contains* some string field does not satisfy the promise. The guest +/// never decodes these; the host adapter parses or serializes them. +#[allow(dead_code)] // read by the boundary audit +pub const OPEN_JSON_TEXT_SLOTS: &[(&str, &str)] = &[ + // Parsed-object bodies cross as serialized text in `value`. + ("agent::parse_json_object", "return.value"), + // The provider request is serialized by the guest through + // `agent::json_text`; the host parses it, bounds it (4 MiB) and requires an + // object before it reaches the provider. ("agent::provider_call", "request"), - ("agent::provider_call", "return"), + // Provider bodies: open request/response JSON over the provider bridge. + ("agent::provider_call", "return.error"), + ("agent::provider_call", "return.response"), + // Approval storage: the durable payload is provider/tool defined JSON, so + // it crosses as text and the host parses it. ("agent::approval_storage", "payload"), - ("agent::approval_storage", "return"), - // Mirrors the descriptor's open app-context JSON slots; the adapter - // validates input resources before forwarding context to the provider. + // The durable transcript handed to the context refresh is open JSON. ("agent::refresh_context", "context"), - ("agent::refresh_context", "return"), + // Guest→host serialization bridge. + ("agent::json_text", "return.text"), + // Tool lifecycle metadata: the host parses the text into prepare metadata. ("agent_runtime::tool_prepare", "metadata"), ("agent_runtime::tool_replay", "metadata"), ("agent_runtime::approval_attest", "metadata"), + // The guest serializes its canonical tool result; the host parses it. ("agent_runtime::tool_commit", "result"), + // Replayed/committed canonical results come back as text. + ("agent_runtime::tool_prepare", "return.result"), + ("agent_runtime::tool_replay", "return.result"), + ("agent_runtime::approval_attest", "return.result"), + ("agent_runtime::tool_commit", "return.result"), + // Artifact metadata is durable, plugin/provider defined JSON. ("cap::artifact_put", "metadata"), ("cap::artifact_put_result", "metadata"), - ("cap::artifact_put", "return"), - ("cap::artifact_put_result", "return"), - ("cap::artifact_get", "return"), - ("cap::artifact_reference", "return"), - ("agent::parse_json_object", "return"), + ("cap::artifact_put", "return.metadata"), + ("cap::artifact_put_result", "return.metadata"), + ("cap::artifact_reference", "return.metadata"), +]; + +/// Parameter slots that accept arbitrary native JSON. +/// +/// This is not a quarantine: a function that receives a provider- or +/// plugin-defined body has no concrete schema to declare, and this pin admits +/// no guest-side JSON serializer, so a dynamic *input* is the only way to pass +/// such a value in. Three rules keep the list honest: the resolved slot must be +/// exactly `Unknown`, it must be a parameter (a dynamic **return** is never +/// accepted — the compiler rejects those outright), and the host adapter that +/// owns it must do its own bounded validation. +#[allow(dead_code)] // read by the boundary audit +pub const OPEN_JSON_ACCEPT_SLOTS: &[(&str, &str)] = &[ + // The single serialization bridge: it exists precisely because the guest + // cannot produce JSON text itself. + ("agent::json_text", "value"), +]; + +/// Slots that still publish a dynamic or legacy-null schema. +/// +/// This is a quarantine, not an allow-list of correct behaviour: the audit +/// asserts the set **equals** the dynamic slots the catalog actually declares, +/// so it can only shrink. Converting a slot without deleting its entry fails, +/// and declaring a new dynamic slot without listing it fails. +#[allow(dead_code)] // read by the boundary audit +pub const PENDING_DYNAMIC_SLOTS: &[(&str, &str)] = &[ + // Open provider/tool JSON inputs: the adapter validates resources before + // forwarding them, but the parameter schema is still dynamic. + ("cap::process_spawn", "limits"), + // ---- auth: revealed only because the audit now covers every published + // namespace; the auth/oauth/config trunks are a later round. + // ---- config: revealed only because the audit now covers every published + // namespace; the auth/oauth/config trunks are a later round. + // ---- oauth: revealed only because the audit now covers every published + // namespace; the auth/oauth/config trunks are a later round. + ("oauth::transport_capture", "payload"), + ("oauth::transport_capture_result", "payload"), + ("oauth::transport_payload", "payload"), + ("oauth::transport_payload_result", "payload"), + // ---- state: revealed only because the audit now covers every published + // namespace; the auth/oauth/config trunks are a later round. + ("state::read", "return"), + ("state::write", "return"), + // ---- stream: revealed only because the audit now covers every published + // namespace; the auth/oauth/config trunks are a later round. + ("stream::emit", "value"), + // ---- timer: revealed only because the audit now covers every published + // namespace; the auth/oauth/config trunks are a later round. + ("timer::at", "callback"), + ("timer::every", "callback"), + // ---- workspace: revealed only because the audit now covers every published + // namespace; the auth/oauth/config trunks are a later round. ]; /// Standard runtime builtins the restricted registry may admit. @@ -100,21 +170,28 @@ pub fn absorb_catalog(builder: &mut HostApiBuilder, catalog: &HostApiCatalog) { } } +/// The core stream adapter yields Unit; publish that concrete result instead +/// of the descriptor's legacy dynamic return, retaining its original adapter. +pub fn typed_stream_emit_descriptor() -> HostFunctionDescriptor { + let mut descriptor = standard_host_modules() + .iter() + .find(|module| module.name == "context") + .expect("standard context module") + .owned_descriptors() + .into_iter() + .find(|descriptor| descriptor.schema.name == "stream::emit") + .expect("standard stream adapter"); + descriptor.schema.return_type = HostTypeSchema::Unit; + descriptor +} + pub fn compose_with_standard(modules: &[HostModuleDescriptor]) -> Arc { let mut builder = HostApiBuilder::new(); absorb_catalog(&mut builder, standard_host_catalog().as_ref()); // `stream::emit` is descriptor-only in frozen core (not on the guest - // catalog). Agent RSS still imports it, so publish the owned schema so - // compile fingerprints match the runtime registry. json/bytes stay - // namespaced builtins via allow_builtin. - for module in standard_host_modules() { - if module.name != "context" { - continue; - } - for descriptor in module.owned_descriptors() { - builder.function(descriptor.schema.clone()); - } - } + // catalog). Its adapter yields Unit; publish the concrete result so + // compile fingerprints match the runtime registry. + builder.function(typed_stream_emit_descriptor().schema); for module in modules { absorb_catalog( &mut builder, @@ -140,48 +217,341 @@ mod tests { use super::*; use crate::runtime::agent_host::{agent_host_catalog, agent_host_module}; use rustscript_vm::HostFunctionRegistry; + use std::collections::BTreeSet; - fn slot_allowed(function: &str, slot: &str) -> bool { - DYNAMIC_HOST_SLOTS - .iter() - .any(|(name, allowed)| *name == function && *allowed == slot) + /// One declared host slot, addressed exactly as the audit lists it. + struct Slot { + function: String, + slot: String, + schema: HostTypeSchema, } - #[test] - fn dynamic_slots_are_explicit_and_complete_for_agent_module() { - let catalog = agent_host_catalog(); - let mut unexpected = Vec::new(); + /// Every published slot is audited: the whole composed catalog, not only + /// the agent/capability namespaces. A slot added by config/auth/oauth is + /// held to the same rules. + fn slot_table(catalog: &HostApiCatalog) -> Vec { + let mut table = Vec::new(); for function in catalog.functions() { - if !function.name.starts_with("agent::") - && !function.name.starts_with("agent_runtime::") - && !function.name.starts_with("cap::") - { + for param in &function.params { + table.push(Slot { + function: function.name.clone(), + slot: param.name.clone(), + schema: param.ty.clone(), + }); + } + table.push(Slot { + function: function.name.clone(), + slot: "return".to_string(), + schema: function.return_type.clone(), + }); + } + table + } + + /// Path lookup used by the open-slot lists, which may address one field of a + /// declared named struct. Resolved from the same table the quarantine uses, + /// so a mutated table moves every direction of the audit together. + fn table_schema<'a>( + slots: &'a [Slot], + function: &str, + slot: &str, + ) -> Option<&'a HostTypeSchema> { + if let Some(field_name) = slot.strip_prefix("return.") { + let entry = slots + .iter() + .find(|entry| entry.function == function && entry.slot == "return")?; + let HostTypeSchema::Named { fields, .. } = &entry.schema else { + return None; + }; + return fields + .iter() + .find(|field| field.name == field_name) + .map(|field| &field.ty); + } + slots + .iter() + .find(|entry| entry.function == function && entry.slot == slot) + .map(|entry| &entry.schema) + } + + /// A dynamic or legacy-null slot anywhere in the declared schema. + fn dynamic(schema: &HostTypeSchema) -> bool { + match schema { + HostTypeSchema::Unknown | HostTypeSchema::Null | HostTypeSchema::Optional(_) => true, + HostTypeSchema::Array(inner) + | HostTypeSchema::Map(inner) + | HostTypeSchema::Option(inner) => dynamic(inner), + HostTypeSchema::Result { ok, err } => dynamic(ok) || dynamic(err), + HostTypeSchema::Callable { params, result } => { + params.iter().any(dynamic) || dynamic(result) + } + HostTypeSchema::Named { fields, .. } => fields.iter().any(|field| dynamic(&field.ty)), + HostTypeSchema::Unit + | HostTypeSchema::Int + | HostTypeSchema::Float + | HostTypeSchema::Number + | HostTypeSchema::Bool + | HostTypeSchema::String + | HostTypeSchema::Bytes + | HostTypeSchema::Resource(_) => false, + } + } + + /// A slot that is exactly one serialized-text field (not a struct that + /// happens to contain text somewhere). + fn exactly_text(schema: &HostTypeSchema) -> bool { + matches!(schema, HostTypeSchema::String) + } + + /// A slot that accepts arbitrary native JSON. + fn exactly_open(schema: &HostTypeSchema) -> bool { + matches!(schema, HostTypeSchema::Unknown) + } + + fn entries(list: &[(&str, &str)]) -> BTreeSet<(String, String)> { + list.iter() + .map(|(function, slot)| (function.to_string(), slot.to_string())) + .collect() + } + + /// Bidirectional audit. The three lists are the contract: + /// + /// * no declared **return** slot is dynamic — the guest compiler rejects + /// those outright, so publishing one is a host bug; + /// * every dynamic parameter slot is either quarantined or an explicit + /// open-input slot, and every entry in either list is still dynamic (so + /// the quarantine can only shrink); + /// * every open-text entry resolves to a slot that is itself `String`; + /// * every open-input entry resolves to a slot that is itself `Unknown` and + /// is a parameter; + /// * the lists never overlap. + fn audit(slots: &[Slot]) -> Vec { + let mut problems = Vec::new(); + let pending = entries(PENDING_DYNAMIC_SLOTS); + let text = entries(OPEN_JSON_TEXT_SLOTS); + let accept = entries(OPEN_JSON_ACCEPT_SLOTS); + + let dynamic_returns: BTreeSet<(String, String)> = slots + .iter() + .filter(|slot| slot.slot == "return" && dynamic(&slot.schema)) + .map(|slot| (slot.function.clone(), slot.slot.clone())) + .collect(); + // A dynamic return is legal only while it is still quarantined; it can + // never be an accepted open input, because the guest cannot receive one. + for (function, slot) in dynamic_returns.difference(&pending) { + problems.push(format!( + "dynamic return slot is not quarantined: {function} {slot}" + )); + } + + let dynamic_slots: BTreeSet<(String, String)> = slots + .iter() + .filter(|slot| dynamic(&slot.schema)) + .map(|slot| (slot.function.clone(), slot.slot.clone())) + .collect(); + + for (function, slot) in dynamic_slots.difference(&pending) { + if accept.contains(&(function.clone(), slot.clone())) { continue; } - for param in &function.params { - if schema_is_unknown(¶m.ty) && !slot_allowed(&function.name, ¶m.name) { - unexpected.push(format!("{} param {}", function.name, param.name)); - } + problems.push(format!( + "dynamic slot is not quarantined in PENDING_DYNAMIC_SLOTS: {function} {slot}" + )); + } + for (function, slot) in pending.difference(&dynamic_slots) { + problems.push(format!( + "PENDING_DYNAMIC_SLOTS entry is no longer dynamic: {function} {slot}" + )); + } + for (function, slot) in accept.difference(&dynamic_slots) { + problems.push(format!( + "OPEN_JSON_ACCEPT_SLOTS entry is not an open schema: {function} {slot}" + )); + } + for (function, slot) in &text { + if pending.contains(&(function.clone(), slot.clone())) + || accept.contains(&(function.clone(), slot.clone())) + { + problems.push(format!( + "slot is listed as both open text and open/quarantined: {function} {slot}" + )); } - match &function.return_type { - HostTypeSchema::Named { fields, .. } => { - for field in fields { - let slot = format!("return.{}", field.name); - if schema_is_unknown(&field.ty) && !slot_allowed(&function.name, &slot) { - unexpected.push(format!("{} {slot}", function.name)); - } - } - } - other if schema_is_unknown(other) && !slot_allowed(&function.name, "return") => { - unexpected.push(format!("{} return", function.name)); - } - _ => {} + } + for (function, slot) in &accept { + if slot == "return" { + problems.push(format!( + "OPEN_JSON_ACCEPT_SLOTS must name a parameter, not a return: {function}" + )); + } + match table_schema(slots, function, slot) { + None => problems.push(format!( + "OPEN_JSON_ACCEPT_SLOTS entry has no matching declared slot: {function} {slot}" + )), + Some(entry) if !exactly_open(entry) => problems.push(format!( + "open-input slot is not exactly Unknown: {function} {slot}" + )), + Some(_) => {} } } + for (function, slot) in &text { + match table_schema(slots, function, slot) { + None => problems.push(format!( + "OPEN_JSON_TEXT_SLOTS entry has no matching declared slot: {function} {slot}" + )), + Some(entry) if dynamic(entry) => problems.push(format!( + "open-text slot is dynamic again: {function} {slot}" + )), + Some(entry) if !exactly_text(entry) => problems.push(format!( + "open-text slot is not itself a String: {function} {slot}" + )), + Some(_) => {} + } + } + problems + } + + #[test] + fn host_slot_audit_is_bidirectional_for_the_published_catalog() { + let slots = slot_table(agent_host_catalog().as_ref()); + let problems = audit(&slots); + assert!(problems.is_empty(), "host slot audit failed: {problems:#?}"); + assert!( + !PENDING_DYNAMIC_SLOTS.is_empty(), + "the quarantine must keep naming the slots that still have to move" + ); + } + + #[test] + fn host_slot_audit_covers_every_published_namespace() { + let catalog = agent_host_catalog(); + let slots = slot_table(catalog.as_ref()); + let audited: BTreeSet<&str> = slots + .iter() + .map(|slot| slot.function.split_once("::").map_or("", |(root, _)| root)) + .collect(); + for root in ["config", "oauth", "auth", "cap", "agent", "agent_runtime"] { + assert!( + audited.contains(root), + "the audit must cover the '{root}' namespace" + ); + } + } + + #[test] + fn host_slot_audit_catches_every_direction_of_drift() { + let mut slots = slot_table(agent_host_catalog().as_ref()); + + // A converted slot regressing to a dynamic schema is caught. + let converted = slots + .iter_mut() + .find(|slot| slot.function == "agent::parse_json_object" && slot.slot == "return") + .expect("converted slot is declared"); + converted.schema = HostTypeSchema::Unknown; + let problems = audit(&slots); + assert!( + problems + .iter() + .any(|problem| problem.contains("OPEN_JSON_TEXT_SLOTS entry") + || problem.contains("open-text slot is dynamic again")), + "a regressed open-text slot must fail the audit: {problems:#?}" + ); + assert!( + problems + .iter() + .any(|problem| problem.contains("not quarantined in PENDING_DYNAMIC_SLOTS")), + "an unlisted dynamic slot must fail the audit: {problems:#?}" + ); + + // A dynamic return slot is a hard failure, not a quarantine entry. + let mut slots = slot_table(agent_host_catalog().as_ref()); + let returned = slots + .iter_mut() + .find(|slot| slot.function == "cap::fs_metadata" && slot.slot == "return") + .expect("declared return"); + returned.schema = HostTypeSchema::Unknown; + let problems = audit(&slots); + assert!( + problems + .iter() + .any(|problem| problem.contains("dynamic return slot is not quarantined")), + "an unquarantined dynamic return must fail: {problems:#?}" + ); + + // A converted slot whose quarantine entry was kept is caught too. + let mut slots = slot_table(agent_host_catalog().as_ref()); + // The mutation targets a slot that is *still* quarantined; a converted + // slot (for example `agent::approval_storage payload`, which is text + // now) can no longer stand in for the stale-entry direction. + let pending = slots + .iter_mut() + .find(|slot| slot.function == "cap::process_spawn" && slot.slot == "limits") + .expect("quarantined slot is declared"); + pending.schema = HostTypeSchema::String; + let problems = audit(&slots); + assert!( + problems + .iter() + .any(|problem| problem.contains("no longer dynamic")), + "a stale quarantine entry must fail the audit: {problems:#?}" + ); + + // An open-text entry that resolves to a struct with some text field is + // not enough: the slot itself has to be the text. + let mut slots = slot_table(agent_host_catalog().as_ref()); + let nested = slots + .iter_mut() + .find(|slot| slot.function == "agent::parse_json_object" && slot.slot == "return") + .expect("converted slot is declared"); + nested.schema = HostTypeSchema::Named { + name: "EnvelopeWithText".to_string(), + fields: vec![rustscript_vm::HostStructField::new( + "value", + HostTypeSchema::Named { + name: "NestedText".to_string(), + fields: vec![rustscript_vm::HostStructField::new( + "text", + HostTypeSchema::String, + )], + }, + )], + }; + let problems = audit(&slots); + assert!( + problems + .iter() + .any(|problem| problem.contains("not itself a String")), + "only the exact field may be listed: {problems:#?}" + ); + } + + #[test] + fn open_text_slots_are_concrete_and_disjoint_from_the_quarantine() { + for (function, slot) in OPEN_JSON_TEXT_SLOTS { + assert!( + !PENDING_DYNAMIC_SLOTS + .iter() + .any(|(name, allowed)| name == function && allowed == slot), + "{function} {slot} must not be quarantined and converted at once" + ); + } assert!( - unexpected.is_empty(), - "unlisted Unknown host slots: {unexpected:?}" + !PENDING_DYNAMIC_SLOTS + .iter() + .any(|(name, _)| *name == "agent::parse_json_object"), + "a converted function must not stay quarantined" ); + let slots = slot_table(agent_host_catalog().as_ref()); + for (function, slot) in OPEN_JSON_TEXT_SLOTS { + let entry = table_schema(&slots, function, slot) + .unwrap_or_else(|| panic!("{function} {slot} is declared")); + assert!(!dynamic(entry), "{function} {slot} must be concrete"); + assert!(exactly_text(entry), "{function} {slot} is the text itself"); + } + for (function, slot) in OPEN_JSON_ACCEPT_SLOTS { + let entry = table_schema(&slots, function, slot) + .unwrap_or_else(|| panic!("{function} {slot} is declared")); + assert!(exactly_open(entry), "{function} {slot} is open"); + } } #[test] diff --git a/src/runtime/host_shapes.rs b/src/runtime/host_shapes.rs new file mode 100644 index 0000000..de116b3 --- /dev/null +++ b/src/runtime/host_shapes.rs @@ -0,0 +1,469 @@ +//! Declared-shape carriers at the host→guest return boundary. +//! +//! Two distinct nullabilities exist in the pinned core and they are *not* +//! interchangeable: +//! +//! * `HostTypeSchema::Optional(T)` is the legacy nullable slot. Its physical +//! carrier is the internal `Null` value. +//! * `HostTypeSchema::Option(T)` is the guest `Option`. Its physical carrier +//! is the canonical map `{tag: Bool, payload?}` (`tag: false` = `None`, +//! `tag: true` + `payload` = `Some`). `Some(())` is `{tag: true, payload: +//! null}`, so a physical null payload never selects the variant — only the +//! Boolean tag does. +//! +//! Because a physical null can be `None`, `Some(())`, or `Some(None)`, this +//! module never *infers* an `Option` from a null. An adapter that owns a +//! confirmed legacy nullable leaf constructs its carrier explicitly through +//! [`logical_option`], and a value that already carries a canonical carrier is +//! rejected instead of being wrapped twice. Anything the adapter cannot decide +//! is a hard error, never a guess. +//! +//! Shape checking itself is delegated to the VM's own native validator +//! (`Vm::validate_guest_result_value`): it walks `Result`, `Map`, `Optional`, +//! `Option`, `Named` and `Array` over native [`Value`]s, keeps a resource field +//! as its original `Value::Int` handle and checks that handle against the live +//! resource table, and never converts `Bytes` through JSON. Calling it from +//! inside the adapter — instead of leaving the check to the post-return pass — +//! is what lets an adapter that has already issued resources observe the +//! failure inside its own rollback scope. + +use std::fmt; + +use rustscript_vm::{ + CallOutcome, CallReturn, HostTypeSchema, Value, Vm, VmError, VmResult, option, +}; + +/// Why a declared return slot could not be shaped. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct ShapeError { + slot: String, + detail: String, +} + +impl ShapeError { + fn new(slot: impl Into, detail: impl Into) -> Self { + Self { + slot: slot.into(), + detail: detail.into(), + } + } + + /// Stable host error code used when the failure crosses the boundary. + pub(crate) fn code(&self) -> &'static str { + "host_return_shape_invalid" + } +} + +impl fmt::Display for ShapeError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "{}: {}", self.slot, self.detail) + } +} + +impl std::error::Error for ShapeError {} + +impl From for VmError { + fn from(error: ShapeError) -> Self { + VmError::HostError(format!("{} ({})", error, error.code())) + } +} + +/// Builds the canonical carrier for one confirmed nullable leaf slot. +/// +/// `legacy` is what the adapter itself observed for that field: `None` when the +/// field was absent or physically null on the old wire, `Some(value)` when the +/// adapter put a value there. It must never be handed a value it re-read from an +/// already-shaped result. The declared `inner` type then decides the rest: +/// +/// * a nested `Option`/`Result` slot must already carry a canonical carrier, +/// so `Some(None)` and `Some(Some(v))` stay distinguishable; +/// * a `Unit` slot accepts the physical null payload that means `Some(())`; +/// * any other slot must not hand in a null (that would be indistinguishable +/// from `None`) and must not hand in an already-shaped carrier (that would be +/// wrapped a second time). +pub(crate) fn logical_option( + inner: &HostTypeSchema, + legacy: Option<&Value>, +) -> Result { + let Some(value) = legacy else { + return Ok(option::none_carrier()); + }; + let nested = matches!( + inner, + HostTypeSchema::Option(_) | HostTypeSchema::Result { .. } + ); + if nested { + if ensure_carrier(value).is_err() { + return Err(ShapeError::new( + "Option", + "nested Option/Result payload must already be one canonical {tag, payload} carrier", + )); + } + return Ok(option::some_carrier(value.clone())); + } + if matches!(value, Value::Null) && !matches!(inner, HostTypeSchema::Unit) { + return Err(ShapeError::new( + "Option", + "a physical null payload cannot decide this Option; pass no value or an explicit nested carrier", + )); + } + if ensure_carrier(value).is_ok() { + return Err(ShapeError::new( + "Option", + "value already carries a canonical {tag, payload} carrier; refusing to wrap it twice", + )); + } + Ok(option::some_carrier(value.clone())) +} + +/// Checks the canonical carrier shape strictly: only `tag` (Boolean, required) +/// and `payload` may appear, and `tag: false` must not carry a payload. +fn ensure_carrier(value: &Value) -> Result<(), ShapeError> { + let Value::Map(entries) = value else { + return Err(ShapeError::new("carrier", "not a map")); + }; + if entries.len() > 2 { + return Err(ShapeError::new( + "carrier", + "more than the two canonical fields", + )); + } + for (key, _) in entries.iter() { + match key { + Value::String(name) if name.as_str() == "tag" || name.as_str() == "payload" => {} + _ => { + return Err(ShapeError::new( + "carrier", + "non-canonical field in a {tag, payload} carrier", + )); + } + } + } + let Some(Value::Bool(tag)) = entries.get(&Value::string("tag")) else { + return Err(ShapeError::new("carrier", "missing Boolean tag")); + }; + let payload = entries.get(&Value::string("payload")); + if !*tag && payload.is_some() { + return Err(ShapeError::new("carrier", "None must not carry a payload")); + } + Ok(()) +} + +/// Whether `value` is already one canonical `Option`/`Result` carrier. +#[cfg(test)] +fn is_carrier(value: &Value) -> bool { + ensure_carrier(value).is_ok() +} + +/// Converts a value that the adapter built on the **old** nullable wire into the +/// declared shape, constructing one canonical carrier for every declared +/// `Option` slot. +/// +/// Only the declared schema decides *where* a carrier belongs, so this is not a +/// guess from the data: a declared `Option` field that is absent from a named +/// map or physically null is `None`, an already-canonical carrier is left +/// alone instead of being wrapped twice, and any other declared shape recurses. +/// A legacy `Optional` slot in the target schema, an undecidable null in a +/// non-`Option` slot, and an unsupported schema all fail closed. +pub(crate) fn logical_from_legacy( + schema: &HostTypeSchema, + value: &Value, +) -> Result { + match schema { + HostTypeSchema::Option(inner) => { + if ensure_carrier(value).is_ok() { + return Ok(value.clone()); + } + if matches!(value, Value::Null) { + return logical_option(inner, None); + } + // The payload of a `Some` keeps its own declared shape: a named or + // array payload still has to have every nested nullable leaf + // converted before it is wrapped, otherwise the inner Option slots + // would still be physical nulls under the carrier. + if matches!( + inner.as_ref(), + HostTypeSchema::Named { .. } | HostTypeSchema::Array(_) + ) { + let converted = logical_from_legacy(inner, value)?; + return logical_option(inner, Some(&converted)); + } + logical_option(inner, Some(value)) + } + HostTypeSchema::Named { fields, .. } => { + let Value::Map(entries) = value else { + return Err(ShapeError::new("Named", "not a map")); + }; + let mut shaped = Vec::with_capacity(fields.len()); + for field in fields { + let entry = entries.get(&Value::string(&field.name)); + let converted = match (entry, &field.ty) { + (Some(entry), ty) => logical_from_legacy(ty, entry)?, + (None, HostTypeSchema::Option(inner)) => logical_option(inner, None)?, + (None, HostTypeSchema::Optional(_)) => continue, + (None, _) => { + return Err(ShapeError::new( + &field.name, + "required field is missing from the old wire shape", + )); + } + }; + shaped.push((Value::string(&field.name), converted)); + } + Ok(Value::map(shaped)) + } + HostTypeSchema::Array(inner) => { + let Value::Array(items) = value else { + return Err(ShapeError::new("Array", "not an array")); + }; + let mut shaped = Vec::with_capacity(items.len()); + for item in items.iter() { + shaped.push(logical_from_legacy(inner, item)?); + } + Ok(Value::Array(shaped.into())) + } + HostTypeSchema::Null => Err(ShapeError::new( + "Null", + "a declared physical Null is not an Option and cannot be converted", + )), + HostTypeSchema::Optional(_) => Err(ShapeError::new( + "Optional", + "the converted shape must not keep a legacy nullable slot", + )), + HostTypeSchema::Unknown | HostTypeSchema::Map(_) | HostTypeSchema::Callable { .. } => Err( + ShapeError::new("schema", "an open slot cannot be converted field by field"), + ), + HostTypeSchema::Result { .. } => Err(ShapeError::new( + "Result", + "a Result carrier is built by its owner, not converted from null", + )), + HostTypeSchema::Unit + | HostTypeSchema::Int + | HostTypeSchema::Float + | HostTypeSchema::Number + | HostTypeSchema::Bool + | HostTypeSchema::String + | HostTypeSchema::Bytes + | HostTypeSchema::Resource(_) => { + if matches!(value, Value::Null) { + return Err(ShapeError::new( + "leaf", + "a physical null cannot stand in for a non-Option leaf", + )); + } + Ok(value.clone()) + } + } +} + +/// The inverse of [`logical_from_legacy`] for a converted **parameter**. +/// +/// A guest now sends the declared shape (with `Option` carriers), while the +/// durable record still has the old nullable JSON shape: `Some(payload)` becomes +/// the payload and `None` becomes a physical null, which is exactly how the +/// serde structs still model an absent field. Only the declared schema decides +/// where a carrier is read; an undeclared shape fails closed. +pub(crate) fn legacy_from_logical( + schema: &HostTypeSchema, + value: &Value, +) -> Result { + match schema { + HostTypeSchema::Option(inner) => match option_payload(inner, value)? { + None => Ok(Value::Null), + Some(payload) => legacy_from_logical(inner, payload), + }, + HostTypeSchema::Named { fields, .. } => { + let Value::Map(entries) = value else { + return Err(ShapeError::new("Named", "not a map")); + }; + let mut legacy = Vec::with_capacity(fields.len()); + for field in fields { + match entries.get(&Value::string(&field.name)) { + Some(entry) => { + legacy.push(( + Value::string(&field.name), + legacy_from_logical(&field.ty, entry)?, + )); + } + None if matches!(field.ty, HostTypeSchema::Option(_)) => { + legacy.push((Value::string(&field.name), Value::Null)); + } + None => { + return Err(ShapeError::new( + &field.name, + "required field is missing from the declared shape", + )); + } + } + } + Ok(Value::map(legacy)) + } + HostTypeSchema::Array(inner) => { + let Value::Array(items) = value else { + return Err(ShapeError::new("Array", "not an array")); + }; + let mut legacy = Vec::with_capacity(items.len()); + for item in items.iter() { + legacy.push(legacy_from_logical(inner, item)?); + } + Ok(Value::Array(legacy.into())) + } + HostTypeSchema::Unknown + | HostTypeSchema::Null + | HostTypeSchema::Optional(_) + | HostTypeSchema::Map(_) + | HostTypeSchema::Callable { .. } + | HostTypeSchema::Result { .. } => Err(ShapeError::new( + "schema", + "this slot has no declared concrete shape to read back", + )), + HostTypeSchema::Unit + | HostTypeSchema::Int + | HostTypeSchema::Float + | HostTypeSchema::Number + | HostTypeSchema::Bool + | HostTypeSchema::String + | HostTypeSchema::Bytes + | HostTypeSchema::Resource(_) => Ok(value.clone()), + } +} + +/// Reads one logical `Option` carrier out of a guest-supplied parameter. +/// +/// `Ok(None)` is a `None` carrier. `Ok(Some(payload))` is a live payload: a +/// resource payload stays its `Value::Int` handle. A `Some` without a payload is +/// only legal for a logical Unit, so a resource payload that is missing fails +/// closed instead of being treated as absent. +pub(crate) fn option_payload<'a>( + inner: &HostTypeSchema, + value: &'a Value, +) -> Result, ShapeError> { + let (some, payload) = rustscript_vm::option::payload(value).map_err(|_| { + ShapeError::new( + "Option", + "parameter is not one canonical {tag, payload} carrier", + ) + })?; + if !some { + return Ok(None); + } + match payload { + Some(payload) => Ok(Some(payload)), + None if matches!(inner, HostTypeSchema::Unit) => Ok(Some(&Value::Null)), + None => Err(ShapeError::new( + "Option", + "a Some carrier for this payload type must carry its payload", + )), + } +} + +/// Validates a finished native value against the declared return schema inside +/// the calling adapter, then returns it. +/// +/// Resource slots keep the `Value::Int` handle the adapter issued and are +/// checked against the live resource table; `Bytes` never leaves the VM. +pub(crate) fn shaped(vm: &mut Vm, schema: &HostTypeSchema, value: Value) -> VmResult { + ensure_shaped(vm, schema, &value)?; + Ok(CallOutcome::Return(CallReturn::One(value))) +} + +/// The same check without consuming the value, for an adapter that still has to +/// fill a resource handle into the checked shape or embed it in a larger +/// envelope. +pub(crate) fn ensure_shaped(vm: &Vm, schema: &HostTypeSchema, value: &Value) -> VmResult<()> { + vm.validate_guest_result_value(value, schema) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn text(value: &str) -> Value { + Value::string(value) + } + + #[test] + fn absent_legacy_nullable_becomes_none() { + assert_eq!( + logical_option(&HostTypeSchema::String, None).unwrap(), + option::none_carrier() + ); + } + + #[test] + fn present_unit_payload_becomes_some_unit() { + let carrier = logical_option(&HostTypeSchema::Unit, Some(&Value::Null)).unwrap(); + assert_eq!(carrier, option::some_carrier(Value::Null)); + assert!(is_carrier(&carrier), "Some(()) is a canonical carrier"); + assert_ne!(carrier, option::none_carrier(), "tag, not payload, selects"); + } + + #[test] + fn nested_none_becomes_some_none() { + let inner = HostTypeSchema::Option(Box::new(HostTypeSchema::String)); + let present = option::none_carrier(); + let carrier = logical_option(&inner, Some(&present)).unwrap(); + assert_eq!(carrier, option::some_carrier(option::none_carrier())); + assert!(is_carrier(&carrier)); + } + + #[test] + fn nested_some_becomes_some_some() { + let inner = HostTypeSchema::Option(Box::new(HostTypeSchema::String)); + let present = option::some_carrier(text("alpha")); + let carrier = logical_option(&inner, Some(&present)).unwrap(); + assert_eq!(carrier, option::some_carrier(present)); + } + + #[test] + fn result_slot_payload_must_already_be_a_carrier() { + let inner = HostTypeSchema::Result { + ok: Box::new(HostTypeSchema::String), + err: Box::new(HostTypeSchema::String), + }; + let present = rustscript_vm::result::carrier(true, text("done")); + let carrier = logical_option(&inner, Some(&present)).unwrap(); + assert_eq!(carrier, option::some_carrier(present)); + assert!( + logical_option(&inner, Some(&Value::Null)).is_err(), + "a physical null cannot stand in for a nested Result" + ); + } + + #[test] + fn a_second_conversion_is_refused() { + let converted = option::some_carrier(text("alpha")); + let error = logical_option(&HostTypeSchema::String, Some(&converted)) + .expect_err("an already-shaped value must not be wrapped twice"); + assert_eq!(error.code(), "host_return_shape_invalid"); + assert!(error.to_string().contains("twice"), "{error}"); + } + + #[test] + fn a_physical_null_for_a_plain_leaf_is_undecidable() { + let error = logical_option(&HostTypeSchema::String, Some(&Value::Null)) + .expect_err("null cannot decide Some(empty) versus None"); + assert!(error.to_string().contains("cannot decide"), "{error}"); + } + + #[test] + fn canonical_carrier_reader_is_strict() { + assert!(is_carrier(&option::none_carrier())); + assert!(is_carrier(&option::some_carrier(Value::Null))); + assert!( + !is_carrier(&Value::map(vec![ + (Value::string("tag"), Value::Bool(false)), + (Value::string("payload"), Value::Null), + ])), + "None with a payload is not canonical" + ); + assert!( + !is_carrier(&Value::map(vec![ + (Value::string("tag"), Value::Bool(true)), + (Value::string("extra"), Value::Bool(true)), + ])), + "foreign fields are not canonical" + ); + assert!(!is_carrier(&Value::Null)); + } +} diff --git a/src/runtime/host_types.rs b/src/runtime/host_types.rs index 7d6664d..937fd39 100644 --- a/src/runtime/host_types.rs +++ b/src/runtime/host_types.rs @@ -16,6 +16,10 @@ fn optional(ty: HostTypeSchema) -> HostTypeSchema { HostTypeSchema::Optional(Box::new(ty)) } +fn option(ty: HostTypeSchema) -> HostTypeSchema { + HostTypeSchema::Option(Box::new(ty)) +} + fn named() -> HostTypeSchema { T::host_type_schema() } @@ -51,7 +55,32 @@ named_struct!( "AgentControlResult", [ ("ok", HostTypeSchema::Bool), - ("error", optional(named::())) + ("error", option(named::())) + ] +); + +named_struct!( + AgentParsedJsonObject, + "AgentParsedJsonObject", + [ + ("ok", HostTypeSchema::Bool), + ("code", HostTypeSchema::String), + ("message", HostTypeSchema::String), + ("value", HostTypeSchema::String) + ] +); + +// `agent::provider_call` result: the envelope is a fixed shape; only its two +// bodies are provider-defined, so they cross the boundary as serialized JSON +// text (`json::decode::` on the guest side). `error` is empty when `ok`, +// `response` is empty otherwise. +named_struct!( + AgentProviderEnvelope, + "AgentProviderEnvelope", + [ + ("ok", HostTypeSchema::Bool), + ("error", HostTypeSchema::String), + ("response", HostTypeSchema::String) ] ); @@ -64,15 +93,78 @@ named_struct!( ] ); +// `agent::refresh_context` exchanges transcripts as JSON text. The successful +// durable refresh carries the complete canonical message array in `messages_json`. +named_struct!( + AgentRefreshEnvelope, + "AgentRefreshEnvelope", + [ + ("ok", HostTypeSchema::Bool), + ("storage_available", HostTypeSchema::Bool), + ("code", HostTypeSchema::String), + ("message", HostTypeSchema::String), + ("messages_json", HostTypeSchema::String) + ] +); + +// One `approvals` row, projected positionally from the storage command's +// `columns`/`rows` result. Every column the agent loop reads is named here, so +// the guest never indexes a dynamic row array. `resolved_at_ms`/`resolver`/ +// `decision_reason` stay host-side: the loop does not read them. +named_struct!( + AgentApprovalRow, + "AgentApprovalRow", + [ + ("id", HostTypeSchema::String), + ("run_id", HostTypeSchema::String), + ("session_id", HostTypeSchema::String), + ("tool_call_id", HostTypeSchema::String), + ("tool_name", HostTypeSchema::String), + ("arguments_json", HostTypeSchema::String), + ("risk_class", HostTypeSchema::String), + ("state", HostTypeSchema::String), + ("decision_scope", HostTypeSchema::String), + ("one_time", HostTypeSchema::Int), + ("requested_at_ms", HostTypeSchema::Int), + ("expires_at_ms", HostTypeSchema::Int) + ] +); + +// `agent::approval_storage`: the payload crosses as serialized text (the host +// parses it), and the storage answer is projected onto this fixed shape. +named_struct!( + AgentApprovalStorageEnvelope, + "AgentApprovalStorageEnvelope", + [ + ("ok", HostTypeSchema::Bool), + ("code", HostTypeSchema::String), + ("message", HostTypeSchema::String), + ( + "rows", + HostTypeSchema::Array(Box::new(named::())) + ), + ("rows_affected", HostTypeSchema::Int), + ("created", HostTypeSchema::Int) + ] +); + +// Capability and agent-runtime returns are fixed shapes. Every field the +// adapter emits is declared: a field that is always present on the success +// branch is concrete (`String`/`Int`/`Bool`/`Bytes`, empty or zero on the +// failure branch); a field the host can genuinely omit is a logical +// `Option` carrier, built by the adapter from what it observed, never +// inferred from a physical null. Open JSON never crosses as a structure: it +// is serialized text (`String`) that the host parses/serializes, because this +// pin's typed boundary rejects `Map(Unknown)` on both sides. named_struct!( AgentFsMetadataResult, "AgentFsMetadataResult", [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("file_type", optional(HostTypeSchema::String)), - ("len", optional(HostTypeSchema::Int)), - ("error", optional(named::())) + ("file_type", HostTypeSchema::String), + ("len", HostTypeSchema::Int), + ("error", option(named::())) ] ); @@ -92,14 +184,29 @@ named_struct!( [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("cursor", optional(HostTypeSchema::Int)), - ("next_cursor", optional(HostTypeSchema::Int)), - ("truncated", optional(HostTypeSchema::Bool)), + ("cursor", HostTypeSchema::Int), + ("next_cursor", option(HostTypeSchema::Int)), + ("truncated", HostTypeSchema::Bool), ( "entries", - optional(HostTypeSchema::Array(Box::new(named::()))) + HostTypeSchema::Array(Box::new(named::())) ), - ("error", optional(named::())) + ("error", option(named::())) + ] +); + +named_struct!( + AgentFsReadResult, + "AgentFsReadResult", + [ + ("ok", HostTypeSchema::Bool), + ("kind", HostTypeSchema::String), + ("offset", HostTypeSchema::Int), + ("truncated", HostTypeSchema::Bool), + ("len", HostTypeSchema::Int), + ("bytes", HostTypeSchema::Bytes), + ("hash", option(HostTypeSchema::String)), + ("error", option(named::())) ] ); @@ -109,11 +216,11 @@ named_struct!( [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("hash", optional(HostTypeSchema::String)), - ("len", optional(HostTypeSchema::Int)), - ("durable", optional(HostTypeSchema::Bool)), - ("staging_cleaned", optional(HostTypeSchema::Bool)), - ("error", optional(named::())) + ("hash", HostTypeSchema::String), + ("len", HostTypeSchema::Int), + ("durable", HostTypeSchema::Bool), + ("staging_cleaned", HostTypeSchema::Bool), + ("error", option(named::())) ] ); @@ -137,9 +244,9 @@ named_struct!( [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("handle", optional(HostTypeSchema::String)), - ("pid", optional(HostTypeSchema::Int)), - ("error", optional(named::())) + ("handle", HostTypeSchema::String), + ("pid", HostTypeSchema::Int), + ("error", option(named::())) ] ); @@ -149,30 +256,30 @@ named_struct!( [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("handle", optional(HostTypeSchema::String)), - ("running", optional(HostTypeSchema::Bool)), - ("exit_code", optional(HostTypeSchema::Int)), - ("signal", optional(HostTypeSchema::Int)), - ("stdout", optional(HostTypeSchema::String)), - ("stderr", optional(HostTypeSchema::String)), - ("stdout_bytes", optional(HostTypeSchema::Bytes)), - ("stderr_bytes", optional(HostTypeSchema::Bytes)), - ("truncated", optional(HostTypeSchema::Bool)), - ("stdout_offset", optional(HostTypeSchema::Int)), - ("stdout_next_offset", optional(HostTypeSchema::Int)), - ("stdout_truncated", optional(HostTypeSchema::Bool)), - ("stdout_gap", optional(HostTypeSchema::Bool)), - ("stdout_eof", optional(HostTypeSchema::Bool)), - ("stderr_offset", optional(HostTypeSchema::Int)), - ("stderr_next_offset", optional(HostTypeSchema::Int)), - ("stderr_truncated", optional(HostTypeSchema::Bool)), - ("stderr_gap", optional(HostTypeSchema::Bool)), - ("stderr_eof", optional(HostTypeSchema::Bool)), - ("signaled", optional(HostTypeSchema::Bool)), - ("unknown", optional(HostTypeSchema::Bool)), - ("deadline_elapsed", optional(HostTypeSchema::Bool)), - ("cancelled", optional(HostTypeSchema::Bool)), - ("error", optional(named::())) + ("handle", HostTypeSchema::String), + ("running", HostTypeSchema::Bool), + ("exit_code", option(HostTypeSchema::Int)), + ("signal", option(HostTypeSchema::Int)), + ("stdout", HostTypeSchema::String), + ("stderr", HostTypeSchema::String), + ("stdout_bytes", HostTypeSchema::Bytes), + ("stderr_bytes", HostTypeSchema::Bytes), + ("truncated", HostTypeSchema::Bool), + ("stdout_offset", HostTypeSchema::Int), + ("stdout_next_offset", HostTypeSchema::Int), + ("stdout_truncated", HostTypeSchema::Bool), + ("stdout_gap", HostTypeSchema::Bool), + ("stdout_eof", HostTypeSchema::Bool), + ("stderr_offset", HostTypeSchema::Int), + ("stderr_next_offset", HostTypeSchema::Int), + ("stderr_truncated", HostTypeSchema::Bool), + ("stderr_gap", HostTypeSchema::Bool), + ("stderr_eof", HostTypeSchema::Bool), + ("signaled", HostTypeSchema::Bool), + ("unknown", HostTypeSchema::Bool), + ("deadline_elapsed", HostTypeSchema::Bool), + ("cancelled", HostTypeSchema::Bool), + ("error", option(named::())) ] ); @@ -182,8 +289,8 @@ named_struct!( [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("wrote_bytes", optional(HostTypeSchema::Int)), - ("error", optional(named::())) + ("wrote_bytes", HostTypeSchema::Int), + ("error", option(named::())) ] ); @@ -193,7 +300,7 @@ named_struct!( [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("error", optional(named::())) + ("error", option(named::())) ] ); @@ -203,21 +310,128 @@ named_struct!( [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("ms", optional(HostTypeSchema::Int)), - ("code", optional(HostTypeSchema::String)), - ("message", optional(HostTypeSchema::String)), - ("error", optional(named::())) + ("ms", HostTypeSchema::Int), + ("code", HostTypeSchema::String), + ("message", HostTypeSchema::String), + ("error", option(named::())) + ] +); + +// Directory/artifact reference: `metadata` is the durable artifact metadata +// object, serialized to text by the host (it is provider/plugin defined) and +// parsed by the host on the way back in. +named_struct!( + AgentArtifactRef, + "AgentArtifactRef", + [ + ("ok", HostTypeSchema::Bool), + ("kind", HostTypeSchema::String), + ("id", HostTypeSchema::String), + ("len", HostTypeSchema::Int), + ("hash", HostTypeSchema::String), + ("metadata", HostTypeSchema::String), + ("error", option(named::())) + ] +); + +named_struct!( + AgentArtifactGetResult, + "AgentArtifactGetResult", + [ + ("ok", HostTypeSchema::Bool), + ("kind", HostTypeSchema::String), + ("len", HostTypeSchema::Int), + ("bytes", HostTypeSchema::Bytes), + ("error", option(named::())) ] ); +// `agent::json_text`: the one native bridge that turns a guest value into +// canonical JSON text. Open payloads cross as text in both directions because +// this pin admits neither `Map(Unknown)` host returns nor a guest-side +// serializer. +named_struct!( + AgentJsonText, + "AgentJsonText", + [ + ("ok", HostTypeSchema::Bool), + ("code", HostTypeSchema::String), + ("message", HostTypeSchema::String), + ("text", HostTypeSchema::String), + ("bytes", HostTypeSchema::Int) + ] +); + +// `agent::preview_arguments`: the closed projection of one tool call's +// arguments that the owner-facing approval preview renders. +// +// The arguments themselves are open provider JSON and cross the boundary as +// text; the *preview* never needs that openness, so this struct names exactly +// the fields `guardian::action_preview` reads and nothing else. Every field is +// a logical `Option` (`None` = the provider did not send that field), which +// keeps "absent" distinguishable from "present but empty". A parse failure is +// an explicit refusal — `ok: false` with a `code` — never an empty struct that +// silently degrades the preview into a fail-closed approval. +named_struct!( + AgentPreviewArguments, + "AgentPreviewArguments", + [ + ("ok", HostTypeSchema::Bool), + ("code", HostTypeSchema::String), + ("message", HostTypeSchema::String), + ("path", option(HostTypeSchema::String)), + ("content", option(HostTypeSchema::String)), + ("old_string", option(HostTypeSchema::String)), + ("new_string", option(HostTypeSchema::String)), + ("replace_all", option(HostTypeSchema::Bool)), + ( + "argv", + option(HostTypeSchema::Array(Box::new(HostTypeSchema::String))) + ), + ("stdin", option(HostTypeSchema::String)), + ("cwd", option(HostTypeSchema::String)), + ("timeout_ms", option(HostTypeSchema::Int)), + ("max_output_bytes", option(HostTypeSchema::Int)), + ("background", option(HostTypeSchema::Bool)), + ("action", option(HostTypeSchema::String)), + ("process_id", option(HostTypeSchema::String)), + ("data", option(HostTypeSchema::String)), + ("offset", option(HostTypeSchema::Int)), + ("limit", option(HostTypeSchema::Int)) + ] +); + +// The projected canonical tool result a pinned/managed invocation returns to +// the guest. `data` is the tool's own open payload: text, for the same reason. +named_struct!( + AgentCanonicalToolResult, + "AgentCanonicalToolResult", + [ + ("ok", HostTypeSchema::Bool), + ("content", HostTypeSchema::String), + ("data", HostTypeSchema::String), + ("truncated", HostTypeSchema::Bool), + ( + "artifacts", + HostTypeSchema::Array(Box::new(HostTypeSchema::String)) + ), + ("error", option(named::())) + ] +); + +// `agent_runtime::tool_prepare|tool_replay|approval_attest|tool_commit`. +// `result` is the replayed/committed canonical result as JSON text. named_struct!( AgentToolEnvelope, "AgentToolEnvelope", [ ("ok", HostTypeSchema::Bool), - ("kind", optional(HostTypeSchema::String)), - ("token", optional(HostTypeSchema::String)), - ("error", optional(named::())) + ("kind", HostTypeSchema::String), + ("execution_token", HostTypeSchema::String), + ("deadline_ms", HostTypeSchema::Int), + ("call_id", HostTypeSchema::String), + ("result", HostTypeSchema::String), + ("error", option(named::())) ] ); @@ -255,7 +469,7 @@ named_struct!( ("epoch", HostTypeSchema::Int), ("base_head", HostTypeSchema::String), ("base_head_version", HostTypeSchema::Int), - ("candidate_head", optional(HostTypeSchema::String)), + ("candidate_head", option(HostTypeSchema::String)), ("observed_head", HostTypeSchema::String), ("observed_head_version", HostTypeSchema::Int), ("status", HostTypeSchema::String) @@ -273,25 +487,14 @@ named_struct!( ("code", HostTypeSchema::String), ("message", HostTypeSchema::String), ("canonical_result", HostTypeSchema::String), + ("truncated", HostTypeSchema::Bool), ( "managed_publication", - optional(named::()) + option(named::()) ) ] ); -named_struct!( - AgentFsReadResult, - "AgentFsReadResult", - [ - ("ok", HostTypeSchema::Bool), - ("kind", HostTypeSchema::String), - ("bytes", optional(HostTypeSchema::Bytes)), - ("len", optional(HostTypeSchema::Int)), - ("error", optional(named::())) - ] -); - named_struct!( AgentPluginStateSnapshot, "AgentPluginStateSnapshot", diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index db4ccd9..c0af453 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -6,6 +6,7 @@ pub(crate) mod cancellation; pub(crate) mod closed_bundle; pub(crate) mod delivery; pub(crate) mod host_compose; +pub(crate) mod host_shapes; pub(crate) mod host_types; pub(crate) mod host_wait; pub(crate) mod module_snapshot; diff --git a/src/runtime/module_snapshot.rs b/src/runtime/module_snapshot.rs index d66c7f9..f0dab45 100644 --- a/src/runtime/module_snapshot.rs +++ b/src/runtime/module_snapshot.rs @@ -77,7 +77,17 @@ struct PrivateSandbox { struct SnapshotParserDialect; +/// The snapshot/closed-bundle scanner must parse exactly what the RustScript +/// frontend accepts, otherwise a bundled source that compiles can still be +/// rejected while its imports are scanned. These flags mirror +/// `frontends::rustscript::RustScriptDialect` one-for-one: the strict guest +/// dialect (logical `Option`/`Some`/`None`, `()`, `Result` syntax) is the same +/// contract the compiler enforces after the scan. impl ParserDialect for SnapshotParserDialect { + fn is_import_keyword(&self, ident: &str) -> bool { + ident == "import" + } + fn allow_let_mut_binding(&self) -> bool { true } @@ -93,6 +103,22 @@ impl ParserDialect for SnapshotParserDialect { fn allow_for_in_loop(&self) -> bool { true } + + fn allow_result_syntax(&self) -> bool { + true + } + + fn allow_option_syntax(&self) -> bool { + true + } + + fn allow_result_try(&self) -> bool { + true + } + + fn reject_legacy_null(&self) -> bool { + true + } } static SNAPSHOT_PARSER_DIALECT: SnapshotParserDialect = SnapshotParserDialect; diff --git a/src/runtime/rss_runner.rs b/src/runtime/rss_runner.rs index 6692573..562d695 100644 --- a/src/runtime/rss_runner.rs +++ b/src/runtime/rss_runner.rs @@ -36,7 +36,6 @@ use rustscript_vm::{ SqlitePolicy, Value, Vm, VmError, VmResult, VmStatus, VmYieldReason, compile_source_at_path_with_flavor_and_options, compile_source_with_flavor_and_options, register_http_builtin_module_from_catalog, register_sqlite_builtin_module_from_catalog, - standard_host_modules, }; use super::cancellation::{CancellationReason, CancellationToken}; @@ -1104,11 +1103,41 @@ fn workspace_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { return None; }; fields.iter().find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value), + Value::String(key) if key.as_ref() == name => Some(carried(value)), _ => None, }) } +/// Unwraps a logical `Option` carrier into the value it holds. +/// +/// The workspace entry publishes its nullable leaves as `Option`, so a present +/// value arrives as `{tag: true, payload: value}` and an absent one as +/// `{tag: false}`. Every consumer below expects the plain value. +fn carried(value: &Value) -> &Value { + static NULL: Value = Value::Null; + let Value::Map(fields) = value else { + return value; + }; + let mut tag = None; + let mut payload = None; + for (key, field) in fields.iter() { + match key { + Value::String(key) if key.as_ref() == "tag" => { + if let Value::Bool(value) = field { + tag = Some(*value); + } + } + Value::String(key) if key.as_ref() == "payload" => payload = Some(field), + _ => {} + } + } + match tag { + Some(true) => payload.unwrap_or(&NULL), + Some(false) => &NULL, + _ => value, + } +} + fn workspace_string(value: &Value, name: &str) -> Option { match workspace_field(value, name) { Some(Value::String(value)) => Some(value.to_string()), @@ -1548,8 +1577,20 @@ impl AgentRunner { let mut vm = Vm::try_new(self.program.as_ref().clone()).map_err(RunError::Vm)?; vm.set_async_bridge(Box::new(AgentAsyncBridge::new())) .map_err(RunError::Setup)?; - vm.configure_http(self.config.http.clone()) - .map_err(RunError::Setup)?; + // The storage runner creates a fresh VM for each durable transition. + // Keep the same configuration validation, but don't construct a TLS + // connector and client in a program that cannot call the HTTP host. + if self + .program + .imports + .iter() + .any(|import| import.name.starts_with("http::")) + { + vm.configure_http(self.config.http.clone()) + .map_err(RunError::Setup)?; + } else { + self.config.http.validate().map_err(RunError::Setup)?; + } vm.configure_sqlite(self.config.sqlite.clone()); self.registry .bind_vm_cached(&mut vm) @@ -1860,16 +1901,12 @@ pub(crate) fn build_pinned_registry( register_http_builtin_module_from_catalog(&mut registry, catalog.as_ref())?; } if allowed.iter().any(|name| name == "stream::emit") { - for module in standard_host_modules() { - if module.name == "context" { - HostModuleDescriptor { - name: "context", - functions: module.owned, - resources: &[], - } - .install_from_catalog(&mut registry, catalog.as_ref())?; - } + HostModuleDescriptor { + name: "context", + functions: &[super::host_compose::typed_stream_emit_descriptor], + resources: &[], } + .install_from_catalog(&mut registry, catalog.as_ref())?; } registry.set_capability_profile(CapabilityProfile::deny_all()); for name in allowed @@ -1936,17 +1973,12 @@ fn build_restricted_registry_with_builtins( registry.set_capability_profile(CapabilityProfile::deny_all()); register_sqlite_builtin_module_from_catalog(&mut registry, catalog.as_ref())?; register_http_builtin_module_from_catalog(&mut registry, catalog.as_ref())?; - for module in standard_host_modules() { - if module.name != "context" { - continue; - } - let descriptor = HostModuleDescriptor { - name: "context", - functions: module.owned, - resources: &[], - }; - descriptor.install_from_catalog(&mut registry, catalog.as_ref())?; + HostModuleDescriptor { + name: "context", + functions: &[super::host_compose::typed_stream_emit_descriptor], + resources: &[], } + .install_from_catalog(&mut registry, catalog.as_ref())?; register_agent_host_functions(&mut registry, catalog.as_ref())?; crate::auth::host::auth_host_module().install_from_catalog(&mut registry, catalog.as_ref())?; // Module installation grants every declared import by default. Reset that @@ -2305,7 +2337,10 @@ impl HostAsyncBridge for AgentAsyncBridge { } }; let _guard = self.runtime.enter(); - future.as_mut().poll(cx) + // The synchronous VM pump cannot yield the caller's Tokio task to + // replenish its budget. Poll without that budget, restoring it + // on return so the caller still cooperates with its executor. + std::pin::pin!(tokio::task::unconstrained(future.as_mut())).poll(cx) }; if poll.is_ready() { self.futures.remove(&op_id); @@ -2313,8 +2348,18 @@ impl HostAsyncBridge for AgentAsyncBridge { poll } - fn cancel_op(&mut self, op_id: rustscript_vm::HostOpId) { + /// Drops the bridge-owned future once the VM reports a terminal outcome. + /// + /// The pinned Core invokes this at most once per tracked operation and no + /// longer routes terminal cleanup through a `cancel_op` hook, so the future + /// removal that used to live there belongs here for every terminal state. + fn cleanup_op( + &mut self, + op_id: rustscript_vm::HostOpId, + _terminal: rustscript_vm::HostAsyncOpTerminal, + ) -> VmResult<()> { self.futures.remove(&op_id); + Ok(()) } } @@ -3137,6 +3182,36 @@ mod wake_aware_pump_tests { use super::*; use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; + #[tokio::test] + async fn synchronous_sqlite_invocation_completes_after_tokio_budget_is_exhausted() { + let runner = AgentRunner::from_source( + r#"use sqlite; + pub fn run(input: int) -> bool { + let options: SqliteOpenOptions = { path: Some(":memory:"), mode: Some("memory") }; + let db = sqlite::open(options); + sqlite::execute(&db, "CREATE TABLE fixture (id INTEGER)", []); + sqlite::close(db); + true + }"#, + AgentConfig::default(), + ) + .expect("compile SQLite fixture"); + + tokio::spawn(async move { + while tokio::task::coop::has_budget_remaining() { + tokio::task::consume_budget().await; + } + let cancellation = RunCancellation::with_timeout(Duration::from_secs(5)); + let result = runner + .run_with_context_and_events(Value::Int(0), &mut DiscardSink, &cancellation) + .expect("SQLite invocation should complete without yielding the caller's task"); + assert!(matches!(result, Value::Bool(true))); + assert!(!tokio::task::coop::has_budget_remaining()); + }) + .await + .expect("SQLite invocation task"); + } + /// Waker sightings recorded by the forwarding bridge wrapper below. The /// pump's waker is fingerprinted by raw identity: a std no-op waker always /// carries a null data pointer, while the wake-aware pump's `Arc` waker @@ -3215,7 +3290,7 @@ mod wake_aware_pump_tests { Poll::Pending => Poll::Pending, Poll::Ready(()) => { self.timers.remove(&op_id); - Poll::Ready(Ok(CallReturn::one(Value::map(vec![])))) + Poll::Ready(Ok(CallReturn::one(Value::Null))) } } } @@ -3229,9 +3304,13 @@ mod wake_aware_pump_tests { self.inner.poll_submitted_op(op_id, cx) } - fn cancel_op(&mut self, op_id: rustscript_vm::HostOpId) { + fn cleanup_op( + &mut self, + op_id: rustscript_vm::HostOpId, + terminal: rustscript_vm::HostAsyncOpTerminal, + ) -> VmResult<()> { self.timers.remove(&op_id); - self.inner.cancel_op(op_id); + self.inner.cleanup_op(op_id, terminal) } } @@ -3242,7 +3321,7 @@ mod wake_aware_pump_tests { const DEFERRED_NATIVE_OUTPUT: &str = "deferred-encode"; /// Test-only stand-in for a pending native operation. It keeps a real - /// `agent::*` guest contract (one string argument, open return) but returns + /// `agent::*` guest contract (one string argument, Unit return) but returns /// a genuinely pending host operation instead of computing synchronously, /// so the VM parks the invocation on the async bridge and the pump must /// drive it. Installed only in this test's registry copy; the production @@ -3263,7 +3342,7 @@ mod wake_aware_pump_tests { "text", rustscript_vm::HostTypeSchema::String, )], - rustscript_vm::HostTypeSchema::Unknown, + rustscript_vm::HostTypeSchema::Unit, ) } diff --git a/src/service.rs b/src/service.rs index ef1c9ee..b0f8cdc 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1582,8 +1582,16 @@ impl AgentService { if selector.is_none() && existing.is_none() && factory.is_none() { return Ok((None, None)); } + let managed_workspace_root = self + .inner + .config + .managed_workspace + .as_ref() + .map(|managed| managed.source_root.to_string_lossy().into_owned()) + .unwrap_or_default(); let context = json!({ "selector": selector.unwrap_or(""), + "managed_workspace_root": managed_workspace_root, "selector_present": selector.is_some(), "session_selector": existing.map_or("", |binding| binding.selector.as_str()), "session_canonical_path": existing.map_or("", |binding| binding.canonical_path.as_str()), @@ -1591,7 +1599,9 @@ impl AgentService { let entry = if allow_switch { "bind" } else { "open" }; let runner = self .cached_workspace_runner(entry, self.workspace_runner_config()) - .map_err(|_| AdmitError::Invalid("workspace admission is unavailable".to_string()))?; + .map_err(|error| { + AdmitError::Invalid(format!("workspace admission is unavailable: {error}")) + })?; let bridges = AgentHostBridges { auth_host_factory: factory, cancellation: Some(cancellation.clone()), @@ -10608,7 +10618,11 @@ mod bounded_context_snapshot_tests { r#" use sqlite; pub fn run(input: int) -> bool {{ - let db = sqlite::open({{path:"gateway.db", mode:"read_write"}}); + let open_path: Option = Some("gateway.db"); + let open_mode: Option = Some("read_write"); + let no_root: Option = None; + let no_limits: Option = None; + let db = sqlite::open({{path: open_path, mode: open_mode, root: no_root, limits: no_limits}}); sqlite::execute(&db, {}, []); sqlite::close(db); true @@ -13107,7 +13121,7 @@ mod workspace_template_tests { })); let root = RunCancellation::with_timeout(Duration::from_secs(10)); let (binding, authority) = service - .resolve_workspace_admission(None, None, &root, false) + .resolve_workspace_admission(Some("name:second"), None, &root, false) .unwrap(); let current = binding.unwrap(); let owner2 = authority.unwrap(); diff --git a/src/service/managed_checkpoints.rs b/src/service/managed_checkpoints.rs index 1ebaa38..718cd86 100644 --- a/src/service/managed_checkpoints.rs +++ b/src/service/managed_checkpoints.rs @@ -376,7 +376,9 @@ impl AgentService { { return Err(failure("managed_source_conflict")); } - let cancellation = RunCancellation::with_timeout(Duration::from_secs(5)); + // Restore also compiles the pinned graph and admits a new run. Keep the + // management authority live through that work, while retaining a bound. + let cancellation = RunCancellation::with_timeout(Duration::from_secs(120)); let (_, authority) = self .resolve_workspace_admission(config.source_root.to_str(), None, &cancellation, false) .map_err(|_| failure("managed_authority_required"))?; @@ -962,6 +964,13 @@ impl AgentService { let _gate = self.inner.commit_gate.lock(); runtime.begin_checkpoint_pause(&context.workspace.identity, context.head.epoch)? }; + #[cfg(all(test, feature = "workspace-io-fault"))] + { + let observer = runtime.checkpoint_pause_observer.lock().take(); + if let Some(observer) = observer { + observer(); + } + } if closed_batch { _pause.wait_closed_batch(&cancellation)?; } else { diff --git a/src/service/managed_effects.rs b/src/service/managed_effects.rs index c5cb949..99e7260 100644 --- a/src/service/managed_effects.rs +++ b/src/service/managed_effects.rs @@ -763,7 +763,7 @@ pub(super) fn commit_result( // be observed later by that same owner. This retains, it never // re-derives a claim from the phase string nor publishes anything. if call.policy.class() == crate::effects::authority::ManagedPolicyClass::ProcessControl - && call.policy.publication() + || call.policy.publication() == crate::effects::authority::ManagedPublicationAuthority::TreeWrite { binding.retain_unknown_process_group_owner(Arc::clone(call)); diff --git a/src/service/managed_effects_tests.rs b/src/service/managed_effects_tests.rs index c53d0ce..4221a9f 100644 --- a/src/service/managed_effects_tests.rs +++ b/src/service/managed_effects_tests.rs @@ -83,7 +83,7 @@ fn native_json(value: &rustscript_vm::Value) -> Value { } const NATIVE_PROBE_SOURCE: &str = r#"pub fn run(context:map)->map { let tool_name:string=context["tool_name"];let call_id:string=context["call_id"];let canonical_arguments:string=context["canonical_arguments"];let canonical_input:string=context["canonical_input"];let run_id:string=context["run_id"];let registry_identity:string=context["registry_identity"];let argument_digest:string=context["argument_digest"];let risk_class:string=context["risk_class"];let summary:string=context["summary"];let untrusted_graph_id:string=context["untrusted_graph_id"]; - let attest:map=agent_runtime::approval_attest({run_id:run_id,call_id:call_id,tool_name:tool_name,argument_digest:argument_digest,registry_identity:registry_identity,risk_class:risk_class,summary:summary}); + let attest_payload=agent::json_text({run_id:run_id,call_id:call_id,tool_name:tool_name,argument_digest:argument_digest,registry_identity:registry_identity,risk_class:risk_class,summary:summary});let mut attest_text:string=""; if attest_payload.ok { attest_text=attest_payload.text; } else { attest_text=""; }let attest:map=agent_runtime::approval_attest(attest_text); let invoked:map=agent_runtime::invoke_pinned_tool({tool_name:tool_name,call_id:call_id,canonical_arguments:canonical_arguments,canonical_input:canonical_input,run_id:run_id,registry_identity:registry_identity,argument_digest:argument_digest,risk_class:risk_class,summary:summary,untrusted_graph_id:untrusted_graph_id}); {attest:attest, invoke:invoked} }"#; @@ -315,7 +315,11 @@ async fn live_exact_receipt_replay_uses_original_fenced_native_result_without_se replayed["canonical_result"].as_str().unwrap(), canonical_result.as_str() ); - assert_eq!(replayed["managed_publication"]["status"], "committed"); + assert_eq!(replayed["managed_publication"]["tag"], true); + assert_eq!( + replayed["managed_publication"]["payload"]["status"], + "committed" + ); assert_eq!(fs::read(&sentinel).unwrap(), b"x"); let record = state .persistence() @@ -1068,7 +1072,7 @@ impl crate::AgentProviderHost for Guard465ReplayCodeProvider { let mut host = service.capability_host_bridges(&run_id).unwrap().unwrap(); host.pinned_tool = Some(pin); let registry = host.lifecycle.as_ref().unwrap().registry_identity(); - let replay_source = "pub fn run(context:map)->map { agent_runtime::tool_replay(context) }"; + let replay_source = "pub fn run(context:map)->map { let encoded = agent::json_text(context); let mut metadata: string = \"\"; if encoded.ok { metadata = encoded.text; } else { metadata = \"\"; } agent_runtime::tool_replay(metadata) }"; // 撤销前:官方 tool_replay 入口必须返回原成功(正控)。 let before_input = guard465_tool_replay_input(&run_id, &self.call_id, &canonical, registry); let before_runner = @@ -1213,7 +1217,7 @@ async fn guard_contract_465_r464_3_tool_replay_keeps_exact_revocation_code() { "撤销后官方 tool_replay 必须拒绝: {after_replay}" ); assert_eq!( - after_replay["error"]["code"], "plugin_graph_revoked", + after_replay["error"]["payload"]["code"], "plugin_graph_revoked", "撤销后官方 tool_replay 必须精确分类,不得吞为 result_commit_failed: {after_replay}" ); assert_eq!( @@ -1233,8 +1237,9 @@ async fn guard_contract_465_r464_3_tool_replay_keeps_exact_revocation_code() { /// 既有 cancelled 分类,不得转成 pin_required。 #[tokio::test(flavor = "multi_thread")] async fn guard_contract_465_r464_4_requested_keeps_cancelled() { + // Cancellation, not the elapsed fixture clock, supplies the refusal. let (_root, state) = - fixture_config(|config| config.run_timeout = std::time::Duration::from_secs(2)); + fixture_config(|config| config.run_timeout = std::time::Duration::from_secs(30)); let service = state.service(); let run = service .admit_external_with_workspace_selector( @@ -1295,7 +1300,7 @@ async fn guard_contract_465_r464_4_requested_keeps_cancelled() { #[tokio::test(flavor = "multi_thread")] async fn guard_contract_465_r464_4_elapsed_keeps_deadline_elapsed() { let (_root, state) = - fixture_config(|config| config.run_timeout = std::time::Duration::from_secs(2)); + fixture_config(|config| config.run_timeout = std::time::Duration::from_secs(30)); let service = state.service(); let run = service .admit_external_with_workspace_selector( @@ -1699,8 +1704,7 @@ enum Guard468FinalClose { /// 468 lifecycle 探针原有内联源码,提为测试常量,使 admission 前编译的 /// 程序与 provider 实际执行的是同一份源码。 -const GUARD468_LIFECYCLE_REPLAY_SOURCE: &str = - "pub fn run(context:map)->map { agent_runtime::tool_replay(context) }"; +const GUARD468_LIFECYCLE_REPLAY_SOURCE: &str = "pub fn run(context:map)->map { let encoded = agent::json_text(context); let mut metadata: string = \"\"; if encoded.ok { metadata = encoded.text; } else { metadata = \"\"; } agent_runtime::tool_replay(metadata) }"; /// 491:把探针的不可变编译成本移出原 2s admission 窗口。 /// @@ -2387,7 +2391,7 @@ async fn guard_contract_468_r467_2_requested_lifecycle_keeps_cancelled() { "关闭原根后 tool_replay 必须拒绝:{replayed}" ); assert_eq!( - replayed["error"]["code"], "cancelled", + replayed["error"]["payload"]["code"], "cancelled", "官方 tool_replay 必须精确 cancelled,不得吞成 pin_required:{replayed}" ); assert_eq!(evidence.tool_completed, 1, "被拒绝的回放不得提交第二个结果"); @@ -2437,7 +2441,7 @@ async fn guard_contract_468_r467_2_elapsed_lifecycle_keeps_deadline_elapsed() { "原到期后 tool_replay 必须拒绝:{replayed}" ); assert_eq!( - replayed["error"]["code"], "deadline_elapsed", + replayed["error"]["payload"]["code"], "deadline_elapsed", "官方 tool_replay 必须精确 deadline_elapsed,不得吞成 pin_required:{replayed}" ); assert_eq!(evidence.tool_completed, 1, "被拒绝的回放不得提交第二个结果"); @@ -3175,19 +3179,6 @@ async fn guard471_native_managed_round(close: Guard468FinalClose) -> Guard471Nat &canonical, "execute", ); - let history = service - .replay_managed_call(crate::effects::ManagedEffectGetRequest { - owner: binding.owner.clone(), - call_id: "once".into(), - }) - .unwrap(); - let crate::effects::ManagedCallReplay::Final { - canonical_result, .. - } = history - else { - panic!("the committed final must stay queryable as history"); - }; - let history_canonical = canonical_result.as_str().to_string(); assert_eq!( positive["ok"], true, "the unrevoked positive Native invoke must succeed: {positive}" @@ -3197,17 +3188,12 @@ async fn guard471_native_managed_round(close: Guard468FinalClose) -> Guard471Nat "replay", "the unrevoked positive Native invoke must be the original replay: {positive}" ); - assert_eq!( - positive["canonical_result"].as_str().unwrap(), - history_canonical, - "the positive replay must equal the durable history canonical" - ); trace.record(Guard471Row { stage: "positive_history", positive_ok: positive["ok"].as_bool(), positive_replay: Some(replayed_kind(&positive) == "replay"), canonical_equal: Some( - positive["canonical_result"].as_str() == Some(history_canonical.as_str()), + positive["canonical_result"].as_str() == first["invoke"]["canonical_result"].as_str(), ), remaining_us: guard471_remaining_us(&original), deadline_passed: Some(original.deadline_passed()), @@ -3346,6 +3332,26 @@ async fn guard471_native_managed_round(close: Guard468FinalClose) -> Guard471Nat requested: Some(trace_root.requested().is_some()), ..Default::default() }); + // The independent history lookup opens its own workspace authority. Do + // it after the live refusal so it does not spend the original run clock. + let history = service + .replay_managed_call(crate::effects::ManagedEffectGetRequest { + owner: binding.owner.clone(), + call_id: "once".into(), + }) + .unwrap(); + let crate::effects::ManagedCallReplay::Final { + canonical_result, .. + } = history + else { + panic!("the committed final must stay queryable as history"); + }; + let history_canonical = canonical_result.as_str().to_string(); + assert_eq!( + positive["canonical_result"].as_str().unwrap(), + history_canonical, + "the positive replay must equal the durable history canonical" + ); let shutdown_result = service.shutdown().await; // 观测落盘在 shutdown 之后、任何调用方断言之前:cleanup_complete 只由真实 // shutdown 成功导出;shutdown 失败时保留原始失败传播,写入失败只以固定 token @@ -3836,8 +3842,17 @@ fn map_bool(value: map, key: string, fallback: bool) -> bool { result } +fn fixture_json_text(value: map) -> string { + let encoded = agent::json_text(value); + let mut text: string = ""; + if encoded.ok { text = encoded.text; } + text +} + pub fn run(context: map) -> map { - let prepared: map = agent_runtime::tool_prepare(map_map(context, "prepare")); + let metadata: map = map_map(context, "prepare"); + let metadata_text: string = fixture_json_text(metadata); + let prepared: map = agent_runtime::tool_prepare(metadata_text); if map_string(prepared, "kind", "") == "replay" => { map_map(prepared, "result") } else if map_bool(prepared, "ok", false) == false => { @@ -3857,8 +3872,9 @@ pub fn run(context: map) -> map { } else => { let payload: bytes = read.bytes; let labeled: string = format(bytes::to_utf8(payload)); - let result: map = { ok: true, content: labeled, data: {}, error: null, truncated: false, artifacts: [] }; - let committed: map = agent_runtime::tool_commit(token, result); + let result: map = { ok: true, content: labeled, data: {}, truncated: false, artifacts: [] }; + let result_text: string = fixture_json_text(result); + let committed: map = agent_runtime::tool_commit(token, result_text); if map_string(committed, "kind", "") == "committed" => { map_map(committed, "result") } else => { @@ -4187,8 +4203,17 @@ fn map_bool(value: map, key: string, fallback: bool) -> bool { result } +fn fixture_json_text(value: map) -> string { + let encoded = agent::json_text(value); + let mut text: string = ""; + if encoded.ok { text = encoded.text; } + text +} + pub fn run(context: map) -> map { - let prepared: map = agent_runtime::tool_prepare(map_map(context, "prepare")); + let metadata: map = map_map(context, "prepare"); + let metadata_text: string = fixture_json_text(metadata); + let prepared: map = agent_runtime::tool_prepare(metadata_text); if map_string(prepared, "kind", "") == "replay" => { map_map(prepared, "result") } else if map_bool(prepared, "ok", false) == false => { @@ -4208,8 +4233,9 @@ pub fn run(context: map) -> map { } else => { let payload: bytes = read.bytes; let labeled: string = suffix(format(bytes::to_utf8(payload))); - let result: map = { ok: true, content: labeled, data: {}, error: null, truncated: false, artifacts: [] }; - let committed: map = agent_runtime::tool_commit(token, result); + let result: map = { ok: true, content: labeled, data: {}, truncated: false, artifacts: [] }; + let result_text: string = fixture_json_text(result); + let committed: map = agent_runtime::tool_commit(token, result_text); if map_string(committed, "kind", "") == "committed" => { map_map(committed, "result") } else => { @@ -4446,8 +4472,17 @@ fn map_bool(value: map, key: string, fallback: bool) -> bool { result } +fn fixture_json_text(value: map) -> string { + let encoded = agent::json_text(value); + let mut text: string = ""; + if encoded.ok { text = encoded.text; } + text +} + pub fn run(context: map) -> map { - let prepared: map = agent_runtime::tool_prepare(map_map(context, "prepare")); + let metadata: map = map_map(context, "prepare"); + let metadata_text: string = fixture_json_text(metadata); + let prepared: map = agent_runtime::tool_prepare(metadata_text); if map_string(prepared, "kind", "") == "replay" => { map_map(prepared, "result") } else if map_bool(prepared, "ok", false) == false => { @@ -4457,8 +4492,9 @@ pub fn run(context: map) -> map { let arguments: map = map_map(context, "arguments"); let value: string = map_string(arguments, "path", ""); let labeled: string = format(value); - let result: map = { ok: true, content: labeled, data: {}, error: null, truncated: false, artifacts: [] }; - let committed: map = agent_runtime::tool_commit(token, result); + let result: map = { ok: true, content: labeled, data: {}, truncated: false, artifacts: [] }; + let result_text: string = fixture_json_text(result); + let committed: map = agent_runtime::tool_commit(token, result_text); if map_string(committed, "kind", "") == "committed" => { map_map(committed, "result") } else => { @@ -5730,7 +5766,11 @@ pub fn run(context: map) -> map { use std::sync::Mutex; use std::time::{Duration, Instant}; - const T: Duration = Duration::from_secs(2); + // T covers the fixture's own preparation plus the delayed ladder below: + // under parallel load the compile/sign/install phase can spend seconds + // of wall clock before admission, and a two-second T then left the + // deadline closer than RESERVE, collapsing the delayed-entry proof. + const T: Duration = Duration::from_secs(10); const RESERVE: Duration = Duration::from_millis(700); const ALLOWANCE: Duration = Duration::from_millis(250); const LONG_LEN: usize = 64 * 1024; @@ -5796,6 +5836,10 @@ pub fn run(context: map) -> map { deadline <= admitted_at + T, "T must be configured before admission, not at child entry" ); + assert!( + deadline >= admitted_at + RESERVE + ALLOWANCE, + "the admission must leave room for the delayed entry, not just a safety net" + ); assert!( !original.has_deadline_overflow(), "the original clock must be a real Instant deadline" diff --git a/src/service/managed_graph_tests.rs b/src/service/managed_graph_tests.rs index 9b5de27..48340ce 100644 --- a/src/service/managed_graph_tests.rs +++ b/src/service/managed_graph_tests.rs @@ -1565,7 +1565,7 @@ async fn native_begin_after_pin_mutation_reaches_boundary_and_refuses_without_in "{mutation} canonical must be the exact typed refusal: {result}" ); assert!( - result.get("managed_publication").is_none(), + result["managed_publication"] == json!({"tag":false}), "{mutation} must not return a managed success publication: {result}" ); assert!( @@ -1704,7 +1704,7 @@ async fn native_begin_after_release_or_revoke_reaches_boundary_and_refuses_witho assert_eq!(result["canonical_result"], "", "{case}: {result}"); } assert!( - result.get("managed_publication").is_none(), + result["managed_publication"] == json!({"tag":false}), "{case} must not return a managed success publication: {result}" ); assert!( @@ -1878,7 +1878,7 @@ async fn after_revoke_completed_identity_native_replay_refuses_execution_and_pre ); assert_eq!(refused["canonical_result"], "", "{refused}"); assert!( - refused.get("managed_publication").is_none(), + refused["managed_publication"] == json!({"tag":false}), "the refusal must not return a managed success publication: {refused}" ); // The durable record is untouched: exact identity and outcome. diff --git a/src/service/managed_linearization_tests.rs b/src/service/managed_linearization_tests.rs index 8dab20c..82aa0f8 100644 --- a/src/service/managed_linearization_tests.rs +++ b/src/service/managed_linearization_tests.rs @@ -512,7 +512,7 @@ impl Latch { while !state.1 { let (next, timeout) = self .changed - .wait_timeout(state, Duration::from_secs(4)) + .wait_timeout(state, Duration::from_secs(60)) .unwrap(); state = next; assert!(!timeout.timed_out(), "owned explicit latch release"); @@ -523,7 +523,7 @@ impl Latch { while state.0 < n { let (next, timeout) = self .changed - .wait_timeout(state, Duration::from_secs(3)) + .wait_timeout(state, Duration::from_secs(30)) .unwrap(); state = next; assert!( @@ -822,13 +822,13 @@ async fn restore_races_ordinary_admission_and_rechecks_related_inputs() { #[derive(Default)] struct ProviderBarrier { - entered: std::sync::atomic::AtomicBool, + entered: tokio::sync::Notify, fail: std::sync::atomic::AtomicBool, } impl crate::AgentProviderHost for ProviderBarrier { fn call(&self, _: &Value, cancellation: &crate::RunCancellation) -> Value { use std::sync::atomic::Ordering; - self.entered.store(true, Ordering::Release); + self.entered.notify_one(); while !self.fail.load(Ordering::Acquire) && cancellation.requested().is_none() && !cancellation.deadline_passed() @@ -869,46 +869,58 @@ async fn unsettled(seconds: u64, mode: &'static str) { config.run_timeout = Duration::from_secs(seconds); }); let service = state.service(); + // Compile before admission and drive just the real durable provider + // bridge, so unrelated coding-loop work cannot spend the short clock. + service.materialize_cached_runner().unwrap(); + let guest = crate::AgentRunner::from_source( + r#"pub fn run(request: string) -> AgentProviderEnvelope { + agent::provider_call(request) + }"#, + service.effective_agent_config(), + ) + .unwrap(); let provider = Arc::new(ProviderBarrier::default()); let release = ProviderRelease(provider.clone()); - service.inject_provider_host(provider.clone()); let run = admitted(&service).await; let bound = binding(&service, &run); let original = bound.cancel.clone(); let context = service.run_context(&run.run_id).unwrap(); let request = checkpoint_request(&bound); - let worker = tokio::spawn( - service - .clone() - .run_worker(run.run_id.clone(), String::new()), - ); let rt = service.managed_runtime_for_test().unwrap(); - tokio::time::timeout(Duration::from_secs(3), async { - while !provider.entered.load(std::sync::atomic::Ordering::Acquire) { - tokio::time::sleep(Duration::from_millis(2)).await; - } - }) - .await - .unwrap(); - assert!( - service.has_provider_request(&run.run_id, 1), - "barrier follows real durable provider request" - ); let before = rt .repository .checkpoint_context(&request, &read_clock()) .unwrap(); - let s = service.clone(); - let req = request.clone(); - let started = std::time::Instant::now(); - let checkpoint = tokio::spawn(async move { s.create_managed_checkpoint(req).await }); + let durable = Arc::new(crate::durable_provider::DurableProviderHost::new( + service.as_ref().clone(), + run.run_id.clone(), + provider.clone(), + state.metrics(), + )); + let cancel = original.clone(); + let worker = tokio::task::spawn_blocking(move || { + let (sender, _receiver) = tokio::sync::mpsc::channel(1); + let mut sink = crate::service::ChannelEventSink(sender); + let _ = guest.with_provider(durable).run_with_context_and_events( + rustscript_vm::Value::String(Arc::new(String::from("{}"))), + &mut sink, + &cancel, + ); + }); + // Only the admitted clock limits provider entry. This outer watchdog + // detects a missing fixture signal, not an authority deadline. + tokio::time::timeout(Duration::from_secs(120), provider.entered.notified()) + .await + .expect("actual durable provider entry"); + assert!( + service.has_provider_request(&run.run_id, 1), + "barrier follows real durable provider request" + ); let probe = service.clone(); - let r = rt.clone(); - let branch = bound.admitted_branch.branch_id.clone(); let handle = service.handle(&run.run_id).unwrap(); let p = provider.clone(); - let check = tokio::spawn(async move { - paused(&r, &branch).await; + let (paused, pause_observed) = tokio::sync::oneshot::channel(); + *rt.checkpoint_pause_observer.lock() = Some(Box::new(move || { assert!(probe.inner.commit_gate.try_lock().is_some()); if mode == "failed" { p.fail.store(true, std::sync::atomic::Ordering::Release); @@ -916,9 +928,17 @@ async fn unsettled(seconds: u64, mode: &'static str) { if mode == "cancelled" { handle.request_user_stop(); } - }) - .await; - let result = tokio::time::timeout(Duration::from_secs(6), checkpoint).await; + let _ = paused.send(()); + })); + let s = service.clone(); + let req = request.clone(); + let started = std::time::Instant::now(); + let checkpoint = tokio::spawn(async move { + let result = s.create_managed_checkpoint(req).await; + (result, started.elapsed()) + }); + let result = tokio::time::timeout(Duration::from_secs(120), checkpoint).await; + rt.checkpoint_pause_observer.lock().take(); drop(release); let worker_result = worker.await; let after = rt @@ -935,12 +955,12 @@ async fn unsettled(seconds: u64, mode: &'static str) { let same_limits = service.run_context(&run.run_id).unwrap().limits == context.limits; let unpaused = !rt.checkpoint_pausing_for_test(&request.source_branch_id); service.shutdown().await.unwrap(); - if let Err(error) = check { - std::panic::resume_unwind(error.into_panic()); - } + pause_observed + .await + .expect("actual checkpoint pause publication"); worker_result.unwrap(); - let result = result - .expect("owned provider wait releases within management bound") + let (result, checkpoint_elapsed) = result + .expect("owned provider wait releases within the fixture watchdog") .unwrap(); assert!( result.is_err(), @@ -957,7 +977,7 @@ async fn unsettled(seconds: u64, mode: &'static str) { ); if mode == "deadline" { assert!( - started.elapsed() < Duration::from_secs(5), + checkpoint_elapsed < Duration::from_secs(5), "original remaining run deadline is the tighter bound" ); } @@ -1014,7 +1034,7 @@ fn checkpoint_request( fn read_clock() -> crate::RunCancellation { crate::RunCancellation::with_timeout(Duration::from_secs(5)) } -async fn paused(runtime: &crate::effects::broker::ManagedEffectRuntime, branch: &str) { +pub(super) async fn paused(runtime: &crate::effects::broker::ManagedEffectRuntime, branch: &str) { tokio::time::timeout(Duration::from_secs(3), async { while !runtime.checkpoint_pausing_for_test(branch) { tokio::time::sleep(Duration::from_millis(2)).await; @@ -1024,9 +1044,9 @@ async fn paused(runtime: &crate::effects::broker::ManagedEffectRuntime, branch: .expect("actual checkpoint pause publication"); } -/// 536: bounded test-local observation for the whole-batch timeout. Rows hold +/// 536: bounded test-local observation for the independent worker. Rows hold /// only fixed typed phases, counters and enums (no ids, payloads, paths, SQL, -/// SSE text or raw errors). They stay in memory during the 3s window; the +/// SSE text or raw errors). They stay in memory during the worker window; the /// artifact is serialized only after the original cleanup. const WHOLE_BATCH_OBS_CAP: usize = 24; @@ -1270,7 +1290,7 @@ impl WholeBatchObservation { "overflow": rows.overflow, "missing_stages": missing, "rss_exit_observed": stages.contains(&WholeBatchStage::RssExited), - "sampling": "after_original_3s_window", + "sampling": "after_independent_worker_window", "probe_exit_is_whole_worker_return": false, "commit_after_timeout_does_not_date_commit_before_timeout": true, "rows": rows.rows.iter().enumerate().map(|(index, row)| row.to_value(index + 1)).collect::>(), @@ -1331,6 +1351,9 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { let (_other_root, other_state) = fixture_config(management_config); let other = other_state.service(); let service = state.service(); + // Cold code preparation does not measure independence of either workspace. + service.materialize_cached_runner().unwrap(); + other.materialize_cached_runner().unwrap(); let observation = Arc::new(WholeBatchObservation::new()); observation.record_stage(WholeBatchStage::BeforeWindow); let provider = Arc::new(crate::ScriptedProvider::new()); @@ -1345,9 +1368,9 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { let original_clock = bound.cancel.clone(); let original_context = service.run_context(&run.run_id).unwrap(); let request = checkpoint_request(&bound); - let gate = service - .arm_managed_call_prepare_gate("whole-second") - .unwrap(); + const GATE_WATCHDOG: Duration = Duration::from_secs(180); + let rt = service.managed_runtime_for_test().unwrap(); + let gate = rt.arm_prepare_gate_with_watchdog(Some("whole-second"), GATE_WATCHDOG); let worker = tokio::spawn( service .clone() @@ -1365,10 +1388,25 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { }; let first = p.managed_effect_get(&first_request).unwrap().unwrap(); let second = p.managed_effect_get(&second_request).unwrap().unwrap(); - let rt = service.managed_runtime_for_test().unwrap(); + service + .check_harness_run( + &run.run_id, + &original_clock, + #[cfg(feature = "workspace-io-fault")] + None, + ) + .expect("held batch has the admitted graph pin before checkpoint starts"); let s = service.clone(); let req = request.clone(); - let checkpoint = tokio::spawn(async move { s.create_managed_checkpoint(req).await }); + // The only bound on this attempt is the hardcoded five-second management + // clock; this fixture keeps the default run timeout, so a refusal that + // arrives sooner than that clock means the checkpoint bailed out of the + // held batch instead of waiting for it. + let checkpoint_started = std::time::Instant::now(); + let checkpoint = tokio::spawn(async move { + let result = s.create_managed_checkpoint(req).await; + (result, checkpoint_started.elapsed()) + }); let other_provider = Arc::new(crate::ScriptedProvider::new()); other_provider.push_ok(response(Some(json!({"id":"other-write","name":"write_file","arguments":{"path":"other.txt","content":"independent"}})))); other_provider.push_ok(response(None)); @@ -1412,7 +1450,7 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { observed.inner.commit_gate.try_lock().is_some(), "global commit gate is released while waiting on real tool IO" ); - let other_run = tokio::time::timeout(Duration::from_secs(3), admitted(&independent)) + let other_run = tokio::time::timeout(GATE_WATCHDOG / 3, admitted(&independent)) .await .expect("other workspace admit cannot wait on batch IO"); let other_binding = binding(&independent, &other_run); @@ -1428,8 +1466,10 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { observation: observation_probe.clone(), })); observation_probe.record_stage(WholeBatchStage::ProbeInstalled); + // Give admission and the worker independent watchdogs within the + // held fixture gate. Pending-before-release proves the ordering. let other_worker_result = tokio::time::timeout( - Duration::from_secs(3), + GATE_WATCHDOG / 3, independent .clone() .run_worker(other_run.run_id.clone(), String::new()), @@ -1522,16 +1562,39 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { ); }) .await; - let premature = checkpoint.is_finished(); + // Keep the second call held until the management attempt settles. Releasing + // here used to race the five-second deadline: the checkpoint could acquire + // the freeze with too little time left to read the still-required graph pin. + // This attempt proves refusal while the batch is open; the retry below must + // still verify the actual pin and capture the two closed outputs. + let checkpoint_result = checkpoint.await; + let original_worker_pending = !worker.is_finished(); gate.release(); let worker_result = worker.await; - let checkpoint_result = checkpoint.await; let first = p.managed_effect_get(&first_request).unwrap().unwrap(); let second = p.managed_effect_get(&second_request).unwrap().unwrap(); let final_context = rt .repository .checkpoint_context(&request, &read_clock()) .unwrap(); + let (result, checkpoint_elapsed) = checkpoint_result.unwrap(); + let error = result.expect_err("no mixed checkpoint before second durable output"); + assert_eq!(error.to_string(), "managed_checkpoint_busy"); + assert!( + checkpoint_elapsed >= Duration::from_secs(5), + "checkpoint refused the held batch before its management clock expired" + ); + assert!( + rt.repository + .checkpoint_receipt(&request, &read_clock(), None) + .unwrap() + .is_none(), + "a refused attempt must publish no checkpoint" + ); + let receipt = service + .create_managed_checkpoint(request.clone()) + .await + .expect("same enumerated batch closes on retry"); service.shutdown().await.unwrap(); other.shutdown().await.unwrap(); other_state.service().shutdown().await.unwrap(); @@ -1548,12 +1611,9 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { } worker_result.unwrap(); assert!( - !premature, - "no mixed checkpoint before second durable output" + original_worker_pending, + "independent workspace commits while the original batch worker remains blocked" ); - let receipt = checkpoint_result - .unwrap() - .expect("same enumerated batch closes using original tokens"); let refs = &receipt.checkpoint.history.closed_output_refs; assert_eq!(refs.len(), 2); for record in [first, second] { diff --git a/src/service/managed_protocol_tests.rs b/src/service/managed_protocol_tests.rs index dbcbc7b..4abd8ad 100644 --- a/src/service/managed_protocol_tests.rs +++ b/src/service/managed_protocol_tests.rs @@ -1092,6 +1092,8 @@ async fn maintenance_source_timeout( ) { let (root, state) = fixture_config(|config| { config.run_timeout = timeout; + // The maintenance source pin must survive follow-up fixture work. + config.terminal_run_ttl = std::time::Duration::from_secs(600); config.bearer_token = Some("synthetic-maintenance-fixture".into()); let policy = &mut config .managed_workspace @@ -1174,13 +1176,17 @@ pub(super) async fn restore_request( state: &AgentGatewayState, source: &crate::effects::ManagedCheckpointCreateRequest, ) -> crate::effects::ManagedRestoreAndAdmitRequest { - let checkpoint = state - .service() - .create_managed_checkpoint(source.clone()) - .await - .unwrap() - .checkpoint; - let runtime = state.service().managed_runtime_for_test().unwrap(); + let service = state.service(); + let receipt = match service.create_managed_checkpoint(source.clone()).await { + Err(crate::effects::ManagedMaintenanceFailure::SubmittedUnknown { .. }) => { + // Recover the setup checkpoint by its original request proof. + // This reconciles a submitted transaction; it does not submit a new one. + service.create_managed_checkpoint(source.clone()).await + } + result => result, + }; + let checkpoint = receipt.unwrap().checkpoint; + let runtime = service.managed_runtime_for_test().unwrap(); let context = runtime .repository .checkpoint_context( @@ -1852,7 +1858,7 @@ async fn restore_and_admission_rollback_together() { #[tokio::test(flavor = "multi_thread")] async fn committed_publication_survives_expiry() { let (_root, state, source) = - maintenance_source_timeout(std::time::Duration::from_secs(3)).await; + maintenance_source_timeout(std::time::Duration::from_secs(60)).await; let service = state.service(); let request = restore_request(&state, &source).await; let persistence = state.persistence().unwrap(); @@ -2042,7 +2048,7 @@ async fn unknown_reconciliation_reuses_pending_capacity() { #[tokio::test(flavor = "multi_thread")] async fn checkpoint_unknown_replays_same_frozen_root() { let (_root, state, request) = - maintenance_source_timeout(std::time::Duration::from_secs(3)).await; + maintenance_source_timeout(std::time::Duration::from_secs(60)).await; let service = state.service(); let persistence = state.persistence().unwrap(); persistence.inject_drop_managed_commit_ack(); @@ -2136,6 +2142,7 @@ async fn checkpoint_waits_for_whole_tool_batch() { .create_checkpoint = true; }); let service = state.service(); + service.materialize_cached_runner().unwrap(); let provider = Arc::new(crate::ScriptedProvider::new()); provider.push_ok(json!({"text":"", "tool_calls":[ {"id":"batch-first", "name":"write_file", "arguments":{"path":"first.txt","content":"one"}}, @@ -2153,9 +2160,9 @@ async fn checkpoint_waits_for_whole_tool_batch() { ) .await .unwrap(); - let gate = service - .arm_managed_call_prepare_gate("batch-second") - .unwrap(); + let runtime = service.managed_runtime_for_test().unwrap(); + let gate = runtime + .arm_prepare_gate_with_watchdog(Some("batch-second"), std::time::Duration::from_secs(120)); let worker_service = service.clone(); let worker_id = run.run_id.clone(); let worker = @@ -2184,9 +2191,10 @@ async fn checkpoint_waits_for_whole_tool_batch() { mode: crate::effects::ManagedCheckpointMode::AtClosedBatch, }; let maintenance_service = service.clone(); + let branch = request.source_branch_id.clone(); let checkpoint = tokio::spawn(async move { maintenance_service.create_managed_checkpoint(request).await }); - tokio::time::sleep(std::time::Duration::from_millis(50)).await; + super::linearization::paused(&runtime, &branch).await; let prematurely_finished = checkpoint.is_finished(); gate.release(); worker.await.unwrap(); @@ -2606,13 +2614,12 @@ async fn managed_precommit_native_revocation_generation_and_original_deadline_ne for cut in ["policy", "generation", "deadline"] { let (root, state) = fixture_config(|config| { if cut == "deadline" { - config.run_timeout = std::time::Duration::from_secs(10); + // Reach the Native gate under the admitted clock even on a + // cold worker. The expiry cut below still waits for that clock. + config.run_timeout = std::time::Duration::from_secs(60); } }); let service = state.service(); - let gate = service - .arm_managed_call_prepare_gate("fenced-native") - .unwrap(); let provider = Arc::new(crate::ScriptedProvider::new()); provider.push_ok(response(Some(json!({"id":"fenced-native","name":"write_file","arguments":{"path":"never.txt","content":"private-only"}})))); provider.push_ok(response(None)); @@ -2627,6 +2634,13 @@ async fn managed_precommit_native_revocation_generation_and_original_deadline_ne ) .await .unwrap(); + let gate = service + .managed_runtime_for_test() + .unwrap() + .arm_prepare_gate_with_watchdog( + Some("fenced-native"), + std::time::Duration::from_secs(120), + ); let worker = tokio::spawn( service .clone() @@ -2748,14 +2762,14 @@ async fn managed_transport_bytes_and_exact_row_decoders_reject_before_sdk_enqueu let mut maximum = 0; let mut parameter_max = 0; for line in source.lines() { - if line.contains("let statements:") { + let trimmed = line.trim_start(); + if trimmed.starts_with("let statements:") || trimmed.starts_with("let mut statements:") { statements = 0; } - if line.trim_start().starts_with("{ sql:") { + if trimmed.starts_with("{sql:") || trimmed.starts_with("{ sql:") { statements += 1; maximum = maximum.max(statements); - if let Some((_, tail)) = line.split_once("params: [") { - let params = tail.split_once("], query:").unwrap().0; + if let Some((_, params)) = line.split_once("params:") { parameter_max = parameter_max .max(params.matches("text(").count() + params.matches("integer(").count()); } @@ -2764,10 +2778,10 @@ async fn managed_transport_bytes_and_exact_row_decoders_reject_before_sdk_enqueu assert!((1..=32).contains(&maximum)); assert!((1..=128).contains(¶meter_max)); assert!( - source.contains("max_rows: 512") - && source.contains("max_result_bytes: 1048576") - && source.contains("max_columns: 16") - && source.contains("max_transaction_ms: 5000") + source.contains("max_rows: Some(512)") + && source.contains("max_result_bytes: Some(1048576)") + && source.contains("max_columns: Some(16)") + && source.contains("max_transaction_ms: Some(5000)") ); state.service().shutdown().await.unwrap(); } @@ -3104,8 +3118,10 @@ async fn restored_slow_reprepare_keeps_initial_context_binding_envelope_and_rece #[cfg(feature = "workspace-io-fault")] #[tokio::test(flavor = "multi_thread")] async fn naturally_expired_frozen_handle_has_no_permit_or_managed_codepin_authority() { + // Allow the real admission transaction to finish before exercising expiry + // on its original clock. Do not replace that clock after admission. let (_root, state) = - fixture_config(|config| config.run_timeout = std::time::Duration::from_secs(2)); + fixture_config(|config| config.run_timeout = std::time::Duration::from_secs(10)); let service = state.service(); let run = service .admit_external_with_workspace_selector( @@ -3395,46 +3411,85 @@ fn t07_caller_manifest() -> String { } const T07_HELPERS: &str = r#" +fn t07_callee_data() -> map { + let payload: map = { callee: true }; + payload +} +fn t07_failure(code: string, message: string) -> map { + let mut result: map = {}; + result = { ok: false, content: "", data: {}, error: { code: code, message: message }, truncated: false, artifacts: [] }; + result +} +fn t07_success(content: string, data: map) -> map { + let mut result: map = {}; + result = { ok: true, content: content, data: data, truncated: false, artifacts: [] }; + result +} fn t07_string(value: map, key: string, fallback: string) -> string { let mut result: string = fallback; if value.has(key) { - if type(value[key]) == "string" { - let coerced: string = value[key]; - result = coerced; - } + let coerced: string = value[key]; + result = coerced; } result } fn t07_map(value: map, key: string) -> map { let mut result: map = {}; if value.has(key) { - if type(value[key]) == "map" { - let coerced: map = value[key]; - result = coerced; - } + let coerced: map = value[key]; + result = coerced; } result } fn t07_int(value: map, key: string, fallback: int) -> int { let mut result: int = fallback; if value.has(key) { - if type(value[key]) == "int" { - let coerced: int = value[key]; - result = coerced; - } + let coerced: int = value[key]; + result = coerced; } result } fn t07_bool(value: map, key: string, fallback: bool) -> bool { let mut result: bool = fallback; if value.has(key) { - if type(value[key]) == "bool" { - let coerced: bool = value[key]; - result = coerced; - } + let coerced: bool = value[key]; + result = coerced; } result } +fn t07_denied(value: map, fallback: string) -> map { + let error: map = t07_map(value, "error"); + let mut code: string = fallback; + let mut message: string = "prepare failed"; + if t07_bool(error, "tag", false) { + let payload: map = t07_map(error, "payload"); + code = t07_string(payload, "code", fallback); + message = t07_string(payload, "message", "prepare failed"); + } + t07_failure(code, message) +} +"#; + +// Only the caller's host grants agent::json_text; the child has a narrower host. +const T07_CALLER_HELPERS: &str = r#" +fn t07_json_text(value: map) -> string { + let encoded = agent::json_text(value); + let mut text: string = ""; + if encoded.ok { text = encoded.text; } + text +} +fn t07_prepare_text(prepare: map) -> string { + let metadata: map = { + run_id: t07_string(prepare, "run_id", ""), + call_id: t07_string(prepare, "call_id", ""), + name: t07_string(prepare, "name", "read_file"), + argument_digest: t07_string(prepare, "argument_digest", ""), + registry_identity: t07_string(prepare, "registry_identity", ""), + risk_class: t07_string(prepare, "risk_class", "read"), + summary: t07_string(prepare, "summary", "t07") + }; + t07_json_text(metadata) +} "#; /// The captured caller: one declared direct read plus one delegated read. @@ -3447,31 +3502,38 @@ fn t07_caller_source(second_call: bool) -> String { "" }; format!( - r#"{T07_HELPERS} + r#"{T07_HELPERS}{T07_CALLER_HELPERS} pub fn run(context: map) -> map {{ let prepare: map = t07_map(context, "prepare"); - let prepared: map = agent_runtime::tool_prepare(prepare); + let metadata: string = t07_prepare_text(prepare); + let prepared: map = agent_runtime::tool_prepare(metadata); if t07_bool(prepared, "ok", false) == false => {{ - prepared + t07_denied(prepared, "prepare_failed") }} else => {{ let token: string = t07_string(prepared, "execution_token", ""); let arguments: map = t07_map(context, "arguments"); let path: string = t07_string(arguments, "path", ""); - let meta: map = cap::fs_metadata(token, path); - if t07_bool(meta, "ok", false) == false => {{ - meta + let meta = cap::fs_metadata(token, path); + if meta.ok == false => {{ + let code: string = match meta.error {{ Some(error) => error.code, None => "metadata_failed" }}; + t07_failure(code, "metadata failed") }} else => {{ - let caller_len: int = t07_int(meta, "len", 0); - let canonical: string = json::encode(arguments); + let caller_len: int = meta.len; + let canonical: string = t07_json_text(arguments); let mut second_code: string = "ok"; - let child: map = agent_runtime::invoke_pinned_plugin_child({{ canonical_arguments: canonical }}); + let child_args: PinnedPluginChildInvokeRequest = {{ canonical_arguments: canonical }}; + let child: map = agent_runtime::invoke_pinned_plugin_child(child_args); {second} if t07_bool(child, "ok", false) == false => {{ - let failed: map = {{ ok: false, content: "", data: {{ second: second_code }}, error: {{ code: t07_string(child, "code", "child_failed"), message: t07_string(child, "message", "child failed") }}, truncated: false, artifacts: [] }}; - let _ignored: map = agent_runtime::tool_commit(token, failed); + let failed: map = t07_failure(t07_string(child, "code", "child_failed"), t07_string(child, "message", "child failed")); + let failed_text: string = t07_json_text(failed); + let _ignored: map = agent_runtime::tool_commit(token, failed_text); failed }} else => {{ - let result: map = {{ ok: true, content: "caller:" + t07_int(meta, "len", 0) + ":" + t07_string(child, "canonical_result", ""), data: {{ caller_len: caller_len, second: second_code }}, error: null, truncated: false, artifacts: [] }}; - agent_runtime::tool_commit(token, result) + let child_text: string = t07_string(child, "canonical_result", ""); + let data: map = {{ caller_len: caller_len, second: second_code }}; + let result: map = t07_success("caller:" + caller_len + ":" + child_text, data); + let result_text: string = t07_json_text(result); + agent_runtime::tool_commit(token, result_text) }} }} }} @@ -3484,23 +3546,29 @@ pub fn run(context: map) -> map {{ /// (and therefore detail-backed) parent identity can reach the delegation. fn t07_caller_source_without_read_success() -> String { format!( - r#"{T07_HELPERS} + r#"{T07_HELPERS}{T07_CALLER_HELPERS} pub fn run(context: map) -> map {{ let prepare: map = t07_map(context, "prepare"); - let prepared: map = agent_runtime::tool_prepare(prepare); + let metadata: string = t07_prepare_text(prepare); + let prepared: map = agent_runtime::tool_prepare(metadata); if t07_bool(prepared, "ok", false) == false => {{ - prepared + t07_denied(prepared, "prepare_failed") }} else => {{ let token: string = t07_string(prepared, "execution_token", ""); let arguments: map = t07_map(context, "arguments"); - let canonical: string = json::encode(arguments); - let child: map = agent_runtime::invoke_pinned_plugin_child({{ canonical_arguments: canonical }}); + let canonical: string = t07_json_text(arguments); + let child_args: PinnedPluginChildInvokeRequest = {{ canonical_arguments: canonical }}; + let child: map = agent_runtime::invoke_pinned_plugin_child(child_args); if t07_bool(child, "ok", false) == false => {{ - let failed: map = {{ ok: false, content: "", data: {{}}, error: {{ code: t07_string(child, "code", "child_failed"), message: t07_string(child, "message", "child failed") }}, truncated: false, artifacts: [] }}; - agent_runtime::tool_commit(token, failed) + let failed: map = t07_failure(t07_string(child, "code", "child_failed"), t07_string(child, "message", "child failed")); + let text: string = t07_json_text(failed); + agent_runtime::tool_commit(token, text) }} else => {{ - let result: map = {{ ok: true, content: t07_string(child, "canonical_result", ""), data: {{}}, error: null, truncated: false, artifacts: [] }}; - agent_runtime::tool_commit(token, result) + let content: string = t07_string(child, "canonical_result", ""); + let empty: map = {{}}; + let result: map = t07_success(content, empty); + let text: string = t07_json_text(result); + agent_runtime::tool_commit(token, text) }} }} }} @@ -3511,22 +3579,26 @@ pub fn run(context: map) -> map {{ /// A caller that never invokes the delegated child. fn t07_caller_source_without_child() -> String { format!( - r#"{T07_HELPERS} + r#"{T07_HELPERS}{T07_CALLER_HELPERS} pub fn run(context: map) -> map {{ let prepare: map = t07_map(context, "prepare"); - let prepared: map = agent_runtime::tool_prepare(prepare); + let metadata: string = t07_prepare_text(prepare); + let prepared: map = agent_runtime::tool_prepare(metadata); if t07_bool(prepared, "ok", false) == false => {{ - prepared + t07_denied(prepared, "prepare_failed") }} else => {{ let token: string = t07_string(prepared, "execution_token", ""); let arguments: map = t07_map(context, "arguments"); let path: string = t07_string(arguments, "path", ""); - let meta: map = cap::fs_metadata(token, path); - if t07_bool(meta, "ok", false) == false => {{ - meta + let meta = cap::fs_metadata(token, path); + if meta.ok == false => {{ + let code: string = match meta.error {{ Some(error) => error.code, None => "metadata_failed" }}; + t07_failure(code, "metadata failed") }} else => {{ - let result: map = {{ ok: true, content: "caller-only", data: {{}}, error: null, truncated: false, artifacts: [] }}; - agent_runtime::tool_commit(token, result) + let empty: map = {{}}; + let result: map = t07_success("caller-only", empty); + let text: string = t07_json_text(result); + agent_runtime::tool_commit(token, text) }} }} }} @@ -3538,17 +3610,22 @@ pub fn run(context: map) -> map {{ /// parent argument object. fn t07_caller_source_projection_mismatch() -> String { format!( - r#"{T07_HELPERS} + r#"{T07_HELPERS}{T07_CALLER_HELPERS} pub fn run(context: map) -> map {{ let prepare: map = t07_map(context, "prepare"); - let prepared: map = agent_runtime::tool_prepare(prepare); + let metadata: string = t07_prepare_text(prepare); + let prepared: map = agent_runtime::tool_prepare(metadata); if t07_bool(prepared, "ok", false) == false => {{ - prepared + t07_denied(prepared, "prepare_failed") }} else => {{ let token: string = t07_string(prepared, "execution_token", ""); - let child: map = agent_runtime::invoke_pinned_plugin_child({{ canonical_arguments: "{{\"path\":\"other.txt\"}}" }}); - let result: map = {{ ok: false, content: "", data: {{ child_code: t07_string(child, "code", "") }}, error: {{ code: t07_string(child, "code", "child_failed"), message: t07_string(child, "message", "") }}, truncated: false, artifacts: [] }}; - agent_runtime::tool_commit(token, result) + let child_args: PinnedPluginChildInvokeRequest = {{ canonical_arguments: "{{\"path\":\"other.txt\"}}" }}; + let child: map = agent_runtime::invoke_pinned_plugin_child(child_args); + let code: string = t07_string(child, "code", "child_failed"); + let message: string = t07_string(child, "message", ""); + let result: map = t07_failure(code, message); + let text: string = t07_json_text(result); + agent_runtime::tool_commit(token, text) }} }} "# @@ -3562,17 +3639,22 @@ fn t07_callee_source(token_sentinel: &str) -> String { pub fn run(context: map) -> map {{ let arguments: map = t07_map(context, "arguments"); let path: string = t07_string(arguments, "path", ""); - let meta: map = cap::fs_metadata("{token_sentinel}", path); - if t07_bool(meta, "ok", false) == false => {{ - meta + let meta = cap::fs_metadata("{token_sentinel}", path); + if meta.ok == false => {{ + let code: string = match meta.error {{ Some(error) => error.code, None => "metadata_failed" }}; + t07_failure(code, "metadata failed") }} else => {{ - let len: int = t07_int(meta, "len", 0); - let read: map = cap::fs_read_range("{token_sentinel}", path, 0, len); - if t07_bool(read, "ok", false) == false => {{ - read + let len: int = meta.len; + let read = cap::fs_read_range("{token_sentinel}", path, 0, len); + if read.ok == false => {{ + let code: string = match read.error {{ Some(error) => error.code, None => "read_failed" }}; + t07_failure(code, "read failed") }} else => {{ let text: string = bytes::to_utf8(read.bytes); - {{ ok: true, content: text, data: {{ callee: true }}, error: null, truncated: false, artifacts: [] }} + let data: map = t07_callee_data(); + let mut result: map = {{}}; + result = {{ ok: true, content: text, data: data, truncated: false, artifacts: [] }}; + result }} }} }} @@ -3588,17 +3670,20 @@ fn t07_callee_source_doubled() -> String { pub fn run(context: map) -> map {{ let arguments: map = t07_map(context, "arguments"); let path: string = t07_string(arguments, "path", ""); - let meta: map = cap::fs_metadata("", path); - if t07_bool(meta, "ok", false) == false => {{ - meta + let meta = cap::fs_metadata("", path); + if meta.ok == false => {{ + let code: string = match meta.error {{ Some(error) => error.code, None => "metadata_failed" }}; + t07_failure(code, "metadata failed") }} else => {{ - let len: int = t07_int(meta, "len", 0); - let read: map = cap::fs_read_range("", path, 0, len); - if t07_bool(read, "ok", false) == false => {{ - read + let len: int = meta.len; + let read = cap::fs_read_range("", path, 0, len); + if read.ok == false => {{ + let code: string = match read.error {{ Some(error) => error.code, None => "read_failed" }}; + t07_failure(code, "read failed") }} else => {{ let text: string = bytes::to_utf8(read.bytes); - {{ ok: true, content: text + text, data: {{ callee: true }}, error: null, truncated: false, artifacts: [] }} + let data: map = t07_callee_data(); + t07_success(text + text, data) }} }} }} @@ -3921,11 +4006,11 @@ async fn t07_stateless_caller_callee_completes_one_parent_read_transaction() { let output = case.probe("t07-call", T07_ARGUMENTS); assert_eq!(output["ok"], true, "{output}"); assert_eq!( - output["managed_publication"]["status"], "read_only", + output["managed_publication"]["payload"]["status"], "read_only", "{output}" ); assert!( - output["managed_publication"]["effect_id"] + output["managed_publication"]["payload"]["effect_id"] .as_str() .is_some(), "{output}" @@ -4297,7 +4382,7 @@ async fn t07_unknown_intent_write_aborts_without_child_io_and_never_retries() { async fn t07_topology_and_callee_role_rejections_happen_at_prepare() { // A callee that imports the caller lifecycle surface is refused. let lifecycle_callee = format!( - "{T07_HELPERS}pub fn run(context: map) -> map {{ agent_runtime::tool_prepare({{}}) }}\n" + "{T07_HELPERS}pub fn run(context: map) -> map {{ agent_runtime::tool_prepare(\"{{}}\") }}\n" ); let (root, state) = fixture_config(|_| {}); let database = root.join("gateway.sqlite"); @@ -4793,7 +4878,9 @@ async fn t07_child_native_read_denies_closed_original_clock(close: T07OriginalCl &t07_callee_source(""), move |config| { if close == T07OriginalClockClose::Elapsed { - config.run_timeout = std::time::Duration::from_secs(2); + // The deadline must expire inside the child; package activation + // and the original admission run before that observed boundary. + config.run_timeout = std::time::Duration::from_secs(30); } }, ) @@ -5712,16 +5799,19 @@ fn t07_callee_source_raw_bytes() -> String { pub fn run(context: map) -> map {{ let arguments: map = t07_map(context, "arguments"); let path: string = t07_string(arguments, "path", ""); - let meta: map = cap::fs_metadata("", path); - if t07_bool(meta, "ok", false) == false => {{ - meta + let meta = cap::fs_metadata("", path); + if meta.ok == false => {{ + let code: string = match meta.error {{ Some(error) => error.code, None => "metadata_failed" }}; + t07_failure(code, "metadata failed") }} else => {{ - let len: int = t07_int(meta, "len", 0); - let read: map = cap::fs_read_range("", path, 0, len); - if t07_bool(read, "ok", false) == false => {{ - read + let len: int = meta.len; + let read = cap::fs_read_range("", path, 0, len); + if read.ok == false => {{ + let code: string = match read.error {{ Some(error) => error.code, None => "read_failed" }}; + t07_failure(code, "read failed") }} else => {{ - {{ ok: true, content: "", data: {{ raw: read.bytes }}, error: null, truncated: false, artifacts: [] }} + let data: map = {{ raw: read.bytes }}; + {{ ok: true, content: "", data: data, truncated: false, artifacts: [] }} }} }} }} @@ -5961,7 +6051,8 @@ async fn t07_s1_checkpoint_restores_the_captured_stateless_callee() { provider.push_ok(response(Some(json!({ "id": "t07-call", "name": "read_file", - "arguments": {"path": "note.txt"} + "arguments_json": T07_ARGUMENTS, + "arguments": {} })))); provider.push_ok(response(None)); service.inject_provider_host(provider); @@ -6100,7 +6191,8 @@ async fn t07_s1_checkpoint_restores_the_captured_stateless_callee() { restored_provider.push_ok(response(Some(json!({ "id": "t07-restored", "name": "read_file", - "arguments": {"path": "note.txt"} + "arguments_json": T07_ARGUMENTS, + "arguments": {} })))); restored_provider.push_ok(response(None)); service.inject_provider_host(restored_provider); @@ -6195,7 +6287,8 @@ async fn t07_s1_checkpoint_restore_refuses_a_revoked_captured_graph() { provider.push_ok(response(Some(json!({ "id": "t07-call", "name": "read_file", - "arguments": {"path": "note.txt"} + "arguments_json": T07_ARGUMENTS, + "arguments": {} })))); provider.push_ok(response(None)); service.inject_provider_host(provider); @@ -6260,15 +6353,14 @@ async fn two_real_prepared_effects_cas_one_winner_and_persist_exact_conflict_pro use std::sync::{Condvar, Mutex}; use std::time::{Duration, Instant}; - // Same arrival budget as the existing managed prepare-gate fixture. This - // is a test-local release barrier, not a product wait or a new timeout. - const ARRIVAL_DEADLINE: Duration = Duration::from_secs(20); + // The held callbacks must allow a second real admission and register to + // finish. This watchdog is separate from both runs' original clocks. + const ARRIVAL_DEADLINE: Duration = Duration::from_secs(60); struct RegisterArrivals { arrived: AtomicUsize, permits: Mutex<[bool; 2]>, cv: Condvar, - deadline: Instant, } impl RegisterArrivals { fn new() -> Self { @@ -6276,13 +6368,13 @@ async fn two_real_prepared_effects_cas_one_winner_and_persist_exact_conflict_pro arrived: AtomicUsize::new(0), permits: Mutex::new([false, false]), cv: Condvar::new(), - deadline: Instant::now() + ARRIVAL_DEADLINE, } } fn arrive(&self, index: usize) { + let deadline = Instant::now() + ARRIVAL_DEADLINE; let mut permits = self.permits.lock().unwrap(); while !permits[index] { - let remaining = self.deadline.saturating_duration_since(Instant::now()); + let remaining = deadline.saturating_duration_since(Instant::now()); assert!( !remaining.is_zero(), "managed fixture register release deadline" diff --git a/src/service/managed_review_tests.rs b/src/service/managed_review_tests.rs index 5201c80..cd9299d 100644 --- a/src/service/managed_review_tests.rs +++ b/src/service/managed_review_tests.rs @@ -29,8 +29,9 @@ async fn live_register_lookup_keeps_original_clock_and_never_enqueues_after_canc let mut results = Vec::new(); for target in ["effect.head", "effect.object_metadata"] { for expire in [false, true] { - let original = - crate::RunCancellation::with_timeout(std::time::Duration::from_millis(150)); + // The clock is spent at the selected request boundary below, + // not during setup or the preceding head lookup. + let original = crate::RunCancellation::with_timeout(std::time::Duration::from_secs(1)); let binding = admitted.with_cancellation_for_test(original.clone()); let observed = Arc::new(std::sync::Mutex::new(Vec::new())); let capture = Arc::clone(&observed); @@ -394,6 +395,8 @@ async fn expired_original_lost_commit_ack_does_not_query_with_new_authority_and_ let (root, state) = fixture_config(|config| config.run_timeout = std::time::Duration::from_secs(10)); let service = state.service(); + // Prepare the runner and its host code before the expiring run is admitted. + service.materialize_cached_runner().unwrap(); let persistence = state.persistence().unwrap(); let provider = Arc::new(crate::ScriptedProvider::new()); let sentinel = root.join("expired-ack-sentinel"); @@ -424,6 +427,8 @@ async fn expired_original_lost_commit_ack_does_not_query_with_new_authority_and_ let capture = Arc::clone(&observed); let committed = Arc::new(std::sync::atomic::AtomicBool::new(false)); let after_commit = Arc::clone(&committed); + let (observed_expiry, expiry_observed) = tokio::sync::oneshot::channel(); + let observed_expiry = std::sync::Mutex::new(Some(observed_expiry)); *persistence.managed_request_hook.lock().unwrap() = Some(Arc::new(move |op, cancel, enqueue| { if op == "effect.commit" && enqueue { @@ -437,6 +442,9 @@ async fn expired_original_lost_commit_ack_does_not_query_with_new_authority_and_ .lock() .unwrap() .push((enqueue, cancel.shares_original_clock(&original))); + if !enqueue && let Some(signal) = observed_expiry.lock().unwrap().take() { + let _ = signal.send(()); + } } })); persistence.inject_drop_managed_commit_ack(); @@ -444,6 +452,13 @@ async fn expired_original_lost_commit_ack_does_not_query_with_new_authority_and_ .clone() .run_worker(run.run_id.clone(), "ignored".into()) .await; + // The run worker may return on cancellation before the blocking readback + // probe has recorded its result. Wait for that exact boundary before + // disarming the hook and reading the durable record. + tokio::time::timeout(std::time::Duration::from_secs(120), expiry_observed) + .await + .expect("lost commit acknowledgement readback watchdog") + .expect("lost commit acknowledgement readback observation"); *persistence.managed_request_hook.lock().unwrap() = None; let rows = observed.lock().unwrap().clone(); let request = ManagedEffectGetRequest { diff --git a/src/sqlite_storage_rows.rs b/src/sqlite_storage_rows.rs index 98f4190..0722cbc 100644 --- a/src/sqlite_storage_rows.rs +++ b/src/sqlite_storage_rows.rs @@ -45,12 +45,28 @@ pub fn sqlite_storage_cell(value: &Value) -> Result { return Ok(value.clone()); }; let cell = match kind { - "int" => obj.get("int_value").cloned().unwrap_or(Value::Null), - "float" => obj.get("float_value").cloned().unwrap_or(Value::Null), - "text" => obj.get("text_value").cloned().unwrap_or(Value::Null), - "blob" => obj.get("blob_value").cloned().unwrap_or(Value::Null), + "int" => carried(&obj.get("int_value").cloned().unwrap_or(Value::Null)), + "float" => carried(&obj.get("float_value").cloned().unwrap_or(Value::Null)), + "text" => carried(&obj.get("text_value").cloned().unwrap_or(Value::Null)), + "blob" => carried(&obj.get("blob_value").cloned().unwrap_or(Value::Null)), "null" => Value::Null, _ => value.clone(), }; Ok(cell) } + +/// Unwraps a logical `Option` carrier into the plain value it holds. +/// +/// The strict guest dialect cannot spell a null literal, so a `SqliteValue` +/// leaf that is absent arrives as `{tag: false}` and a present one as +/// `{tag: true, payload: value}`. Row consumers expect the plain value. +fn carried(value: &Value) -> Value { + let Some(obj) = value.as_object() else { + return value.clone(); + }; + match obj.get("tag") { + Some(Value::Bool(true)) => obj.get("payload").cloned().unwrap_or(Value::Null), + Some(Value::Bool(false)) if obj.len() == 1 => Value::Null, + _ => value.clone(), + } +} diff --git a/tests/agent_loop_tests.rs b/tests/agent_loop_tests.rs index c9e025e..6ccf06b 100644 --- a/tests/agent_loop_tests.rs +++ b/tests/agent_loop_tests.rs @@ -836,6 +836,7 @@ fn loop_canonical_replay_payload_is_not_a_transport_wrapper() { .results .lock() .insert("replayed-1".to_string(), original.clone()); + let runner = loop_runner(); let host = loop_host_with_durable(8, None, root.clone(), durable); let provider = ScriptedProvider::new(); provider.push_ok(tool_response( @@ -845,12 +846,12 @@ fn loop_canonical_replay_payload_is_not_a_transport_wrapper() { ]), )); provider.push_ok(text_response("replay complete")); - let runner = configure_loop_runner(loop_runner(), provider.clone(), Some(host)); + let runner = configure_loop_runner(runner, provider.clone(), Some(host)); let decision = decide( &runner, run_context(4, 8, loop_config(false, false), echo_tool()), ); - assert_eq!(decision["kind"], "run.completed"); + assert_eq!(decision["kind"], "run.completed", "decision: {decision}"); assert_eq!(executor.count.load(Ordering::SeqCst), 0); let followup = &provider.requests()[1]; let block = &followup["messages"][2]["content"][0]; @@ -1661,35 +1662,13 @@ fn temporary_root(label: &str) -> PathBuf { temporary_root_in(&agent_test_root(), label, nonce) } -/// The path builder itself: the base directory is explicit so the unit test -/// below pins the layout without touching the process-global env var -/// (parallel tests must never set it). The pid + nanosecond nonce keeps -/// concurrent tests in this process from colliding. +/// The pid + nanosecond nonce keeps concurrent tests in this process from colliding. fn temporary_root_in(root: &std::path::Path, label: &str, nonce: u128) -> PathBuf { let path = root.join(format!("{label}-{}-{nonce}", std::process::id())); fs::create_dir_all(&path).expect("temporary storage root should be created"); path } -#[test] -fn agent_test_artifacts_land_under_an_explicit_root() { - let base = std::env::temp_dir().join(format!("agent-root-{}", std::process::id())); - let root = temporary_root_in(&base, "layout", 1); - assert!( - root.starts_with(&base), - "the storage root must live under the explicit root, got {root:?}" - ); - assert!( - root.file_name() - .expect("root name") - .to_string_lossy() - .starts_with("layout-"), - "the label must prefix the unique directory name" - ); - fs::remove_dir_all(&root).expect("temporary storage root should be removed"); - fs::remove_dir_all(&base).expect("temporary root should be removed"); -} - fn storage_command( db_name: &str, request_id: &str, @@ -2004,7 +1983,8 @@ fn transaction_rows_affected(result: &JsonValue, index: usize) -> JsonValue { json!("execute"), "SqliteTransactionResult[{index}] must be kind=execute, got {first}" ); - let affected = &first["execute"]["rows_affected"]; + assert_eq!(first["execute"]["tag"], json!(true)); + let affected = &first["execute"]["payload"]["rows_affected"]; assert!( affected.is_number(), "SqliteTransactionResult[{index}].execute.rows_affected must be present, got {first}" diff --git a/tests/agent_rss_contract_tests.rs b/tests/agent_rss_contract_tests.rs new file mode 100644 index 0000000..f6ecf86 --- /dev/null +++ b/tests/agent_rss_contract_tests.rs @@ -0,0 +1,11 @@ +#[path = "cases/bundled_agent_rss_entry_tests.rs"] +mod bundled_agent_rss_entry_tests; + +#[path = "cases/bundled_resource_runtime_tests.rs"] +mod bundled_resource_runtime_tests; + +#[path = "cases/runner_tests.rs"] +mod runner_tests; + +#[path = "cases/workspace_rss_entry_tests.rs"] +mod workspace_rss_entry_tests; diff --git a/tests/auth_cli_contract_tests.rs b/tests/auth_cli_contract_tests.rs new file mode 100644 index 0000000..8dcd11b --- /dev/null +++ b/tests/auth_cli_contract_tests.rs @@ -0,0 +1,26 @@ +#[path = "support/credential_use.rs"] +mod credential_use; + +#[path = "support/typed_carriers.rs"] +mod typed_carriers; + +#[path = "cases/auth_cli_login_host_tests.rs"] +mod auth_cli_login_host_tests; +#[path = "cases/auth_cli_login_rss_tests.rs"] +mod auth_cli_login_rss_tests; +#[path = "cases/auth_cli_path_rss_tests.rs"] +mod auth_cli_path_rss_tests; +#[path = "cases/auth_cli_rss_entry_tests.rs"] +mod auth_cli_rss_entry_tests; +#[path = "cases/auth_cli_ui_tests.rs"] +mod auth_cli_ui_tests; +#[path = "cases/auth_command_host_tests.rs"] +mod auth_command_host_tests; +#[path = "cases/auth_enrollment_host_tests.rs"] +mod auth_enrollment_host_tests; +#[path = "cases/auth_public_json_tests.rs"] +mod auth_public_json_tests; +#[path = "cases/auth_request_body_tests.rs"] +mod auth_request_body_tests; +#[path = "cases/auth_resource_host_tests.rs"] +mod auth_resource_host_tests; diff --git a/tests/auth_config_fixture_tests.rs b/tests/auth_config_fixture_tests.rs new file mode 100644 index 0000000..ba82379 --- /dev/null +++ b/tests/auth_config_fixture_tests.rs @@ -0,0 +1,12 @@ +#[path = "cases/auth_bridge_rss_tests.rs"] +mod auth_bridge_rss_tests; +#[path = "cases/auth_store_rss_tests.rs"] +mod auth_store_rss_tests; +#[path = "cases/auth_store_tests.rs"] +mod auth_store_tests; +#[path = "cases/config_auth_rss_tests.rs"] +mod config_auth_rss_tests; +#[path = "cases/oauth_flow_tests.rs"] +mod oauth_flow_tests; +#[path = "cases/oauth_rss_entry_tests.rs"] +mod oauth_rss_entry_tests; diff --git a/tests/auth_rss_transport_tests.rs b/tests/auth_rss_transport_tests.rs new file mode 100644 index 0000000..bebb737 --- /dev/null +++ b/tests/auth_rss_transport_tests.rs @@ -0,0 +1,21 @@ +#[path = "support/typed_carriers.rs"] +mod typed_carriers; + +#[path = "cases/auth_access_result_rss_tests.rs"] +mod auth_access_result_rss_tests; +#[path = "cases/auth_fallible_bridge_tests.rs"] +mod auth_fallible_bridge_tests; +#[path = "cases/auth_refresh_result_rss_tests.rs"] +mod auth_refresh_result_rss_tests; +#[path = "cases/auth_sanitized_stream_rss_tests.rs"] +mod auth_sanitized_stream_rss_tests; +#[path = "cases/auth_transport_policy_tests.rs"] +mod auth_transport_policy_tests; +#[path = "cases/authorization_instruction_rss_tests.rs"] +mod authorization_instruction_rss_tests; +#[path = "cases/callback_result_rss_tests.rs"] +mod callback_result_rss_tests; +#[path = "cases/token_metadata_rss_tests.rs"] +mod token_metadata_rss_tests; +#[path = "cases/token_save_rss_tests.rs"] +mod token_save_rss_tests; diff --git a/tests/auth_access_result_rss_tests.rs b/tests/cases/auth_access_result_rss_tests.rs similarity index 88% rename from tests/auth_access_result_rss_tests.rs rename to tests/cases/auth_access_result_rss_tests.rs index 7e4dcea..4e7af86 100644 --- a/tests/auth_access_result_rss_tests.rs +++ b/tests/cases/auth_access_result_rss_tests.rs @@ -70,6 +70,14 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { .unwrap_or_else(|| panic!("missing RSS field {name} in {value:?}")) } +fn option_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { + match rustscript_vm::option::payload(field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(payload)) if !matches!(payload, Value::Null) => Some(payload), + _ => panic!("expected a non-Unit RSS Option for {name}"), + } +} + fn fixture_vm(host: &AuthHost, catalog: &rustscript_vm::HostApiCatalog) -> Vm { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/access_result/main.rss"); @@ -137,9 +145,12 @@ fn genuine_access_issuance_result_publishes_transports_and_reports_actual_codes( ) .expect("unadmitted issuance is a guest-readable result"); assert_eq!(field(&unadmitted, "ok"), &Value::Bool(false)); - assert!(matches!(field(&unadmitted, "value"), Value::Null)); + assert!(option_field(&unadmitted, "value").is_none()); assert_eq!( - field(field(&unadmitted, "error"), "code"), + field( + option_field(&unadmitted, "error").expect("error is present"), + "code" + ), &Value::string("credential_not_admitted") ); assert!( @@ -159,9 +170,12 @@ fn genuine_access_issuance_result_publishes_transports_and_reports_actual_codes( ) .expect("wrong generation is a guest-readable result"); assert_eq!(field(&wrong_generation, "ok"), &Value::Bool(false)); - assert!(matches!(field(&wrong_generation, "value"), Value::Null)); + assert!(option_field(&wrong_generation, "value").is_none()); assert_eq!( - field(field(&wrong_generation, "error"), "code"), + field( + option_field(&wrong_generation, "error").expect("error is present"), + "code" + ), &Value::string("access_unavailable") ); assert!( @@ -181,10 +195,10 @@ fn genuine_access_issuance_result_publishes_transports_and_reports_actual_codes( ) .expect("valid issuance is a typed result"); assert_eq!(field(&issued, "ok"), &Value::Bool(true)); - assert!(matches!(field(&issued, "error"), Value::Null)); - let inner = field(&issued, "value"); + assert!(option_field(&issued, "error").is_none()); + let inner = option_field(&issued, "value").expect("value is present"); assert_eq!(field(inner, "ok"), &Value::Bool(true)); - assert!(matches!(field(inner, "error"), Value::Null)); + assert!(option_field(inner, "error").is_none()); let access = field(inner, "handle").clone(); assert!( transport.take_sent().is_empty(), @@ -204,7 +218,7 @@ fn genuine_access_issuance_result_publishes_transports_and_reports_actual_codes( ) .expect("native transport result"); assert_eq!(field(&sent_result, "ok"), &Value::Bool(true)); - let response = field(&sent_result, "value"); + let response = option_field(&sent_result, "value").expect("value is present"); assert_eq!(field(response, "status"), &Value::Int(200)); assert_eq!( field(response, "body_without_secret_fields"), diff --git a/tests/auth_bridge_rss_tests.rs b/tests/cases/auth_bridge_rss_tests.rs similarity index 100% rename from tests/auth_bridge_rss_tests.rs rename to tests/cases/auth_bridge_rss_tests.rs diff --git a/tests/auth_cli_login_host_tests.rs b/tests/cases/auth_cli_login_host_tests.rs similarity index 98% rename from tests/auth_cli_login_host_tests.rs rename to tests/cases/auth_cli_login_host_tests.rs index 2692df8..0be48f8 100644 --- a/tests/auth_cli_login_host_tests.rs +++ b/tests/cases/auth_cli_login_host_tests.rs @@ -181,13 +181,19 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { let Value::Map(fields) = value else { panic!("expected RSS record, got {value:?}"); }; - fields + let field = fields .iter() .find_map(|(key, value)| match key { Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing {name} in {value:?}")) + .unwrap_or_else(|| panic!("missing {name} in {value:?}")); + match name { + "error" | "value" | "account_id" | "last_refresh_at_ms" => { + crate::typed_carriers::nullable(field) + } + _ => field, + } } fn invoke_result(vm: &mut Vm, name: &str, args: &[Value]) -> Result { @@ -245,16 +251,19 @@ fn save_request( ("token_type", Value::string("Bearer")), ("expires_at_ms", Value::Int(2_000_000_000_000)), ("scopes", Value::array(vec![])), - ("account_id", Value::string("synthetic-login-account")), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string("synthetic-login-account")), + ), ("generation", Value::Int(expected_generation)), ("status", Value::string("active")), - ("last_refresh_at_ms", Value::Null), + ("last_refresh_at_ms", rustscript_vm::option::none_carrier()), ("has_refresh_token", Value::Bool(false)), ]), ), ("access_slot", access_slot), ("refresh_action", Value::string("clear")), - ("refresh_slot", Value::Null), + ("refresh_slot", rustscript_vm::option::none_carrier()), ]) } diff --git a/tests/auth_cli_login_rss_tests.rs b/tests/cases/auth_cli_login_rss_tests.rs similarity index 95% rename from tests/auth_cli_login_rss_tests.rs rename to tests/cases/auth_cli_login_rss_tests.rs index 4d648c5..d7cc635 100644 --- a/tests/auth_cli_login_rss_tests.rs +++ b/tests/cases/auth_cli_login_rss_tests.rs @@ -31,44 +31,30 @@ const REFRESH_SECRET: &str = "SYNTHETIC_REFRESH_TOKEN_SECRET_d6e2"; const ALIAS: &str = "fixture-device-alias"; const USER_CODE: &str = "PUBLIC-CODE-7QK"; -const STARTER_ENTRY: &str = r#" -use auth; -struct AuthCommandInput { operation: string, provider: string?, credential_id: string? } -struct AuthEntryResult { ok: bool, operation: string, credential_id: string?, generation: int, status: string, error: string? } -pub fn run(home: resource, command: AuthCommandInput) -> AuthEntryResult { - {ok: false, operation: command.operation, credential_id: null, generation: 0, status: "unsupported_operation", error: "unsupported_operation"} -} -"#; -const STARTER_SOURCES: &[(&str, &str)] = &[ - (ENTRY_PATH, STARTER_ENTRY), - (CODEX_PATH, include_str!("../rss/auth/codex_device.rss")), - (TYPES_PATH, include_str!("../rss/auth/types.rss")), -]; - const LOGIN_SOURCES: &[(&str, &str)] = &[ - (ENTRY_PATH, include_str!("../rss/auth/login.rss")), + (ENTRY_PATH, include_str!("../../rss/auth/login.rss")), ( "rss/auth/pkce_login.rss", - include_str!("../rss/auth/pkce_login.rss"), + include_str!("../../rss/auth/pkce_login.rss"), ), ( "rss/auth/pkce_instruction_session.rss", - include_str!("../rss/auth/pkce_instruction_session.rss"), + include_str!("../../rss/auth/pkce_instruction_session.rss"), ), ( "rss/auth/pkce_session.rss", - include_str!("../rss/auth/pkce_session.rss"), + include_str!("../../rss/auth/pkce_session.rss"), ), ( "rss/auth/token_metadata.rss", - include_str!("../rss/auth/token_metadata.rss"), + include_str!("../../rss/auth/token_metadata.rss"), ), ( "rss/auth/token_save.rss", - include_str!("../rss/auth/token_save.rss"), + include_str!("../../rss/auth/token_save.rss"), ), - (CODEX_PATH, include_str!("../rss/auth/codex_device.rss")), - (TYPES_PATH, include_str!("../rss/auth/types.rss")), + (CODEX_PATH, include_str!("../../rss/auth/codex_device.rss")), + (TYPES_PATH, include_str!("../../rss/auth/types.rss")), ]; struct FixtureHome(PathBuf); @@ -268,11 +254,6 @@ fn command_with_credential( } } -fn starter_runner() -> AuthEntryRunner { - AuthEntryRunner::from_bundle(ENTRY_PATH, STARTER_SOURCES) - .expect("compile guest baseline bundle") -} - fn login_runner() -> AuthEntryRunner { AuthEntryRunner::from_bundle(ENTRY_PATH, LOGIN_SOURCES) .expect("compile production login bundle") @@ -330,13 +311,6 @@ fn run_cli( }) } -fn run_starter(input: AuthCommandInput) -> Value { - let home = FixtureHome::new(None, "codex-device"); - let transport = Arc::new(ScriptedHttpsTransport::new()); - run_value(&starter_runner(), &home, transport, None, input) - .expect("execute guest baseline with the configured-login host") -} - fn queue_success(transport: &ScriptedHttpsTransport) { transport.push_raw( 200, @@ -490,8 +464,19 @@ fn pre_oauth_rejections_leave_fresh_home_without_auth_store_or_lock() { #[test] fn guest_login_baseline_reports_unsupported_operation() { - let result = run_starter(command("auth.login", None)); + let home = FixtureHome::new(None, "codex-device"); + let transport = Arc::new(ScriptedHttpsTransport::new()); + let result = run_value( + &login_runner(), + &home, + transport.clone(), + None, + command("auth.unsupported", None), + ) + .expect("run the production login command's unsupported-operation branch"); assert_failure(&result, "unsupported_operation"); + assert!(transport.take_sent().is_empty()); + assert!(!home.auth_exists()); } #[test] diff --git a/tests/auth_cli_path_rss_tests.rs b/tests/cases/auth_cli_path_rss_tests.rs similarity index 93% rename from tests/auth_cli_path_rss_tests.rs rename to tests/cases/auth_cli_path_rss_tests.rs index 4a794a8..301ce1c 100644 --- a/tests/auth_cli_path_rss_tests.rs +++ b/tests/cases/auth_cli_path_rss_tests.rs @@ -12,40 +12,46 @@ use std::sync::Arc; use std::time::{Duration, Instant}; const ENTRY_PATH: &str = "rss/config/path.rss"; -const SOURCES: &[(&str, &str)] = &[(ENTRY_PATH, include_str!("../rss/config/path.rss"))]; +const SOURCES: &[(&str, &str)] = &[(ENTRY_PATH, include_str!("../../rss/config/path.rss"))]; const AUTH_ENTRY_PATH: &str = "rss/auth/commands.rss"; const AUTH_SOURCES: &[(&str, &str)] = &[ - (AUTH_ENTRY_PATH, include_str!("../rss/auth/commands.rss")), + (AUTH_ENTRY_PATH, include_str!("../../rss/auth/commands.rss")), ( "rss/config/selection.rss", - include_str!("../rss/config/selection.rss"), + include_str!("../../rss/config/selection.rss"), + ), + ( + "rss/auth/login.rss", + include_str!("../../rss/auth/login.rss"), ), - ("rss/auth/login.rss", include_str!("../rss/auth/login.rss")), ( "rss/auth/pkce_login.rss", - include_str!("../rss/auth/pkce_login.rss"), + include_str!("../../rss/auth/pkce_login.rss"), ), ( "rss/auth/pkce_instruction_session.rss", - include_str!("../rss/auth/pkce_instruction_session.rss"), + include_str!("../../rss/auth/pkce_instruction_session.rss"), ), ( "rss/auth/pkce_session.rss", - include_str!("../rss/auth/pkce_session.rss"), + include_str!("../../rss/auth/pkce_session.rss"), ), ( "rss/auth/token_metadata.rss", - include_str!("../rss/auth/token_metadata.rss"), + include_str!("../../rss/auth/token_metadata.rss"), ), ( "rss/auth/token_save.rss", - include_str!("../rss/auth/token_save.rss"), + include_str!("../../rss/auth/token_save.rss"), ), ( "rss/auth/codex_device.rss", - include_str!("../rss/auth/codex_device.rss"), + include_str!("../../rss/auth/codex_device.rss"), + ), + ( + "rss/auth/types.rss", + include_str!("../../rss/auth/types.rss"), ), - ("rss/auth/types.rss", include_str!("../rss/auth/types.rss")), ]; struct TempArea(PathBuf); @@ -341,8 +347,8 @@ use config; struct AuthCommandInput { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } pub fn run(home: resource, command: AuthCommandInput) -> ConfigPathResultEnvelope { @@ -363,13 +369,15 @@ use config; struct AuthCommandInput { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } -pub fn run(home: resource, command: AuthCommandInput) -> map { +struct TruncatedPathReport { ok: bool, status: Option } + +pub fn run(home: resource, command: AuthCommandInput) -> TruncatedPathReport { let native = config::resolved_path_result(&home); - {ok: true, status: native.value.copy()} + {ok: true, status: native.value} } "#; let truncated_runner = AuthEntryRunner::from_bundle("main.rss", &[("main.rss", truncated)]) @@ -502,8 +510,8 @@ use config; struct AuthCommandInput { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } pub fn run(home: resource, command: AuthCommandInput) -> ConfigPathResultEnvelope { @@ -543,13 +551,22 @@ pub fn run(home: resource, command: AuthCommandInput) -> Confi field(&bound, "host_home").clone(), Value::map(vec![ (Value::string("operation"), Value::string("config.path")), - (Value::string("provider"), Value::Null), - (Value::string("credential_id"), Value::Null), + ( + Value::string("provider"), + rustscript_vm::option::none_carrier(), + ), + ( + Value::string("credential_id"), + rustscript_vm::option::none_carrier(), + ), ]), ], ) .expect("invoke native path primitive"); assert_eq!(field(&native, "ok"), &Value::Bool(true)); - assert!(matches!(field(&native, "value"), Value::String(_))); + assert!(matches!( + rustscript_vm::option::payload(field(&native, "value")).expect("canonical path Option"), + (true, Some(Value::String(_))) + )); host.revoke(); } diff --git a/tests/auth_cli_rss_entry_tests.rs b/tests/cases/auth_cli_rss_entry_tests.rs similarity index 97% rename from tests/auth_cli_rss_entry_tests.rs rename to tests/cases/auth_cli_rss_entry_tests.rs index 58eec62..63cd1cf 100644 --- a/tests/auth_cli_rss_entry_tests.rs +++ b/tests/cases/auth_cli_rss_entry_tests.rs @@ -16,37 +16,43 @@ use std::time::Duration; const ENTRY_PATH: &str = "rss/auth/commands.rss"; const SOURCES: &[(&str, &str)] = &[ - (ENTRY_PATH, include_str!("../rss/auth/commands.rss")), + (ENTRY_PATH, include_str!("../../rss/auth/commands.rss")), ( "rss/config/selection.rss", - include_str!("../rss/config/selection.rss"), + include_str!("../../rss/config/selection.rss"), + ), + ( + "rss/auth/login.rss", + include_str!("../../rss/auth/login.rss"), ), - ("rss/auth/login.rss", include_str!("../rss/auth/login.rss")), ( "rss/auth/pkce_login.rss", - include_str!("../rss/auth/pkce_login.rss"), + include_str!("../../rss/auth/pkce_login.rss"), ), ( "rss/auth/pkce_instruction_session.rss", - include_str!("../rss/auth/pkce_instruction_session.rss"), + include_str!("../../rss/auth/pkce_instruction_session.rss"), ), ( "rss/auth/pkce_session.rss", - include_str!("../rss/auth/pkce_session.rss"), + include_str!("../../rss/auth/pkce_session.rss"), ), ( "rss/auth/token_metadata.rss", - include_str!("../rss/auth/token_metadata.rss"), + include_str!("../../rss/auth/token_metadata.rss"), ), ( "rss/auth/token_save.rss", - include_str!("../rss/auth/token_save.rss"), + include_str!("../../rss/auth/token_save.rss"), ), ( "rss/auth/codex_device.rss", - include_str!("../rss/auth/codex_device.rss"), + include_str!("../../rss/auth/codex_device.rss"), + ), + ( + "rss/auth/types.rss", + include_str!("../../rss/auth/types.rss"), ), - ("rss/auth/types.rss", include_str!("../rss/auth/types.rss")), ]; const LOGIN_ACCESS: &str = "SYNTHETIC_UNIFIED_ACCESS_TOKEN"; @@ -187,13 +193,19 @@ fn command( } } +fn command_runner() -> &'static AuthEntryRunner { + static RUNNER: std::sync::OnceLock = std::sync::OnceLock::new(); + RUNNER.get_or_init(|| { + AuthEntryRunner::from_bundle(ENTRY_PATH, SOURCES).expect("compile checked-in RSS bundle") + }) +} + fn run_command_with_cancellation( + runner: &AuthEntryRunner, home: &Path, input: AuthCommandInput, cancellation: RunCancellation, ) -> Result { - let runner = - AuthEntryRunner::from_bundle(ENTRY_PATH, SOURCES).expect("compile checked-in RSS bundle"); runner.run_with_host_factory(input, &cancellation, { let home = home.to_path_buf(); move |child| AuthHost::new_for_commands(home, child) @@ -205,6 +217,7 @@ fn run_command( input: AuthCommandInput, ) -> Result { run_command_with_cancellation( + command_runner(), home, input, RunCancellation::with_timeout(Duration::from_secs(10)), @@ -924,6 +937,7 @@ fn cancelled_deadline_and_corrupt_auth_inputs_fail_closed_without_artifacts() { }; let result = run_command_with_cancellation( + command_runner(), &home.0, command("auth.logout", None, Some("fixture-codex")), cancellation, diff --git a/tests/auth_cli_ui_tests.rs b/tests/cases/auth_cli_ui_tests.rs similarity index 98% rename from tests/auth_cli_ui_tests.rs rename to tests/cases/auth_cli_ui_tests.rs index fbc4d20..75dc3dc 100644 --- a/tests/auth_cli_ui_tests.rs +++ b/tests/cases/auth_cli_ui_tests.rs @@ -27,27 +27,12 @@ use std::{ time::{Duration, Instant, SystemTime, UNIX_EPOCH}, }; -const RSS_SOURCE: &str = r#" -use auth; -use config; -use oauth; - -pub fn snapshot(home: resource) -> resource { - config::load_snapshot(&home).policy_handle -} - +const TIMING_SOURCE: &str = include_str!("../../rss/auth/timing_contract.rss"); +const UI_PROBE: &str = r#" pub fn emit_instruction(verification_url: string, user_code: string, expires_in_seconds: int) -> DeviceInstructionResultEnvelope { oauth::emit_device_instruction_result(verification_url, user_code, expires_in_seconds) } -pub fn now_result(policy: resource) -> OAuthNowResultEnvelope { - oauth::now_ms_result(&policy) -} - -pub fn wait_result(policy: resource, millis: int) -> OAuthWaitResultEnvelope { - oauth::wait_ms_result(&policy, millis) -} - pub fn send_result(policy: resource) -> TransportResultEnvelope { let request = { policy_handle: policy, @@ -57,7 +42,7 @@ pub fn send_result(policy: resource) -> TransportResultEnvelope { public_headers: [], public_body: "" }; - let usage = {kind: "none"}; + let usage: TransportCredentialUse = {kind: "none"}; oauth::transport_result(&request, &usage) } "#; @@ -106,7 +91,7 @@ fn registered_vm(auth: &AuthHost) -> Vm { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/auth/timing_contract.rss"); let compiled = compile_source_at_path_with_flavor_and_options( &path, - RSS_SOURCE, + &[TIMING_SOURCE, UI_PROBE].concat(), SourceFlavor::RustScript, CompileSourceFileOptions::default().with_host_api_catalog(Arc::clone(&catalog)), ) @@ -132,13 +117,17 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { let Value::Map(entries) = value else { panic!("expected typed RSS record, got {value:?}"); }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing {name} in {value:?}")) + .unwrap_or_else(|| panic!("missing {name} in {value:?}")); + match name { + "error" | "value" => crate::typed_carriers::nullable(field), + _ => field, + } } fn map_keys(value: &Value) -> Vec { @@ -197,6 +186,8 @@ fn assert_device_result(value: &Value, ok: bool, code: Option<&str>) { "code".to_owned(), "deadline_exceeded".to_owned(), "revoked".to_owned(), + "transport_category".to_owned(), + "transport_phase".to_owned(), ] ); assert_eq!(text(field(error, "code")), code); diff --git a/tests/auth_command_host_tests.rs b/tests/cases/auth_command_host_tests.rs similarity index 98% rename from tests/auth_command_host_tests.rs rename to tests/cases/auth_command_host_tests.rs index 84cf23d..9190b67 100644 --- a/tests/auth_command_host_tests.rs +++ b/tests/cases/auth_command_host_tests.rs @@ -1,3 +1,5 @@ +use crate::credential_use; + use rustscript_agent::{ CancellationReason, RunCancellation, auth::{ @@ -206,13 +208,19 @@ fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected named RSS record, got {value:?}"); }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value.clone()), + Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing field {name} in {value:?}")) + .unwrap_or_else(|| panic!("missing field {name} in {value:?}")); + let value = match name { + "error" | "value" | "code_handle" | "verifier_handle" | "device_handle" | "access_slot" + | "auth" => crate::typed_carriers::nullable(field), + _ => field, + }; + value.clone() } fn string(value: &Value) -> &str { @@ -509,7 +517,7 @@ fn commands_snapshot_saves_a_generation_zero_native_flow_and_reload_stays_frozen "/api/accounts/deviceauth/usercode", r#"{"client_id":"public-client-id"}"#, ), - record(vec![("kind", Value::string("none"))]), + credential_use::none("none"), ], ); assert_eq!(field(&started, "status"), Value::Int(200)); @@ -531,10 +539,7 @@ fn commands_snapshot_saves_a_generation_zero_native_flow_and_reload_stays_frozen "/oauth/token", r#"{"grant_type":"device_code"}"#, ), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -566,11 +571,10 @@ fn commands_snapshot_saves_a_generation_zero_native_flow_and_reload_stays_frozen "/oauth/token", r#"{"grant_type":"authorization_code"}"#, ), - record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code), - ("verifier_handle", verifier), - ]), + credential_use::with_handles( + "code_verifier", + &[("code_handle", code), ("verifier_handle", verifier)], + ), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("authorization_code")), @@ -594,10 +598,15 @@ fn commands_snapshot_saves_a_generation_zero_native_flow_and_reload_stays_frozen ("token_type", Value::string("Bearer")), ("expires_at_ms", Value::Int(1_900_000_000_000)), ("scopes", Value::array(vec![])), - ("account_id", Value::string("synthetic-command-account")), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string( + "synthetic-command-account", + )), + ), ("generation", Value::Int(0)), ("status", Value::string("active")), - ("last_refresh_at_ms", Value::Null), + ("last_refresh_at_ms", rustscript_vm::option::none_carrier()), ("has_refresh_token", Value::Bool(true)), ]), ), diff --git a/tests/auth_enrollment_host_tests.rs b/tests/cases/auth_enrollment_host_tests.rs similarity index 93% rename from tests/auth_enrollment_host_tests.rs rename to tests/cases/auth_enrollment_host_tests.rs index 280fbb9..3866754 100644 --- a/tests/auth_enrollment_host_tests.rs +++ b/tests/cases/auth_enrollment_host_tests.rs @@ -1,3 +1,5 @@ +use crate::credential_use; + use rustscript_agent::{ RunCancellation, auth::{ @@ -63,6 +65,17 @@ impl Drop for FixtureHome { let _ = fs::remove_dir_all(&self.0); } } +/// One converted `Option>` slot: asserts the canonical +/// `{tag, payload}` carrier the host publishes and returns its payload — the live +/// `Value::Int` resource handle. +fn some_slot(slot: impl std::borrow::Borrow) -> Value { + let slot = slot.borrow(); + let (some, payload) = rustscript_vm::option::payload(slot) + .unwrap_or_else(|error| panic!("slot is not a canonical Option carrier: {error}")); + assert!(some, "expected a Some carrier, got {slot:?}"); + payload.cloned().expect("Some carries a payload") +} + fn cancellation() -> RunCancellation { RunCancellation::with_timeout(Duration::from_secs(30)) } @@ -137,13 +150,18 @@ fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected named record: {value:?}"); }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value.clone()), + Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing field {name}")) + .unwrap_or_else(|| panic!("missing field {name}")); + let value = match name { + "auth" => crate::typed_carriers::nullable(field), + _ => field, + }; + value.clone() } fn record(fields: Vec<(&str, Value)>) -> Value { Value::map( @@ -287,12 +305,12 @@ fn exchange( "transport", &[ request(policy, TARGET, r#"{"client_id":"public-client-id"}"#), - record(vec![("kind", Value::string("none"))]), + credential_use::none("none"), ], ); assert_public(&started); assert_eq!(field(&started, "status"), Value::Int(200)); - let device = field(&started, "device_handle"); + let device = some_slot(field(&started, "device_handle")); assert!(matches!(device, Value::Int(_))); assert_eq!(vm.host_context().resource_count(), roots + 1); native_count(auth, 1); @@ -315,10 +333,7 @@ fn exchange( "capture", &[ request(policy, TARGET, r#"{"grant_type":"device_code"}"#), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -338,8 +353,14 @@ fn exchange( field(&field(&captured, "response"), "body_without_secret_fields"), Value::string(r#"{"public":"captured"}"#) ); - assert!(matches!(field(&captured, "code_handle"), Value::Int(_))); - assert!(matches!(field(&captured, "verifier_handle"), Value::Int(_))); + assert!(matches!( + some_slot(field(&captured, "code_handle")), + Value::Int(_) + )); + assert!(matches!( + some_slot(field(&captured, "verifier_handle")), + Value::Int(_) + )); assert_eq!(vm.host_context().resource_count(), roots + 2); native_count(auth, 1); let sent = transport.take_sent(); @@ -363,11 +384,16 @@ fn exchange( "exchange_selected", &[ request(policy, TARGET, r#"{"grant_type":"authorization_code"}"#), - record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", field(&captured, "code_handle")), - ("verifier_handle", field(&captured, "verifier_handle")), - ]), + credential_use::with_handles( + "code_verifier", + &[ + ("code_handle", some_slot(field(&captured, "code_handle"))), + ( + "verifier_handle", + some_slot(field(&captured, "verifier_handle")), + ), + ], + ), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("authorization_code")), @@ -382,9 +408,15 @@ fn exchange( field(&exchanged, "body_without_secret_fields"), Value::string(r#"{"expires_in":3600,"public":"exchanged","token_type":"Bearer"}"#) ); - assert!(matches!(field(&exchanged, "access_slot"), Value::Int(_))); + assert!(matches!( + some_slot(field(&exchanged, "access_slot")), + Value::Int(_) + )); assert_eq!( - matches!(field(&exchanged, "refresh_slot"), Value::Int(_)), + matches!( + crate::typed_carriers::nullable(&field(&exchanged, "refresh_slot")), + Value::Int(_) + ), refresh ); assert_eq!( @@ -416,14 +448,19 @@ fn save_request(policy: &Value, exchanged: &Value, expected: i64, refresh: bool) ("token_type", Value::string("Bearer")), ("expires_at_ms", Value::Int(1_900_000_000_000)), ("scopes", Value::array(vec![])), - ("account_id", Value::string("synthetic-enrollment-account")), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string( + "synthetic-enrollment-account", + )), + ), ("generation", Value::Int(expected)), ("status", Value::string("active")), - ("last_refresh_at_ms", Value::Null), + ("last_refresh_at_ms", rustscript_vm::option::none_carrier()), ("has_refresh_token", Value::Bool(refresh)), ]), ), - ("access_slot", field(exchanged, "access_slot")), + ("access_slot", some_slot(field(exchanged, "access_slot"))), ( "refresh_action", Value::string(if refresh { "replace" } else { "clear" }), @@ -616,10 +653,7 @@ fn enrollment_denies_other_valid_credential_without_damaging_target_slots_or_sto ), ( "transport", - vec![ - request(&policy, OTHER, "{}"), - record(vec![("kind", Value::string("none"))]), - ], + vec![request(&policy, OTHER, "{}"), credential_use::none("none")], ), ] { let failure = call_result(&mut vm, export, &args).expect_err("single credential policy"); diff --git a/tests/auth_fallible_bridge_tests.rs b/tests/cases/auth_fallible_bridge_tests.rs similarity index 94% rename from tests/auth_fallible_bridge_tests.rs rename to tests/cases/auth_fallible_bridge_tests.rs index 710472c..8a3bb79 100644 --- a/tests/auth_fallible_bridge_tests.rs +++ b/tests/cases/auth_fallible_bridge_tests.rs @@ -1,3 +1,6 @@ +#[path = "../support/credential_use.rs"] +mod credential_use; + use rustscript_agent::{ RunCancellation, auth::{ @@ -63,13 +66,19 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { let Value::Map(fields) = value else { panic!("expected RSS record") }; - fields + let field = fields .iter() .find_map(|(key, value)| match key { Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing RSS field {name}")) + .unwrap_or_else(|| panic!("missing RSS field {name}")); + match name { + "value" | "error" | "code_handle" | "device_handle" | "access_slot" | "refresh_slot" => { + crate::typed_carriers::nullable(field) + } + _ => field, + } } fn record(fields: Vec<(&str, Value)>) -> Value { @@ -186,10 +195,7 @@ fn native_result_slots(vm: &mut Vm, policy: &Value) -> Value { r#"{"grant_type":"device_code"}"#, "/api/accounts/deviceauth/token", ), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -207,17 +213,18 @@ fn native_result_slots(vm: &mut Vm, policy: &Value) -> Value { .expect("native capture result"); assert_eq!(field(&captured, "ok"), &Value::Bool(true)); let code = field(field(&captured, "value"), "code_handle").clone(); - let verifier = field(field(&captured, "value"), "verifier_handle").clone(); + let verifier = + crate::typed_carriers::nullable(field(field(&captured, "value"), "verifier_handle")) + .clone(); let exchanged = invoke( vm, "exchange_selected_result", &[ request(r#"{"grant_type":"authorization_code"}"#, "/oauth/token"), - record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code), - ("verifier_handle", verifier), - ]), + credential_use::with_handles( + "code_verifier", + &[("code_handle", code), ("verifier_handle", verifier)], + ), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("authorization_code")), @@ -278,10 +285,7 @@ fn native_capture_args(policy: &Value, device_handle: Value) -> [Value; 4] { r#"{"grant_type":"device_code"}"#, "/api/accounts/deviceauth/token", ), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device_handle), - ]), + credential_use::with_handles("device_session", &[("device_handle", device_handle)]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -325,7 +329,7 @@ fn actual_malformed_transport_is_readable_but_legacy_call_still_traps() { invoke( &mut vm, "transport_result", - &[forged_policy, record(vec![("kind", Value::string("none"))])], + &[forged_policy, credential_use::none("none")], ) .is_err(), "Core resource validation remains a trap" @@ -336,7 +340,7 @@ fn actual_malformed_transport_is_readable_but_legacy_call_still_traps() { ); assert_eq!(vm.host_context().resource_count(), 2); let request = request(&policy); - let usage = record(vec![("kind", Value::string("none"))]); + let usage = credential_use::none("none"); let readable = invoke( &mut vm, @@ -394,7 +398,7 @@ fn rss_cleanup_after_failure_closes_only_the_native_owner_access_handle() { let unrelated = issue_unrelated(&mut vm); assert_eq!(vm.host_context().resource_count(), 4); let request = request(&policy); - let usage = record(vec![("kind", Value::string("none"))]); + let usage = credential_use::none("none"); let continued = invoke( &mut vm, @@ -594,10 +598,7 @@ fn cleanup_is_vm_owner_bound_and_closes_pairs_and_unsaved_slots() { r#"{"grant_type":"device_code"}"#, "/api/accounts/deviceauth/token", ), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device_b), - ]), + credential_use::with_handles("device_session", &[("device_handle", device_b)]), payload(), capture_intent(), ], @@ -605,7 +606,9 @@ fn cleanup_is_vm_owner_bound_and_closes_pairs_and_unsaved_slots() { .expect("owner B native pair after owner A cleanup"); assert_eq!(field(&captured_b, "ok"), &Value::Bool(true)); let code_b = field(field(&captured_b, "value"), "code_handle").clone(); - let verifier_b = field(field(&captured_b, "value"), "verifier_handle").clone(); + let verifier_b = + crate::typed_carriers::nullable(field(field(&captured_b, "value"), "verifier_handle")) + .clone(); assert_eq!(vm_b.host_context().resource_count(), 4); assert_eq!( invoke(&mut vm_b, "cleanup", std::slice::from_ref(&policy_b)).expect("cleanup pair"), @@ -630,11 +633,10 @@ fn cleanup_is_vm_owner_bound_and_closes_pairs_and_unsaved_slots() { r#"{"grant_type":"authorization_code"}"#, "/oauth/token", ), - record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code_b), - ("verifier_handle", verifier_b), - ]), + credential_use::with_handles( + "code_verifier", + &[("code_handle", code_b), ("verifier_handle", verifier_b),] + ), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("authorization_code")), @@ -661,13 +663,13 @@ fn cleanup_is_vm_owner_bound_and_closes_pairs_and_unsaved_slots() { r#"{"grant_type":"device_code"}"#, "/api/accounts/deviceauth/token", ), - record(vec![ - ("kind", Value::string("device_session")), - ( + credential_use::with_handles( + "device_session", + &[( "device_handle", field(field(&started_again, "value"), "device_handle").clone(), - ), - ]), + )], + ), payload(), capture_intent(), ], @@ -682,17 +684,23 @@ fn cleanup_is_vm_owner_bound_and_closes_pairs_and_unsaved_slots() { r#"{"grant_type":"authorization_code"}"#, "/oauth/token", ), - record(vec![ - ("kind", Value::string("code_verifier")), - ( - "code_handle", - field(field(&captured_again, "value"), "code_handle").clone(), - ), - ( - "verifier_handle", - field(field(&captured_again, "value"), "verifier_handle").clone(), - ), - ]), + credential_use::with_handles( + "code_verifier", + &[ + ( + "code_handle", + field(field(&captured_again, "value"), "code_handle").clone(), + ), + ( + "verifier_handle", + crate::typed_carriers::nullable(field( + field(&captured_again, "value"), + "verifier_handle", + )) + .clone(), + ), + ], + ), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("authorization_code")), @@ -1123,10 +1131,13 @@ fn native_result_cas_conflict_and_adoption_preserve_winner() { "scopes", Value::array(vec![Value::string("scope.synthetic")]), ), - ("account_id", Value::string("native-cas-account")), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string("native-cas-account")), + ), ("generation", Value::Int(expected)), ("status", Value::string("active")), - ("last_refresh_at_ms", Value::Null), + ("last_refresh_at_ms", rustscript_vm::option::none_carrier()), ("has_refresh_token", Value::Bool(false)), ]); record(vec![ @@ -1136,7 +1147,7 @@ fn native_result_cas_conflict_and_adoption_preserve_winner() { ("metadata", metadata), ("access_slot", field(slots, "access_slot").clone()), ("refresh_action", Value::string("clear")), - ("refresh_slot", Value::Null), + ("refresh_slot", rustscript_vm::option::none_carrier()), ]) }; @@ -1321,10 +1332,7 @@ fn native_capture_exchange_and_save_result_commits_generation_zero_once() { r#"{"grant_type":"device_code"}"#, "/api/accounts/deviceauth/token", ), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -1343,18 +1351,21 @@ fn native_capture_exchange_and_save_result_commits_generation_zero_once() { assert_eq!(field(&captured, "ok"), &Value::Bool(true)); let captured_value = field(&captured, "value"); let code = field(captured_value, "code_handle").clone(); - let verifier = field(captured_value, "verifier_handle").clone(); + let verifier = + crate::typed_carriers::nullable(field(captured_value, "verifier_handle")).clone(); let exchanged = invoke( &mut vm, "exchange_selected_result", &[ request(r#"{"grant_type":"authorization_code"}"#, "/oauth/token"), - record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code.clone()), - ("verifier_handle", verifier.clone()), - ]), + credential_use::with_handles( + "code_verifier", + &[ + ("code_handle", code.clone()), + ("verifier_handle", verifier.clone()), + ], + ), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("authorization_code")), @@ -1387,10 +1398,13 @@ fn native_capture_exchange_and_save_result_commits_generation_zero_once() { "scopes", Value::array(vec![Value::string("scope.synthetic")]), ), - ("account_id", Value::string("rss-native-account")), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string("rss-native-account")), + ), ("generation", Value::Int(0)), ("status", Value::string("active")), - ("last_refresh_at_ms", Value::Null), + ("last_refresh_at_ms", rustscript_vm::option::none_carrier()), ("has_refresh_token", Value::Bool(include_refresh)), ]); let save_request = record(vec![ @@ -1403,7 +1417,14 @@ fn native_capture_exchange_and_save_result_commits_generation_zero_once() { "refresh_action", Value::string(if include_refresh { "replace" } else { "clear" }), ), - ("refresh_slot", refresh_slot.clone()), + ( + "refresh_slot", + if include_refresh { + rustscript_vm::option::some_carrier(refresh_slot.clone()) + } else { + rustscript_vm::option::none_carrier() + }, + ), ]); let saved = invoke(&mut vm, "save_result", std::slice::from_ref(&save_request)) .expect("native RSS CAS result"); @@ -1468,11 +1489,10 @@ fn native_capture_exchange_and_save_result_commits_generation_zero_once() { "exchange_selected_result", &[ request(r#"{"grant_type":"authorization_code"}"#, "/oauth/token"), - record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code), - ("verifier_handle", verifier), - ]), + credential_use::with_handles( + "code_verifier", + &[("code_handle", code), ("verifier_handle", verifier),] + ), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("authorization_code")), @@ -1698,7 +1718,8 @@ fn native_selected_exchange_malformed_response_returns_null_and_rolls_back_capab assert_eq!(field(&capture, "ok"), &Value::Bool(true)); let captured_value = field(&capture, "value"); let code = field(captured_value, "code_handle").clone(); - let verifier = field(captured_value, "verifier_handle").clone(); + let verifier = + crate::typed_carriers::nullable(field(captured_value, "verifier_handle")).clone(); let capture_sent = transport.take_sent(); assert_eq!(capture_sent.len(), 1, "native capture sends once"); assert_eq!( @@ -1716,11 +1737,13 @@ fn native_selected_exchange_malformed_response_returns_null_and_rolls_back_capab r#"{"grant_type":"authorization_code"}"#, "/oauth/token", ); - let exchange_use = record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code.clone()), - ("verifier_handle", verifier.clone()), - ]); + let exchange_use = credential_use::with_handles( + "code_verifier", + &[ + ("code_handle", code.clone()), + ("verifier_handle", verifier.clone()), + ], + ); let exchange_payload = record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("authorization_code")), diff --git a/tests/auth_public_json_tests.rs b/tests/cases/auth_public_json_tests.rs similarity index 92% rename from tests/auth_public_json_tests.rs rename to tests/cases/auth_public_json_tests.rs index 290cd06..19cc850 100644 --- a/tests/auth_public_json_tests.rs +++ b/tests/cases/auth_public_json_tests.rs @@ -17,10 +17,19 @@ use std::sync::Arc; use std::time::Duration; const AUTH_ENTRY_TRANSPORT_PROOF_RSS: &str = r#" -pub fn run(home: resource, command: map) -> map { +use config; +use oauth; + +struct AuthCommandInput { operation: string, provider: Option, credential_id: Option } +struct AuthEntryResult { + ok: bool, operation: string, credential_id: Option, generation: int, + status: string, error: Option +} + +pub fn run(home: resource, command: AuthCommandInput) -> AuthEntryResult { let snapshot_result = config::load_snapshot(&home); let policy_generation = snapshot_result.policy_generation; - let request = { + let request: TransportPublicRequest = { policy_handle: snapshot_result.policy_handle, credential_id: "fixture-codex", method: "GET", @@ -28,7 +37,10 @@ pub fn run(home: resource, command: map) -> map { public_headers: [{name: "accept", value: "application/json"}], public_body: "" }; - let usage = {kind: "none"}; + let usage: TransportCredentialUse = { + kind: "none", access_handle: None, refresh_handle: None, code_handle: None, + verifier_handle: None, device_handle: None + }; let response = oauth::transport(&request, &usage); let body = response.body_without_secret_fields; let field_policy = config::load_snapshot(&home).policy_handle; @@ -38,16 +50,20 @@ pub fn run(home: resource, command: map) -> map { let refresh_token_field = oauth::public_json_field(&field_policy, body, "/refresh_token"); let sanitized_secrets_missing = access_token_field.kind == "missing" && refresh_token_field.kind == "missing"; - if public_name.string_value.copy() != null && public_generation.int_value.copy() != null && sanitized_secrets_missing => { - let name: string = public_name.string_value; - let generation: int = public_generation.int_value; + let name_opt: Option = public_name.string_value; + let generation_opt: Option = public_generation.int_value; + let name_present: bool = match name_opt.copy() { Some(_) => true, None => false }; + let generation_present: bool = match generation_opt.copy() { Some(_) => true, None => false }; + if name_present && generation_present && sanitized_secrets_missing => { + let name: string = match name_opt { Some(value) => value, None => "" }; + let generation: int = match generation_opt { Some(value) => value, None => 0 }; { ok: response.ok && sanitized_secrets_missing, operation: command.operation, credential_id: command.credential_id, generation: generation, status: name, - error: null + error: None } } else => { { @@ -56,7 +72,7 @@ pub fn run(home: resource, command: map) -> map { credential_id: command.credential_id, generation: policy_generation, status: "public_fields_missing", - error: "public_fields_missing" + error: Some("public_fields_missing") } } } @@ -111,13 +127,19 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { let Value::Map(entries) = value else { panic!("expected result map, got {value:?}"); }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing result field {name}")) + .unwrap_or_else(|| panic!("missing result field {name}")); + match name { + "bool_value" | "float_value" | "int_value" | "length" | "string_value" => { + crate::typed_carriers::nullable(field) + } + _ => field, + } } fn assert_envelope_shape(value: &Value) { @@ -146,7 +168,7 @@ fn assert_envelope_shape(value: &Value) { } fn bound_vm(home: &FixtureHome) -> (Vm, AuthHost, Value, Value) { - let source = include_str!("../rss/auth/public_json_contract.rss"); + let source = include_str!("../../rss/auth/public_json_contract.rss"); let catalog = auth_host_catalog(); let compiled = compile_source_with_flavor_and_options( source, @@ -179,7 +201,7 @@ fn bound_vm(home: &FixtureHome) -> (Vm, AuthHost, Value, Value) { } fn vm_with_public_json_policy(auth: &AuthHost) -> (Vm, Value) { - let source = include_str!("../rss/auth/public_json_contract.rss"); + let source = include_str!("../../rss/auth/public_json_contract.rss"); let catalog = auth_host_catalog(); let compiled = compile_source_with_flavor_and_options( source, @@ -210,7 +232,7 @@ fn vm_with_public_json_policy(auth: &AuthHost) -> (Vm, Value) { fn runner_reads_nested_sanitized_public_json_fields() { let source = format!( "{}\n{}", - include_str!("../rss/auth/public_json_contract.rss"), + include_str!("../../rss/auth/public_json_contract.rss"), AUTH_ENTRY_TRANSPORT_PROOF_RSS ); compile_source_with_flavor_and_options( @@ -878,7 +900,9 @@ fn auth_entry_runner_denies_non_auth_import_roots() { #[test] fn restricted_auth_binding_does_not_authorize_standard_json_builtin() { - let source = "use json; pub fn invoke() -> string { json::encode(1) }"; + let source = "use json; use config;\n\ + pub fn allowed(home: resource) -> bool { config::load_snapshot(&home).ok }\n\ + pub fn invoke(value: int) -> string { json::encode(value) }"; let home = FixtureHome::new(); let (_base_vm, auth, _policy, _host_home) = bound_vm(&home); let catalog = auth_host_catalog(); @@ -897,14 +921,25 @@ fn restricted_auth_binding_does_not_authorize_standard_json_builtin() { for descriptor in auth_host_module().descriptors() { registry.authorize_registered_builtin_import(&descriptor.schema.name); } - let rejected = registry.bind_vm_cached(&mut vm).is_err() - || vm.run().is_err() - || vm - .resolve_exported_callable("invoke") - .map(|callable| vm.invoke_callable(callable, &[]).is_err()) - .unwrap_or(true); + registry + .bind_vm_cached(&mut vm) + .expect("bind exact auth registry"); + let bound = auth.bind_vm(&mut vm).expect("bind auth owner"); + vm.run().expect("run the root frame"); + let allowed = vm + .resolve_exported_callable("allowed") + .expect("allowed export"); + assert_eq!( + vm.invoke_callable(allowed, &[field(&bound, "host_home").clone()]) + .expect("allowed auth host import"), + Value::Bool(true) + ); + let invoke = vm.resolve_exported_callable("invoke").expect("JSON export"); + let rejected = vm + .invoke_callable(invoke, &[Value::Int(1)]) + .expect_err("standard JSON encoder must remain unavailable"); assert!( - rejected, - "standard json builtin must remain unavailable to the restricted auth registry" + rejected.to_string().contains("auth_json_builtin_denied"), + "expected the restricted binding refusal, got {rejected}" ); } diff --git a/tests/auth_refresh_result_rss_tests.rs b/tests/cases/auth_refresh_result_rss_tests.rs similarity index 93% rename from tests/auth_refresh_result_rss_tests.rs rename to tests/cases/auth_refresh_result_rss_tests.rs index c770ae4..e6e9e01 100644 --- a/tests/auth_refresh_result_rss_tests.rs +++ b/tests/cases/auth_refresh_result_rss_tests.rs @@ -70,6 +70,14 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { .unwrap_or_else(|| panic!("missing RSS field {name} in {value:?}")) } +fn option_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { + match rustscript_vm::option::payload(field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(payload)) if !matches!(payload, Value::Null) => Some(payload), + _ => panic!("expected a non-Unit RSS Option for {name}"), + } +} + fn fixture_vm(host: &AuthHost, catalog: &rustscript_vm::HostApiCatalog) -> Vm { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/refresh_result/main.rss"); @@ -145,9 +153,12 @@ fn genuine_refresh_issuance_denials_and_publication_are_typed() { ) .expect("unadmitted issuance is a guest-readable result"); assert_eq!(field(&unadmitted, "ok"), &Value::Bool(false)); - assert!(matches!(field(&unadmitted, "value"), Value::Null)); + assert!(option_field(&unadmitted, "value").is_none()); assert_eq!( - field(field(&unadmitted, "error"), "code"), + field( + option_field(&unadmitted, "error").expect("error is present"), + "code" + ), &Value::string("credential_not_admitted") ); assert!( @@ -167,9 +178,12 @@ fn genuine_refresh_issuance_denials_and_publication_are_typed() { ) .expect("wrong generation is a guest-readable result"); assert_eq!(field(&wrong_generation, "ok"), &Value::Bool(false)); - assert!(matches!(field(&wrong_generation, "value"), Value::Null)); + assert!(option_field(&wrong_generation, "value").is_none()); assert_eq!( - field(field(&wrong_generation, "error"), "code"), + field( + option_field(&wrong_generation, "error").expect("error is present"), + "code" + ), &Value::string("refresh_unavailable") ); assert!( diff --git a/tests/auth_request_body_tests.rs b/tests/cases/auth_request_body_tests.rs similarity index 100% rename from tests/auth_request_body_tests.rs rename to tests/cases/auth_request_body_tests.rs diff --git a/tests/auth_resource_host_tests.rs b/tests/cases/auth_resource_host_tests.rs similarity index 100% rename from tests/auth_resource_host_tests.rs rename to tests/cases/auth_resource_host_tests.rs diff --git a/tests/auth_sanitized_stream_rss_tests.rs b/tests/cases/auth_sanitized_stream_rss_tests.rs similarity index 96% rename from tests/auth_sanitized_stream_rss_tests.rs rename to tests/cases/auth_sanitized_stream_rss_tests.rs index 1026e6c..806e3a6 100644 --- a/tests/auth_sanitized_stream_rss_tests.rs +++ b/tests/cases/auth_sanitized_stream_rss_tests.rs @@ -204,6 +204,14 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { .unwrap_or_else(|| panic!("missing RSS field {name} in {value:?}")) } +fn option_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { + match rustscript_vm::option::payload(field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(payload)) if !matches!(payload, Value::Null) => Some(payload), + _ => panic!("expected a non-Unit RSS Option for {name}"), + } +} + fn fixture_vm(host: &AuthHost, catalog: &rustscript_vm::HostApiCatalog) -> Vm { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("tests/fixtures/auth_sanitized_stream/main.rss"); @@ -369,7 +377,9 @@ fn guest_stream_orders_literal_json_then_terminal_eof() { ) .expect("open typed stream"); assert_eq!(field(&opened, "ok"), &Value::Bool(true), "{opened:?}"); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); assert!( !matches!(stream, Value::Null), "stream resource is published" @@ -436,7 +446,9 @@ fn guest_stream_terminal_eof_survives_owner_cancellation() { &start_args(&policy, markers(&[])), ) .expect("open typed stream"); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); let first = invoke( &mut vm, "stream_read_one_result", @@ -481,7 +493,9 @@ fn guest_stream_cancellation_cannot_publish_buffered_records() { &start_args(&policy, markers(&[])), ) .expect("open typed stream"); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); let first = invoke( &mut vm, "stream_read_one_result", @@ -559,7 +573,9 @@ fn guest_stream_fails_closed_on_malformed_non_json_and_read_error() { &start_args(&policy, markers(&["DONE"])), ) .expect("open typed stream"); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); let read = invoke(&mut vm, "stream_read_one_result", &[stream]).expect("read one"); assert_eq!( field(&read, "ok"), @@ -620,7 +636,9 @@ fn guest_stream_unsupported_mime_fails_read_without_reading_body() { .expect("open typed stream"); assert_eq!(field(&opened, "status"), &Value::Int(500)); assert_eq!(field(&opened, "content_type"), &Value::string("")); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); let read = invoke(&mut vm, "stream_read_one_result", &[stream]).expect("read one"); assert_eq!(field(&read, "ok"), &Value::Bool(false)); assert_eq!(field(&read, "code"), &Value::string("malformed_response")); @@ -724,7 +742,9 @@ fn guest_stream_close_releases_reader_and_stale_handle_is_rejected() { &start_args(&policy, markers(&[])), ) .expect("open typed stream"); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); let baseline = vm.host_context().resource_count(); assert_eq!( invoke(&mut vm, "stream_close", std::slice::from_ref(&stream)).expect("close"), @@ -790,7 +810,9 @@ fn guest_stream_json_mime_error_body_is_sanitized() { field(&opened, "content_type"), &Value::string("application/json") ); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); let read = invoke(&mut vm, "stream_read_one_result", &[stream]).expect("read one"); assert_eq!(field(&read, "ok"), &Value::Bool(true), "{read:?}"); assert_eq!(field(&read, "kind"), &Value::string("json")); @@ -829,7 +851,9 @@ fn guest_stream_buffered_expiry_blocks_the_second_record() { &start_args(&policy, markers(&[])), ) .expect("open typed stream"); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); let first = invoke( &mut vm, "stream_read_one_result", @@ -873,7 +897,7 @@ fn guest_stream_vm_drop_releases_an_open_reader() { ) .expect("open typed stream"); assert!( - !matches!(field(&opened, "stream"), Value::Null), + option_field(&opened, "stream").is_some(), "a real open stream exists before the VM is dropped" ); drop(vm); @@ -959,7 +983,11 @@ fn guest_stream_publication_limit_is_unreachable_through_the_accepted_issuer() { ) .expect("quota step"); assert_eq!(field(&step, "ok"), &Value::Bool(true), "{step:?}"); - held.push(field(&step, "handle").clone()); + held.push( + option_field(&step, "handle") + .expect("issued access") + .clone(), + ); } assert_eq!( vm.host_context().resource_count(), @@ -1038,7 +1066,9 @@ fn guest_stream_same_vm_rebind_is_structurally_unreachable() { &start_args(&policy, markers(&[])), ) .expect("open typed stream"); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); let (second_transport, _second_state) = event_stream(200, vec![Injected::Eof]); let second = AuthHost::new( @@ -1084,7 +1114,9 @@ fn guest_stream_foreign_vm_rejects_handle_without_reading_or_closing_owner_strea ) .expect("open owner stream"); assert_eq!(field(&opened, "ok"), &Value::Bool(true)); - let stream = field(&opened, "stream").clone(); + let stream = option_field(&opened, "stream") + .expect("open stream") + .clone(); assert_eq!(locked(&state).reads, 0); let (foreign_transport, foreign_state) = event_stream(200, vec![Injected::Eof]); @@ -1249,7 +1281,9 @@ fn provider_request_body_beyond_provider_bound_is_rejected_and_keeps_access() { &Value::string("request_too_large"), "the rejection keeps its public code" ); - let handle = field(&probe, "handle").clone(); + let handle = option_field(&probe, "handle") + .expect("retained access") + .clone(); assert!( !matches!(handle, Value::Null), "a rejected provider body leaves the one-shot access handle live" diff --git a/tests/auth_store_rss_tests.rs b/tests/cases/auth_store_rss_tests.rs similarity index 100% rename from tests/auth_store_rss_tests.rs rename to tests/cases/auth_store_rss_tests.rs diff --git a/tests/auth_store_tests.rs b/tests/cases/auth_store_tests.rs similarity index 100% rename from tests/auth_store_tests.rs rename to tests/cases/auth_store_tests.rs diff --git a/tests/auth_transport_policy_tests.rs b/tests/cases/auth_transport_policy_tests.rs similarity index 100% rename from tests/auth_transport_policy_tests.rs rename to tests/cases/auth_transport_policy_tests.rs diff --git a/tests/authorization_instruction_rss_tests.rs b/tests/cases/authorization_instruction_rss_tests.rs similarity index 97% rename from tests/authorization_instruction_rss_tests.rs rename to tests/cases/authorization_instruction_rss_tests.rs index 0eb993f..553d514 100644 --- a/tests/authorization_instruction_rss_tests.rs +++ b/tests/cases/authorization_instruction_rss_tests.rs @@ -160,13 +160,29 @@ fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected typed RSS record"); }; - entries + let raw = entries .iter() .find_map(|(key, value)| match key { Value::String(key) if key.as_ref() == name => Some(value.clone()), _ => None, }) - .unwrap_or_else(|| panic!("missing typed RSS field {name}")) + .unwrap_or_else(|| panic!("missing typed RSS field {name}")); + if let Value::Map(carrier) = &raw { + let tag = carrier + .iter() + .find_map(|(key, value)| (key == &Value::string("tag")).then_some(value)); + if tag == Some(&Value::Bool(false)) { + return Value::Null; + } + if tag == Some(&Value::Bool(true)) + && let Some((_, payload)) = carrier + .iter() + .find(|(key, _)| *key == &Value::string("payload")) + { + return payload.clone(); + } + } + raw } fn string_field(value: &Value, name: &str) -> String { diff --git a/tests/bundled_agent_rss_entry_tests.rs b/tests/cases/bundled_agent_rss_entry_tests.rs similarity index 100% rename from tests/bundled_agent_rss_entry_tests.rs rename to tests/cases/bundled_agent_rss_entry_tests.rs diff --git a/tests/bundled_resource_runtime_tests.rs b/tests/cases/bundled_resource_runtime_tests.rs similarity index 97% rename from tests/bundled_resource_runtime_tests.rs rename to tests/cases/bundled_resource_runtime_tests.rs index 6ab5ba8..fa33e04 100644 --- a/tests/bundled_resource_runtime_tests.rs +++ b/tests/cases/bundled_resource_runtime_tests.rs @@ -259,21 +259,23 @@ fn bundled_default_dispatcher_runs_with_injected_native_auth() { let home = FixtureHome::new(); let transport = Arc::new(ScriptedHttpsTransport::new()); transport.push_json(200, canonical_chat_body()); - let cancellation = RunCancellation::with_timeout(Duration::from_secs(30)); - let bridges = AgentHostBridges { - auth_host_factory: Some(fixture_factory(&home, transport.clone())), - ..AgentHostBridges::default() - }; let runner = AgentRunner::from_bundled_named( "rss/llm/provider_dispatch.rss", "dispatch", AgentConfig::default(), ) - .expect("embedded provider dispatcher must compile") - .with_host(bridges) - .with_cancellation(cancellation.clone()); - + .expect("embedded provider dispatcher must compile"); + let factory = fixture_factory(&home, transport.clone()); let context = json_to_vm_value(&json!({ "request": public_request() })); + let cancellation = RunCancellation::with_timeout(Duration::from_secs(30)); + let deadline = cancellation.deadline_instant().expect("finite deadline"); + let runner = runner.with_host(AgentHostBridges { + auth_host_factory: Some(Arc::new(move |incoming| { + assert_eq!(incoming.deadline_instant(), Some(deadline)); + factory(incoming) + })), + ..AgentHostBridges::default() + }); let result = runner .run_named_with_context_and_events("dispatch", context, &mut DiscardSink, &cancellation) .unwrap_or_else(|error| panic!("bundled dispatch failed: {error:?}")); diff --git a/tests/callback_result_rss_tests.rs b/tests/cases/callback_result_rss_tests.rs similarity index 91% rename from tests/callback_result_rss_tests.rs rename to tests/cases/callback_result_rss_tests.rs index 6ea3249..5dbcb5e 100644 --- a/tests/callback_result_rss_tests.rs +++ b/tests/cases/callback_result_rss_tests.rs @@ -26,6 +26,17 @@ use rustscript_vm::{ }; use url::Url; +/// One converted `Option>` slot: asserts the canonical +/// `{tag, payload}` carrier the host publishes and returns its payload — the live +/// `Value::Int` resource handle. +fn some_slot(slot: impl std::borrow::Borrow) -> Value { + let slot = slot.borrow(); + let (some, payload) = rustscript_vm::option::payload(slot) + .unwrap_or_else(|error| panic!("slot is not a canonical Option carrier: {error}")); + assert!(some, "expected a Some carrier, got {slot:?}"); + payload.cloned().expect("Some carries a payload") +} + const NATIVE_CALLBACK_CODE: &str = "NATIVE_RSS_CALLBACK_CODE_MARKER"; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -194,10 +205,24 @@ fn callback_contract_with_rss_branch() -> String { source.push_str( r#" +struct BeginBranch { + branch: string, + first_ok: bool, + first_error: Option, + second_ok: bool, + second_error: Option +} + +struct WaitBranch { + branch: string, + wait_error: Option, + next_action: string +} + pub fn branch_after_begin_failure( home: resource, intent: BoundedPublicOAuthIntent -) -> map { +) -> BeginBranch { let snapshot: ConfigSnapshotEnvelope = config::load_snapshot(&home); let first: PkceBeginResultEnvelope = oauth::pkce_begin_result(&snapshot.policy_handle, intent); @@ -207,7 +232,7 @@ pub fn branch_after_begin_failure( first_ok: true, first_error: first.error, second_ok: true, - second_error: null + second_error: None } } else => { let unsupported: BoundedPublicOAuthIntent = { @@ -235,13 +260,13 @@ pub fn branch_after_begin_failure( pub fn branch_after_wait_failure( callback_handle: resource -) -> map { +) -> WaitBranch { let waited: CallbackResultEnvelope = oauth::callback_wait_result(&callback_handle); if waited.ok => { { branch: "wait_succeeded", - wait_error: null, + wait_error: None, next_action: "continue" } } else => { @@ -288,6 +313,14 @@ fn compiled_callback_rss() -> Vm { vm } +fn option_field(value: &Value, name: &str) -> Option { + match rustscript_vm::option::payload(&field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value.clone()), + _ => panic!("invalid Option payload for {name}"), + } +} + fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected named envelope") @@ -352,7 +385,10 @@ fn callback_results_allow_rss_branch_after_unavailable_begin() { .expect("typed PKCE begin failure is returned as data"); assert_eq!(field(&passthrough, "ok"), Value::Bool(false)); assert_eq!( - field(&field(&passthrough, "error"), "code"), + field( + &option_field(&passthrough, "error").expect("failure detail"), + "code" + ), Value::string("callback_unavailable") ); @@ -366,12 +402,18 @@ fn callback_results_allow_rss_branch_after_unavailable_begin() { assert_eq!(field(&result, "branch"), Value::string("after_error")); assert_eq!(field(&result, "first_ok"), Value::Bool(false)); assert_eq!( - field(&field(&result, "first_error"), "code"), + field( + &option_field(&result, "first_error").expect("failure detail"), + "code" + ), Value::string("callback_unavailable") ); assert_eq!(field(&result, "second_ok"), Value::Bool(false)); assert_eq!( - field(&field(&result, "second_error"), "code"), + field( + &option_field(&result, "second_error").expect("failure detail"), + "code" + ), Value::string("oauth_flow_unsupported") ); assert_eq!(vm.host_context().resource_count(), 2); @@ -415,7 +457,7 @@ fn native_callback_result_reports_malformed_and_mismatched_facts() { ) .expect("native PKCE begin result"); assert_eq!(field(&begun, "ok"), Value::Bool(true)); - let begin_value = field(&begun, "value"); + let begin_value = option_field(&begun, "value").expect("present value"); let callback = field(&begin_value, "callback_handle"); let redirect_uri = match field(&begin_value, "redirect_uri") { Value::String(value) => value.to_string(), @@ -435,7 +477,7 @@ fn native_callback_result_reports_malformed_and_mismatched_facts() { field(&result, "next_action"), Value::string("stop_and_report") ); - let failure = field(&result, "wait_error"); + let failure = option_field(&result, "wait_error").expect("present wait_error"); assert_eq!(field(&failure, "code"), Value::string(expected_code)); assert_eq!(field(&failure, "revoked"), Value::Bool(false)); assert_eq!(field(&failure, "cancelled"), Value::Bool(false)); @@ -473,7 +515,7 @@ fn native_callback_result_reports_cancellation_and_reclaims_partial_socket() { &[field(&owner, "host_home"), public_intent("browser")], ) .expect("native PKCE begin result"); - let begin_value = field(&begun, "value"); + let begin_value = option_field(&begun, "value").expect("present value"); let callback = field(&begin_value, "callback_handle"); let redirect_uri = match field(&begin_value, "redirect_uri") { Value::String(value) => value.to_string(), @@ -504,12 +546,12 @@ fn native_callback_result_reports_cancellation_and_reclaims_partial_socket() { assert!(cancelled_at.elapsed() <= Duration::from_millis(750)); let result = result.expect("cancellation returned as typed callback facts"); assert_eq!(field(&result, "ok"), Value::Bool(false)); - let failure = field(&result, "error"); + let failure = option_field(&result, "error").expect("present error"); assert_eq!(field(&failure, "code"), Value::string("callback_cancelled")); assert_eq!(field(&failure, "cancelled"), Value::Bool(true)); assert_eq!(field(&failure, "deadline_exceeded"), Value::Bool(false)); assert_eq!(field(&failure, "revoked"), Value::Bool(false)); - assert_eq!(field(&result, "value"), Value::Null); + assert!(option_field(&result, "value").is_none()); assert_eq!(resource_count, 2); drop(partial); assert_callback_port_released(&redirect_uri); @@ -539,7 +581,7 @@ fn native_callback_result_reports_deadline_and_reclaims_listener() { &[field(&owner, "host_home"), public_intent("browser")], ) .expect("native PKCE begin before deadline"); - let begin_value = field(&begun, "value"); + let begin_value = option_field(&begun, "value").expect("present value"); let callback = field(&begin_value, "callback_handle"); let redirect_uri = match field(&begin_value, "redirect_uri") { Value::String(value) => value.to_string(), @@ -552,7 +594,7 @@ fn native_callback_result_reports_deadline_and_reclaims_listener() { .invoke_callable(wait, std::slice::from_ref(&callback)) .expect("deadline returned as typed callback facts"); assert_eq!(field(&result, "ok"), Value::Bool(false)); - let failure = field(&result, "error"); + let failure = option_field(&result, "error").expect("present error"); assert_eq!(field(&failure, "code"), Value::string("callback_timeout")); assert_eq!(field(&failure, "deadline_exceeded"), Value::Bool(true)); assert_eq!(field(&failure, "cancelled"), Value::Bool(false)); @@ -585,7 +627,7 @@ fn native_callback_result_reports_explicit_revoke_without_leaking_flow() { &[field(&owner, "host_home"), public_intent("browser")], ) .expect("native PKCE begin result"); - let begin_value = field(&begun, "value"); + let begin_value = option_field(&begun, "value").expect("present value"); let callback = field(&begin_value, "callback_handle"); let redirect_uri = match field(&begin_value, "redirect_uri") { Value::String(value) => value.to_string(), @@ -606,7 +648,7 @@ fn native_callback_result_reports_explicit_revoke_without_leaking_flow() { field(&result, "next_action"), Value::string("stop_and_report") ); - let failure = field(&result, "wait_error"); + let failure = option_field(&result, "wait_error").expect("present wait_error"); assert_eq!(field(&failure, "code"), Value::string("policy_inactive")); assert_eq!(field(&failure, "revoked"), Value::Bool(true)); assert_eq!(field(&failure, "cancelled"), Value::Bool(false)); @@ -639,7 +681,7 @@ fn native_callback_result_vm_drop_releases_open_listener() { &[field(&owner, "host_home"), public_intent("browser")], ) .expect("native PKCE begin result"); - let begin_value = field(&begun, "value"); + let begin_value = option_field(&begun, "value").expect("present value"); let redirect_uri = match field(&begin_value, "redirect_uri") { Value::String(value) => value.to_string(), _ => panic!("native redirect URI must be public text"), @@ -696,7 +738,7 @@ fn native_callback_results_preserve_live_family() { "native callback begin dependency result: {:?}", field(&begun, "error") ); - let begin_value = field(&begun, "value"); + let begin_value = option_field(&begun, "value").expect("present value"); let callback = field(&begin_value, "callback_handle"); let callback_for_stale_rejection = callback.clone(); let verifier = field(&begin_value, "verifier_handle"); @@ -751,9 +793,9 @@ fn native_callback_results_preserve_live_family() { "native callback wait result: {:?}", field(&waited, "error") ); - let wait_value = field(&waited, "value"); + let wait_value = option_field(&waited, "value").expect("present value"); assert_eq!(field(&wait_value, "outcome"), Value::string("success")); - let code = field(&wait_value, "code_handle"); + let code = some_slot(field(&wait_value, "code_handle")); assert!(matches!(code, Value::Int(_))); let receipt = browser.receipt(); assert!(receipt.starts_with("HTTP/1.1 200 OK")); @@ -765,7 +807,10 @@ fn native_callback_results_preserve_live_family() { .expect("callback replay is a typed failure result"); assert_eq!(field(&replay, "ok"), Value::Bool(false)); assert_eq!( - field(&field(&replay, "error"), "code"), + field( + &option_field(&replay, "error").expect("failure detail"), + "code" + ), Value::string("handle_replayed") ); assert_eq!(vm.host_context().resource_count(), 5); diff --git a/tests/codex_device_login_tests.rs b/tests/cases/codex_device_login_tests.rs similarity index 97% rename from tests/codex_device_login_tests.rs rename to tests/cases/codex_device_login_tests.rs index 8e36e66..612c64f 100644 --- a/tests/codex_device_login_tests.rs +++ b/tests/cases/codex_device_login_tests.rs @@ -47,6 +47,13 @@ fn record(fields: Vec<(&str, Value)>) -> Value { ) } +fn present(value: Value) -> Value { + match rustscript_vm::option::payload(&value).expect("canonical Option carrier") { + (true, Some(payload)) if !matches!(payload, Value::Null) => payload.clone(), + _ => panic!("metadata is present"), + } +} + fn field(value: &Value, name: &str) -> Value { let Value::Map(fields) = value else { panic!("expected RSS record: {value:?}") @@ -135,7 +142,7 @@ fn fixture_catalog() -> Arc { HostParamSchema::value("user_code", HostTypeSchema::String), HostParamSchema::value("expires_in_seconds", HostTypeSchema::Int), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); Arc::new(builder.build().expect("test catalog")) } @@ -459,7 +466,7 @@ fn actual_rss_login_enrolls_saves_and_reloads_native_credentials() { sent.len() ); assert_eq!(field(&result, "code"), Value::string("ok")); - let metadata = field(&result, "metadata"); + let metadata = present(field(&result, "metadata")); assert_eq!(field(&metadata, "ok"), Value::Bool(true)); assert_eq!( field(&field(&metadata, "metadata"), "generation"), @@ -588,7 +595,7 @@ fn native_update_clears_absent_refresh_and_cas_adopts_the_winner() { Value::Bool(true), "stale candidate adopts the committed winner" ); - let adopted_metadata = field(&field(&adopted, "metadata"), "metadata"); + let adopted_metadata = field(&present(field(&adopted, "metadata")), "metadata"); assert_eq!(field(&adopted_metadata, "generation"), Value::Int(1)); assert_eq!( field(&adopted_metadata, "has_refresh_token"), @@ -615,7 +622,7 @@ fn native_update_clears_absent_refresh_and_cas_adopts_the_winner() { "generation update result code: {:?}", field(&second, "code") ); - let updated_metadata = field(&field(&second, "metadata"), "metadata"); + let updated_metadata = field(&present(field(&second, "metadata")), "metadata"); assert_eq!(field(&updated_metadata, "generation"), Value::Int(2)); assert_eq!( field(&updated_metadata, "has_refresh_token"), @@ -893,14 +900,14 @@ fn native_403_and_404_are_pending_before_successful_exchange() { assert_eq!(field(&result, "ok"), Value::Bool(true)); assert_eq!( field( - &field(&field(&result, "metadata"), "metadata"), + &field(&present(field(&result, "metadata")), "metadata"), "generation" ), Value::Int(1) ); assert_eq!( field( - &field(&field(&result, "metadata"), "metadata"), + &field(&present(field(&result, "metadata")), "metadata"), "has_refresh_token" ), Value::Bool(false) @@ -925,13 +932,19 @@ fn native_403_and_404_are_pending_before_successful_exchange() { #[test] fn token_endpoint_origin_mismatch_fails_before_native_transport() { let mut fixture = NativeFixture::new(); - let oauth = field(&fixture.provider, "oauth"); + let oauth = present(field(&fixture.provider, "oauth")); let invalid_oauth = replace_field( &oauth, "token_endpoint", - Value::string("https://other.example.test/oauth/token"), + rustscript_vm::option::some_carrier(Value::string( + "https://other.example.test/oauth/token", + )), + ); + let invalid_provider = replace_field( + &fixture.provider, + "oauth", + rustscript_vm::option::some_carrier(invalid_oauth), ); - let invalid_provider = replace_field(&fixture.provider, "oauth", invalid_oauth); let result = fixture.login_with_provider(0, invalid_provider); assert_eq!(field(&result, "ok"), Value::Bool(false)); assert_eq!( diff --git a/tests/codex_device_rss_entry_tests.rs b/tests/cases/codex_device_rss_entry_tests.rs similarity index 95% rename from tests/codex_device_rss_entry_tests.rs rename to tests/cases/codex_device_rss_entry_tests.rs index 887c94d..8705017 100644 --- a/tests/codex_device_rss_entry_tests.rs +++ b/tests/cases/codex_device_rss_entry_tests.rs @@ -272,14 +272,31 @@ fn response_error(code: &'static str) -> Value { Value::Bool(code == "wait_deadline_exceeded"), ), ("revoked", Value::Bool(false)), + ("transport_phase", carried(None)), + ("transport_category", carried(None)), ]) } +fn carried(value: Option) -> Value { + value.map_or_else( + rustscript_vm::option::none_carrier, + rustscript_vm::option::some_carrier, + ) +} + +fn option_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { + match rustscript_vm::option::payload(field(value, name)).expect("canonical Option carrier") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value), + _ => panic!("invalid Option payload for {name}"), + } +} + fn result_envelope(ok: bool, error: Option<&'static str>, value: Value) -> Value { record(vec![ ("ok", Value::Bool(ok)), - ("error", error.map(response_error).unwrap_or(Value::Null)), - ("value", value), + ("error", carried(error.map(response_error))), + ("value", carried(ok.then_some(value))), ]) } @@ -468,20 +485,26 @@ fn response_value( }; Ok(record(vec![ ("ok", Value::Bool(response.failure.is_none())), - ("error", Value::Null), + ("error", carried(None)), ("status", Value::Int(response.status)), ("public_headers", Value::array(Vec::new())), ("body_without_secret_fields", Value::string(&response.body)), ( "retry_after_ms", - response - .retry_after_ms - .map(Value::Int) - .unwrap_or(Value::Null), + carried(response.retry_after_ms.map(Value::Int)), + ), + ( + "access_slot", + carried((!matches!(access, Value::Null)).then_some(access)), + ), + ( + "refresh_slot", + carried((!matches!(refresh, Value::Null)).then_some(refresh)), + ), + ( + "device_handle", + carried((!matches!(device, Value::Null)).then_some(device)), ), - ("access_slot", access), - ("refresh_slot", refresh), - ("device_handle", device), ])) } @@ -499,8 +522,7 @@ fn transport_adapter(vm: &mut Vm, args: &[Value], mode: TransportMode) -> VmResu let body = text(field(request, "public_body")).to_owned(); let usage = text(field(use_value, "kind")).to_owned(); if mode == TransportMode::Capture - && let Some(device) = optional_field(use_value, "device_handle") - && !matches!(device, Value::Null) + && let Some(device) = option_field(use_value, "device_handle") && issued_owner(&state, device, ResourceClass::Device) != Some(owner) { return Err(VmError::HostError("scripted_device_provenance".into())); @@ -510,7 +532,7 @@ fn transport_adapter(vm: &mut Vm, args: &[Value], mode: TransportMode) -> VmResu ("code_handle", ResourceClass::Code), ("verifier_handle", ResourceClass::Verifier), ] { - if optional_field(use_value, name) + if option_field(use_value, name) .is_none_or(|value| issued_owner(&state, value, class) != Some(owner)) { return Err(VmError::HostError("scripted_pair_provenance".into())); @@ -548,8 +570,14 @@ fn transport_adapter(vm: &mut Vm, args: &[Value], mode: TransportMode) -> VmResu ("ok", Value::Bool(inner_ok)), ("error", inner_error), ("response", value), - ("code_handle", code_handle), - ("verifier_handle", verifier_handle), + ( + "code_handle", + carried((!matches!(code_handle, Value::Null)).then_some(code_handle)), + ), + ( + "verifier_handle", + carried((!matches!(verifier_handle, Value::Null)).then_some(verifier_handle)), + ), ]); let error = (!inner_ok).then_some("transport_error"); Ok(CallOutcome::Return(CallReturn::One(result_envelope( @@ -646,7 +674,7 @@ fn wait_ms_result(vm: &mut Vm, args: &[Value]) -> VmResult { }; Ok(CallOutcome::Return(CallReturn::One(record(vec![ ("ok", Value::Bool(failure.is_none())), - ("error", failure.map(response_error).unwrap_or(Value::Null)), + ("error", carried(failure.map(response_error))), ])))) } @@ -763,11 +791,26 @@ fn public_json_value( ) -> Value { record(vec![ ("kind", Value::string(kind)), - ("string_value", string_value), - ("int_value", int_value), - ("float_value", float_value), - ("bool_value", bool_value), - ("length", length), + ( + "string_value", + carried((!matches!(string_value, Value::Null)).then_some(string_value)), + ), + ( + "int_value", + carried((!matches!(int_value, Value::Null)).then_some(int_value)), + ), + ( + "float_value", + carried((!matches!(float_value, Value::Null)).then_some(float_value)), + ), + ( + "bool_value", + carried((!matches!(bool_value, Value::Null)).then_some(bool_value)), + ), + ( + "length", + carried((!matches!(length, Value::Null)).then_some(length)), + ), ]) } @@ -781,8 +824,8 @@ fn save_if_generation_result(vm: &mut Vm, args: &[Value]) -> VmResult VmResult Arc { HostParamSchema::value("user_code", HostTypeSchema::String), HostParamSchema::value("expires_in_seconds", HostTypeSchema::Int), ], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); Arc::new(builder.build().expect("restricted scripted test catalog")) })) @@ -1041,27 +1077,40 @@ fn scripted_vm(plan: Plan) -> (Vm, HarnessState, Value, ResourceHandle, Value) { fn provider_with(auth_id: &str, flow: &str) -> Value { let oauth = record(vec![ - ("flow", Value::string(flow)), - ("issuer", Value::string("https://auth.example.test")), - ("client_id", Value::string("synthetic-public-client")), + ("flow", carried(Some(Value::string(flow)))), + ( + "issuer", + carried(Some(Value::string("https://auth.example.test"))), + ), + ( + "client_id", + carried(Some(Value::string("synthetic-public-client"))), + ), ( "device_user_code_path", - Value::string("/api/accounts/deviceauth/usercode"), + carried(Some(Value::string("/api/accounts/deviceauth/usercode"))), ), ( "device_poll_path", - Value::string("/api/accounts/deviceauth/token"), + carried(Some(Value::string("/api/accounts/deviceauth/token"))), + ), + ( + "authorization_path", + carried(Some(Value::string("/codex/device"))), ), - ("authorization_path", Value::string("/codex/device")), ( "token_endpoint", - Value::string("https://auth.example.test/oauth/token"), + carried(Some(Value::string("https://auth.example.test/oauth/token"))), ), ( "redirect_uri", - Value::string("https://auth.example.test/deviceauth/callback"), + carried(Some(Value::string( + "https://auth.example.test/deviceauth/callback", + ))), ), ("refresh_skew_seconds", Value::Int(0)), + ("scopes", carried(None)), + ("authorization_parameters", carried(None)), ]); record(vec![ ("name", Value::string("fixture-provider")), @@ -1070,8 +1119,8 @@ fn provider_with(auth_id: &str, flow: &str) -> Value { "base_url", Value::string("https://api.example.test/backend"), ), - ("auth", Value::string(auth_id)), - ("oauth", oauth), + ("auth", carried(Some(Value::string(auth_id)))), + ("oauth", carried(Some(oauth))), ]) } @@ -1189,7 +1238,7 @@ fn actual_rss_happy_path_uses_typed_queue_and_cleans_only_its_owner() { let result = login(&mut vm, &policy, 0).expect("invoke actual imported RSS login"); assert_eq!(field(&result, "ok"), &Value::Bool(true)); assert_eq!(field(&result, "code"), &Value::string("ok")); - let metadata = field(&result, "metadata"); + let metadata = option_field(&result, "metadata").expect("login metadata"); assert_eq!(field(metadata, "ok"), &Value::Bool(true)); assert_eq!( field(field(metadata, "metadata"), "generation"), @@ -2196,7 +2245,7 @@ fn refresh_absent_clears_and_save_rejection_stays_static() { let (mut vm, state, _, unrelated, secondary, result) = run_plan(happy_plan(false)); let result = result.expect("login without refresh succeeds"); assert_eq!(field(&result, "ok"), &Value::Bool(true)); - let envelope = field(&result, "metadata"); + let envelope = option_field(&result, "metadata").expect("login metadata"); assert!( optional_field(envelope, "metadata").is_some(), "refresh-absent metadata envelope: {envelope:?}" @@ -2218,7 +2267,7 @@ fn refresh_absent_clears_and_save_rejection_stays_static() { let (mut vm, state, _, unrelated, secondary, result) = run_plan(plan); let result = result.expect("save rejection is a typed login result"); assert_eq!(result_code(&result), "save_failed"); - assert!(optional_field(&result, "metadata").is_none_or(|value| matches!(value, Value::Null))); + assert!(option_field(&result, "metadata").is_none()); let receipts = state.0.lock().expect("scripted receipts").receipts.clone(); assert_eq!(request_receipts(&receipts).len(), 3); assert_eq!( @@ -2247,7 +2296,10 @@ fn expiry_addition_is_checked_at_the_integer_boundary() { let (mut vm, state, _, unrelated, secondary, result) = run_plan(boundary_plan); let result = result.expect("representable expiry reaches save"); assert_eq!(field(&result, "ok"), &Value::Bool(true)); - let saved = field(field(&result, "metadata"), "metadata"); + let saved = field( + option_field(&result, "metadata").expect("login metadata"), + "metadata", + ); assert_eq!(field(saved, "expires_at_ms"), &Value::Int(max)); assert_terminal_resources(&mut vm, &state, unrelated, &secondary); diff --git a/tests/codex_metadata_rss_tests.rs b/tests/cases/codex_metadata_rss_tests.rs similarity index 98% rename from tests/codex_metadata_rss_tests.rs rename to tests/cases/codex_metadata_rss_tests.rs index 9f018cd..d18a0fa 100644 --- a/tests/codex_metadata_rss_tests.rs +++ b/tests/cases/codex_metadata_rss_tests.rs @@ -1,8 +1,6 @@ //! Native trusted transport identity regression through the production harness. -#[path = "support/provider_opaque_fixture.rs"] -#[allow(dead_code)] -pub mod provider_opaque_fixture; +use super::provider_opaque_fixture; use provider_opaque_fixture::{ canonical_request, harness_context, read_provider_fixture, sse_events, vm_value_to_json, @@ -532,6 +530,15 @@ async fn bundled_main_codex_responses_tool_continuation_is_durable_and_access_on .expect("public Codex Responses profile"), ); service.set_auth_host_factory(factory); + service + .materialize_cached_runner() + .expect("prepare Main and native tools before timed admission"); + AgentRunner::from_bundled_named( + "rss/llm/provider_dispatch.rss", + "dispatch", + AgentConfig::default(), + ) + .expect("prepare the real provider dispatcher before timed admission"); let admitted = service .admit(AdmitRunRequest { @@ -848,6 +855,15 @@ async fn run_bundled_main_with_sse(transcript: String) -> MainRegressionResult { .expect("public Codex Responses profile"), ); service.set_auth_host_factory(factory); + service + .materialize_cached_runner() + .expect("prepare Main and native tools before timed admission"); + AgentRunner::from_bundled_named( + "rss/llm/provider_dispatch.rss", + "dispatch", + AgentConfig::default(), + ) + .expect("prepare the real provider dispatcher before timed admission"); let admitted = service .admit(AdmitRunRequest { diff --git a/tests/codex_responses_codec_tests.rs b/tests/cases/codex_responses_codec_tests.rs similarity index 90% rename from tests/codex_responses_codec_tests.rs rename to tests/cases/codex_responses_codec_tests.rs index 4ef6a3b..f58dc9d 100644 --- a/tests/codex_responses_codec_tests.rs +++ b/tests/cases/codex_responses_codec_tests.rs @@ -7,63 +7,71 @@ use std::path::PathBuf; use std::sync::Arc; -use rustscript_agent::auth::host::auth_host_catalog; -use rustscript_vm::{ - CapabilityProfile, CompileSourceFileOptions, HostFunctionRegistry, SourceFlavor, Value, Vm, - VmStatus, compile_source_at_path_with_flavor_and_options, - register_http_builtin_module_from_catalog, +use rustscript_agent::runtime::{ + AgentConfig, AgentRunner, RunCancellation, RunDeliveryError, RunEventSink, }; +use rustscript_vm::Value; use serde_json::{Value as JsonValue, json}; -fn fixture_path() -> PathBuf { - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/providers/responses_codec.rss") +struct CodecFixture { + runner: AgentRunner, + _sources: CodecSources, } -fn codec_vm() -> Vm { - let path = fixture_path(); - let source = std::fs::read_to_string(&path).expect("read committed codec fixture"); - let catalog = auth_host_catalog(); - let compiled = compile_source_at_path_with_flavor_and_options( - &path, - &source, - SourceFlavor::RustScript, - CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()), +struct CodecSources(PathBuf); + +impl Drop for CodecSources { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +fn codec_vm() -> CodecFixture { + let repo = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let sources = CodecSources( + std::env::temp_dir().join(format!("responses-codec-{}", uuid::Uuid::new_v4())), + ); + std::fs::create_dir_all(sources.0.join("llm")).expect("create codec module root"); + let entry = sources.0.join("codec.rss"); + std::fs::copy( + repo.join("tests/fixtures/providers/responses_codec.rss"), + &entry, ) - .expect("compile production codec fixture against the restricted catalog"); - let mut vm = Vm::new_shared(Arc::new(compiled.program)); - let mut registry = HostFunctionRegistry::empty(); - registry.set_capability_profile(CapabilityProfile::deny_all()); - register_http_builtin_module_from_catalog(&mut registry, catalog.as_ref()) - .expect("install frozen http module from the restricted catalog"); + .expect("copy codec entry"); for name in [ - "json::encode", - "json::decode", - "bytes::to_utf8", - "bytes::to_utf8_lossy", - "bytes::to_array_u8", - "bytes::from_utf8", + "openai_responses_codec.rss", + "types.rss", + "tool_descriptor.rss", ] { - if registry.contains_name(name) { - registry.authorize_registered_builtin_import(name); - } else { - registry - .allow_builtin(name) - .expect("admit standard builtin"); - } + let module = PathBuf::from("llm").join(name); + std::fs::copy(repo.join("rss").join(&module), sources.0.join(&module)) + .expect("copy current production codec module"); + } + let runner = AgentRunner::from_file(entry, AgentConfig::default()) + .expect("compile production codec fixture against the restricted catalog"); + CodecFixture { + runner, + _sources: sources, + } +} + +struct NoEvents; + +impl RunEventSink for NoEvents { + fn deliver(&mut self, value: Value) -> Result<(), RunDeliveryError> { + panic!("pure codec unexpectedly emitted an event: {value:?}"); } - registry - .bind_vm_cached(&mut vm) - .expect("bind restricted registry"); - assert!(matches!( - vm.run().expect("RSS declarations"), - VmStatus::Halted - )); - vm } -fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> Value { - let callable = vm.resolve_exported_callable(name).expect("RSS export"); - vm.invoke_callable(callable, args).expect("RSS invoke") +fn invoke(vm: &mut CodecFixture, name: &str, args: &[Value]) -> Value { + vm.runner + .run_named_with_context_and_events( + name, + Value::Array(Arc::new(args.to_vec())), + &mut NoEvents, + &RunCancellation::new(), + ) + .expect("RSS invoke") } fn json_to_value(value: &JsonValue) -> Value { @@ -117,8 +125,18 @@ fn value_to_json(value: &Value) -> JsonValue { } } -fn collect(vm: &mut Vm, chunks: &JsonValue, done: bool) -> JsonValue { - let result = invoke(vm, "collect", &[json_to_value(chunks), Value::Bool(done)]); +fn collect(vm: &mut CodecFixture, chunks: &JsonValue, done: bool) -> JsonValue { + let chunks = chunks + .as_array() + .expect("event array") + .iter() + .map(|chunk| Value::string(chunk.to_string())) + .collect(); + let result = invoke( + vm, + "collect", + &[Value::Array(Arc::new(chunks)), Value::Bool(done)], + ); value_to_json(&result) } @@ -256,11 +274,17 @@ fn production_descriptor_schema_maps_as_wire_object() { } }] }); - let body = invoke(&mut vm, "request_body", &[json_to_value(&request)]); + let schema = &request["tools"][0]["schema"]; + let body = invoke( + &mut vm, + "native_descriptor_body", + &[json_to_value(&request)], + ); let Value::String(body) = body else { panic!("request_body must return a string"); }; let decoded: JsonValue = serde_json::from_str(&body).expect("decode Responses body"); + assert_eq!(&decoded["tools"][0]["parameters"], schema); assert_eq!(decoded["tools"][0]["parameters"]["type"], "object"); assert_eq!(decoded["tools"][0]["parameters"]["required"], json!([])); assert_eq!( @@ -615,7 +639,13 @@ fn function_call_item_is_mapped_to_canonical_tool_call() { assert_eq!(calls.as_array().map(Vec::len), Some(1), "result: {result}"); assert_eq!(calls[0]["id"], json!("call_xy")); assert_eq!(calls[0]["name"], json!("read_file")); - assert_eq!(calls[0]["arguments"], json!({"path": "README.md"})); + assert_eq!( + serde_json::from_str::( + calls[0]["arguments_json"].as_str().expect("argument text") + ) + .expect("argument object"), + json!({"path": "README.md"}) + ); assert_eq!(result["response"]["stop_reason"], json!("tool_calls")); } @@ -635,7 +665,13 @@ fn accumulated_arguments_without_final_done_event_report_canonical_call() { let calls = &result["response"]["tool_calls"]; assert_eq!(calls.as_array().map(Vec::len), Some(1), "result: {result}"); assert_eq!(calls[0]["id"], json!("call_9")); - assert_eq!(calls[0]["arguments"], json!({"q": 1})); + assert_eq!( + serde_json::from_str::( + calls[0]["arguments_json"].as_str().expect("argument text") + ) + .expect("argument object"), + json!({"q": 1}) + ); } #[test] @@ -703,7 +739,11 @@ fn buffered_body_parses_text_reasoning_and_usage() { "usage": {"input_tokens": 11, "output_tokens": 22, "total_tokens": 33}, "error": null }); - let result = invoke(&mut vm, "parse_body", &[json_to_value(&body)]); + let result = invoke( + &mut vm, + "parse_body_text", + &[Value::string(body.to_string())], + ); let result = value_to_json(&result); assert_eq!(result["ok"], json!(true), "result: {result}"); assert_eq!(result["response"]["text"], json!("final answer")); diff --git a/tests/codex_responses_rss_entry_tests.rs b/tests/cases/codex_responses_rss_entry_tests.rs similarity index 97% rename from tests/codex_responses_rss_entry_tests.rs rename to tests/cases/codex_responses_rss_entry_tests.rs index c0be336..1141e71 100644 --- a/tests/codex_responses_rss_entry_tests.rs +++ b/tests/cases/codex_responses_rss_entry_tests.rs @@ -11,13 +11,11 @@ #![cfg(feature = "config-fixture")] -#[path = "support/provider_opaque_fixture.rs"] +#[path = "../support/provider_opaque_fixture.rs"] #[allow(dead_code)] pub mod provider_opaque_fixture; -#[path = "support/opaque_chat_stream.rs"] -#[allow(dead_code)] -mod opaque_chat_stream; +use super::opaque_chat_stream; use provider_opaque_fixture::{ FixtureHome, canonical_request, harness_context, harness_runner, read_provider_fixture, @@ -203,7 +201,13 @@ fn admitted_responses_buffered_function_call_returns_canonical_tool_call() { assert_eq!(calls.as_array().map(Vec::len), Some(1), "{result}"); assert_eq!(calls[0]["id"], json!("call_xy")); assert_eq!(calls[0]["name"], json!("read_file")); - assert_eq!(calls[0]["arguments"], json!({"path": "README.md"})); + assert_eq!( + serde_json::from_str::( + calls[0]["arguments_json"].as_str().expect("argument text") + ) + .expect("argument object"), + json!({"path": "README.md"}) + ); assert_eq!(result["response"]["stop_reason"], json!("tool_calls")); let sent = transport.take_sent(); @@ -244,7 +248,13 @@ fn admitted_responses_stream_returns_canonical_text_and_tool() { let calls = &result["response"]["tool_calls"]; assert_eq!(calls.as_array().map(Vec::len), Some(1), "{result}"); assert_eq!(calls[0]["id"], json!("call_xy")); - assert_eq!(calls[0]["arguments"], json!({"path": "README.md"})); + assert_eq!( + serde_json::from_str::( + calls[0]["arguments_json"].as_str().expect("argument text") + ) + .expect("argument object"), + json!({"path": "README.md"}) + ); let sent = transport.counts.sent.lock().expect("sent").clone(); assert_eq!(sent.len(), 1); diff --git a/tests/commit_step_timestamp_contract.rs b/tests/cases/commit_step_timestamp_contract.rs similarity index 97% rename from tests/commit_step_timestamp_contract.rs rename to tests/cases/commit_step_timestamp_contract.rs index 2492bfb..23a8921 100644 --- a/tests/commit_step_timestamp_contract.rs +++ b/tests/cases/commit_step_timestamp_contract.rs @@ -5,7 +5,7 @@ #[test] fn commit_step_source_reuses_reserved_event_timestamp() { - let body = commit_step_body(include_str!("../src/service.rs")); + let body = commit_step_body(include_str!("../../src/service.rs")); let marker = "let mut event = event_candidate("; let reservation = body .find(marker) diff --git a/tests/config_auth_rss_tests.rs b/tests/cases/config_auth_rss_tests.rs similarity index 98% rename from tests/config_auth_rss_tests.rs rename to tests/cases/config_auth_rss_tests.rs index d8146e7..f912610 100644 --- a/tests/config_auth_rss_tests.rs +++ b/tests/cases/config_auth_rss_tests.rs @@ -250,7 +250,8 @@ fn rss_config_auth_entry_denies_stringify_serialize_and_path_supply() { let home = copy_fixture_home("rss-opaque", "config.yaml", "auth.yaml"); let stringify = run_kind(&home, "stringify_handle"); assert_eq!(stringify["ok"], true); - assert_eq!(stringify["handle_type"], "callable"); + assert_eq!(stringify["handle_type"], "OpaquePolicyHandle"); + assert_eq!(stringify["carrier_type"], "int"); let rendered = stringify["rendered"].as_str().unwrap_or_default(); assert!( !rendered.contains('{'), @@ -266,8 +267,9 @@ fn rss_config_auth_entry_denies_stringify_serialize_and_path_supply() { let serialize = run_kind(&home, "serialize_handle"); assert_eq!(serialize["ok"], true); - assert_eq!(serialize["handle_type"], "callable"); - assert_eq!(serialize["echoed"], ""); + assert_eq!(serialize["handle_type"], "OpaquePolicyHandle"); + assert_eq!(serialize["carrier_type"], "int"); + assert_eq!(serialize["echoed"], ""); assert_secret_free_complete(&serialize); let supplied = run_kind(&home, "supply_path"); diff --git a/tests/contract_fixtures.rs b/tests/cases/contract_fixtures.rs similarity index 100% rename from tests/contract_fixtures.rs rename to tests/cases/contract_fixtures.rs diff --git a/tests/docs_consistency_tests.rs b/tests/cases/docs_consistency_tests.rs similarity index 100% rename from tests/docs_consistency_tests.rs rename to tests/cases/docs_consistency_tests.rs diff --git a/tests/documentation_consistency_tests.rs b/tests/cases/documentation_consistency_tests.rs similarity index 86% rename from tests/documentation_consistency_tests.rs rename to tests/cases/documentation_consistency_tests.rs index 008b387..b6ded6c 100644 --- a/tests/documentation_consistency_tests.rs +++ b/tests/cases/documentation_consistency_tests.rs @@ -22,11 +22,14 @@ use std::path::PathBuf; use std::sync::Arc; +use std::time::Duration; -use rustscript_agent::{AuthConfig, ConfigFile}; +use rustscript_agent::auth::host::AuthHost; +use rustscript_agent::{AuthConfig, ConfigFile, RunCancellation}; use rustscript_vm::{ CompileSourceFileOptions, HostFunctionRegistry, InvocationItem, InvocationPoll, SourceFlavor, Value, Vm, VmStatus, catalog_named_struct_schemas, compile_source_with_flavor_and_options, + option, }; fn repo_root() -> PathBuf { @@ -615,88 +618,56 @@ fn docs_example_auth_has_no_secret_material() { // Real RSS source-selection gate over the shipped example. // --------------------------------------------------------------------------- -fn json_to_vm(value: &serde_json::Value) -> Value { - match value { - serde_json::Value::Null => Value::Null, - serde_json::Value::Bool(value) => Value::Bool(*value), - serde_json::Value::Number(value) => value - .as_i64() - .map(Value::Int) - .or_else(|| value.as_f64().map(Value::Float)) - .unwrap_or(Value::Null), - serde_json::Value::String(value) => Value::string(value), - serde_json::Value::Array(values) => { - Value::Array(values.iter().map(json_to_vm).collect::>().into()) - } - serde_json::Value::Object(entries) => Value::map( - entries - .iter() - .map(|(key, value)| (Value::string(key), json_to_vm(value))) - .collect(), - ), +struct SelectionHome(PathBuf); + +impl Drop for SelectionHome { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); } } -/// Builds the RSS-visible public-config value the same way the production -/// `config::load_snapshot` host does: the loaded `ConfigFile` JSON with the -/// provider map projected to the admitted `ProviderEntry` array. -fn public_config_value(config: &ConfigFile) -> Value { - let mut public = serde_json::to_value(config).expect("config serialises"); - let providers = public - .get("providers") - .and_then(|value| value.as_object()) - .map(|map| { - map.iter() - .map(|(name, provider)| { - serde_json::json!({ - "name": name, - "protocol": provider.get("protocol").cloned().unwrap_or(serde_json::Value::Null), - "base_url": provider.get("base_url").cloned().unwrap_or(serde_json::Value::Null), - "auth": provider.get("auth").cloned().unwrap_or(serde_json::Value::Null), - "oauth": provider.get("oauth").cloned().unwrap_or(serde_json::Value::Null), - }) - }) - .collect::>() - }) - .unwrap_or_default(); - public["providers"] = serde_json::Value::Array(providers); - // Match the production display-only WorkspaceConfig projection. These - // fixture paths do not grant authority and no workspace host is invoked. - let workspaces = &config.workspaces; - public["workspaces"] = serde_json::json!({ - "allowed_roots": workspaces.allowed_roots.iter() - .map(|path| path.to_string_lossy().into_owned()).collect::>(), - "default": workspaces.default.as_ref() - .map(|path| path.to_string_lossy().into_owned()), - "named": workspaces.named.iter() - .map(|(name, path)| serde_json::json!({"name": name, "path": path.to_string_lossy()})) - .collect::>(), - "generation": 1, - }); - json_to_vm(&public) +/// Load the documented example through the production snapshot bridge and +/// return its typed public config for calls to the real selection export. +fn selection_vm(config_yaml: &str) -> (SelectionHome, Vm, Value) { + let root = std::env::var_os("RUSTSCRIPT_AGENT_TEST_TMP") + .map(PathBuf::from) + .unwrap_or_else(std::env::temp_dir); + let path = root.join(format!("docs-selection-{}", uuid::Uuid::new_v4())); + let mut builder = std::fs::DirBuilder::new(); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt; + builder.mode(0o700); + } + builder + .create(&path) + .expect("create private selection home"); + let home = SelectionHome(path); + std::fs::write(home.0.join("config.yaml"), config_yaml).expect("write example config"); + + let selection = read_relative("rss/config/selection.rss"); + let source = format!( + "use config;\n{selection}\n{}", + r#" +pub fn example_snapshot(home: resource) -> ConfigSnapshotEnvelope { + config::load_snapshot(&home) } - -/// Runs the real `rss/config/selection.rss` `effective_selection` entry with a -/// public config and optional provider override, returning the typed result. -/// -/// `effective_selection` performs no host calls, so a fresh VM with the module -/// root frame driven to halt is sufficient (the proven pure-RSS invocation -/// pattern). -fn run_effective_selection(public_config: Value, provider_override: Value) -> Value { - let source = read_relative("rss/config/selection.rss"); +"#, + ); let catalog = rustscript_agent::auth::host::auth_host_catalog(); let options = CompileSourceFileOptions::default().with_host_api_catalog(Arc::clone(&catalog)); let program = compile_source_with_flavor_and_options(source.as_str(), SourceFlavor::RustScript, options) - .expect("rss/config/selection.rss must compile against the auth host catalog") + .expect("selection and snapshot probe must compile against the auth host catalog") .program; - // `effective_selection` performs no host calls, but its typed parameters - // use the admitted named struct `BoundedPublicConfig`, so the catalog's - // named-struct schemas must be installed for invocation-time resolution. - let mut registry = HostFunctionRegistry::new(); - registry - .install_named_struct_schemas(catalog_named_struct_schemas(catalog.as_ref())) - .expect("install catalog named-struct schemas"); + let auth = AuthHost::new( + &home.0, + RunCancellation::with_timeout(Duration::from_secs(10)), + ) + .expect("load example config in the auth host"); + let mut registry = HostFunctionRegistry::restricted(); + auth.register(&mut registry, &catalog) + .expect("register typed auth host"); let mut vm = Vm::try_new(program).expect("fresh VM for selection policy"); registry .bind_vm_cached(&mut vm) @@ -705,24 +676,33 @@ fn run_effective_selection(public_config: Value, provider_override: Value) -> Va vm.run().expect("run selection module root"), VmStatus::Halted ); + let context = auth.bind_vm(&mut vm).expect("bind selection fixture home"); + let host_home = field(&context, "host_home").clone(); + let snapshot = invoke_selection_export(&mut vm, "example_snapshot", vec![host_home]); + assert_eq!(field(&snapshot, "ok"), &Value::Bool(true)); + let public = field(&snapshot, "public_config").clone(); + (home, vm, public) +} + +fn invoke_selection_export(vm: &mut Vm, name: &str, args: Vec) -> Value { let callable = vm - .resolve_exported_callable("effective_selection") - .expect("selection module must export effective_selection"); + .resolve_exported_callable(name) + .unwrap_or_else(|error| panic!("resolve {name}: {error:?}")); vm.set_fuel(5_000_000); let mut invocation = vm - .start_invocation(callable, vec![public_config, provider_override]) - .expect("start effective_selection invocation"); + .start_invocation(callable, args) + .unwrap_or_else(|error| panic!("start {name}: {error:?}")); loop { match invocation.poll_next().expect("poll invocation") { InvocationPoll::Pending => std::thread::sleep(std::time::Duration::from_millis(1)), InvocationPoll::Ready(Some(Ok(InvocationItem::Complete(value)))) => return value, InvocationPoll::Ready(Some(Ok(InvocationItem::Event(value)))) => { - panic!("effective_selection emitted an unexpected event: {value:?}") + panic!("{name} emitted an unexpected event: {value:?}") } InvocationPoll::Ready(Some(Err(error))) => { - panic!("effective_selection invocation failed: {error:?}") + panic!("{name} invocation failed: {error:?}") } - InvocationPoll::Ready(None) => panic!("effective_selection stream ended early"), + InvocationPoll::Ready(None) => panic!("{name} stream ended early"), } } } @@ -740,16 +720,6 @@ fn field<'a>(value: &'a Value, key: &str) -> &'a Value { .unwrap_or_else(|| panic!("map is missing field {key}: {value:?}")) } -fn optional_field<'a>(value: &'a Value, key: &str) -> Option<&'a Value> { - let Value::Map(entries) = value else { - panic!("expected a map, got {value:?}"); - }; - entries.iter().find_map(|(k, v)| match k { - Value::String(name) if name.as_ref() == key => Some(v), - _ => None, - }) -} - fn text(value: &Value) -> String { match value { Value::String(value) => value.to_string(), @@ -761,21 +731,23 @@ fn text(value: &Value) -> String { fn docs_example_selection_runs_through_the_real_rss_source_selection() { let config_yaml = read_relative("docs/examples/config.yaml"); let config = ConfigFile::from_str(&config_yaml).expect("example config must load"); - let public = public_config_value(&config); + let (_home, mut vm, public) = selection_vm(&config_yaml); + let mut select = |provider_override| { + invoke_selection_export( + &mut vm, + "effective_selection", + vec![public.clone(), provider_override], + ) + }; // No override: the example's configured model.provider is selected by the // real RSS entry (the shipped example configures that provider entry). - let selected = run_effective_selection(public.clone(), Value::Null); + let selected = select(option::none_carrier()); assert_eq!(field(&selected, "ok"), &Value::Bool(true), "{selected:?}"); assert_eq!(text(field(&selected, "provider")), config.model.provider); assert_eq!(text(field(&selected, "model")), config.model.model); assert_eq!(text(field(&selected, "source")), config.agent.source); - // A successful selection carries no error (the optional `error` field is - // absent when null). - assert!( - optional_field(&selected, "error").is_none_or(|value| matches!(value, Value::Null)), - "a successful selection must not carry an error: {selected:?}" - ); + assert_eq!(field(&selected, "error"), &option::none_carrier()); // The selection must be for a provider the example actually admits. assert!( config.providers.contains_key(&config.model.provider), @@ -783,18 +755,24 @@ fn docs_example_selection_runs_through_the_real_rss_source_selection() { ); // An explicit non-empty override that is not admitted fails static. - let missing = run_effective_selection(public.clone(), Value::string("not-admitted")); + let missing = select(option::some_carrier(Value::string("not-admitted"))); assert_eq!(field(&missing, "ok"), &Value::Bool(false), "{missing:?}"); - assert_eq!(text(field(&missing, "error")), "provider_not_found"); + assert_eq!( + field(&missing, "error"), + &option::some_carrier(Value::string("provider_not_found")) + ); // An explicit empty override requires a provider. - let empty_override = run_effective_selection(public, Value::string("")); + let empty_override = select(option::some_carrier(Value::string(""))); assert_eq!( field(&empty_override, "ok"), &Value::Bool(false), "{empty_override:?}" ); - assert_eq!(text(field(&empty_override, "error")), "provider_required"); + assert_eq!( + field(&empty_override, "error"), + &option::some_carrier(Value::string("provider_required")) + ); } /// Runs the real `rss/agent/source_policy.rss` `run` entry with the given diff --git a/tests/domain_contract_tests.rs b/tests/cases/domain_contract_tests.rs similarity index 100% rename from tests/domain_contract_tests.rs rename to tests/cases/domain_contract_tests.rs diff --git a/tests/effect_admin_api_tests.rs b/tests/cases/effect_admin_api_tests.rs similarity index 100% rename from tests/effect_admin_api_tests.rs rename to tests/cases/effect_admin_api_tests.rs diff --git a/tests/effect_checkpoint_tests.rs b/tests/cases/effect_checkpoint_tests.rs similarity index 100% rename from tests/effect_checkpoint_tests.rs rename to tests/cases/effect_checkpoint_tests.rs diff --git a/tests/gateway_tests.rs b/tests/cases/gateway_tests.rs similarity index 97% rename from tests/gateway_tests.rs rename to tests/cases/gateway_tests.rs index 0fa8193..cd3df8d 100644 --- a/tests/gateway_tests.rs +++ b/tests/cases/gateway_tests.rs @@ -30,14 +30,13 @@ fn gateway_db_path(label: &str) -> std::path::PathBuf { /// commit. The arrival signal is a Tokio oneshot so the test can await it /// without blocking the current-thread runtime (which must keep polling the /// worker task). -fn spawn_holding_fixture() -> ( - u16, +fn spawn_holding_fixture( + listener: TcpListener, +) -> ( tokio::sync::oneshot::Receiver<()>, std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>, ) { - let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind fixture"); - let port = listener.local_addr().expect("fixture address").port(); let (arrived_tx, arrived_rx) = tokio::sync::oneshot::channel(); let (release_tx, release_rx) = std::sync::mpsc::channel(); listener.set_nonblocking(true).expect("nonblocking fixture"); @@ -73,7 +72,7 @@ fn spawn_holding_fixture() -> ( loop { let read = stream.read(&mut buffer).expect("read fixture request"); if read == 0 { - break; + return; } request.extend_from_slice(&buffer[..read]); if request.windows(4).any(|window| window == b"\r\n\r\n") { @@ -88,7 +87,15 @@ fn spawn_holding_fixture() -> ( .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 8\r\nX-Agent: fixture\r\n\r\nagent-ok") .expect("write fixture response"); }); - (port, arrived_rx, release_tx, handle) + (arrived_rx, release_tx, handle) +} + +// Reserve the loopback address for source generation without running the +// listener's arrival clock during guest compilation and SQLite setup. +fn bind_fixture_listener() -> (TcpListener, u16) { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind fixture"); + let port = listener.local_addr().expect("fixture address").port(); + (listener, port) } /// Owns a held HTTP fixture's release sender and worker thread so the gate @@ -125,7 +132,8 @@ impl Drop for HoldingFixtureGuard { #[test] fn holding_http_fixture_can_be_released_before_connection() { - let (_, _, release_tx, fixture) = spawn_holding_fixture(); + let (listener, _) = bind_fixture_listener(); + let (_, release_tx, fixture) = spawn_holding_fixture(listener); release_tx.send(()).expect("release unused fixture"); let (joined_tx, joined_rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { @@ -582,17 +590,10 @@ async fn probe_admit( /// Spawns the holding fixture and builds the config/source pair that parks a /// worker inside an HTTP call before its terminal commit. -fn spawn_holding_run_env( +fn prepare_holding_run_env( config_overrides: impl FnOnce(AgentGatewayConfig) -> AgentGatewayConfig, -) -> ( - u16, - tokio::sync::oneshot::Receiver<()>, - std::sync::mpsc::Sender<()>, - std::thread::JoinHandle<()>, - AgentGatewayConfig, - String, -) { - let (port, arrived_rx, release_tx, fixture) = spawn_holding_fixture(); +) -> (TcpListener, AgentGatewayConfig, String) { + let (listener, port) = bind_fixture_listener(); let http = rustscript_vm::HttpConfig { allowed_hosts: vec!["127.0.0.1".to_string()], allowed_schemes: vec!["http".to_string()], @@ -614,7 +615,7 @@ fn spawn_holding_run_env( http, ..AgentGatewayConfig::default() }); - (port, arrived_rx, release_tx, fixture, config, source) + (listener, config, source) } /// Reads one run's full SSE body. @@ -1815,11 +1816,7 @@ async fn terminal_commit_is_retried_after_storage_recovers_exactly_once() { // merely admission. The trailing infinite loop keeps the worker // never-normally-completing so the stop below commits a cancelled // terminal, exactly as before. - let (port, arrived_rx, release_tx, fixture) = spawn_holding_fixture(); - let mut holder = HoldingFixtureGuard { - release: Some(release_tx), - worker: Some(fixture), - }; + let (listener, port) = bind_fixture_listener(); let path = gateway_db_path("terminal-retry"); let state = AgentGatewayState::with_agent_source_and_sqlite( AgentGatewayConfig { @@ -1837,9 +1834,11 @@ async fn terminal_commit_is_retried_after_storage_recovers_exactly_once() { format!( r#" use http; + use stream; pub fn run(input: map) -> string {{ let request = http::request::new("GET", "http://127.0.0.1:{port}/"); let response = http::client::request(request); + stream::emit({{type: "model.delta", delta: "loopback returned"}}); while true {{ 1; }} @@ -1852,6 +1851,11 @@ async fn terminal_commit_is_retried_after_storage_recovers_exactly_once() { .expect("SQLite state should open"); let service = state.service(); let app = build_agent_gateway_app(state); + let (arrived_rx, release_tx, fixture) = spawn_holding_fixture(listener); + let mut holder = HoldingFixtureGuard { + release: Some(release_tx), + worker: Some(fixture), + }; let (status, run) = json_request( &app, @@ -1872,6 +1876,20 @@ async fn terminal_commit_is_retried_after_storage_recovers_exactly_once() { .expect("the worker must pass validation and reach the held HTTP call") .expect("fixture arrival signal"); + // Cancel the guest after the HTTP host operation has returned. An + // outstanding HTTP operation cannot acknowledge VM cancellation. + holder.release(); + let returned = wait_until(std::time::Duration::from_secs(10), || { + service.run_events(&run_id).iter().any(|event| { + event["event"] == "model.delta" && event["data"]["delta"] == "loopback returned" + }) + }) + .await; + assert!( + returned, + "worker must return from the loopback hop before storage is cut" + ); + // Storage goes down: replace the SQLite file with a directory so every // storage command fails (the worker opens the DB per command). let broken = path.with_extension("db.broken"); @@ -1890,11 +1908,6 @@ async fn terminal_commit_is_retried_after_storage_recovers_exactly_once() { .await; assert_eq!(stop_status, StatusCode::OK); - // Release the held call: the worker (already stop-requested, parked past - // the HTTP response in its never-ending loop) observes cancellation and - // commits a cancelled terminal, which cannot persist while storage is - // down. - holder.release(); let pending = wait_until(std::time::Duration::from_secs(10), || { service.pending_terminal_count() == 1 }) @@ -2105,7 +2118,8 @@ fn spawn_gated_fixture() -> ( std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>, ) { - let (port, arrived_rx, release_tx, handle, _stages) = spawn_gated_fixture_inner(false); + let (listener, port) = bind_fixture_listener(); + let (arrived_rx, release_tx, handle, _stages) = spawn_gated_fixture_inner(listener, false); (port, arrived_rx, release_tx, handle) } @@ -2118,7 +2132,8 @@ fn spawn_gated_fixture_with_stages() -> ( std::thread::JoinHandle<()>, GatedFixtureStages, ) { - let (port, arrived_rx, release_tx, handle, stages) = spawn_gated_fixture_inner(true); + let (listener, port) = bind_fixture_listener(); + let (arrived_rx, release_tx, handle, stages) = spawn_gated_fixture_inner(listener, true); ( port, arrived_rx, @@ -2132,17 +2147,15 @@ fn spawn_gated_fixture_with_stages() -> ( /// (no stage channels exist, zero behavior change); `true` only for the two /// F02 stage tests, which receive the bounded stage observer. fn spawn_gated_fixture_inner( + listener: TcpListener, observe: bool, ) -> ( - u16, tokio::sync::oneshot::Receiver<()>, std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>, Option, ) { use std::io::{Read as _, Write as _}; - let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind fixture"); - let port = listener.local_addr().expect("fixture address").port(); let (arrived_tx, arrived_rx) = tokio::sync::oneshot::channel(); let (release_tx, release_rx) = std::sync::mpsc::channel::<()>(); // Per-fixture opt-in stage channels: three capacity-1 senders, each @@ -2286,7 +2299,7 @@ fn spawn_gated_fixture_inner( // turn cleanup into a panic. let _ = stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok"); }); - (port, arrived_rx, release_tx, handle, stages) + (arrived_rx, release_tx, handle, stages) } /// P2: the completed terminal (assistant message + delta + completed @@ -2298,11 +2311,7 @@ async fn completed_terminal_with_message_is_retried_after_storage_recovers() { // Entered rendezvous: the gated fixture signals once the real worker's // HTTP request arrives, proving context/pin validation already passed // while storage was healthy. The guard releases and joins on all paths. - let (port, arrived_rx, release_tx, fixture) = spawn_gated_fixture(); - let mut holder = HoldingFixtureGuard { - release: Some(release_tx), - worker: Some(fixture), - }; + let (listener, port) = bind_fixture_listener(); let http = rustscript_vm::HttpConfig { allowed_schemes: vec!["http".to_string()], allowed_hosts: vec!["127.0.0.1".to_string()], @@ -2334,6 +2343,11 @@ async fn completed_terminal_with_message_is_retried_after_storage_recovers() { .expect("SQLite state should open"); let service = state.service(); let app = build_agent_gateway_app(state); + let (arrived_rx, release_tx, fixture, _) = spawn_gated_fixture_inner(listener, false); + let mut holder = HoldingFixtureGuard { + release: Some(release_tx), + worker: Some(fixture), + }; let (status, run) = json_request( &app, @@ -2767,11 +2781,7 @@ impl Drop for ConflictHoldingRequest { /// the durable recovery terminal stays the single source of truth. #[tokio::test] async fn terminal_retry_conflict_never_publishes_a_fabricated_terminal() { - let (port, arrived, release, worker) = spawn_holding_fixture(); - let mut request = ConflictHoldingRequest { - release: Some(release), - worker: Some(worker), - }; + let (listener, port) = bind_fixture_listener(); let path = gateway_db_path("terminal-retry-conflict"); let state = AgentGatewayState::with_agent_source_and_sqlite( AgentGatewayConfig { @@ -2789,9 +2799,11 @@ async fn terminal_retry_conflict_never_publishes_a_fabricated_terminal() { format!( r#" use http; + use stream; pub fn run(input: map) -> string {{ let request = http::request::new("GET", "http://127.0.0.1:{port}/"); let response = http::client::request(request); + stream::emit({{type: "model.delta", delta: "loopback returned"}}); while true {{ 1; }} @@ -2806,6 +2818,11 @@ async fn terminal_retry_conflict_never_publishes_a_fabricated_terminal() { let metrics = service.metrics(); let persistence = state.persistence().expect("original persistence"); let app = build_agent_gateway_app(state); + let (arrived, release, worker) = spawn_holding_fixture(listener); + let mut request = ConflictHoldingRequest { + release: Some(release), + worker: Some(worker), + }; let (status, run) = json_request( &app, @@ -2821,6 +2838,17 @@ async fn terminal_retry_conflict_never_publishes_a_fabricated_terminal() { .expect("real RSS must reach the held HTTP request before storage breaks") .expect("HTTP arrival acknowledgement"); + request.release(); + let returned = wait_until(std::time::Duration::from_secs(10), || { + service.run_events(&run_id).iter().any(|event| { + event["event"] == "model.delta" && event["data"]["delta"] == "loopback returned" + }) + }) + .await; + assert!( + returned, + "worker must return from the loopback hop before storage is cut" + ); let broken = path.with_extension("db.broken"); std::fs::rename(&path, &broken).expect("move the db aside"); std::fs::create_dir(&path).expect("break storage with a directory"); @@ -2833,7 +2861,6 @@ async fn terminal_retry_conflict_never_publishes_a_fabricated_terminal() { .await; assert_eq!(stop_status, StatusCode::OK); assert_eq!(stop["status"], "stopping"); - request.release(); let pending = wait_until(std::time::Duration::from_secs(10), || { service.pending_terminal_count() == 1 }) @@ -3009,22 +3036,6 @@ async fn stop_waits_on_a_blocking_thread_during_a_storage_stall() { .expect("persistence handle should be exposed"); let app = build_agent_gateway_app(state); - // Admit and finish a run while storage is healthy. - let (status, run) = json_request( - &app, - axum::http::Method::POST, - "/v1/runs", - json!({"input": "stop-stall"}), - ) - .await; - assert_eq!(status, StatusCode::ACCEPTED); - let run_id = run["run_id"].as_str().expect("run id").to_string(); - let finished = wait_until(std::time::Duration::from_secs(10), || { - service.available_capacity() == AgentGatewayConfig::default().max_concurrent_runs - }) - .await; - assert!(finished, "the run must finish before the stall"); - // Seed enough state that a full reload occupies the storage worker for // a while, then stall the worker with that reload. Large session // prompts make the reload byte-bound (one row per page), so a handful @@ -3052,6 +3063,22 @@ async fn stop_waits_on_a_blocking_thread_during_a_storage_stall() { })) .expect("session create should commit"); } + // Admit and finish a run while storage is healthy. + let (status, run) = json_request( + &app, + axum::http::Method::POST, + "/v1/runs", + json!({"input": "stop-stall"}), + ) + .await; + assert_eq!(status, StatusCode::ACCEPTED); + let run_id = run["run_id"].as_str().expect("run id").to_string(); + let finished = wait_until(std::time::Duration::from_secs(10), || { + service.available_capacity() == AgentGatewayConfig::default().max_concurrent_runs + }) + .await; + assert!(finished, "the run must finish before the stall"); + let slow_persistence = persistence.clone(); let slow_load = tokio::task::spawn_blocking(move || { slow_persistence.load().expect("reload should succeed"); @@ -3547,13 +3574,12 @@ async fn failed_replay_persist_keeps_the_original_idempotency_record() { #[tokio::test] async fn failed_terminal_persist_parks_a_pending_terminal_without_publishing() { - let (_, arrived_rx, release_tx, fixture, config, source) = - spawn_holding_run_env(|config| AgentGatewayConfig { - terminal_persist_retries: 2, - terminal_persist_retry_delay: std::time::Duration::from_millis(5), - janitor_interval: std::time::Duration::from_millis(50), - ..config - }); + let (listener, config, source) = prepare_holding_run_env(|config| AgentGatewayConfig { + terminal_persist_retries: 2, + terminal_persist_retry_delay: std::time::Duration::from_millis(5), + janitor_interval: std::time::Duration::from_millis(50), + ..config + }); let path = std::env::temp_dir().join(format!("rustscript-agent-terminal-{}.db", Uuid::new_v4())); let backup = path.with_extension("db.bak"); @@ -3561,6 +3587,7 @@ async fn failed_terminal_persist_parks_a_pending_terminal_without_publishing() { .expect("SQLite state should open"); let service = state.service(); let app = build_agent_gateway_app(state); + let (arrived_rx, release_tx, fixture) = spawn_holding_fixture(listener); let (status, run) = json_request( &app, @@ -3660,20 +3687,20 @@ async fn failed_terminal_persist_parks_a_pending_terminal_without_publishing() { #[tokio::test] async fn recovered_storage_commits_the_parked_terminal_once_and_releases_the_permit() { - let (_, arrived_rx, release_tx, fixture, config, source) = - spawn_holding_run_env(|config| AgentGatewayConfig { - max_concurrent_runs: 1, - terminal_persist_retries: 2, - terminal_persist_retry_delay: std::time::Duration::from_millis(5), - janitor_interval: std::time::Duration::from_millis(50), - ..config - }); + let (listener, config, source) = prepare_holding_run_env(|config| AgentGatewayConfig { + max_concurrent_runs: 1, + terminal_persist_retries: 2, + terminal_persist_retry_delay: std::time::Duration::from_millis(5), + janitor_interval: std::time::Duration::from_millis(50), + ..config + }); let path = std::env::temp_dir().join(format!("rustscript-agent-recover-{}.db", Uuid::new_v4())); let backup = path.with_extension("db.bak"); let state = AgentGatewayState::with_agent_source_and_sqlite(config, source, &path) .expect("SQLite state should open"); let service = state.service(); let app = build_agent_gateway_app(state); + let (arrived_rx, release_tx, fixture) = spawn_holding_fixture(listener); let (status, run) = json_request( &app, @@ -3778,20 +3805,20 @@ async fn recovered_storage_commits_the_parked_terminal_once_and_releases_the_per #[tokio::test] async fn sustained_persistence_failure_never_permanently_exhausts_capacity() { - let (_, arrived_rx, release_tx, fixture, config, source) = - spawn_holding_run_env(|config| AgentGatewayConfig { - max_concurrent_runs: 1, - terminal_persist_retries: 2, - terminal_persist_retry_delay: std::time::Duration::from_millis(5), - janitor_interval: std::time::Duration::from_millis(50), - ..config - }); + let (listener, config, source) = prepare_holding_run_env(|config| AgentGatewayConfig { + max_concurrent_runs: 1, + terminal_persist_retries: 2, + terminal_persist_retry_delay: std::time::Duration::from_millis(5), + janitor_interval: std::time::Duration::from_millis(50), + ..config + }); let path = std::env::temp_dir().join(format!("rustscript-agent-capacity-{}.db", Uuid::new_v4())); let state = AgentGatewayState::with_agent_source_and_sqlite(config, source, &path) .expect("SQLite state should open"); let service = state.service(); let app = build_agent_gateway_app(state); + let (arrived_rx, release_tx, fixture) = spawn_holding_fixture(listener); let (status, run) = json_request( &app, @@ -3940,16 +3967,16 @@ async fn failed_admission_persist_rolls_back_an_existing_sessions_message() { #[tokio::test] async fn legacy_chat_timeout_is_bounded_while_the_worker_is_blocked() { - let (_, mut arrived_rx, release_tx, fixture, config, source) = - spawn_holding_run_env(|config| AgentGatewayConfig { - // Include cold worker compilation without extending the 3s response bound. - run_timeout: std::time::Duration::from_secs(2), - cancellation_grace: std::time::Duration::from_millis(50), - ..config - }); + let (listener, config, source) = prepare_holding_run_env(|config| AgentGatewayConfig { + // Include cold worker compilation without extending the 3s response bound. + run_timeout: std::time::Duration::from_secs(2), + cancellation_grace: std::time::Duration::from_millis(50), + ..config + }); let state = AgentGatewayState::with_agent_source(config, source).expect("RSS source should compile"); let app = build_agent_gateway_app(state); + let (mut arrived_rx, release_tx, fixture) = spawn_holding_fixture(listener); let (create_status, created) = json_request( &app, @@ -4170,9 +4197,10 @@ async fn gateway_restart_recovery_fails_pending_compaction_and_allows_retry() { .expect("the retry compaction should commit"); assert_eq!(committed["results"][0]["kind"], json!("execute")); assert_eq!( - committed["results"][0]["execute"]["rows_affected"], + committed["results"][0]["execute"]["payload"]["rows_affected"], json!(1) ); + assert_eq!(committed["results"][0]["execute"]["tag"], true); let committed_row = restored_persistence .compaction_get("compaction-1") .expect("compaction after commit"); @@ -4651,14 +4679,14 @@ async fn rate_limit_isolates_peer_ips_on_one_listener() { #[tokio::test] async fn keep_running_default_survives_disconnect_and_events_replay_by_cursor() { - let (_, arrived_rx, release_tx, fixture, config, source) = - spawn_holding_run_env(|config| AgentGatewayConfig { - sse_keepalive_interval: Duration::from_millis(100), - ..config - }); + let (listener, config, source) = prepare_holding_run_env(|config| AgentGatewayConfig { + sse_keepalive_interval: Duration::from_millis(100), + ..config + }); let state = AgentGatewayState::with_agent_source(config, source).expect("RSS source should compile"); let server = spawn_gateway_server(state).await; + let (arrived_rx, release_tx, fixture) = spawn_holding_fixture(listener); let (status, _, session) = raw_json( server.addr, @@ -4826,7 +4854,7 @@ async fn cancel_on_disconnect_cancels_when_last_subscriber_disconnects() { sse_keepalive_interval: Duration::from_millis(100), ..AgentGatewayConfig::default() }, - include_str!("fixtures/gateway/cpu_loop.rss"), + include_str!("../fixtures/gateway/cpu_loop.rss"), ) .expect("RSS source should compile"); let server = spawn_gateway_server(state).await; @@ -4906,7 +4934,7 @@ async fn multi_subscriber_and_reconnect_races_never_cancel_while_a_subscriber_re sse_keepalive_interval: Duration::from_millis(100), ..AgentGatewayConfig::default() }, - include_str!("fixtures/gateway/cpu_loop.rss"), + include_str!("../fixtures/gateway/cpu_loop.rss"), ) .expect("RSS source should compile"); let server = spawn_gateway_server(state).await; @@ -5020,7 +5048,7 @@ async fn normal_terminal_end_never_requests_client_disconnect_cancellation() { sse_keepalive_interval: Duration::from_millis(100), ..AgentGatewayConfig::default() }, - include_str!("fixtures/gateway/complete_fast.rss"), + include_str!("../fixtures/gateway/complete_fast.rss"), ) .expect("RSS source should compile"); let server = spawn_gateway_server(state).await; @@ -5095,7 +5123,7 @@ async fn normal_terminal_end_never_requests_client_disconnect_cancellation() { /// exactly one terminal — never two. #[tokio::test] async fn concurrent_stops_commit_exactly_one_terminal() { - let (port, arrived, release, fixture) = spawn_holding_fixture(); + let (listener, port) = bind_fixture_listener(); let http = rustscript_vm::HttpConfig { allowed_hosts: vec!["127.0.0.1".to_string()], allowed_schemes: vec!["http".to_string()], @@ -5121,6 +5149,7 @@ async fn concurrent_stops_commit_exactly_one_terminal() { ) .expect("RSS source should compile"); let app = build_agent_gateway_app(state); + let (arrived, release, fixture) = spawn_holding_fixture(listener); let (status, run) = json_request( &app, axum::http::Method::POST, @@ -5262,7 +5291,7 @@ async fn stop_during_terminal_persist_retry_commits_exactly_one_terminal() { /// replays the complete ordered history. #[tokio::test] async fn sse_subscriber_lag_emits_typed_error_and_replay_recovers() { - let (port, arrived, release, fixture) = spawn_holding_fixture(); + let (listener, port) = bind_fixture_listener(); let http = rustscript_vm::HttpConfig { allowed_hosts: vec!["127.0.0.1".to_string()], allowed_schemes: vec!["http".to_string()], @@ -5295,6 +5324,7 @@ async fn sse_subscriber_lag_emits_typed_error_and_replay_recovers() { let metrics = state.metrics(); let service = state.service(); let app = build_agent_gateway_app(state); + let (arrived, release, fixture) = spawn_holding_fixture(listener); let (status, run) = json_request( &app, axum::http::Method::POST, @@ -5379,7 +5409,7 @@ async fn sse_subscriber_lag_emits_typed_error_and_replay_recovers() { /// restart recovery repairs the durable side exactly once. #[tokio::test] async fn storage_worker_shutdown_mid_run_parks_terminal_and_restart_recovers() { - let (port, arrived, release, fixture) = spawn_holding_fixture(); + let (listener, port) = bind_fixture_listener(); let http = rustscript_vm::HttpConfig { allowed_hosts: vec!["127.0.0.1".to_string()], allowed_schemes: vec!["http".to_string()], @@ -5418,6 +5448,7 @@ async fn storage_worker_shutdown_mid_run_parks_terminal_and_restart_recovers() { .persistence() .expect("persistence handle should be exposed"); let app = build_agent_gateway_app(state); + let (arrived, release, fixture) = spawn_holding_fixture(listener); let (status, run) = json_request( &app, axum::http::Method::POST, @@ -5839,7 +5870,7 @@ async fn combined_guards_gauge_lag_disconnect_and_replay_agree_exactly() { broadcast_capacity: 2, ..AgentGatewayConfig::default() }, - include_str!("fixtures/gateway/cpu_loop.rss"), + include_str!("../fixtures/gateway/cpu_loop.rss"), ) .expect("RSS source should compile"); let metrics = state.metrics(); @@ -5945,7 +5976,7 @@ async fn combined_guards_gauge_lag_disconnect_and_replay_agree_exactly() { // gauge slot, and the registry counts the lag. The subscriber body is // held unread (router oneshot), so the bounded broadcast channel fills // deterministically before the stream is polled. - let (port, arrived, release, fixture) = spawn_holding_fixture(); + let (listener, port) = bind_fixture_listener(); let http = rustscript_vm::HttpConfig { allowed_hosts: vec!["127.0.0.1".to_string()], allowed_schemes: vec!["http".to_string()], @@ -5979,6 +6010,7 @@ async fn combined_guards_gauge_lag_disconnect_and_replay_agree_exactly() { .expect("RSS source should compile"); let metrics = state.metrics(); let app = build_agent_gateway_app(state.clone()); + let (arrived, release, fixture) = spawn_holding_fixture(listener); let (status, run) = json_request( &app, axum::http::Method::POST, diff --git a/tests/gateway_workspace_tests.rs b/tests/cases/gateway_workspace_tests.rs similarity index 99% rename from tests/gateway_workspace_tests.rs rename to tests/cases/gateway_workspace_tests.rs index b04448f..ee6d601 100644 --- a/tests/gateway_workspace_tests.rs +++ b/tests/cases/gateway_workspace_tests.rs @@ -16,7 +16,7 @@ use std::sync::Arc; use std::time::{Duration, Instant}; use tower::ServiceExt; -#[path = "support/gateway_teardown.rs"] +#[path = "../support/gateway_teardown.rs"] mod gateway_teardown; fn private_test_root(prefix: &str) -> PathBuf { @@ -587,6 +587,9 @@ async fn workspace_selector_errors_leave_no_session_and_default_resolves() { .expect("gateway state") .with_workspace_home(home); let service = state.service(); + service + .materialize_cached_runner() + .expect("prepare the real workspace admission runner"); let app = build_agent_gateway_app(state.clone()); let invalid = [ (String::new(), "workspace_selector_blank"), @@ -732,6 +735,9 @@ async fn http_main_runs_without_workspace_and_persists_typed_effect_denials() { provider.push_ok(scripted_guardian_allow_response()); provider.push_ok(scripted_text_response("ordinary Main run completed")); let service = state.service(); + service + .materialize_cached_runner() + .expect("prepare Main and native tools before timed admission"); service.inject_provider_host(Arc::new(provider.clone())); let app = build_agent_gateway_app(state.clone()); @@ -764,7 +770,10 @@ async fn http_main_runs_without_workspace_and_persists_typed_effect_denials() { event["event"] == "tool.failed" && event["data"]["tool_call_id"] == call_id }) .unwrap_or_else(|| panic!("missing durable denial for {call_id}: {events:?}")); - assert_eq!(denied["data"]["error_code"], "workspace_unavailable"); + assert_eq!( + denied["data"]["error_code"], "workspace_unavailable", + "typed denial for {call_id}: {denied:?}" + ); let result = provider_tool_result(&provider, call_id); assert_eq!(result["is_error"], true); assert_eq!(result["error"]["code"], "workspace_unavailable"); diff --git a/tests/http_api_migration_tests.rs b/tests/cases/http_api_migration_tests.rs similarity index 98% rename from tests/http_api_migration_tests.rs rename to tests/cases/http_api_migration_tests.rs index 847f50f..3eece2d 100644 --- a/tests/http_api_migration_tests.rs +++ b/tests/cases/http_api_migration_tests.rs @@ -110,11 +110,11 @@ fn production_and_embedded_http_sources_have_no_map_transport() { source_files(&root.join("rss"), &mut files); source_files(&root.join("examples"), &mut files); for name in [ - "runner_tests.rs", - "gateway_tests.rs", - "metrics_tests.rs", + "cases/runner_tests.rs", + "cases/gateway_tests.rs", + "cases/metrics_tests.rs", "telegram_tests.rs", - "provider_tests.rs", + "cases/provider_tests.rs", ] { files.push(root.join("tests").join(name)); } diff --git a/tests/managed_checkpoint_api_tests.rs b/tests/cases/managed_checkpoint_api_tests.rs similarity index 100% rename from tests/managed_checkpoint_api_tests.rs rename to tests/cases/managed_checkpoint_api_tests.rs diff --git a/tests/managed_checkpoint_restore_tests.rs b/tests/cases/managed_checkpoint_restore_tests.rs similarity index 98% rename from tests/managed_checkpoint_restore_tests.rs rename to tests/cases/managed_checkpoint_restore_tests.rs index a7fb81d..2056247 100644 --- a/tests/managed_checkpoint_restore_tests.rs +++ b/tests/cases/managed_checkpoint_restore_tests.rs @@ -71,6 +71,33 @@ impl rustscript_agent::AgentProviderHost for CancellationGateProvider { } } +// Keep the pre-effect head fixed until the HTTP admission projection has +// been observed. Entry is signalled from the worker's actual provider call. +struct HeldProvider { + first: std::sync::Mutex>, + scripted: Arc, +} + +struct HeldProviderCall { + entered: tokio::sync::oneshot::Sender<()>, + release: std::sync::mpsc::Receiver<()>, +} + +impl rustscript_agent::AgentProviderHost for HeldProvider { + fn call(&self, request: &Value, cancellation: &rustscript_agent::RunCancellation) -> Value { + if let Some(first) = self.first.lock().unwrap().take() { + let _ = first.entered.send(()); + let remaining = cancellation + .remaining_deadline() + .expect("finite run deadline"); + if first.release.recv_timeout(remaining).is_err() { + return json!({"ok":false,"error":{"code":"cancelled","message":"fixture provider released without a response","retryable":false}}); + } + } + self.scripted.call(request, cancellation) + } +} + #[tokio::test(flavor = "multi_thread")] async fn managed_files_checkpoint_restore_new_admission() { roundtrip(false, false, false).await; @@ -151,6 +178,9 @@ async fn roundtrip_case(check_replay: bool, active_source: bool, compacted: bool }) .with_workspace_home(&home); let service = state.service(); + service + .materialize_cached_runner() + .expect("prepare the real workspace and tool runners"); service .set_provider_profile( rustscript_agent::config::ProviderProfile::new( @@ -263,7 +293,7 @@ async fn roundtrip_case(check_replay: bool, active_source: bool, compacted: bool } { tokio::time::sleep(Duration::from_millis(10)).await; } - }).await.unwrap(); + }).await.unwrap_or_else(|error| panic!("source advancement did not finish: {error}; events={:?}", observed.run_events(later_run))); let source_before = persistence.managed_workspace_head(&record.identity.workspace).unwrap(); if !active_source { for index in 0..3 { @@ -317,7 +347,7 @@ async fn roundtrip_case(check_replay: bool, active_source: bool, compacted: bool while !observed.run_events(new_run).iter().any(|event| event["event"] == "run.completed") { tokio::time::sleep(Duration::from_millis(10)).await; } - }).await.unwrap(); + }).await.unwrap_or_else(|error| panic!("restored run did not complete: {error}; events={:?}", observed.run_events(new_run))); let provider_requests = provider.requests(); assert_eq!(provider_requests[0]["provider_options"]["temperature"], 0.25, "restore consumes private frozen safe options"); assert_eq!(provider_requests[0]["provider_options"]["max_output_tokens"], 1024); @@ -1428,6 +1458,9 @@ async fn explicit_workspace_publish_is_a_durable_receipt_not_an_effect_commit() .unwrap() .with_workspace_home(&home); let service = state.service(); + service + .materialize_cached_runner() + .expect("prepare the real workspace and tool runners"); service .set_provider_profile( rustscript_agent::config::ProviderProfile::new( @@ -1613,7 +1646,15 @@ async fn explicit_workspace_publish_is_a_durable_receipt_not_an_effect_commit() let next = Arc::new(ScriptedProvider::new()); next.push_ok(json!({"text":"", "tool_calls":[{"id":"write-c", "name":"write_file", "arguments":{"path":"other.txt", "content":"C"}}], "usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}, "reasoning":"", "stop_reason":"tool_calls"})); next.push_ok(json!({"text":"closed C", "tool_calls":[], "usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}, "reasoning":"", "stop_reason":"stop"})); - observed.inject_provider_host(next); + let (entered, entered_rx) = tokio::sync::oneshot::channel(); + let (release, release_rx) = std::sync::mpsc::channel(); + observed.inject_provider_host(Arc::new(HeldProvider { + first: std::sync::Mutex::new(Some(HeldProviderCall { + entered, + release: release_rx, + })), + scripted: next, + })); let (status, advanced_run) = request( address, "/v1/runs", @@ -1621,11 +1662,18 @@ async fn explicit_workspace_publish_is_a_durable_receipt_not_an_effect_commit() ) .await; assert_eq!(status, 202, "head-advancing admission: {advanced_run}"); + tokio::time::timeout(Duration::from_secs(20), entered_rx) + .await + .expect("worker reaches the actual provider before writing") + .expect("provider entry signal"); assert_eq!( advanced_run["publication"], json!("committed"), "the workspace pointer is already committed at this branch/head" ); + release + .send(()) + .expect("release the head-advancing provider call"); let advanced_id = advanced_run["run_id"].as_str().unwrap().to_string(); wait_terminal(&observed, &advanced_id).await; let advanced_head = persistence diff --git a/tests/metrics_tests.rs b/tests/cases/metrics_tests.rs similarity index 94% rename from tests/metrics_tests.rs rename to tests/cases/metrics_tests.rs index e81934c..ac35525 100644 --- a/tests/metrics_tests.rs +++ b/tests/cases/metrics_tests.rs @@ -38,26 +38,11 @@ fn gateway_db_path(label: &str) -> std::path::PathBuf { gateway_db_path_in(&metrics_test_root(), label) } -/// The path builder itself: the base directory is explicit so the unit test -/// below pins the layout without touching the process-global env var -/// (parallel tests must never set it). fn gateway_db_path_in(root: &std::path::Path, label: &str) -> std::path::PathBuf { std::fs::create_dir_all(root).expect("gateway test root should be created"); root.join(format!("{label}-{}.db", Uuid::new_v4())) } -#[test] -fn metrics_test_artifacts_land_under_an_explicit_root() { - let base = std::env::temp_dir().join(format!("metrics-root-{}", Uuid::new_v4())); - let db = gateway_db_path_in(&base, "layout"); - assert!( - db.starts_with(&base), - "the database must live under the explicit root, got {db:?}" - ); - assert_eq!(db.parent(), Some(base.as_path())); - std::fs::remove_dir_all(&base).expect("temporary root should be removed"); -} - async fn json_request( app: &axum::Router, method: axum::http::Method, @@ -732,16 +717,37 @@ fn spawn_holding_fixture_pair() -> ( std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>, ) { - use std::io::Write; + use std::io::{BufRead, BufReader, Write}; let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).expect("bind fixture"); let port = listener.local_addr().expect("fixture address").port(); let (arrived_tx, arrived_rx) = tokio::sync::oneshot::channel(); let (arrived_tx_2, arrived_rx_2) = tokio::sync::oneshot::channel(); let (release_tx, release_rx) = std::sync::mpsc::channel(); + let read_headers = |stream: &std::net::TcpStream| { + stream + .set_read_timeout(Some(std::time::Duration::from_secs(5))) + .unwrap(); + let mut reader = BufReader::new(stream); + let mut line = String::new(); + assert!(reader.read_line(&mut line).unwrap() > 0); + assert!(line.starts_with("GET / HTTP/1."), "expected worker request"); + loop { + line.clear(); + assert!( + reader.read_line(&mut line).unwrap() > 0, + "complete request headers" + ); + if line == "\r\n" { + break; + } + } + }; let handle = std::thread::spawn(move || { let (mut first, _) = listener.accept().expect("accept first fixture request"); + read_headers(&first); let _ = arrived_tx.send(()); let (mut second, _) = listener.accept().expect("accept second fixture request"); + read_headers(&second); let _ = arrived_tx_2.send(()); release_rx.recv().expect("wait for release"); for stream in [&mut first, &mut second] { @@ -803,45 +809,37 @@ async fn admission_accepted_and_typed_rejections_are_counted() { let metrics = state.metrics(); let app = build_agent_gateway_app(state); - let (status, run) = json_request_with_headers( - &app, - axum::http::Method::POST, - "/v1/runs", - json!({"input": "keyed"}), - &[("idempotency-key", "admission-K")], - ) - .await; - assert_eq!(status, StatusCode::ACCEPTED); - let first_run = serde_json::from_str::(&run).expect("run json"); - - // Same key, different body: typed idempotency conflict. - let (status, _) = json_request_with_headers( + let (status, _) = json_request( &app, axum::http::Method::POST, "/v1/runs", - json!({"input": "different-body"}), - &[("idempotency-key", "admission-K")], + json!({"input": "orphan", "parent_run_id": "missing-parent"}), ) .await; - assert_eq!(status, StatusCode::CONFLICT); + assert_eq!(status, StatusCode::NOT_FOUND); let (status, _) = json_request( &app, axum::http::Method::POST, "/v1/runs", - json!({"input": "orphan", "parent_run_id": "missing-parent"}), + json!({"input": "ghost", "session_id": "missing-session"}), ) .await; assert_eq!(status, StatusCode::NOT_FOUND); - let (status, _) = json_request( + // Keep only the capacity probe inside the two finite HTTP holds. + // A replay comparison can compile RSS on a cache miss under the full suite. + let (status, run) = json_request_with_headers( &app, axum::http::Method::POST, "/v1/runs", - json!({"input": "ghost", "session_id": "missing-session"}), + json!({"input": "keyed"}), + &[("idempotency-key", "admission-K")], ) .await; - assert_eq!(status, StatusCode::NOT_FOUND); + assert_eq!(status, StatusCode::ACCEPTED); + let first_run = serde_json::from_str::(&run).expect("run json"); + first_run["run_id"].as_str().expect("run id"); let (status, second) = json_request( &app, @@ -853,7 +851,13 @@ async fn admission_accepted_and_typed_rejections_are_counted() { assert_eq!(status, StatusCode::ACCEPTED); assert!(second["run_id"].is_string()); + // Observe both real requests before testing capacity, while the fixture + // still owns both responses. + for arrived in arrived { + arrived.await.expect("both runs must reach the fixture"); + } // Capacity 2 is exhausted by the two held runs. + let active_at_capacity_probe = metrics.snapshot().active_runs; let (status, _) = json_request( &app, axum::http::Method::POST, @@ -861,13 +865,35 @@ async fn admission_accepted_and_typed_rejections_are_counted() { json!({"input": "third"}), ) .await; + let held = metrics.snapshot(); + release.send(()).expect("release the fixture"); + fixture.join().expect("fixture thread"); + assert_eq!( + active_at_capacity_probe, 2, + "both HTTP holds must still own capacity before the third admission" + ); assert_eq!(status, StatusCode::TOO_MANY_REQUESTS); + assert_eq!(held.active_runs, 2); + + assert!( + wait_until(std::time::Duration::from_secs(10), || { + metrics.snapshot().active_runs == 0 + }) + .await, + "released workers must retire their capacity before the conflict probe" + ); + // Same key, different body: typed idempotency conflict. The admission + // receipt is still live, but neither HTTP hold has to survive this work. + let (status, _) = json_request_with_headers( + &app, + axum::http::Method::POST, + "/v1/runs", + json!({"input": "different-body"}), + &[("idempotency-key", "admission-K")], + ) + .await; + assert_eq!(status, StatusCode::CONFLICT); - // Both runs must actually be parked on the fixture before asserting the - // active gauge (deterministic barrier, no sleeps). - for arrived in arrived { - arrived.await.expect("both runs must reach the fixture"); - } let snapshot = metrics.snapshot(); assert_eq!(snapshot.admissions_accepted, 2); assert_eq!( @@ -886,12 +912,6 @@ async fn admission_accepted_and_typed_rejections_are_counted() { snapshot.admissions_rejected_by(AdmitRejectReason::RunLimitReached), 1 ); - assert_eq!(snapshot.active_runs, 2); - - release.send(()).expect("release the fixture"); - fixture.join().expect("fixture thread"); - let run_id = first_run["run_id"].as_str().expect("run id").to_string(); - let _ = run_id; } #[tokio::test] @@ -903,10 +923,23 @@ async fn health_detailed_and_metrics_share_one_gauge_snapshot() { http: http_config(port), ..AgentGatewayConfig::default() }, - holding_source(port), + format!( + r#" + use http; + use stream; + pub fn run(input: map) -> string {{ + let request = http::request::new("GET", "http://127.0.0.1:{port}/"); + let response = http::client::request(request); + stream::emit({{type: "model.delta", delta: "loopback returned"}}); + while true {{ 1; }} + "unreachable" + }} + "# + ), ) .expect("RSS source should compile"); let metrics = state.metrics(); + let service = state.service(); let app = build_agent_gateway_app(state); let (status, first) = json_request( @@ -944,6 +977,26 @@ async fn health_detailed_and_metrics_share_one_gauge_snapshot() { assert_eq!(metrics.snapshot().active_runs, 2); assert!(metrics.render_prometheus().contains("agent_active_runs 2")); + // Complete the actual HTTP operation before exercising VM stop. The HTTP + // bridge has no cancellation acknowledgement for a pending host operation. + release.send(()).expect("release the fixture"); + fixture.join().expect("fixture thread"); + let returned = wait_until(std::time::Duration::from_secs(10), || { + [&first, &second].iter().all(|run| { + service + .run_events(run["run_id"].as_str().unwrap()) + .iter() + .any(|event| { + event["event"] == "model.delta" && event["data"]["delta"] == "loopback returned" + }) + }) + }) + .await; + assert!( + returned, + "both workers must return from loopback and enter the guest stop phase" + ); + // Stop both runs; each commits exactly one cancelled terminal. for run in [&first, &second] { let run_id = run["run_id"].as_str().expect("run id"); @@ -957,13 +1010,17 @@ async fn health_detailed_and_metrics_share_one_gauge_snapshot() { assert_eq!(stop_status, StatusCode::OK); assert_eq!(stop["status"], "stopping"); } - release.send(()).expect("release the fixture"); - fixture.join().expect("fixture thread"); let finished = wait_until(std::time::Duration::from_secs(10), || { - metrics.snapshot().active_runs == 0 + let snapshot = metrics.snapshot(); + snapshot.active_runs == 0 && snapshot.runs_terminal_by(TerminalStatus::Cancelled) == 2 }) .await; - assert!(finished, "both runs must commit their terminals"); + assert!( + finished, + "both runs must commit their terminals: first={:?}; second={:?}", + service.run_events(first["run_id"].as_str().unwrap()), + service.run_events(second["run_id"].as_str().unwrap()) + ); let snapshot = metrics.snapshot(); assert_eq!(snapshot.runs_terminal_by(TerminalStatus::Cancelled), 2); diff --git a/tests/native_callback_rss_tests.rs b/tests/cases/native_callback_rss_tests.rs similarity index 98% rename from tests/native_callback_rss_tests.rs rename to tests/cases/native_callback_rss_tests.rs index 686d9c8..66d18e8 100644 --- a/tests/native_callback_rss_tests.rs +++ b/tests/cases/native_callback_rss_tests.rs @@ -424,7 +424,7 @@ fn native_pkce_and_callback_wait_work_through_restricted_typed_rss() { .invoke_callable(wait.clone(), std::slice::from_ref(&callback)) .expect("native loopback callback wait through typed RSS"); assert_eq!(field(&waited, "outcome"), Value::string("success")); - let code = field(&waited, "code_handle"); + let code = some_slot(field(&waited, "code_handle")); assert!(matches!(code, Value::Int(_))); let receipt = browser.read_receipt(); assert!(receipt.starts_with("HTTP/1.1 200 OK")); @@ -733,6 +733,17 @@ fn native_owner_revoke_and_vm_reset_close_callback_listener() { assert_callback_port_released(&reset_redirect); } +/// One converted `Option>` slot: asserts the canonical +/// `{tag, payload}` carrier the host publishes and returns its payload — the live +/// `Value::Int` resource handle. +fn some_slot(slot: impl std::borrow::Borrow) -> Value { + let slot = slot.borrow(); + let (some, payload) = rustscript_vm::option::payload(slot) + .unwrap_or_else(|error| panic!("slot is not a canonical Option carrier: {error}")); + assert!(some, "expected a Some carrier, got {slot:?}"); + payload.cloned().expect("Some carries a payload") +} + #[cfg(feature = "config-fixture")] #[test] fn native_owner_does_not_enable_fixture_callback_injection() { diff --git a/tests/native_callback_tests.rs b/tests/cases/native_callback_tests.rs similarity index 100% rename from tests/native_callback_tests.rs rename to tests/cases/native_callback_tests.rs diff --git a/tests/native_exchange_pair_tests.rs b/tests/cases/native_exchange_pair_tests.rs similarity index 100% rename from tests/native_exchange_pair_tests.rs rename to tests/cases/native_exchange_pair_tests.rs diff --git a/tests/native_manual_input_tests.rs b/tests/cases/native_manual_input_tests.rs similarity index 100% rename from tests/native_manual_input_tests.rs rename to tests/cases/native_manual_input_tests.rs diff --git a/tests/native_payload_transport_tests.rs b/tests/cases/native_payload_transport_tests.rs similarity index 100% rename from tests/native_payload_transport_tests.rs rename to tests/cases/native_payload_transport_tests.rs diff --git a/tests/oauth_callback_bridge_tests.rs b/tests/cases/oauth_callback_bridge_tests.rs similarity index 98% rename from tests/oauth_callback_bridge_tests.rs rename to tests/cases/oauth_callback_bridge_tests.rs index ad227fe..6c7dac2 100644 --- a/tests/oauth_callback_bridge_tests.rs +++ b/tests/cases/oauth_callback_bridge_tests.rs @@ -186,6 +186,10 @@ struct QuotaResource; #[cfg(feature = "config-fixture")] impl HostResource for QuotaResource {} +#[cfg(feature = "config-fixture")] +/// One converted `Option>` slot: asserts the canonical +/// `{tag, payload}` carrier the host publishes and returns its payload — the live +/// `Value::Int` resource handle. #[cfg(feature = "config-fixture")] fn fill_resource_arena(vm: &mut Vm, target: usize) -> Vec { let mut handles = Vec::new(); @@ -284,19 +288,25 @@ fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected named result, got {value:?}"); }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value.clone()), + Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing {name} in {value:?}")) + .unwrap_or_else(|| panic!("missing {name} in {value:?}")); + let value = match name { + "code_handle" => crate::typed_carriers::nullable(field), + _ => field, + }; + value.clone() } #[cfg(feature = "config-fixture")] fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> Value { let callable = vm.resolve_exported_callable(name).expect("RSS export"); - vm.invoke_callable(callable, args).expect("RSS call") + vm.invoke_callable(callable, args) + .unwrap_or_else(|err| panic!("{name}: {err:?}")) } fn public_query(name: &str, value: &str) -> Value { @@ -417,13 +427,15 @@ fn catalog_exposes_exact_resource_backed_pkce_callback_contract() { let HostTypeSchema::Named { fields, .. } = &wait.return_type else { panic!("wait result must be a named envelope"); }; - let HostTypeSchema::Optional(code_type) = &fields + // A callback may deliver no code, so the slot is a logical + // `Option>` carrier. + let HostTypeSchema::Option(code_type) = &fields .iter() .find(|field| field.name == "code_handle") .expect("code field") .ty else { - panic!("callback code handle must be optional"); + panic!("callback code handle must be a logical Option"); }; assert_eq!( **code_type, @@ -597,6 +609,7 @@ fn restricted_real_rss_waits_once_and_callback_close_preserves_siblings() { assert_eq!(field(&waited, "outcome"), Value::string("success")); let code = field(&waited, "code_handle"); assert!(matches!(code, Value::Int(_))); + // `verifier` comes from `pkce_begin`, whose handles are concrete resources. assert!(matches!(verifier, Value::Int(_))); assert!(!format!("{waited:?}").contains(SYNTHETIC_CODE)); assert!(!format!("{waited:?}").contains("code_verifier")); @@ -1579,6 +1592,7 @@ fn owner_cancel_during_rss_browser_begin(revoke_owner: bool, name: &str) { assert_eq!(field(&waited_b, "outcome"), Value::string("success")); let code_b = field(&waited_b, "code_handle"); assert!(matches!(code_b, Value::Int(_))); + // `verifier_b` comes from `pkce_begin`, a concrete resource handle. assert!(matches!(verifier_b, Value::Int(_))); assert_eq!( auth_b diff --git a/tests/oauth_capture_bridge_tests.rs b/tests/cases/oauth_capture_bridge_tests.rs similarity index 95% rename from tests/oauth_capture_bridge_tests.rs rename to tests/cases/oauth_capture_bridge_tests.rs index 5ef2a9a..d2dce5c 100644 --- a/tests/oauth_capture_bridge_tests.rs +++ b/tests/cases/oauth_capture_bridge_tests.rs @@ -1,3 +1,5 @@ +use crate::credential_use; + #[cfg(feature = "config-fixture")] use rustscript_agent::CancellationReason; use rustscript_agent::auth::oauth::{ @@ -171,10 +173,7 @@ fn failure_capture_args(policy: &Value, device: &Value) -> [Value; 4] { "/oauth/token", r#"{"grant_type":"device_code"}"#, ), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -428,13 +427,20 @@ fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected named record, got {value:?}"); }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value.clone()), + Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing field {name}")) + .unwrap_or_else(|| panic!("missing field {name}")); + let value = match name { + "code_handle" | "verifier_handle" | "device_handle" | "access_slot" | "refresh_slot" => { + crate::typed_carriers::nullable(field) + } + _ => field, + }; + value.clone() } fn record(fields: Vec<(&str, Value)>) -> Value { @@ -531,10 +537,7 @@ fn actual_rss_capture_injects_device_secret_and_returns_opaque_exchange_pair() { "/oauth/token", r#"{"grant_type":"device_code"}"#, ), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -569,10 +572,7 @@ fn actual_rss_capture_injects_device_secret_and_returns_opaque_exchange_pair() { "/oauth/token", r#"{"grant_type":"device_code"}"#, ), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -720,12 +720,12 @@ fn capture_catalog_has_exact_four_typed_arguments_and_nested_pair_envelope() { ); assert!(matches!( &fields[3].ty, - HostTypeSchema::Optional(inner) + HostTypeSchema::Option(inner) if **inner == HostTypeSchema::Resource(ResourceTypeKey::new("oauth.authorization_code").expect("code type")) )); assert!(matches!( &fields[4].ty, - HostTypeSchema::Optional(inner) + HostTypeSchema::Option(inner) if **inner == HostTypeSchema::Resource(ResourceTypeKey::new("oauth.verifier").expect("verifier type")) )); @@ -848,10 +848,10 @@ fn invalid_capture_selectors_and_pending_response_keep_device_retryable() { "capture", &[ valid_request(), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles( + "device_session", + &[("device_handle", device.clone())], + ), valid_payload(), intent, ], @@ -863,29 +863,17 @@ fn invalid_capture_selectors_and_pending_response_keep_device_retryable() { } for (usage, payload) in [ + (credential_use::none("none"), valid_payload()), ( - record(vec![("kind", Value::string("none"))]), - valid_payload(), - ), - ( - record(vec![ - ("kind", Value::string("access")), - ("access_handle", access.clone()), - ]), + credential_use::with_handles("access", &[("access_handle", access.clone())]), valid_payload(), ), ( - record(vec![ - ("kind", Value::string("refresh")), - ("refresh_handle", refresh.clone()), - ]), + credential_use::with_handles("refresh", &[("refresh_handle", refresh.clone())]), valid_payload(), ), ( - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("none")), @@ -920,10 +908,7 @@ fn invalid_capture_selectors_and_pending_response_keep_device_retryable() { "capture", &[ valid_request(), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), valid_payload(), record(vec![ ("code_field", Value::string("code")), @@ -956,10 +941,7 @@ fn invalid_capture_selectors_and_pending_response_keep_device_retryable() { "capture", &[ valid_request(), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), valid_payload(), record(vec![ ("code_field", Value::string("code")), @@ -978,11 +960,13 @@ fn invalid_capture_selectors_and_pending_response_keep_device_retryable() { "capture", &[ valid_request(), - record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code.clone()), - ("verifier_handle", verifier.clone()), - ]), + credential_use::with_handles( + "code_verifier", + &[ + ("code_handle", code.clone()), + ("verifier_handle", verifier.clone()), + ], + ), valid_payload(), record(vec![ ("code_field", Value::string("code")), @@ -1055,10 +1039,7 @@ fn resource_limit_rejects_both_capture_handles_and_reclaims_native_pair() { "capture", &[ request(policy, "/oauth/token", r#"{"grant_type":"device_code"}"#), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), @@ -1127,10 +1108,7 @@ fn owner_cancelled_after_capture_response_reclaims_device_before_pair_publicatio "capture", &[ request(policy, "/oauth/token", r#"{"grant_type":"device_code"}"#), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), record(vec![ ("encoding", Value::string("json_object")), ("secret_fields", Value::string("device")), diff --git a/tests/oauth_code_exchange_bridge_tests.rs b/tests/cases/oauth_code_exchange_bridge_tests.rs similarity index 91% rename from tests/oauth_code_exchange_bridge_tests.rs rename to tests/cases/oauth_code_exchange_bridge_tests.rs index cc66943..bf32c85 100644 --- a/tests/oauth_code_exchange_bridge_tests.rs +++ b/tests/cases/oauth_code_exchange_bridge_tests.rs @@ -33,7 +33,7 @@ use rustscript_agent::{ auth::host::{AuthHost, auth_host_catalog}, }; use rustscript_vm::{ - CompileSourceFileOptions, HostFunctionRegistry, HostParamPassing, HostTypeSchema, + CompileSourceFileOptions, HostFunctionRegistry, HostParamPassing, HostTypeSchema, Program, ResourceTypeKey, SourceFlavor, Value, Vm, VmStatus, compile_source_at_path_with_flavor_and_options, }; @@ -113,22 +113,34 @@ impl Drop for TempHome { } fn compile_vm(auth: &AuthHost) -> Vm { - let path = - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/auth/code_exchange_contract.rss"); - let source = fs::read_to_string(&path).expect("real code exchange RSS"); - compile_vm_source(auth, &path, &source) + bind_exchange_vm(auth, exchange_program().clone()) +} + +fn exchange_program() -> &'static Arc { + static PROGRAM: std::sync::OnceLock> = std::sync::OnceLock::new(); + PROGRAM.get_or_init(|| { + let path = + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/auth/code_exchange_contract.rss"); + let source = fs::read_to_string(&path).expect("real code exchange RSS"); + compile_exchange_program(&path, &source) + }) } -fn compile_vm_source(auth: &AuthHost, path: &Path, source: &str) -> Vm { +fn compile_exchange_program(path: &Path, source: &str) -> Arc { let catalog = auth_host_catalog(); let compiled = compile_source_at_path_with_flavor_and_options( path, source, SourceFlavor::RustScript, - CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()), + CompileSourceFileOptions::default().with_host_api_catalog(catalog), ) .expect("compile RSS against exact auth host catalog"); - let mut vm = Vm::new_shared(Arc::new(compiled.program)); + Arc::new(compiled.program) +} + +fn bind_exchange_vm(auth: &AuthHost, program: Arc) -> Vm { + let catalog = auth_host_catalog(); + let mut vm = Vm::new_shared(program); let mut registry = HostFunctionRegistry::restricted(); auth.register(&mut registry, catalog.as_ref()) .expect("register exact catalog"); @@ -149,7 +161,7 @@ pub fn test_access_handle(policy: resource) -> resource) -> null { +pub fn test_close_access(handle: resource) -> () { auth::close_access(handle) } "#; @@ -160,7 +172,7 @@ fn compile_vm_with_probes(auth: &AuthHost) -> Vm { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/auth/code_exchange_contract.rss"); let mut source = fs::read_to_string(&path).expect("real code exchange RSS"); source.push_str(TEST_TRANSPORT_PROBES); - compile_vm_source(auth, &path, &source) + bind_exchange_vm(auth, compile_exchange_program(&path, &source)) } #[test] @@ -189,8 +201,8 @@ fn catalog_keeps_transport_pair_optional_nominal_and_borrowed() { .iter() .find(|field| field.name == field_name) .unwrap_or_else(|| panic!("missing credential-use field {field_name}")); - let HostTypeSchema::Optional(inner) = &field.ty else { - panic!("credential-use field {field_name} must remain optional"); + let HostTypeSchema::Option(inner) = &field.ty else { + panic!("credential-use field {field_name} must be a logical Option"); }; assert_eq!( **inner, @@ -226,6 +238,15 @@ fn call(vm: &mut Vm, name: &str, args: &[Value]) -> Value { vm.invoke_callable(entry, args).expect("RSS call") } +#[cfg(feature = "config-fixture")] +fn option_field(value: &Value, name: &str) -> Option { + match rustscript_vm::option::payload(&field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value.clone()), + _ => panic!("invalid Option payload for {name}"), + } +} + fn public_intent() -> Value { public_intent_for("fixture-codex") } @@ -256,6 +277,24 @@ fn public_intent_for(credential_id: &str) -> Value { ]) } +#[cfg(feature = "config-fixture")] +fn code_use(code: Value, verifier: Option, access: Option) -> Value { + let carried = |value: Option| { + value.map_or_else( + rustscript_vm::option::none_carrier, + rustscript_vm::option::some_carrier, + ) + }; + record(vec![ + ("kind", Value::string("code_verifier")), + ("access_handle", carried(access)), + ("refresh_handle", carried(None)), + ("code_handle", carried(Some(code))), + ("verifier_handle", carried(verifier)), + ("device_handle", carried(None)), + ]) +} + #[cfg(feature = "config-fixture")] fn public_request(policy: Value, path: &str, public_body: &str) -> Value { public_request_for("fixture-codex", policy, path, public_body) @@ -313,7 +352,7 @@ fn ready_pair_for( auth.fixture_inject_matching_callback(vm, &callback, CALLBACK_CODE) .expect("host-injected matching callback"); let waited = call(vm, "wait", std::slice::from_ref(&callback)); - let code = field(&waited, "code_handle"); + let code = option_field(&waited, "code_handle").expect("issued callback code"); let request = public_request_for( credential_id, policy, @@ -449,9 +488,9 @@ struct ExchangeWorker { #[cfg(feature = "config-fixture")] impl ExchangeWorker { - fn spawn(auth: Arc, cancellation: RunCancellation) -> Self { + fn spawn(program: Arc, auth: Arc, cancellation: RunCancellation) -> Self { let join = thread::spawn(move || { - let mut vm = compile_vm(&auth); + let mut vm = bind_exchange_vm(&auth, program); let (policy, _, code, verifier) = ready_exchange(&auth, &mut vm); let request = public_request(policy, "/oauth/token", "client_id=worker"); let exchange = vm @@ -508,10 +547,16 @@ fn credential_metadata(generation: i64) -> Value { "scopes", Value::array(vec![Value::string("scope.synthetic")]), ), - ("account_id", Value::string("rss-test-account")), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string("rss-test-account")), + ), ("generation", Value::Int(generation)), ("status", Value::string("active")), - ("last_refresh_at_ms", Value::Int(1_790_000_000_000)), + ( + "last_refresh_at_ms", + rustscript_vm::option::some_carrier(Value::Int(1_790_000_000_000)), + ), ("has_refresh_token", Value::Bool(true)), ]) } @@ -530,7 +575,10 @@ fn save_request( ("metadata", credential_metadata(expected_generation)), ("access_slot", access_slot), ("refresh_action", Value::string("replace")), - ("refresh_slot", refresh_slot), + ( + "refresh_slot", + rustscript_vm::option::some_carrier(refresh_slot), + ), ]) } @@ -574,7 +622,7 @@ fn real_rss_exchange_mints_captured_generation_slots_and_saves_by_cas() { auth.fixture_inject_matching_callback(&mut vm, &callback, CALLBACK_CODE) .expect("host-injected matching callback"); let waited = call(&mut vm, "wait", &[callback]); - let code = field(&waited, "code_handle"); + let code = option_field(&waited, "code_handle").expect("issued callback code"); let request = record(vec![ ("policy_handle", policy.clone()), ("credential_id", Value::string("fixture-codex")), @@ -593,8 +641,8 @@ fn real_rss_exchange_mints_captured_generation_slots_and_saves_by_cas() { for secret in [CALLBACK_CODE, NEXT_ACCESS, NEXT_REFRESH] { assert!(!response_text.contains(secret), "RSS response redaction"); } - let access_slot = field(&response, "access_slot"); - let refresh_slot = field(&response, "refresh_slot"); + let access_slot = option_field(&response, "access_slot").expect("access slot"); + let refresh_slot = option_field(&response, "refresh_slot").expect("refresh slot"); assert!(matches!(access_slot, Value::Int(_)) && matches!(refresh_slot, Value::Int(_))); let sent = transport.take_sent(); @@ -667,7 +715,7 @@ fn save_request_preserving(policy: Value, expected_generation: i64, access_slot: ("metadata", credential_metadata(expected_generation)), ("access_slot", access_slot), ("refresh_action", Value::string("preserve")), - ("refresh_slot", Value::Null), + ("refresh_slot", rustscript_vm::option::none_carrier()), ]) } @@ -722,8 +770,8 @@ fn concurrent_store_change_after_begin_keeps_captured_generation_and_does_not_ov &[save_request( policy_a, 4, - field(&exchange, "access_slot"), - field(&exchange, "refresh_slot"), + option_field(&exchange, "access_slot").expect("access slot"), + option_field(&exchange, "refresh_slot").expect("refresh slot"), )], ); assert_eq!(field(&stale_save, "outcome"), Value::string("adopted")); @@ -785,11 +833,7 @@ fn foreign_owner_flow_pair_cannot_cross_vm_arena() { let policy_a = field(&snapshot_a, "policy_handle"); let (_policy_b, request_b, code_b, verifier_b) = ready_exchange(&auth_b, &mut vm_b); let request_a = public_request(policy_a, "/oauth/token", "client_id=owner-a"); - let foreign_usage = record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code_b.clone()), - ("verifier_handle", verifier_b.clone()), - ]); + let foreign_usage = code_use(code_b.clone(), Some(verifier_b.clone()), None); let direct = vm_a .resolve_exported_callable("test_direct_transport") .expect("typed probe"); @@ -863,21 +907,13 @@ fn wrong_class_and_forged_ids_reject_without_closing_the_valid_pair() { .resolve_exported_callable("test_direct_transport") .expect("typed probe"); - let wrong_class = record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", callback), - ("verifier_handle", verifier.clone()), - ]); + let wrong_class = code_use(callback, Some(verifier.clone()), None); let failure = vm .invoke_callable(direct.clone(), &[request.clone(), wrong_class]) .expect_err("callback resource is not a code resource"); assert!(failure.to_string().contains("resource_type_key_mismatch")); - let forged = record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", Value::Int(999_999_999)), - ("verifier_handle", verifier.clone()), - ]); + let forged = code_use(Value::Int(999_999_999), Some(verifier.clone()), None); let _failure = vm .invoke_callable(direct, &[request.clone(), forged]) .expect_err("forged resource id"); @@ -916,11 +952,7 @@ fn mixed_two_flow_pair_is_rejected_and_both_flows_remain_usable() { let direct = vm .resolve_exported_callable("test_direct_transport") .expect("typed probe"); - let mixed = record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code_a.clone()), - ("verifier_handle", verifier_b.clone()), - ]); + let mixed = code_use(code_a.clone(), Some(verifier_b.clone()), None); let failure = vm .invoke_callable(direct, &[request_a.clone(), mixed]) .expect_err("mixed flow pair"); @@ -964,11 +996,7 @@ fn cross_credential_pair_is_rejected_without_consuming_either_flow() { let direct = vm .resolve_exported_callable("test_direct_transport") .expect("typed probe"); - let wrong_credential = record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code_a.clone()), - ("verifier_handle", verifier_a.clone()), - ]); + let wrong_credential = code_use(code_a.clone(), Some(verifier_a.clone()), None); let failure = vm .invoke_callable(direct, &[request_b.clone(), wrong_credential]) .expect_err("first credential pair cannot authorize second credential"); @@ -1011,21 +1039,13 @@ fn malformed_code_use_shapes_reject_without_effect_and_valid_pair_remains_usable .resolve_exported_callable("test_direct_transport") .expect("typed probe"); - let incomplete = record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code.clone()), - ]); + let incomplete = code_use(code.clone(), None, None); let failure = vm .invoke_callable(direct.clone(), &[request.clone(), incomplete]) .expect_err("incomplete pair"); assert!(failure.to_string().contains("credential_use_unsupported")); - let mixed = record(vec![ - ("kind", Value::string("code_verifier")), - ("access_handle", access.clone()), - ("code_handle", code.clone()), - ("verifier_handle", verifier.clone()), - ]); + let mixed = code_use(code.clone(), Some(verifier.clone()), Some(access.clone())); let failure = vm .invoke_callable(direct.clone(), &[request.clone(), mixed]) .expect_err("mixed kinds"); @@ -1033,8 +1053,17 @@ fn malformed_code_use_shapes_reject_without_effect_and_valid_pair_remains_usable let unknown = record(vec![ ("kind", Value::string("code_verifier")), - ("code_handle", code.clone()), - ("verifier_handle", verifier.clone()), + ("access_handle", rustscript_vm::option::none_carrier()), + ("refresh_handle", rustscript_vm::option::none_carrier()), + ( + "code_handle", + rustscript_vm::option::some_carrier(code.clone()), + ), + ( + "verifier_handle", + rustscript_vm::option::some_carrier(verifier.clone()), + ), + ("device_handle", rustscript_vm::option::none_carrier()), ("unexpected", Value::Bool(true)), ]); let failure = vm @@ -1139,7 +1168,7 @@ fn inactive_owner_rejection_cleans_only_its_valid_pair() { &[request_a.clone(), code_a, verifier_a], ) .expect_err("inactive owner A"); - assert!(failure.to_string().contains("policy_inactive")); + assert!(failure.to_string().contains("policy_inactive"), "{failure}"); assert_eq!( auth_a .fixture_live_oauth_flow_count() @@ -1148,11 +1177,7 @@ fn inactive_owner_rejection_cleans_only_its_valid_pair() { ); assert_eq!(vm_a.host_context().resource_count(), 2); - let foreign_usage = record(vec![ - ("kind", Value::string("code_verifier")), - ("code_handle", code_b.clone()), - ("verifier_handle", verifier_b.clone()), - ]); + let foreign_usage = code_use(code_b.clone(), Some(verifier_b.clone()), None); let direct = vm_a .resolve_exported_callable("test_direct_transport") .expect("typed probe"); @@ -1203,8 +1228,8 @@ fn provider_error_response_consumes_pair_without_minting_slots() { ) .expect("sanitized provider error response"); assert_eq!(field(&result, "status"), Value::Int(400)); - assert_eq!(field(&result, "access_slot"), Value::Null); - assert_eq!(field(&result, "refresh_slot"), Value::Null); + assert!(option_field(&result, "access_slot").is_none()); + assert!(option_field(&result, "refresh_slot").is_none()); assert_eq!( auth.fixture_live_oauth_flow_count() .expect("native flow count"), @@ -1270,6 +1295,7 @@ fn transport_failure_consumes_pair_and_mints_no_slots() { #[cfg(feature = "config-fixture")] #[test] fn scoped_transport_observes_owner_cancel_and_revoke_without_stopping_owner_b() { + let program = exchange_program(); let home = TempHome::new("owner-cancel"); let (entered_tx, entered_rx) = mpsc::channel(); let transport = Arc::new(OwnerScopedTransport { @@ -1289,7 +1315,7 @@ fn scoped_transport_observes_owner_cancel_and_revoke_without_stopping_owner_b() let cancellation_a = RunCancellation::with_timeout(Duration::from_secs(30)); let auth_a = fixture_auth_with_oauth(&home, cancellation_a.clone(), oauth.clone()); - let mut worker_a = ExchangeWorker::spawn(auth_a.clone(), cancellation_a.clone()); + let mut worker_a = ExchangeWorker::spawn(program.clone(), auth_a, cancellation_a.clone()); assert_eq!( entered_rx .recv_timeout(Duration::from_secs(10)) @@ -1304,7 +1330,7 @@ fn scoped_transport_observes_owner_cancel_and_revoke_without_stopping_owner_b() let cancellation_c = RunCancellation::with_timeout(Duration::from_secs(30)); let auth_c = fixture_auth_with_oauth(&home, cancellation_c.clone(), oauth.clone()); - let mut worker_c = ExchangeWorker::spawn(auth_c.clone(), cancellation_c.clone()); + let mut worker_c = ExchangeWorker::spawn(program.clone(), auth_c.clone(), cancellation_c); assert_eq!( entered_rx .recv_timeout(Duration::from_secs(10)) @@ -1341,6 +1367,7 @@ fn scoped_transport_observes_owner_cancel_and_revoke_without_stopping_owner_b() #[cfg(feature = "config-fixture")] #[test] fn scoped_transport_deadline_reaches_cooperative_send_and_reclaims_pair() { + let program = exchange_program(); let home = TempHome::new("transport-deadline"); let (entered_tx, entered_rx) = mpsc::channel(); let transport = Arc::new(OwnerScopedTransport { @@ -1359,7 +1386,7 @@ fn scoped_transport_deadline_reaches_cooperative_send_and_reclaims_pair() { )); let cancellation = RunCancellation::with_timeout(Duration::from_secs(30)); let auth = fixture_auth_with_oauth(&home, cancellation.clone(), oauth); - let mut worker = ExchangeWorker::spawn(auth.clone(), cancellation); + let mut worker = ExchangeWorker::spawn(program.clone(), auth, cancellation); assert_eq!( entered_rx .recv_timeout(Duration::from_secs(10)) diff --git a/tests/oauth_code_generation_bridge_tests.rs b/tests/cases/oauth_code_generation_bridge_tests.rs similarity index 94% rename from tests/oauth_code_generation_bridge_tests.rs rename to tests/cases/oauth_code_generation_bridge_tests.rs index 28b72f2..5cf6331 100644 --- a/tests/oauth_code_generation_bridge_tests.rs +++ b/tests/cases/oauth_code_generation_bridge_tests.rs @@ -50,16 +50,16 @@ pub fn exchange_for_save( ) -> TransportResultEnvelope { let usage: TransportCredentialUse = { kind: "code_verifier", - access_handle: null, - refresh_handle: null, - code_handle: code, - verifier_handle: verifier, - device_handle: null + access_handle: None, + refresh_handle: None, + code_handle: Some(code), + verifier_handle: Some(verifier), + device_handle: None }; let payload: TransportPayloadIntent = { encoding: "form", secret_fields: "authorization_code", - device_field: null, + device_field: "", code_field: "code" }; oauth::transport_payload_result(&request, &usage, payload) @@ -79,10 +79,10 @@ pub fn save_selected( token_type: "Bearer", expires_at_ms: 1900003600000, scopes: ["scope.synthetic"], - account_id: "synthetic-generation-bridge", + account_id: Some("synthetic-generation-bridge"), generation: expected_generation, status: "active", - last_refresh_at_ms: 1900000000000, + last_refresh_at_ms: Some(1900000000000), has_refresh_token: has_refresh_token }; let request: SaveCredentialRequest = { @@ -92,12 +92,12 @@ pub fn save_selected( metadata: metadata, access_slot: access_slot, refresh_action: refresh_action, - refresh_slot: null + refresh_slot: None }; auth::save_if_generation_result(&request) } -pub fn cleanup(policy: resource) -> null { +pub fn cleanup(policy: resource) -> () { auth::cleanup_transients(&policy) } "#; @@ -341,6 +341,14 @@ fn compile_vm(auth: &AuthHost) -> Vm { vm } +fn option_field(value: &Value, name: &str) -> Option { + match rustscript_vm::option::payload(&field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value.clone()), + _ => panic!("invalid Option payload for {name}"), + } +} + fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected RSS map: {value:?}"); @@ -430,7 +438,7 @@ fn captured_generation(vm: &mut Vm, policy: &Value, code: &Value, verifier: &Val Value::Bool(true), "captured generation: {result:?}" ); - let Value::Int(generation) = field(&result, "value") else { + let Value::Int(generation) = option_field(&result, "value").expect("present value") else { panic!("captured generation must be an integer"); }; generation @@ -447,9 +455,9 @@ fn exchange_slot(vm: &mut Vm, policy: &Value, client: &str, code: Value, verifie Value::Bool(true), "native exchange: {result:?}" ); - let response = field(&result, "value"); + let response = option_field(&result, "value").expect("present value"); assert_eq!(field(&response, "status"), Value::Int(200)); - field(&response, "access_slot") + option_field(&response, "access_slot").expect("present access_slot") } fn bind_policy(auth: &AuthHost, vm: &mut Vm) -> Value { @@ -469,7 +477,7 @@ fn ready_pair( let verifier = field(&begun, "verifier_handle"); let waited = call(vm, "wait", std::slice::from_ref(&callback)); browser.release_callback_stream(); - let code = field(&waited, "code_handle"); + let code = option_field(&waited, "code_handle").expect("present code_handle"); #[cfg(feature = "config-fixture")] assert!( _auth @@ -515,7 +523,7 @@ fn captured_generation_adopts_concurrent_writer_without_overwrite() { Value::Bool(true), "writer result: {writer:?}" ); - let writer_value = field(&writer, "value"); + let writer_value = option_field(&writer, "value").expect("present value"); assert_eq!(field(&writer_value, "outcome"), Value::string("committed")); assert_eq!( field(&field(&writer_value, "metadata"), "generation"), @@ -539,7 +547,7 @@ fn captured_generation_adopts_concurrent_writer_without_overwrite() { Value::Bool(true), "captured save: {adopted:?}" ); - let adopted_value = field(&adopted, "value"); + let adopted_value = option_field(&adopted, "value").expect("present value"); assert_eq!(field(&adopted_value, "outcome"), Value::string("adopted")); assert_eq!( field(&field(&adopted_value, "metadata"), "generation"), @@ -610,7 +618,7 @@ fn native_first_enrollment_captures_zero_and_commits_generation_one() { Value::Bool(true), "first enrollment save: {saved:?}" ); - let saved_value = field(&saved, "value"); + let saved_value = option_field(&saved, "value").expect("present value"); assert_eq!(field(&saved_value, "outcome"), Value::string("committed")); assert_eq!( field(&field(&saved_value, "metadata"), "generation"), @@ -649,7 +657,10 @@ fn same_owner_mismatched_pair_rejects_without_revoking_either_family() { ); assert_eq!(field(&mismatched, "ok"), Value::Bool(false)); assert_eq!( - field(&field(&mismatched, "error"), "code"), + field( + &option_field(&mismatched, "error").expect("present error"), + "code" + ), Value::string("handle_provenance") ); assert_eq!( @@ -761,7 +772,7 @@ fn owner_cancellation_rolls_back_captured_family() { &[policy.clone(), code, verifier], ); assert_eq!(field(&result, "ok"), Value::Bool(false)); - let failure = field(&result, "error"); + let failure = option_field(&result, "error").expect("present error"); assert_eq!(field(&failure, "code"), Value::string("policy_inactive")); assert_eq!(field(&failure, "cancelled"), Value::Bool(true)); assert_eq!(transport.calls(), 0); @@ -799,7 +810,10 @@ fn oauth_clock_expiry_rolls_back_captured_family() { ); assert_eq!(field(&result, "ok"), Value::Bool(false)); assert_eq!( - field(&field(&result, "error"), "code"), + field( + &option_field(&result, "error").expect("present error"), + "code" + ), Value::string("handle_expired") ); assert_eq!(transport.calls(), 0); diff --git a/tests/oauth_config_public_tests.rs b/tests/cases/oauth_config_public_tests.rs similarity index 87% rename from tests/oauth_config_public_tests.rs rename to tests/cases/oauth_config_public_tests.rs index 02a3bd8..879f5e1 100644 --- a/tests/oauth_config_public_tests.rs +++ b/tests/cases/oauth_config_public_tests.rs @@ -338,10 +338,35 @@ const TYPED_RSS: &str = r#" use config; struct OAuthConfigProjection { - scopes: array?, - authorization_parameters: array?, - first_parameter_name: string?, - first_parameter_value: string? + scopes: Option>, + authorization_parameters: Option>, + first_parameter_name: Option, + first_parameter_value: Option +} + +struct QueryEntryProjection { name: Option, value: Option } + +fn first_parameter(parameters: array) -> QueryEntryProjection { + if parameters.length > 0 => { + let first: PublicQueryEntry = parameters[0].copy(); + {name: Some(first.name), value: Some(first.value)} + } else => { + {name: None, value: None} + } +} + +fn project_oauth(oauth: OAuthConfig) -> OAuthConfigProjection { + let parameters: Option> = oauth.authorization_parameters; + let first: QueryEntryProjection = match parameters.copy() { + Some(values) => first_parameter(values), + None => {name: None, value: None} + }; + { + scopes: oauth.scopes, + authorization_parameters: parameters, + first_parameter_name: first.name, + first_parameter_value: first.value + } } pub fn read_config(home: resource) -> OAuthConfigProjection { @@ -349,28 +374,9 @@ pub fn read_config(home: resource) -> OAuthConfigProjection { let public_config: BoundedPublicConfig = snapshot.public_config; let providers: array = public_config.providers; let provider: ProviderEntry = providers[0].copy(); - let mut scopes: array? = null; - let mut parameters: array? = null; - if provider.oauth.copy() != null { - let oauth: OAuthConfig = provider.oauth.copy(); - scopes = oauth.scopes; - parameters = oauth.authorization_parameters; - } - let mut first_name: string? = null; - let mut first_value: string? = null; - if parameters != null { - let present_parameters: array = parameters; - if present_parameters.length > 0 { - let first: PublicQueryEntry = present_parameters[0].copy(); - first_name = first.name; - first_value = first.value; - } - } - { - scopes: scopes, - authorization_parameters: parameters, - first_parameter_name: first_name, - first_parameter_value: first_value + match provider.oauth { + Some(oauth) => project_oauth(oauth), + None => {scopes: None, authorization_parameters: None, first_parameter_name: None, first_parameter_value: None} } } "#; @@ -418,6 +424,14 @@ fn value_field(value: &Value, name: &str) -> Value { .unwrap_or_else(|| panic!("missing RSS field {name}: {value:?}")) } +fn option_field(value: &Value, name: &str) -> Option { + match rustscript_vm::option::payload(&value_field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value.clone()), + _ => panic!("invalid Option payload for {name}"), + } +} + fn invoke_projection(vm: &mut Vm, home: &AuthHost) -> Value { let context = home.bind_vm(vm).expect("bind genuine host home"); let host_home = value_field(&context, "host_home"); @@ -456,28 +470,22 @@ fn project_typed_fields(fields: &str, label: &str) -> Value { #[test] fn real_auth_host_snapshot_projects_absent_and_explicit_empty_options() { let absent = project_typed_fields(" flow: fixture\n", "typed-absent"); - assert_eq!(optional_value_field(&absent, "scopes"), None); - assert_eq!( - optional_value_field(&absent, "authorization_parameters"), - None - ); - assert_eq!(optional_value_field(&absent, "first_parameter_name"), None); - assert_eq!(optional_value_field(&absent, "first_parameter_value"), None); + assert_eq!(option_field(&absent, "scopes"), None); + assert_eq!(option_field(&absent, "authorization_parameters"), None); + assert_eq!(option_field(&absent, "first_parameter_name"), None); + assert_eq!(option_field(&absent, "first_parameter_value"), None); let empty = project_typed_fields( " flow: fixture\n scopes: []\n authorization_parameters: []\n", "typed-empty", ); + assert_eq!(option_field(&empty, "scopes"), Some(Value::array(vec![]))); assert_eq!( - optional_value_field(&empty, "scopes"), - Some(Value::array(vec![])) - ); - assert_eq!( - optional_value_field(&empty, "authorization_parameters"), + option_field(&empty, "authorization_parameters"), Some(Value::array(vec![])) ); - assert_eq!(optional_value_field(&empty, "first_parameter_name"), None); - assert_eq!(optional_value_field(&empty, "first_parameter_value"), None); + assert_eq!(option_field(&empty, "first_parameter_name"), None); + assert_eq!(option_field(&empty, "first_parameter_value"), None); } #[test] @@ -498,14 +506,14 @@ fn real_auth_host_snapshot_is_read_through_typed_restricted_rss() { let projected = invoke_projection(&mut vm, &host); assert_eq!( - value_field(&projected, "scopes"), + option_field(&projected, "scopes").expect("projected field"), Value::array(vec![ Value::string("scope:first"), Value::string("scope with space"), Value::string("東京"), ]) ); - let parameters = value_field(&projected, "authorization_parameters"); + let parameters = option_field(&projected, "authorization_parameters").expect("projected field"); let Value::Array(entries) = parameters else { panic!("expected typed PublicQueryEntry array, got {parameters:?}"); }; @@ -516,11 +524,11 @@ fn real_auth_host_snapshot_is_read_through_typed_restricted_rss() { Value::string("value with space+plus&ersand&東京") ); assert_eq!( - value_field(&projected, "first_parameter_name"), + option_field(&projected, "first_parameter_name").expect("projected field"), Value::string("audience") ); assert_eq!( - value_field(&projected, "first_parameter_value"), + option_field(&projected, "first_parameter_value").expect("projected field"), Value::string("value with space+plus&ersand&東京") ); assert_eq!( diff --git a/tests/oauth_device_bridge_tests.rs b/tests/cases/oauth_device_bridge_tests.rs similarity index 95% rename from tests/oauth_device_bridge_tests.rs rename to tests/cases/oauth_device_bridge_tests.rs index 754f4a6..488c145 100644 --- a/tests/oauth_device_bridge_tests.rs +++ b/tests/cases/oauth_device_bridge_tests.rs @@ -1,3 +1,5 @@ +use crate::credential_use; + use rustscript_agent::{ CancellationReason, RunCancellation, auth::{ @@ -56,6 +58,9 @@ impl BoundedHttpsTransport for CancelInsidePoll { } } +/// One converted `Option>` slot: asserts the canonical +/// `{tag, payload}` carrier the host publishes and returns its payload — the live +/// `Value::Int` resource handle. fn fixture_home() -> PathBuf { let root = PathBuf::from(std::env::var("RUSTSCRIPT_AGENT_TEST_TMP").expect("assigned temp")); let home = root.join(format!("oauth-device-red-{}", uuid::Uuid::new_v4())); @@ -119,34 +124,36 @@ fn device_contract_vm_with_cancellation( ) -> (Vm, AuthHost, Value) { const SAVE_PROBE: &str = r#" pub fn save_slots(policy: resource, access: resource, refresh: resource, expected: int) -> string { - let saved = auth::save_if_generation({ + let request: SaveCredentialRequest = { credential_id: "fixture-codex", expected_generation: expected, policy_handle: policy, metadata: { provider: "openai-codex", kind: "oauth", source: "synthetic-device-test", token_type: "Bearer", expires_at_ms: 1900000000000, scopes: [], - account_id: "synthetic-device-account", generation: expected, - status: "active", last_refresh_at_ms: 1790000000000, has_refresh_token: true + account_id: Some("synthetic-device-account"), generation: expected, + status: "active", last_refresh_at_ms: Some(1790000000000), has_refresh_token: true }, access_slot: access, refresh_action: "replace", - refresh_slot: refresh - }); + refresh_slot: Some(refresh) + }; + let saved = auth::save_if_generation(&request); saved.outcome } pub fn save_access(policy: resource, access: resource, expected: int) -> int { - let saved = auth::save_if_generation({ + let request: SaveCredentialRequest = { credential_id: "fixture-codex", expected_generation: expected, policy_handle: policy, metadata: { provider: "openai-codex", kind: "oauth", source: "synthetic-device-race", token_type: "Bearer", expires_at_ms: 1900000000000, scopes: [], - account_id: "synthetic-device-account", generation: expected, - status: "active", last_refresh_at_ms: 1790000000000, has_refresh_token: true + account_id: Some("synthetic-device-account"), generation: expected, + status: "active", last_refresh_at_ms: Some(1790000000000), has_refresh_token: true }, - access_slot: access, refresh_action: "preserve" - }); + access_slot: access, refresh_action: "preserve", refresh_slot: None + }; + let saved = auth::save_if_generation(&request); saved.metadata.generation } @@ -237,8 +244,8 @@ fn catalog_adds_exact_typed_device_fields_and_take_owned_close() { .iter() .find(|field| field.name == field_name) .unwrap_or_else(|| panic!("missing {field_name}")); - let HostTypeSchema::Optional(inner) = &field.ty else { - panic!("{field_name} must remain optional"); + let HostTypeSchema::Option(inner) = &field.ty else { + panic!("{field_name} must be a logical Option"); }; assert_eq!( **inner, @@ -252,8 +259,8 @@ fn catalog_adds_exact_typed_device_fields_and_take_owned_close() { .iter() .find(|field| field.name == "device_handle") .expect("additive response handle"); - let HostTypeSchema::Optional(inner) = &device_field.ty else { - panic!("device response handle is optional"); + let HostTypeSchema::Option(inner) = &device_field.ty else { + panic!("device response handle is a logical Option"); }; assert_eq!( **inner, @@ -269,7 +276,7 @@ fn catalog_adds_exact_typed_device_fields_and_take_owned_close() { close.params[0].ty, HostTypeSchema::Resource(ResourceTypeKey::new("oauth.device_session").expect("key")) ); - assert_eq!(close.return_type, HostTypeSchema::Null); + assert_eq!(close.return_type, HostTypeSchema::Unit); } fn transport_request(policy: Value, id: &str, path: &str, body: &str) -> Value { @@ -287,13 +294,17 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { let Value::Map(entries) = value else { panic!("expected RSS record") }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing {name}")) + .unwrap_or_else(|| panic!("missing {name}")); + match name { + "device_handle" | "access_slot" | "refresh_slot" => crate::typed_carriers::nullable(field), + _ => field, + } } #[test] @@ -929,10 +940,7 @@ fn inactive_owner_cleanup_accepts_only_exact_device_usage() { "/oauth/token", "client_id=inactive-a", ); - let mixed = record(vec![ - ("kind", Value::string("none")), - ("device_handle", device_a.clone()), - ]); + let mixed = credential_use::with_handles("none", &[("device_handle", device_a.clone())]); let failure = vm .invoke_callable(direct.clone(), &[request_a.clone(), mixed]) .expect_err("inactive mixed usage is rejected"); @@ -942,10 +950,7 @@ fn inactive_owner_cleanup_accepts_only_exact_device_usage() { 4, "mixed family is not reclaimed" ); - let valid = record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device_a), - ]); + let valid = credential_use::with_handles("device_session", &[("device_handle", device_a)]); let failure = vm .invoke_callable(direct, &[request_a, valid]) .expect_err("inactive exact device use preserves original owner error"); @@ -1088,28 +1093,30 @@ fn unknown_and_mixed_device_usage_do_not_consume_a_valid_sibling() { let direct = vm .resolve_exported_callable("direct_transport") .expect("typed direct probe"); + let Value::Map(exact_usage) = + credential_use::with_handles("device_session", &[("device_handle", device.clone())]) + else { + unreachable!("fixture constructs a named record") + }; + let mut with_extra: Vec<_> = exact_usage + .iter() + .map(|(k, v)| (k.clone(), v.clone())) + .collect(); + with_extra.push((Value::string("unexpected"), Value::Bool(true))); let malformed = [ + (Value::map(with_extra), "credential_use_unsupported"), ( - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ("unexpected", Value::Bool(true)), - ]), + credential_use::with_handles("none", &[("device_handle", device.clone())]), "credential_use_unsupported", ), ( - record(vec![ - ("kind", Value::string("none")), - ("device_handle", device.clone()), - ]), - "credential_use_unsupported", - ), - ( - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ("access_handle", device.clone()), - ]), + credential_use::with_handles( + "device_session", + &[ + ("device_handle", device.clone()), + ("access_handle", device.clone()), + ], + ), "resource_type_key_mismatch", ), ]; diff --git a/tests/oauth_flow_tests.rs b/tests/cases/oauth_flow_tests.rs similarity index 100% rename from tests/oauth_flow_tests.rs rename to tests/cases/oauth_flow_tests.rs diff --git a/tests/oauth_payload_bridge_tests.rs b/tests/cases/oauth_payload_bridge_tests.rs similarity index 93% rename from tests/oauth_payload_bridge_tests.rs rename to tests/cases/oauth_payload_bridge_tests.rs index 45f2b50..00a76c1 100644 --- a/tests/oauth_payload_bridge_tests.rs +++ b/tests/cases/oauth_payload_bridge_tests.rs @@ -1,3 +1,5 @@ +use crate::credential_use; + use rustscript_agent::{ RunCancellation, auth::{ @@ -48,6 +50,9 @@ impl Drop for FixtureHome { } } +/// One converted `Option>` slot: asserts the canonical +/// `{tag, payload}` carrier the host publishes and returns its payload — the live +/// `Value::Int` resource handle. fn vm_for(auth: &AuthHost) -> Vm { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/auth/payload_contract.rss"); let source = fs::read_to_string(&path).expect("real payload contract RSS"); @@ -74,13 +79,18 @@ fn field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected named record, got {value:?}"); }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value.clone()), + Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing field {name} in {value:?}")) + .unwrap_or_else(|| panic!("missing field {name} in {value:?}")); + let value = match name { + "code_handle" | "device_handle" => crate::typed_carriers::nullable(field), + _ => field, + }; + value.clone() } fn record(fields: Vec<(&str, Value)>) -> Value { @@ -220,10 +230,7 @@ fn malformed_payload_selector_does_not_send_and_live_device_can_retry() { let call_args = [ request(policy.clone(), "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), payload( "json_object", "device", @@ -247,10 +254,7 @@ fn malformed_payload_selector_does_not_send_and_live_device_can_retry() { "selected", &[ request(policy, "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), payload( "json_object", "device", @@ -309,10 +313,7 @@ fn real_rss_selected_device_auth_id_json_poll_injects_native_secret() { "selected", &[ request(policy, public_body), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), payload( "json_object", "device", @@ -429,10 +430,10 @@ fn all_device_selector_aliases_use_native_json_or_form_body_injection() { "selected", &[ request_with_content_type(policy.clone(), body, content_type), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles( + "device_session", + &[("device_handle", device.clone())], + ), payload(encoding, "device", Some(("device_field", selector))), ], ); @@ -515,10 +516,10 @@ fn malformed_typed_payload_is_value_free_and_keeps_live_device_retryable() { "selected", &[ request(policy.clone(), "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles( + "device_session", + &[("device_handle", device.clone())], + ), malformed, ], ) @@ -556,10 +557,10 @@ fn malformed_typed_payload_is_value_free_and_keeps_live_device_retryable() { "selected", &[ request(policy.clone(), "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles( + "device_session", + &[("device_handle", device.clone())], + ), malformed, ], ) @@ -580,10 +581,7 @@ fn malformed_typed_payload_is_value_free_and_keeps_live_device_retryable() { "selected", &[ request(policy, "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), payload("json_object", "device", Some(("device_field", "device_id"))), ], ); @@ -619,7 +617,7 @@ fn selected_form_without_content_type_is_canonicalized_and_none_keeps_public_bod "client_id=public-client¬e=literal+%26code%3Dplain", None, ), - record(vec![("kind", Value::string("none"))]), + credential_use::none("none"), record(vec![ ("encoding", Value::string("form")), ("secret_fields", Value::string("none")), @@ -665,10 +663,7 @@ fn selected_native_header_and_retry_denials_keep_credentials_private() { "selected", &[ request(policy.clone(), r#"{"public":true}"#), - record(vec![ - ("kind", Value::string("access")), - ("access_handle", access.clone()), - ]), + credential_use::with_handles("access", &[("access_handle", access.clone())]), payload("json_object", "none", None), ], ); @@ -702,10 +697,10 @@ fn selected_native_header_and_retry_denials_keep_credentials_private() { "selected", &[ bad_request, - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles( + "device_session", + &[("device_handle", device.clone())], + ), payload( "json_object", "device", @@ -727,10 +722,7 @@ fn selected_native_header_and_retry_denials_keep_credentials_private() { "selected", &[ request(policy.clone(), "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), payload( "json_object", "device", @@ -749,10 +741,7 @@ fn selected_native_header_and_retry_denials_keep_credentials_private() { "selected", &[ request(policy, "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), payload( "json_object", "device", @@ -809,10 +798,7 @@ fn selected_transport_rejects_cross_vm_and_credential_mismatch_before_io() { "selected", &[ request(second_policy, "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), payload( "json_object", "device", @@ -831,10 +817,7 @@ fn selected_transport_rejects_cross_vm_and_credential_mismatch_before_io() { "selected", &[ request_for_credential(first_policy.clone(), "not-admitted", "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), payload( "json_object", "device", @@ -857,10 +840,7 @@ fn selected_transport_rejects_cross_vm_and_credential_mismatch_before_io() { "selected", &[ request(first_policy.clone(), "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device.clone()), - ]), + credential_use::with_handles("device_session", &[("device_handle", device.clone())]), payload( "json_object", "device", @@ -888,10 +868,7 @@ fn selected_transport_rejects_cross_vm_and_credential_mismatch_before_io() { "selected", &[ request(first_policy, "{}"), - record(vec![ - ("kind", Value::string("device_session")), - ("device_handle", device), - ]), + credential_use::with_handles("device_session", &[("device_handle", device)]), payload( "json_object", "device", diff --git a/tests/oauth_response_sanitizer_rss_tests.rs b/tests/cases/oauth_response_sanitizer_rss_tests.rs similarity index 97% rename from tests/oauth_response_sanitizer_rss_tests.rs rename to tests/cases/oauth_response_sanitizer_rss_tests.rs index acf2a6e..c3e1c41 100644 --- a/tests/oauth_response_sanitizer_rss_tests.rs +++ b/tests/cases/oauth_response_sanitizer_rss_tests.rs @@ -1,3 +1,5 @@ +use crate::credential_use as credential_use_support; + use std::{fs, path::PathBuf, sync::Arc, time::Duration}; use rustscript_agent::{ @@ -74,13 +76,17 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { let Value::Map(entries) = value else { panic!("expected RSS record, got {value:?}"); }; - entries + let field = entries .iter() .find_map(|(key, value)| match key { Value::String(key) if key.as_ref() == name => Some(value), _ => None, }) - .unwrap_or_else(|| panic!("missing RSS field {name}")) + .unwrap_or_else(|| panic!("missing RSS field {name}")); + match name { + "value" | "error" | "int_value" | "string_value" => crate::typed_carriers::nullable(field), + _ => field, + } } fn compile_vm(home: &FixtureHome, transport: Arc) -> (Vm, AuthHost, Value) { @@ -147,14 +153,7 @@ fn transport_request(policy: &Value) -> Value { } fn credential_use() -> Value { - record(vec![ - ("kind", Value::string("none")), - ("access_handle", Value::Null), - ("refresh_handle", Value::Null), - ("code_handle", Value::Null), - ("verifier_handle", Value::Null), - ("device_handle", Value::Null), - ]) + credential_use_support::none("none") } fn send_response(vm: &mut Vm, policy: &Value) -> Value { diff --git a/tests/oauth_rss_entry_tests.rs b/tests/cases/oauth_rss_entry_tests.rs similarity index 96% rename from tests/oauth_rss_entry_tests.rs rename to tests/cases/oauth_rss_entry_tests.rs index b87b37a..aa41dfb 100644 --- a/tests/oauth_rss_entry_tests.rs +++ b/tests/cases/oauth_rss_entry_tests.rs @@ -370,13 +370,18 @@ fn fully_typed_oauth_schema(schema: &HostTypeSchema) -> bool { HostTypeSchema::Array(inner) | HostTypeSchema::Optional(inner) => { fully_typed_oauth_schema(inner) } + HostTypeSchema::Option(inner) => fully_typed_oauth_schema(inner), + HostTypeSchema::Result { ok, err } => { + fully_typed_oauth_schema(ok) && fully_typed_oauth_schema(err) + } HostTypeSchema::Named { fields, .. } => fields .iter() .all(|field| fully_typed_oauth_schema(&field.ty)), HostTypeSchema::Callable { params, result } => { params.iter().all(fully_typed_oauth_schema) && fully_typed_oauth_schema(result) } - HostTypeSchema::Null + HostTypeSchema::Unit + | HostTypeSchema::Null | HostTypeSchema::Int | HostTypeSchema::Float | HostTypeSchema::Number @@ -464,10 +469,16 @@ fn oauth_catalog_guard_rejects_the_real_dynamic_fixture_schema() { .iter() .find(|f| f.name == "oauth::transport") .unwrap(); - assert!(matches!(raw.return_type, HostTypeSchema::Map(_))); + assert!(matches!( + &raw.return_type, + HostTypeSchema::Named { name, fields } if name == "OAuthFixtureEnvelope" && fields.is_empty() + )); + // The fixture's return migrated to a trivially typed empty struct, so the + // untyped witness now lives on the parameters: the guard must still reject + // the raw fixture because at least one parameter is untyped. assert!( - !fully_typed_oauth_schema(&raw.return_type), - "real raw fixture return must be rejected" + raw.params.iter().any(|p| !fully_typed_oauth_schema(&p.ty)), + "real raw fixture must stay inadmissible through its parameters" ); assert!( raw.params.iter().all(|p| !fully_typed_oauth_schema(&p.ty)), diff --git a/tests/oauth_timing_bridge_tests.rs b/tests/cases/oauth_timing_bridge_tests.rs similarity index 98% rename from tests/oauth_timing_bridge_tests.rs rename to tests/cases/oauth_timing_bridge_tests.rs index bf759c0..a438e68 100644 --- a/tests/oauth_timing_bridge_tests.rs +++ b/tests/cases/oauth_timing_bridge_tests.rs @@ -15,7 +15,7 @@ use rustscript_vm::{ compile_source_at_path_with_flavor_and_options, }; -const TIMING_SOURCE: &str = include_str!("../rss/auth/timing_contract.rss"); +const TIMING_SOURCE: &str = include_str!("../../rss/auth/timing_contract.rss"); struct TempHome(PathBuf); @@ -163,7 +163,7 @@ fn timing_rss_contract_compiles_against_the_exact_catalog() { assert_eq!(wait.params[1].name, "millis"); assert_eq!(wait.params[1].passing, HostParamPassing::Value); assert_eq!(wait.params[1].ty, HostTypeSchema::Int); - assert_eq!(wait.return_type, HostTypeSchema::Null); + assert_eq!(wait.return_type, HostTypeSchema::Unit); compile_source_at_path_with_flavor_and_options( PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/auth/timing_contract.rss"), TIMING_SOURCE, @@ -318,17 +318,17 @@ fn auth_entry_runner_executes_timing_and_returns_a_redacted_command_envelope() { struct TimingCommand { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } struct TimingEnvelope { ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option } pub fn run(home: resource, command: TimingCommand) -> TimingEnvelope { @@ -342,7 +342,7 @@ fn auth_entry_runner_executes_timing_and_returns_a_redacted_command_envelope() { credential_id: command.credential_id, generation: snapshot.policy_generation, status: "timing_complete", - error: null + error: None } } "#; diff --git a/tests/oauth_transport_bridge_tests.rs b/tests/cases/oauth_transport_bridge_tests.rs similarity index 95% rename from tests/oauth_transport_bridge_tests.rs rename to tests/cases/oauth_transport_bridge_tests.rs index c75db86..8b4822a 100644 --- a/tests/oauth_transport_bridge_tests.rs +++ b/tests/cases/oauth_transport_bridge_tests.rs @@ -204,8 +204,14 @@ fn rss_no_credential_transport_returns_sanitized_public_body_and_host_slots() { field(&response, "body_without_secret_fields"), &Value::string(r#"{"public":"accepted"}"#) ); - assert!(matches!(field(&response, "access_slot"), Value::Int(_))); - assert!(matches!(field(&response, "refresh_slot"), Value::Int(_))); + assert!(matches!( + some_slot(field(&response, "access_slot")), + Value::Int(_) + )); + assert!(matches!( + some_slot(field(&response, "refresh_slot")), + Value::Int(_) + )); for secret in ["SYNTHETIC_ACCESS_TOKEN", "SYNTHETIC_REFRESH_TOKEN"] { assert!(!format!("{response:?}").contains(secret)); } @@ -251,7 +257,10 @@ fn access_transport_injects_bearer_and_preserves_slot_generation() { field(&response, "body"), &Value::string(r#"{"public":"ok"}"#) ); - assert!(matches!(field(&response, "access_slot"), Value::Int(_))); + assert!(matches!( + some_slot(field(&response, "access_slot")), + Value::Int(_) + )); assert!(!format!("{response:?}").contains("SYNTHETIC_NEW_ACCESS")); let sent = transport.take_sent(); assert_eq!(sent.len(), 1); @@ -491,7 +500,7 @@ fn refresh_provider_body_above_credential_bound_is_rejected_before_send() { ); assert_eq!(field(&result, "outcome"), &Value::string("")); assert!(transport.take_sent().is_empty(), "zero send on rejection"); - let failure = field(&result, "failure"); + let failure = crate::typed_carriers::nullable(field(&result, "failure")); assert_eq!(field(failure, "cancelled"), &Value::Bool(false)); assert_eq!(field(failure, "deadline_exceeded"), &Value::Bool(false)); assert_eq!(field(failure, "revoked"), &Value::Bool(false)); @@ -618,8 +627,14 @@ fn refresh_transport_injects_form_once_and_closes_alias() { ]; let response = invoke(&mut vm, "send_refresh", &args).expect("refresh send"); assert_eq!(field(&response, "status"), &Value::Int(200)); - assert!(matches!(field(&response, "access_slot"), Value::Int(_))); - assert!(matches!(field(&response, "refresh_slot"), Value::Int(_))); + assert!(matches!( + some_slot(field(&response, "access_slot")), + Value::Int(_) + )); + assert!(matches!( + some_slot(field(&response, "refresh_slot")), + Value::Int(_) + )); assert!(!format!("{response:?}").contains("SYNTHETIC_NEW_REFRESH")); let sent = transport.take_sent(); assert_eq!(sent.len(), 1); @@ -806,7 +821,9 @@ fn transport_slot_saves_only_at_issued_generation() { Value::string(""), ]; let response = invoke(&mut vm, "send_access", &args).expect("response"); - let slot = field(&response, "access_slot").clone(); + // The slot is a logical `Option>`; the resource + // the save probe needs is its `Some` payload. + let slot = some_slot(field(&response, "access_slot")); let wrong = [policy.clone(), slot.clone(), Value::Int(3)]; assert!(invoke(&mut vm, "save_transport_slot", &wrong).is_err()); let right = [policy, slot.clone(), Value::Int(4)]; @@ -985,17 +1002,16 @@ fn rss_transport_never_exposes_hostile_response_headers() { assert!(!debug.contains(secret), "{debug}"); assert!(!debug.contains("SYNTHETIC_ACCESS_TOKEN"), "{debug}"); assert_eq!(field(&response, "status"), &Value::Int(200)); + // `retry_after_ms` is a declared logical `Option` slot, so its + // absence is now a tagged `None` carrier rather than a missing key. + let retry_slot = field(&response, "retry_after_ms"); + let (retry_some, retry_payload) = + rustscript_vm::option::payload(retry_slot).expect("canonical Option carrier"); if expected_retry == Value::Null { - let Value::Map(entries) = &response else { - panic!("expected map") - }; - assert!( - !entries - .iter() - .any(|(key, _)| key == &Value::string("retry_after_ms")) - ); + assert!(!retry_some, "no retry value is exposed: {debug}"); } else { - assert_eq!(field(&response, "retry_after_ms"), &expected_retry); + assert!(retry_some, "the retry value is a Some carrier: {debug}"); + assert_eq!(retry_payload, Some(&expected_retry)); } let Value::Array(headers) = field(&response, "public_headers") else { panic!("typed headers") @@ -1007,7 +1023,10 @@ fn rss_transport_never_exposes_hostile_response_headers() { field(&headers[0], "value"), &Value::string("application/json") ); - assert!(matches!(field(&response, "access_slot"), Value::Int(_))); + assert!(matches!( + some_slot(field(&response, "access_slot")), + Value::Int(_) + )); } else { assert!(headers.is_empty(), "{debug}"); } @@ -1255,7 +1274,7 @@ fn supported_device_handle_and_accompanying_slots_close_without_leaks() { let mut source = fs::read_to_string(&path).expect("real typed device RSS"); source.push_str( r#" -pub fn close_slots(access: resource, refresh: resource) -> null { +pub fn close_slots(access: resource, refresh: resource) -> () { auth::close_slot(access); auth::close_slot(refresh) } @@ -1309,9 +1328,11 @@ pub fn close_slots(access: resource, refresh: resource>`; the handle + // the close probes need is the `Some` payload. + let device = some_slot(field(&started, "device_handle")); + let access = some_slot(field(&started, "access_slot")); + let refresh = some_slot(field(&started, "refresh_slot")); assert!(matches!(device, Value::Int(_)), "typed device capability"); assert!(matches!(access, Value::Int(_)), "typed access slot"); assert!(matches!(refresh, Value::Int(_)), "typed refresh slot"); @@ -1444,6 +1465,18 @@ fn copied_cross_vm_policy_is_denied_before_transport() { assert_eq!(transport.take_sent().len(), 1); } +/// One converted `Option>` slot: asserts the canonical +/// `{tag, payload}` carrier the host now publishes and returns its payload — the +/// live `Value::Int` resource handle. The conversion replaced the physical null +/// with a tag, so a bare nil/absent slot is no longer the declared shape. +fn some_slot(slot: impl std::borrow::Borrow) -> Value { + let slot = slot.borrow(); + let (some, payload) = rustscript_vm::option::payload(slot) + .unwrap_or_else(|error| panic!("slot is not a canonical Option carrier: {error}")); + assert!(some, "expected a Some carrier, got {slot:?}"); + payload.cloned().expect("Some carries a payload") +} + #[cfg(feature = "config-fixture")] #[test] fn partial_slot_issue_rolls_back_first_slot() { diff --git a/tests/pkce_session_rss_tests.rs b/tests/cases/pkce_session_rss_tests.rs similarity index 91% rename from tests/pkce_session_rss_tests.rs rename to tests/cases/pkce_session_rss_tests.rs index 7836122..142619c 100644 --- a/tests/pkce_session_rss_tests.rs +++ b/tests/cases/pkce_session_rss_tests.rs @@ -32,8 +32,8 @@ const MODULE: &str = "/__rustscript_agent_auth_bundle__/rss/auth/pkce_session.rs const CALLBACK_CODE: &str = "PKCE_SESSION_SYNTHETIC_CALLBACK_CODE"; const TEST_SOURCE: &str = r#" use auth; use config; use oauth; use self::pkce_session as session; -struct PkceStartResult { ok: bool, code: string, selected_mode: string, fallback_used: bool, value: PkceBeginEnvelope? } -struct PkceSessionResult { ok: bool, code: string, selected_mode: string, fallback_used: bool, begin: PkceBeginEnvelope?, waited: CallbackEnvelope? } +struct PkceStartResult { ok: bool, code: string, selected_mode: string, fallback_used: bool, value: Option } +struct PkceSessionResult { ok: bool, code: string, selected_mode: string, fallback_used: bool, begin: Option, waited: Option } pub fn policy(home: resource) -> resource { let snapshot: ConfigSnapshotEnvelope = config::load_snapshot(&home); snapshot.policy_handle } pub fn start(policy: resource, intent: BoundedPublicOAuthIntent, allow: bool) -> PkceStartResult { session::begin(policy, intent, allow) } pub fn finish_session(start: PkceStartResult) -> PkceSessionResult { session::finish(start) } @@ -41,9 +41,9 @@ pub fn run_session(policy: resource, intent: BoundedPublicOAuthInte let started: PkceStartResult = session::begin(policy, intent, allow); session::finish(started) } -pub fn close_callback(handle: resource) -> null { oauth::close_callback(handle) } -pub fn close_verifier(handle: resource) -> null { oauth::close_verifier(handle) } -pub fn close_code(handle: resource) -> null { oauth::close_code(handle) } +pub fn close_callback(handle: resource) -> () { oauth::close_callback(handle) } +pub fn close_verifier(handle: resource) -> () { oauth::close_verifier(handle) } +pub fn close_code(handle: resource) -> () { oauth::close_code(handle) } pub fn wait_callback(handle: resource) -> CallbackResultEnvelope { oauth::callback_wait_result(&handle) } "#; @@ -57,40 +57,53 @@ use self::pkce_session as session; struct AuthCommandInput { operation: string, - provider: string?, - credential_id: string? + provider: Option, + credential_id: Option } struct AuthEntryResult { ok: bool, operation: string, - credential_id: string?, + credential_id: Option, generation: int, status: string, - error: string? + error: Option } struct PkceStartResult { ok: bool, code: string, selected_mode: string, fallback_used: bool, - value: PkceBeginEnvelope? + value: Option } struct PkceSessionResult { ok: bool, code: string, selected_mode: string, fallback_used: bool, - begin: PkceBeginEnvelope?, - waited: CallbackEnvelope? + begin: Option, + waited: Option } -struct BeginEnvelopePresence { - value: PkceBeginEnvelope? +fn failure(operation: string, code: string) -> AuthEntryResult { + {ok: false, operation: operation, credential_id: None, generation: 0, status: "failed", error: Some(code)} } -struct CallbackEnvelopePresence { - value: CallbackEnvelope? + +fn finish_report(operation: string, begin: PkceBeginEnvelope, waited: CallbackEnvelope) -> AuthEntryResult { + let code_present = match waited.code_handle { + None => false, + Some(code) => close_runner_code(code) + }; + oauth::close_callback(begin.callback_handle); + oauth::close_verifier(begin.verifier_handle); + if code_present => { + {ok: true, operation: operation, credential_id: None, generation: 0, status: "success", error: None} + } else => { + failure(operation, "host_failure") + } } -struct AuthorizationCodePresence { - value: resource? + +fn close_runner_code(code: resource) -> bool { + oauth::close_code(code); + true } pub fn run(home: resource, command: AuthCommandInput) -> AuthEntryResult { @@ -109,70 +122,18 @@ pub fn run(home: resource, command: AuthCommandInput) -> AuthEnt let started: PkceStartResult = session::begin(snapshot.policy_handle, intent, false); let session_result: PkceSessionResult = session::finish(started); let operation: string = command.operation.copy(); - let begin_key_present: bool = session_result.has("begin"); - let waited_key_present: bool = session_result.has("waited"); let result_ok: bool = session_result.ok; let result_code: string = session_result.code; - let begin_holder: BeginEnvelopePresence = if begin_key_present => { - {value: session_result.begin} + if !result_ok => { + failure(operation, result_code) } else => { - {value: null} - }; - let waited_holder: CallbackEnvelopePresence = if waited_key_present => { - {value: session_result.waited} - } else => { - {value: null} - }; - let begin_present: bool = begin_holder.has("value"); - let waited_present: bool = waited_holder.has("value"); - if result_ok && begin_present && waited_present => { - let begin: PkceBeginEnvelope = begin_holder.value; - let waited: CallbackEnvelope = waited_holder.value; - let code_holder: AuthorizationCodePresence = {value: waited.code_handle}; - let code_present: bool = code_holder.has("value"); - if code_present => { - let code: resource = code_holder.value; - oauth::close_code(code); - oauth::close_callback(begin.callback_handle); - oauth::close_verifier(begin.verifier_handle); - { - ok: true, - operation: operation, - credential_id: null, - generation: 0, - status: "success", - error: null - } - } else => { - oauth::close_callback(begin.callback_handle); - oauth::close_verifier(begin.verifier_handle); - { - ok: false, - operation: operation, - credential_id: null, - generation: 0, - status: "failed", - error: "host_failure" + match session_result.begin { + None => failure(operation, "host_failure"), + Some(begin) => match session_result.waited { + None => failure(operation, "host_failure"), + Some(waited) => finish_report(operation, begin, waited) } } - } else if !result_ok => { - { - ok: false, - operation: operation, - credential_id: null, - generation: 0, - status: "failed", - error: result_code - } - } else => { - { - ok: false, - operation: operation, - credential_id: null, - generation: 0, - status: "failed", - error: "host_failure" - } } } "#; @@ -432,6 +393,14 @@ fn field(value: &Value, name: &str) -> Value { .unwrap_or_else(|| panic!("missing result field {name}")) } +fn option_field(value: &Value, name: &str) -> Option { + match rustscript_vm::option::payload(&field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value.clone()), + _ => panic!("invalid Option payload for {name}"), + } +} + fn optional_field(value: &Value, name: &str) -> Value { let Value::Map(entries) = value else { panic!("expected typed named result") @@ -513,7 +482,10 @@ fn create_auth( fn closed_runner() -> AuthEntryRunner { let sources = [ (RUNNER_ENTRY, RUNNER_SOURCE), - (BUNDLE_MODULE, include_str!("../rss/auth/pkce_session.rss")), + ( + BUNDLE_MODULE, + include_str!("../../rss/auth/pkce_session.rss"), + ), ]; AuthEntryRunner::from_bundle(RUNNER_ENTRY, &sources) .expect("compile actual closed two-source auth bundle") @@ -621,7 +593,7 @@ fn typed_session_begin_finish_hands_off_and_closes_native_capabilities() { let session = run_for(&mut vm, &policy, "browser", false); assert_eq!(field(&session, "ok"), Value::Bool(true)); assert_eq!(field(&session, "selected_mode"), Value::string("browser")); - let begin = field(&session, "begin"); + let begin = option_field(&session, "begin").expect("present begin"); let redirect_uri = match field(&begin, "redirect_uri") { Value::String(value) => value.to_string(), _ => panic!("public redirect"), @@ -632,9 +604,9 @@ fn typed_session_begin_finish_hands_off_and_closes_native_capabilities() { let receipt = browser.receipt(); assert!(receipt.starts_with("HTTP/1.1 200 OK")); assert!(!receipt.contains(CALLBACK_CODE)); - let waited = field(&session, "waited"); - let code = field(&waited, "code_handle"); - let session_begin = field(&session, "begin"); + let waited = option_field(&session, "waited").expect("present waited"); + let code = option_field(&waited, "code_handle").expect("present code_handle"); + let session_begin = option_field(&session, "begin").expect("present begin"); let callback = field(&session_begin, "callback_handle"); let verifier = field(&session_begin, "verifier_handle"); for (name, handle) in [ @@ -669,7 +641,7 @@ fn modes_fallback_and_failure_branches_are_sanitized_and_bounded() { Value::string("oauth_callback_mode_unsupported") ); assert_eq!(field(&unknown, "selected_mode"), Value::string("")); - assert_eq!(optional_field(&unknown, "begin"), Value::Null); + assert!(option_field(&unknown, "begin").is_none()); let no_fallback = run_for(&mut vm, &policy, "browser", false); assert_eq!(field(&no_fallback, "ok"), Value::Bool(false)); @@ -682,7 +654,7 @@ fn modes_fallback_and_failure_branches_are_sanitized_and_bounded() { Value::string("browser") ); assert_eq!(field(&no_fallback, "fallback_used"), Value::Bool(false)); - assert_eq!(optional_field(&no_fallback, "waited"), Value::Null); + assert!(option_field(&no_fallback, "waited").is_none()); let fallback = run_for(&mut vm, &policy, "browser", true); assert_eq!(field(&fallback, "ok"), Value::Bool(false)); @@ -752,8 +724,8 @@ fn native_wait_failure_returns_no_stale_capability() { assert_eq!(field(&failed, "code"), Value::string(expected_code)); assert_eq!(field(&failed, "selected_mode"), Value::string("browser")); assert_eq!(field(&failed, "fallback_used"), Value::Bool(false)); - assert_eq!(optional_field(&failed, "begin"), Value::Null); - assert_eq!(optional_field(&failed, "waited"), Value::Null); + assert!(option_field(&failed, "begin").is_none()); + assert!(option_field(&failed, "waited").is_none()); assert!(!format!("{failed:?}").contains(CALLBACK_CODE)); assert!(browser.receipt().starts_with(expected_receipt)); assert_eq!(vm.host_context().resource_count(), 2); @@ -817,8 +789,8 @@ fn missing_backend_and_denied_inputs_stop_without_fallback_or_browser_open() { assert_eq!(field(&result, "code"), Value::string(expected_code)); assert_eq!(field(&result, "selected_mode"), Value::string("browser")); assert_eq!(field(&result, "fallback_used"), Value::Bool(false)); - assert_eq!(optional_field(&result, "begin"), Value::Null); - assert_eq!(optional_field(&result, "waited"), Value::Null); + assert!(option_field(&result, "begin").is_none()); + assert!(option_field(&result, "waited").is_none()); assert_eq!(browser.opens.load(Ordering::Acquire), 0); assert_eq!(vm.host_context().resource_count(), 2); #[cfg(feature = "config-fixture")] @@ -930,7 +902,7 @@ fn pending_listener_is_reclaimed_when_its_vm_is_dropped() { let policy = invoke(&mut vm, "policy", &[field(&owner, "host_home")]).expect("policy"); let started = start_for(&mut vm, &policy, "browser", false); assert_eq!(field(&started, "ok"), Value::Bool(true)); - let begin = field(&started, "value"); + let begin = option_field(&started, "value").expect("present value"); let redirect_uri = match field(&begin, "redirect_uri") { Value::String(value) => value.to_string(), _ => panic!("native redirect URI"), @@ -1000,8 +972,8 @@ fn pending_wait_cancellation_and_deadline_return_terminal_codes_and_release_reso assert_eq!(field(&result, "ok"), Value::Bool(false)); assert_eq!(field(&result, "code"), Value::string("callback_cancelled")); assert_eq!(field(&result, "fallback_used"), Value::Bool(false)); - assert_eq!(optional_field(&result, "begin"), Value::Null); - assert_eq!(optional_field(&result, "waited"), Value::Null); + assert!(option_field(&result, "begin").is_none()); + assert!(option_field(&result, "waited").is_none()); assert_eq!(resource_count, 2); assert_port_released(&cancel_redirect); #[cfg(feature = "config-fixture")] @@ -1050,8 +1022,8 @@ fn pending_wait_cancellation_and_deadline_return_terminal_codes_and_release_reso assert_eq!(field(&result, "ok"), Value::Bool(false)); assert_eq!(field(&result, "code"), Value::string("callback_timeout")); assert_eq!(field(&result, "fallback_used"), Value::Bool(false)); - assert_eq!(optional_field(&result, "begin"), Value::Null); - assert_eq!(optional_field(&result, "waited"), Value::Null); + assert!(option_field(&result, "begin").is_none()); + assert!(option_field(&result, "waited").is_none()); assert_eq!(deadline_vm.host_context().resource_count(), 2); assert_port_released(&deadline_redirect); #[cfg(feature = "config-fixture")] @@ -1074,7 +1046,7 @@ fn wrong_resource_and_foreign_vm_trap_without_invalidating_the_originating_flow( let policy = invoke(&mut vm, "policy", &[field(&owner, "host_home")]).expect("policy"); let started = start_for(&mut vm, &policy, "browser", false); assert_eq!(field(&started, "ok"), Value::Bool(true)); - let begin = field(&started, "value"); + let begin = option_field(&started, "value").expect("present value"); let callback = field(&begin, "callback_handle"); let verifier = field(&begin, "verifier_handle"); let redirect_uri = match field(&begin, "redirect_uri") { @@ -1126,10 +1098,13 @@ fn wrong_resource_and_foreign_vm_trap_without_invalidating_the_originating_flow( assert_eq!(field(&completed, "ok"), Value::Bool(true)); assert_eq!(field(&completed, "code"), Value::string("ok")); assert!(browser.receipt().starts_with("HTTP/1.1 200 OK")); - let completed_begin = field(&completed, "begin"); - let waited = field(&completed, "waited"); + let completed_begin = option_field(&completed, "begin").expect("present begin"); + let waited = option_field(&completed, "waited").expect("present waited"); for (name, handle) in [ - ("close_code", field(&waited, "code_handle")), + ( + "close_code", + option_field(&waited, "code_handle").expect("present code_handle"), + ), ("close_callback", field(&completed_begin, "callback_handle")), ("close_verifier", field(&completed_begin, "verifier_handle")), ] { diff --git a/tests/plugin_graph_tests.rs b/tests/cases/plugin_graph_tests.rs similarity index 99% rename from tests/plugin_graph_tests.rs rename to tests/cases/plugin_graph_tests.rs index e659857..36df633 100644 --- a/tests/plugin_graph_tests.rs +++ b/tests/cases/plugin_graph_tests.rs @@ -876,7 +876,7 @@ fn signed_completion_protocol_is_role_fenced_and_generic_artifacts_stay_denied() " direct: []\n delegated: []\n", "", ); - let result = "pub fn invoke(token: string) -> map { cap::artifact_put_result(token, bytes::from_utf8(\"result\"), {}) }\n"; + let result = "pub fn invoke(token: string) -> AgentArtifactRef { cap::artifact_put_result(token, bytes::from_utf8(\"result\"), \"{}\") }\n"; prepare_policy_source(&manifest, result).unwrap(); let library = manifest .replace("kind: tool", "kind: library") @@ -886,8 +886,8 @@ fn signed_completion_protocol_is_role_fenced_and_generic_artifacts_stay_denied() .replace("contract: agent.tool/v1", "contract: agent.harness/v1"); for source in [ result, - "pub fn invoke(value: map) -> map { agent_runtime::tool_prepare(value) }\n", - "pub fn invoke(token: string) -> map { agent_runtime::tool_commit(token, {}) }\n", + "pub fn invoke(value: string) -> AgentToolEnvelope { agent_runtime::tool_prepare(value) }\n", + "pub fn invoke(token: string) -> AgentToolEnvelope { agent_runtime::tool_commit(token, \"{}\") }\n", "pub fn invoke(value: int) -> int { agent::sleep_ms(value) }\n", ] { // Harness packages are not an installable role in this manifest contract. @@ -901,9 +901,9 @@ fn signed_completion_protocol_is_role_fenced_and_generic_artifacts_stay_denied() ); } for source in [ - "pub fn invoke(token: string) -> map { cap::artifact_put(token, bytes::from_utf8(\"result\"), {}) }\n", - "pub fn invoke(token: string) -> map { cap::artifact_get(token, \"id\") }\n", - "pub fn invoke(token: string) -> map { cap::artifact_reference(token, \"id\") }\n", + "pub fn invoke(token: string) -> AgentArtifactRef { cap::artifact_put(token, bytes::from_utf8(\"result\"), \"{}\") }\n", + "pub fn invoke(token: string) -> AgentArtifactGetResult { cap::artifact_get(token, \"id\") }\n", + "pub fn invoke(token: string) -> AgentArtifactRef { cap::artifact_reference(token, \"id\") }\n", ] { assert_kind( prepare_policy_source(&manifest, source).unwrap_err(), @@ -1894,7 +1894,7 @@ fn phase_a_install_linked_pair( ); let mut files: Vec<(&str, &str)> = vec![("run.rss", phase_a_run_source())]; if with_catalog { - files.push(("catalog.rss", include_str!("../rss/tools/types.rss"))); + files.push(("catalog.rss", include_str!("../../rss/tools/types.rss"))); } write_signed( &store, diff --git a/tests/plugin_manifest_tests.rs b/tests/cases/plugin_manifest_tests.rs similarity index 100% rename from tests/plugin_manifest_tests.rs rename to tests/cases/plugin_manifest_tests.rs diff --git a/tests/plugin_schema_sync_tests.rs b/tests/cases/plugin_schema_sync_tests.rs similarity index 100% rename from tests/plugin_schema_sync_tests.rs rename to tests/cases/plugin_schema_sync_tests.rs diff --git a/tests/provider_401_refresh_loop_tests.rs b/tests/cases/provider_401_refresh_loop_tests.rs similarity index 99% rename from tests/provider_401_refresh_loop_tests.rs rename to tests/cases/provider_401_refresh_loop_tests.rs index e22cfc0..e1d5736 100644 --- a/tests/provider_401_refresh_loop_tests.rs +++ b/tests/cases/provider_401_refresh_loop_tests.rs @@ -1,8 +1,6 @@ //! Real main/default/native authenticated provider behavior. #![cfg(feature = "config-fixture")] -#[path = "support/provider_opaque_fixture.rs"] -#[allow(dead_code)] -pub mod provider_opaque_fixture; +use super::provider_opaque_fixture; use provider_opaque_fixture::{FixtureHome, json_to_vm_value, vm_value_to_json}; use rustscript_agent::auth::{ host::{AuthHost, auth_host_catalog}, @@ -257,7 +255,7 @@ fn assert_provider_failure_boundary(boundary: NativeFailureBoundary, label: &str .expect("invoke real provider_runtime::call"); assert_eq!(vm_field(&result, "ok"), &VmValue::Bool(false)); - let error = vm_field(&result, "error"); + let error = crate::typed_carriers::nullable(vm_field(&result, "error")); assert_eq!(vm_field(error, "code"), &VmValue::string("policy_inactive")); let sent = transport.take_sent(); assert_eq!( diff --git a/tests/provider_access_failure_tests.rs b/tests/cases/provider_access_failure_tests.rs similarity index 87% rename from tests/provider_access_failure_tests.rs rename to tests/cases/provider_access_failure_tests.rs index a9e8a3f..c88427c 100644 --- a/tests/provider_access_failure_tests.rs +++ b/tests/cases/provider_access_failure_tests.rs @@ -60,7 +60,7 @@ const MODEL_URL: &str = "https://api.openai.com/v1/chat/completions"; const OUTER_SOURCE: &str = r#" use agent; pub fn run(context: map) -> map { - let request: map = context["request"]; + let request: string = context["request"]; agent::provider_call(request) } "#; @@ -71,49 +71,46 @@ const METADATA_PROBE_SOURCE: &str = r#" use auth; use config; -struct MetadataPresence { - metadata: AuthMetadataEnvelope? +struct MetadataView { present: bool, ok: bool, generation: int, status: string } +struct MetadataAccessProbe { + snapshot_ok: bool, metadata_ok: bool, metadata_error_present: bool, + metadata_present: bool, metadata_envelope_ok: bool, metadata_generation: int, + metadata_status: string, issued_ok: bool, issued_error_present: bool, issued_error_code: string +} +struct IssueErrorView { present: bool, code: string } +fn read_metadata(envelope: AuthMetadataEnvelope) -> MetadataView { + let meta: AuthMetadata = envelope.metadata; + {present: true, ok: envelope.ok, generation: meta.generation, status: meta.status} } -pub fn run(home: resource, credential_id: string) -> map { +pub fn run(home: resource, credential_id: string) -> MetadataAccessProbe { let snapshot: ConfigSnapshotEnvelope = config::load_snapshot(&home); let policy: resource = snapshot.policy_handle; let loaded: AuthMetadataResultEnvelope = auth::load_metadata_result(credential_id, &policy); let loaded_ok: bool = loaded.ok; - let loaded_error: AuthBridgeFailure? = loaded.error; - let holder: MetadataPresence = { metadata: loaded.value }; - let metadata_present: bool = holder.has("metadata"); - let mut generation: int = 0; - let mut status: string = ""; - let mut meta_ok: bool = false; - if metadata_present { - let envelope: AuthMetadataEnvelope = holder.metadata; - let meta: AuthMetadata = envelope.metadata; - generation = meta.generation; - status = meta.status; - meta_ok = envelope.ok; - } + let loaded_error: bool = match loaded.error { Some(_) => true, None => false }; + let meta: MetadataView = match loaded.value { + Some(envelope) => read_metadata(envelope), + None => {present: false, ok: false, generation: 0, status: ""} + }; + let generation: int = meta.generation; let issued: AccessHandleResultEnvelope = auth::access_handle_result(credential_id, generation, &policy); let issued_ok: bool = issued.ok; - let maybe_issue_error: AuthBridgeFailure? = issued.error; - let mut issue_error_present: bool = false; - let mut issued_error_code: string = ""; - if maybe_issue_error != null { - let failure: AuthBridgeFailure = maybe_issue_error; - issued_error_code = failure.code; - issue_error_present = true; - } + let issue_error: IssueErrorView = match issued.error { + Some(failure) => {present: true, code: failure.code}, + None => {present: false, code: ""} + }; { snapshot_ok: snapshot.ok, metadata_ok: loaded_ok, - metadata_error_present: loaded_error != null, - metadata_present: metadata_present, - metadata_envelope_ok: meta_ok, + metadata_error_present: loaded_error, + metadata_present: meta.present, + metadata_envelope_ok: meta.ok, metadata_generation: generation, - metadata_status: status, + metadata_status: meta.status, issued_ok: issued_ok, - issued_error_present: issue_error_present, - issued_error_code: issued_error_code + issued_error_present: issue_error.present, + issued_error_code: issue_error.code } } "#; @@ -213,7 +210,6 @@ fn fixture_factory( fn outer_runner( home: &FixtureHome, transport: Arc, - cancellation: RunCancellation, ) -> AgentRunner { let bridges = AgentHostBridges { auth_host_factory: Some(fixture_factory(home, transport)), @@ -222,31 +218,8 @@ fn outer_runner( AgentRunner::from_source(OUTER_SOURCE, AgentConfig::default()) .expect("compile outer provider bridge source") .with_host(bridges) - .with_cancellation(cancellation) -} - -fn json_to_vm_value(value: &JsonValue) -> Value { - match value { - JsonValue::Null => Value::Null, - JsonValue::Bool(value) => Value::Bool(*value), - JsonValue::Number(value) => { - if let Some(value) = value.as_i64() { - Value::Int(value) - } else { - Value::Float(value.as_f64().expect("finite JSON number")) - } - } - JsonValue::String(value) => Value::string(value), - JsonValue::Array(values) => { - Value::Array(Arc::new(values.iter().map(json_to_vm_value).collect())) - } - JsonValue::Object(entries) => Value::map( - entries - .iter() - .map(|(key, value)| (Value::string(key), json_to_vm_value(value))) - .collect(), - ), - } + // Host installation prepares the default dispatcher before the invocation clock. + .with_cancellation(finite()) } fn vm_value_to_json(value: &Value) -> JsonValue { @@ -293,8 +266,23 @@ fn public_request() -> JsonValue { }) } +fn provider_envelope(value: &Value) -> JsonValue { + let frame = vm_value_to_json(value); + let ok = frame["ok"].as_bool().expect("provider envelope ok"); + let field = if ok { "response" } else { "error" }; + let absent = if ok { "error" } else { "response" }; + assert_eq!(frame[absent], "", "inactive envelope field"); + let body: JsonValue = serde_json::from_str(frame[field].as_str().expect("provider body text")) + .expect("public provider body"); + assert!(body.is_object(), "provider body object"); + json!({"ok": ok, field: body, absent: {}}) +} + fn context(request: JsonValue) -> Value { - Value::map(vec![(Value::string("request"), json_to_vm_value(&request))]) + Value::map(vec![( + Value::string("request"), + Value::string(request.to_string()), + )]) } fn finite() -> RunCancellation { @@ -354,9 +342,9 @@ fn default_provider_expired_credential_preflight_reauth_without_model_send() { assert_store_generation_active(&home); let transport = Arc::new(ScriptedHttpsTransport::new()); transport.push_json(400, invalid_grant_rejection()); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); - let result = vm_value_to_json( + let result = provider_envelope( &runner .run_with_context(context(public_request())) .expect("expired stored credential returns a canonical public result"), @@ -411,9 +399,9 @@ fn default_provider_valid_credential_is_authenticated_control() { let home = FixtureHome::new(FUTURE_EXPIRY_MS); let transport = Arc::new(ScriptedHttpsTransport::new()); transport.push_json(200, canonical_chat_body()); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); - let result = vm_value_to_json( + let result = provider_envelope( &runner .run_with_context(context(public_request())) .expect("valid stored credential completes the default dispatch"), @@ -431,8 +419,13 @@ fn default_provider_valid_credential_is_authenticated_control() { ); assert_eq!(result["response"]["tool_calls"][0]["name"], json!("lookup")); assert_eq!( - result["response"]["tool_calls"][0]["arguments"]["query"], - json!("rustscript") + serde_json::from_str::( + result["response"]["tool_calls"][0]["arguments_json"] + .as_str() + .expect("argument text") + ) + .expect("argument object"), + json!({"query": "rustscript"}) ); let sent = transport.take_sent(); @@ -457,9 +450,9 @@ fn default_provider_preflight_reauth_recovers_after_expiry_repair() { let transport = Arc::new(ScriptedHttpsTransport::new()); transport.push_json(400, invalid_grant_rejection()); transport.push_json(200, canonical_chat_body()); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); - let failed = vm_value_to_json( + let failed = provider_envelope( &runner .run_with_context(context(public_request())) .expect("expired invocation returns a canonical public result"), @@ -501,7 +494,7 @@ fn default_provider_preflight_reauth_recovers_after_expiry_repair() { // The queued canonical chat body is still pending: the rejected refresh // must not have consumed it. home.set_expiry(FUTURE_EXPIRY_MS); - let recovered = vm_value_to_json( + let recovered = provider_envelope( &runner .run_with_context(context(public_request())) .expect("repaired invocation completes"), @@ -525,8 +518,13 @@ fn default_provider_preflight_reauth_recovers_after_expiry_repair() { json!("lookup") ); assert_eq!( - recovered["response"]["tool_calls"][0]["arguments"]["query"], - json!("rustscript") + serde_json::from_str::( + recovered["response"]["tool_calls"][0]["arguments_json"] + .as_str() + .expect("argument text") + ) + .expect("argument object"), + json!({"query": "rustscript"}) ); let second_sent = transport.take_sent(); assert_eq!( diff --git a/tests/provider_auth_rss_entry_tests.rs b/tests/cases/provider_auth_rss_entry_tests.rs similarity index 91% rename from tests/provider_auth_rss_entry_tests.rs rename to tests/cases/provider_auth_rss_entry_tests.rs index 98cd52f..b83007a 100644 --- a/tests/provider_auth_rss_entry_tests.rs +++ b/tests/cases/provider_auth_rss_entry_tests.rs @@ -22,8 +22,8 @@ use rustscript_agent::auth::{ }; use rustscript_agent::config::ProviderProfile; use rustscript_agent::{ - AdmitRunRequest, AgentConfig, AgentGatewayConfig, AgentGatewayState, AgentHostBridges, - AgentRunner, CancellationReason, RunCancellation, + AdmitRunRequest, AgentConfig, AgentError, AgentGatewayConfig, AgentGatewayState, + AgentHostBridges, AgentRunner, CancellationReason, RunCancellation, }; use rustscript_vm::Value; use serde_json::json; @@ -70,8 +70,13 @@ fn source() -> &'static str { use auth; use config; use oauth; -pub fn run(context: map) -> map { - let home: resource = context["host_home"]; +struct TransportProbeContext { host_home: resource } +struct TransportProbeReport { + snapshot_ok: bool, metadata_ok: bool, access_ok: bool, + transport_ok: bool, transport_error: Option, status: int +} +pub fn run(context: TransportProbeContext) -> TransportProbeReport { + let home: resource = context.host_home; let snapshot = config::load_snapshot(&home); let snapshot_ok: bool = snapshot.ok; let policy: resource = snapshot.policy_handle; @@ -88,27 +93,22 @@ pub fn run(context: map) -> map { }; let credential_use: TransportCredentialUse = { kind: "access", - access_handle: access.handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null + access_handle: Some(access.handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None }; let response: TransportResultEnvelope = oauth::transport_result(&request, &credential_use); let transport_ok: bool = response.ok; - let maybe_response: TransportResponseEnvelope? = response.value; - let public_response: TransportResponseEnvelope = maybe_response.unwrap_or({ - ok: false, error: null, status: 0, public_headers: [], - body_without_secret_fields: "", retry_after_ms: null, - access_slot: null, refresh_slot: null, device_handle: null - }); + let status: int = match response.value { Some(public_response) => public_response.status, None => 0 }; { snapshot_ok: snapshot_ok, metadata_ok: metadata.ok, access_ok: access_ok, transport_ok: transport_ok, transport_error: response.error, - status: public_response.status + status: status } } "# @@ -316,17 +316,21 @@ fn carrier_source() -> String { " stream::emit({ policy_handle: policy, access_handle: access.handle });\n let fresh_snapshot = config::load_snapshot(&home);\n let fresh_policy: resource = fresh_snapshot.policy_handle;\n let fresh_access = auth::access_handle(\"fixture-codex\", 4, &fresh_policy);\n let request: TransportPublicRequest = {", ) .replace(" policy_handle: policy,", " policy_handle: fresh_policy,") - .replace(" access_handle: access.handle,", " access_handle: fresh_access.handle,") + .replace(" access_handle: Some(access.handle),", " access_handle: Some(fresh_access.handle),") } fn foreign_source() -> String { source() + .replace( + "struct TransportProbeContext { host_home: resource }", + "struct TransportProbeContext { host_home: resource, foreign_policy: resource, foreign_access: resource }", + ) .replace( " let request: TransportPublicRequest = {", - " let foreign_policy: resource = context[\"foreign_policy\"];\n let foreign_access: resource = context[\"foreign_access\"];\n let request: TransportPublicRequest = {", + " let foreign_policy: resource = context.foreign_policy;\n let foreign_access: resource = context.foreign_access;\n let request: TransportPublicRequest = {", ) .replace(" policy_handle: policy,", " policy_handle: foreign_policy,") - .replace(" access_handle: access.handle,", " access_handle: foreign_access,") + .replace(" access_handle: Some(access.handle),", " access_handle: Some(foreign_access),") } struct EventCaptureSink { @@ -453,7 +457,7 @@ fn authentic_event_carriers_are_foreign_while_owner_is_live_and_owner_survives_r let foreign = rejecting_target .run_with_context(foreign_context(&carrier)) .expect_err("foreign-owner handles are rejected by the typed transport call"); - assert!(foreign.to_string().contains("resource_invalid")); + assert_wrong_arena(&foreign); assert!( transport.take_sent().is_empty(), "foreign handles must not send" @@ -534,7 +538,7 @@ fn authentic_carriers_fail_after_success_rejection_and_sink_unwind() { let stale = target .run_with_context(foreign_context(&success_carrier)) .expect_err("stale carrier handles are rejected"); - assert!(stale.to_string().contains("resource_invalid")); + assert_wrong_arena(&stale); assert!( transport.take_sent().is_empty(), "stale handles must not send" @@ -561,7 +565,7 @@ fn authentic_carriers_fail_after_success_rejection_and_sink_unwind() { let rejected_reuse = target .run_with_context(foreign_context(&rejected_carrier)) .expect_err("rejected carrier handles are rejected"); - assert!(rejected_reuse.to_string().contains("resource_invalid")); + assert_wrong_arena(&rejected_reuse); assert!( transport.take_sent().is_empty(), "rejected handles must not send" @@ -589,7 +593,7 @@ fn authentic_carriers_fail_after_success_rejection_and_sink_unwind() { let panic_reuse = target .run_with_context(foreign_context(&panic_carrier)) .expect_err("unwound carrier handles are rejected"); - assert!(panic_reuse.to_string().contains("resource_invalid")); + assert_wrong_arena(&panic_reuse); assert!( transport.take_sent().is_empty(), "unwound handles must not send" @@ -733,24 +737,28 @@ pub fn run(context: map) -> map { #[test] fn provider_json_rejects_nested_live_auth_resources_before_callback() { - let home = FixtureHome::new(); - let transport = Arc::new(ScriptedHttpsTransport::new()); - let calls = Arc::new(AtomicUsize::new(0)); - let capture = Arc::new(ProviderCapture::new()); - let runner = fixture_runner_with_source(&home, transport, calls, provider_resource_source()) - .with_provider(capture.clone()); - - let outcome = runner.run_with_context(Value::map(vec![])); - let error = match outcome { - Err(error) => error, - Ok(_) => panic!("live auth resource reached provider JSON boundary"), - }; + let diagnostic = + match AgentRunner::from_source(provider_resource_source(), AgentConfig::default()) { + Err(AgentError::Compile(diagnostic)) => diagnostic, + Err(error) => panic!("unexpected provider admission error: {error}"), + Ok(_) => panic!("nested live auth resources compiled as a provider request"), + }; assert!( - error.to_string().contains("opaque_nonserializable"), - "provider boundary returned an unexpected error class" + diagnostic.contains("expected string, found object"), + "{diagnostic}" + ); + assert!( + diagnostic.contains("policy_handle: resource"), + "{diagnostic}" + ); + assert!( + diagnostic.contains("best candidate is `agent::provider_call(string)`"), + "{diagnostic}" ); - assert_eq!(capture.calls.load(Ordering::SeqCst), 0); - assert!(capture.request.lock().expect("provider capture").is_none()); } #[test] @@ -762,10 +770,7 @@ fn provider_json_preserves_ordinary_public_nested_values() { let source = r#" use agent; pub fn run(context: map) -> map { - let result: map = agent::provider_call({ - ordinary_integer: 42, - nested: { label: "public", values: [1, 2, 3] } - }); + let result = agent::provider_call("{\"ordinary_integer\":42,\"nested\":{\"label\":\"public\",\"values\":[1,2,3]}}"); { provider_ok: result.ok } } "#; @@ -1023,6 +1028,9 @@ async fn operator_home_factory_forwards_through_service_to_child_auth() { .with_operator_auth_home_and_factory_for_test(home.0.clone(), auth_factory) .expect("install provider-only operator factory"); let service = state.service(); + service + .materialize_cached_runner() + .expect("prepare bundled Main before timed admission"); service.upsert_provider_profile( ProviderProfile::new( OPERATOR_PROVIDER, @@ -1035,6 +1043,12 @@ async fn operator_home_factory_forwards_through_service_to_child_auth() { ) .expect("construct bounded public provider profile"), ); + AgentRunner::from_bundled_named( + "rss/llm/provider_dispatch.rss", + "dispatch", + AgentConfig::default(), + ) + .expect("prepare the real provider dispatcher before timed admission"); let admitted = service .admit(AdmitRunRequest { input: json!({"message": "return the operator fixture answer"}), @@ -1125,6 +1139,15 @@ async fn assert_operator_auth_failure_without_send( let factory = operator_auth_factory(home, Arc::clone(&transport), Arc::clone(&factory_calls)); let state = operator_state_with_factory(home, json!({}), factory); let service = state.service(); + service + .materialize_cached_runner() + .expect("prepare bundled Main before timed admission"); + AgentRunner::from_bundled_named( + "rss/llm/provider_dispatch.rss", + "dispatch", + AgentConfig::default(), + ) + .expect("prepare the real provider dispatcher before timed admission"); let run_id = run_operator_service(&service, platform).await; let events = service.run_events(&run_id); assert_eq!( @@ -1187,9 +1210,16 @@ async fn operator_factory_is_lazy_for_plain_selected_rss_entry() { .expect("install lazy provider-only factory"); state.service().upsert_provider_profile(operator_profile()); let service = state.service(); + service + .materialize_cached_runner() + .expect("prepare the selected plain runner before admission"); let run_id = run_operator_service(&service, "operator_plain_selected_rss").await; let events = service.run_events(&run_id); - assert_eq!(service_event_count(&events, "run.completed"), 1); + assert_eq!( + service_event_count(&events, "run.completed"), + 1, + "{events:?}" + ); assert_eq!(service_event_count(&events, "model.requested"), 0); assert_eq!(factory_calls.load(Ordering::SeqCst), 0); assert!(transport.take_sent().is_empty()); @@ -1274,3 +1304,18 @@ pub fn run(context: map) -> map { assert_eq!(service_event_count(&events, "run.failed"), 0); assert_eq!(service_event_count(&events, "model.requested"), 0); } + +// Core checks arena ownership before resolving even a retired handle's slot. +fn assert_wrong_arena(error: &rustscript_agent::runtime::rss_runner::RunError) { + use rustscript_agent::runtime::rss_runner::RunError; + use rustscript_vm::InvocationError; + + assert!( + matches!( + error, + RunError::Invocation(InvocationError::Host { message }) + if message.starts_with("resource error [resource_handle_wrong_table] in resource::table:") + ), + "{error}" + ); +} diff --git a/tests/provider_concurrent_generation_loop_tests.rs b/tests/cases/provider_concurrent_generation_loop_tests.rs similarity index 98% rename from tests/provider_concurrent_generation_loop_tests.rs rename to tests/cases/provider_concurrent_generation_loop_tests.rs index a8e0c86..80c2ee4 100644 --- a/tests/provider_concurrent_generation_loop_tests.rs +++ b/tests/cases/provider_concurrent_generation_loop_tests.rs @@ -1,8 +1,6 @@ //! Concurrent refresh adoption through the production main/default/native loop. #![cfg(feature = "config-fixture")] -#[path = "support/provider_opaque_fixture.rs"] -#[allow(dead_code)] -pub mod provider_opaque_fixture; +use super::provider_opaque_fixture; use provider_opaque_fixture::{FixtureHome, json_to_vm_value, vm_value_to_json}; use rustscript_agent::auth::oauth::{ BoundedHttpsTransport, OAuthError, OAuthIoContext, PreparedHttpsRequest, RawHttpsResponse, diff --git a/tests/provider_default_opaque_tests.rs b/tests/cases/provider_default_opaque_tests.rs similarity index 89% rename from tests/provider_default_opaque_tests.rs rename to tests/cases/provider_default_opaque_tests.rs index c0e06c5..1d9b18d 100644 --- a/tests/provider_default_opaque_tests.rs +++ b/tests/cases/provider_default_opaque_tests.rs @@ -2,8 +2,7 @@ #![cfg(feature = "config-fixture")] -#[path = "support/opaque_chat_stream.rs"] -mod opaque_chat_stream; +use super::opaque_chat_stream; use std::fs; #[cfg(unix)] @@ -32,7 +31,7 @@ use serde_json::{Value as JsonValue, json}; const OUTER_SOURCE: &str = r#" use agent; pub fn run(context: map) -> map { - let request: map = context["request"]; + let request: string = context["request"]; agent::provider_call(request) } "#; @@ -101,10 +100,14 @@ fn fixture_factory( } /// Outer runner with a native auth host factory and no injected provider. -fn outer_runner( +fn outer_runner(home: &FixtureHome, transport: Arc) -> AgentRunner { + // Arm the invocation clock only once the dispatcher is prepared. + prepare_outer_runner(home, transport).with_cancellation(finite()) +} + +fn prepare_outer_runner( home: &FixtureHome, transport: Arc, - cancellation: RunCancellation, ) -> AgentRunner { let bridges = AgentHostBridges { auth_host_factory: Some(fixture_factory(home, transport)), @@ -113,31 +116,6 @@ fn outer_runner( AgentRunner::from_source(OUTER_SOURCE, AgentConfig::default()) .expect("compile outer provider bridge source") .with_host(bridges) - .with_cancellation(cancellation) -} - -fn json_to_vm_value(value: &JsonValue) -> Value { - match value { - JsonValue::Null => Value::Null, - JsonValue::Bool(value) => Value::Bool(*value), - JsonValue::Number(value) => { - if let Some(value) = value.as_i64() { - Value::Int(value) - } else { - Value::Float(value.as_f64().expect("finite JSON number")) - } - } - JsonValue::String(value) => Value::string(value), - JsonValue::Array(values) => { - Value::Array(Arc::new(values.iter().map(json_to_vm_value).collect())) - } - JsonValue::Object(entries) => Value::map( - entries - .iter() - .map(|(key, value)| (Value::string(key), json_to_vm_value(value))) - .collect(), - ), - } } fn vm_value_to_json(value: &Value) -> JsonValue { @@ -189,8 +167,23 @@ fn public_request() -> JsonValue { }) } +fn provider_envelope(value: &Value) -> JsonValue { + let frame = vm_value_to_json(value); + let ok = frame["ok"].as_bool().expect("provider envelope ok"); + let field = if ok { "response" } else { "error" }; + let absent = if ok { "error" } else { "response" }; + assert_eq!(frame[absent], "", "inactive envelope field"); + let body: JsonValue = serde_json::from_str(frame[field].as_str().expect("provider body text")) + .expect("public provider body"); + assert!(body.is_object(), "provider body object"); + json!({"ok": ok, field: body, absent: {}}) +} + fn context(request: JsonValue) -> Value { - Value::map(vec![(Value::string("request"), json_to_vm_value(&request))]) + Value::map(vec![( + Value::string("request"), + Value::string(request.to_string()), + )]) } fn finite() -> RunCancellation { @@ -222,9 +215,9 @@ fn default_provider_dispatches_opaque_openai_through_injected_native_auth() { let home = FixtureHome::new(); let transport = Arc::new(ScriptedHttpsTransport::new()); transport.push_json(200, canonical_chat_body()); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); - let result = vm_value_to_json( + let result = provider_envelope( &runner .run_with_context(context(public_request())) .expect("genuine default provider dispatch completes"), @@ -246,8 +239,13 @@ fn default_provider_dispatches_opaque_openai_through_injected_native_auth() { ); assert_eq!(result["response"]["tool_calls"][0]["name"], json!("lookup")); assert_eq!( - result["response"]["tool_calls"][0]["arguments"]["query"], - json!("rustscript") + serde_json::from_str::( + result["response"]["tool_calls"][0]["arguments_json"] + .as_str() + .expect("argument text") + ) + .expect("argument object"), + json!({"query": "rustscript"}) ); let sent = transport.take_sent(); @@ -309,7 +307,7 @@ fn default_provider_redacts_child_auth_setup_failure() { .expect("compile outer source") .with_host(bridges) .with_cancellation(finite()); - let result = vm_value_to_json( + let result = provider_envelope( &runner .run_with_context(context(public_request())) .expect("child setup failure returns a public envelope"), @@ -332,7 +330,7 @@ fn default_provider_without_native_auth_factory_fails_closed_without_transport() .expect("compile outer source without factory") .with_cancellation(finite()); - let result = vm_value_to_json( + let result = provider_envelope( &runner .run_with_context(context(public_request())) .expect("static fail-closed envelope"), @@ -345,11 +343,11 @@ fn default_provider_without_native_auth_factory_fails_closed_without_transport() fn default_provider_unadmitted_credential_is_canonical_and_sends_nothing() { let home = FixtureHome::new(); let transport = Arc::new(ScriptedHttpsTransport::new()); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); let mut request = public_request(); request["credential_id"] = json!("not-admitted"); - let result = vm_value_to_json( + let result = provider_envelope( &runner .run_with_context(context(request)) .expect("unadmitted reference returns a canonical public failure"), @@ -364,11 +362,11 @@ fn default_provider_unadmitted_credential_is_canonical_and_sends_nothing() { fn default_provider_missing_credential_is_canonical_and_sends_nothing() { let home = FixtureHome::new(); let transport = Arc::new(ScriptedHttpsTransport::new()); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); let mut request = public_request(); request["credential_id"] = json!(""); - let result = vm_value_to_json( + let result = provider_envelope( &runner .run_with_context(context(request)) .expect("missing credential reference returns a canonical public failure"), @@ -411,7 +409,7 @@ fn default_provider_guards_unsupported_provider_and_private_options_without_star vec![b"data: [DONE]\n\n".to_vec()], false, )); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); let mut request = public_request(); request["stream"] = json!(stream); if label == "unsupported-provider" { @@ -420,7 +418,7 @@ fn default_provider_guards_unsupported_provider_and_private_options_without_star request["provider_options"] = mutation; } - let result = vm_value_to_json(&runner.run_with_context(context(request)).unwrap_or_else( + let result = provider_envelope(&runner.run_with_context(context(request)).unwrap_or_else( |error| panic!("{label} guard must return a canonical value: {error}"), )); assert_eq!(result["ok"], json!(false), "{label} guard must fail closed"); @@ -463,11 +461,11 @@ fn default_provider_stream_credential_guards_start_nothing() { vec![b"data: [DONE]\n\n".to_vec()], false, )); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); let mut request = public_request(); request["stream"] = json!(true); request["credential_id"] = json!(credential); - let result = vm_value_to_json(&runner.run_with_context(context(request)).unwrap_or_else( + let result = provider_envelope(&runner.run_with_context(context(request)).unwrap_or_else( |error| panic!("credential guard must return a canonical value: {error}"), )); assert_eq!(result["ok"], json!(false), "{expected_code}"); @@ -518,8 +516,10 @@ fn default_provider_child_inherits_caller_finite_cancellation() { let transport: Arc = Arc::new(PendingCancelTransport { entered: entered_tx, }); + // Compile the outer and bundled child programs before arming the run clock. + let runner = prepare_outer_runner(&home, transport); let cancellation = RunCancellation::with_timeout(Duration::from_secs(10)); - let runner = outer_runner(&home, transport, cancellation.clone()); + let runner = runner.with_cancellation(cancellation.clone()); let (result_tx, result_rx) = std::sync::mpsc::sync_channel(1); let worker = thread::spawn(move || { @@ -537,7 +537,7 @@ fn default_provider_child_inherits_caller_finite_cancellation() { .expect("inherited cancellation must terminate the run within the bound"); match outcome { Ok(result) => { - let result = vm_value_to_json(&result); + let result = provider_envelope(&result); assert_eq!(result["ok"], json!(false)); let code = result["error"]["code"].clone(); assert!( @@ -576,10 +576,10 @@ fn default_provider_dispatches_opaque_openai_stream_with_indexed_tool_fragments( .to_vec()], false, )); - let runner = outer_runner(&home, transport.clone(), finite()); + let runner = outer_runner(&home, transport.clone()); let mut request = public_request(); request["stream"] = json!(true); - let result = vm_value_to_json(&runner.run_with_context(context(request)).unwrap_or_else( + let result = provider_envelope(&runner.run_with_context(context(request)).unwrap_or_else( |error| { panic!( "default dispatch stream failed: {error}; starts={}, drops={}", @@ -604,8 +604,13 @@ fn default_provider_dispatches_opaque_openai_stream_with_indexed_tool_fragments( ); assert_eq!(result["response"]["tool_calls"][0]["name"], json!("lookup")); assert_eq!( - result["response"]["tool_calls"][0]["arguments"]["query"], - json!("rustscript"), + serde_json::from_str::( + result["response"]["tool_calls"][0]["arguments_json"] + .as_str() + .expect("argument text") + ) + .expect("argument object"), + json!({"query": "rustscript"}), "indexed tool fragments must accumulate: {result}" ); assert_eq!(transport.counts.starts.load(Ordering::SeqCst), 1); diff --git a/tests/provider_expired_refresh_loop_tests.rs b/tests/cases/provider_expired_refresh_loop_tests.rs similarity index 95% rename from tests/provider_expired_refresh_loop_tests.rs rename to tests/cases/provider_expired_refresh_loop_tests.rs index fe9e053..d1ad5a2 100644 --- a/tests/provider_expired_refresh_loop_tests.rs +++ b/tests/cases/provider_expired_refresh_loop_tests.rs @@ -1,10 +1,7 @@ //! Expired OAuth refresh through the real authenticated default serial loop. #![cfg(feature = "config-fixture")] -#[path = "support/opaque_chat_stream.rs"] -mod opaque_chat_stream; -#[path = "support/provider_opaque_fixture.rs"] -#[allow(dead_code)] -pub mod provider_opaque_fixture; +use super::opaque_chat_stream; +use super::provider_opaque_fixture; use provider_opaque_fixture::{FixtureHome, json_to_vm_value, vm_value_to_json}; use rustscript_agent::auth::oauth::{ BoundedHttpsStream, BoundedHttpsTransport, OAuthError, OAuthIoContext, PreparedHttpsRequest, @@ -304,8 +301,10 @@ fn expired_stream_refreshes_before_incremental_reader_and_cleans_up() { let transport = Arc::new(RefreshThenStream {refresh, stream: opaque_chat_stream::StreamTransport::new(200, "text/event-stream", vec![ concat!("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"fresh streamed answer\"},\"finish_reason\":\"stop\"}]}\n\n", "data: [DONE]\n\n").as_bytes().to_vec() ], false), refresh_count: std::sync::atomic::AtomicUsize::new(0)}); - let source = - "use agent; pub fn run(context: map) -> map { agent::provider_call(context[\"request\"]) }"; + let source = r#"use agent; pub fn run(context: map) -> map { + let request: string = context["request"]; + agent::provider_call(request) + }"#; let runner = AgentRunner::from_source(source, AgentConfig::default()) .expect("restricted outer stream entry") .with_host(AgentHostBridges { @@ -313,12 +312,21 @@ fn expired_stream_refreshes_before_incremental_reader_and_cleans_up() { ..AgentHostBridges::default() }) .with_cancellation(RunCancellation::with_timeout(Duration::from_secs(30))); - let result = vm_value_to_json(&runner.run_with_context(json_to_vm_value(&json!({"request": { + let request = json!({ "credential_id": ID, "provider": "openai", "model": "request-model", "stream": true, "messages": [{"role": "user", "content": [{"type": "text", "text": "stream after refresh"}]}], "tools": [] - }}))).expect("actual default stream result")); + }); + let context = json_to_vm_value(&json!({"request": request.to_string()})); + let result = vm_value_to_json( + &runner + .run_with_context(context) + .expect("actual default stream result"), + ); assert_eq!(result["ok"], json!(true), "public stream result: {result}"); - assert_eq!(result["response"]["text"], json!("fresh streamed answer")); + let response: serde_json::Value = + serde_json::from_str(result["response"].as_str().expect("response text")) + .expect("public response object"); + assert_eq!(response["text"], json!("fresh streamed answer")); let counts = &transport.stream.counts; assert_eq!(counts.starts.load(Ordering::SeqCst), 1); assert_eq!(counts.reads.load(Ordering::SeqCst), 1); diff --git a/tests/provider_opaque_bridge_tests.rs b/tests/cases/provider_opaque_bridge_tests.rs similarity index 94% rename from tests/provider_opaque_bridge_tests.rs rename to tests/cases/provider_opaque_bridge_tests.rs index 02d02b4..6a1e43a 100644 --- a/tests/provider_opaque_bridge_tests.rs +++ b/tests/cases/provider_opaque_bridge_tests.rs @@ -158,9 +158,18 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { fn string_context(fields: &[(&str, &str)]) -> Value { Value::map( - fields - .iter() - .map(|(name, value)| (Value::string(*name), Value::string(*value))) + ["credential_id", "path", "header_name"] + .into_iter() + .map(|name| { + let value = fields + .iter() + .find(|(key, _)| *key == name) + .map(|(_, value)| *value); + let carrier = value.map_or_else(rustscript_vm::option::none_carrier, |text| { + rustscript_vm::option::some_carrier(Value::string(text)) + }); + (Value::string(name), carrier) + }) .collect(), ) } @@ -191,7 +200,7 @@ fn agent_runner_uses_metadata_selected_access_handle_and_native_buffered_transpo let runner = fixture_runner(&home, Arc::clone(&transport), Some(cancellation.clone())); let mut events = EventCapture::default(); let result = runner - .run_with_context_and_events(Value::map(vec![]), &mut events, &cancellation) + .run_with_context_and_events(string_context(&[]), &mut events, &cancellation) .expect("same-VM RSS transport"); assert_eq!(field(&result, "ok"), &Value::Bool(true)); @@ -329,7 +338,7 @@ fn canceled_or_expired_run_is_denied_before_native_send() { let runner = fixture_runner(&home, Arc::clone(&transport), Some(cancellation.clone())); let mut events = EventCapture::default(); let result = - runner.run_with_context_and_events(Value::map(vec![]), &mut events, &cancellation); + runner.run_with_context_and_events(string_context(&[]), &mut events, &cancellation); assert!(result.is_err()); assert!(events.0.is_empty()); diff --git a/tests/provider_opaque_loop_tests.rs b/tests/cases/provider_opaque_loop_tests.rs similarity index 98% rename from tests/provider_opaque_loop_tests.rs rename to tests/cases/provider_opaque_loop_tests.rs index 65de553..bba53c7 100644 --- a/tests/provider_opaque_loop_tests.rs +++ b/tests/cases/provider_opaque_loop_tests.rs @@ -8,9 +8,7 @@ #![cfg(feature = "config-fixture")] -#[path = "support/provider_opaque_fixture.rs"] -#[allow(dead_code)] -pub mod provider_opaque_fixture; +use super::provider_opaque_fixture; use provider_opaque_fixture::{FixtureHome, json_to_vm_value, vm_value_to_json}; diff --git a/tests/provider_opaque_tool_loop_tests.rs b/tests/cases/provider_opaque_tool_loop_tests.rs similarity index 81% rename from tests/provider_opaque_tool_loop_tests.rs rename to tests/cases/provider_opaque_tool_loop_tests.rs index 0b50fa8..44dd157 100644 --- a/tests/provider_opaque_tool_loop_tests.rs +++ b/tests/cases/provider_opaque_tool_loop_tests.rs @@ -1,8 +1,6 @@ //! Authenticated real RSS tool follow-up and retry without re-execution. #![cfg(feature = "config-fixture")] -#[path = "support/provider_opaque_fixture.rs"] -#[allow(dead_code)] -pub mod provider_opaque_fixture; +use super::provider_opaque_fixture; use parking_lot::Mutex; use provider_opaque_fixture::{FixtureHome, json_to_vm_value, vm_value_to_json}; use rustscript_agent::auth::oauth::ScriptedHttpsTransport; @@ -12,8 +10,10 @@ use rustscript_agent::capabilities::{ LifecycleError, LifecycleLimits, NeverCancelled, ProcessCapability, ProcessLimits, SystemClock, TokenIssuer, UuidIssuer, }; +use rustscript_agent::gateway::AgentGatewayState; use rustscript_agent::{ - AgentConfig, AgentHostBridges, AgentRunner, RunCancellation, bundled_tool_registry, + AgentConfig, AgentGatewayConfig, AgentHostBridges, AgentRunner, RunCancellation, + bundled_tool_registry, }; use serde_json::{Value as JsonValue, json}; use std::{ @@ -21,7 +21,7 @@ use std::{ fs, path::PathBuf, sync::{ - Arc, + Arc, OnceLock, atomic::{AtomicU64, Ordering}, }, time::Duration, @@ -29,6 +29,22 @@ use std::{ const ID: &str = "synthetic-openai"; const ACCESS: &str = "SYNTHETIC_TOOL_LOOP_ACCESS"; const NOTE: &str = "actual filesystem result through authenticated followup"; +const RUN_BUDGET: Duration = Duration::from_secs(30); + +fn prepare_stateless_tools(entry: &std::path::Path) { + static PREPARED: OnceLock<()> = OnceLock::new(); + PREPARED.get_or_init(|| { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("setup runtime for the in-memory embedding"); + let _entered = runtime.enter(); + // Prepare the same stateless dispatcher without admitting a run or + // minting a tool pin. The embedding itself is not used for execution. + AgentGatewayState::with_agent_file(AgentGatewayConfig::default(), entry) + .expect("prepare real stateless tool dispatcher"); + }); +} #[derive(Default)] struct ReadLifecycle { starts: AtomicU64, @@ -87,14 +103,14 @@ impl Drop for Workspace { let _ = fs::remove_dir_all(&self.0); } } -fn tool_host(workspace: &Workspace, durable: Arc) -> AgentHostBridges { +fn tool_host( + workspace: &Workspace, + durable: Arc, + identity: String, +) -> AgentHostBridges { let owner = CapabilityOwner::new("profile-loop", "session-loop", "run-loop").expect("owner"); - let identity = bundled_tool_registry() - .expect("actual RSS registry") - .identity() - .to_string(); let clock = Arc::new(SystemClock); - let deadline_ms = clock.now_ms() + 30_000; + let deadline_ms = clock.now_ms() + RUN_BUDGET.as_millis() as u64; let lifecycle = Arc::new( CapabilityLifecycle::builder() .owner(owner.clone()) @@ -193,28 +209,41 @@ fn run_tool_followup_with_content(retry: bool, stateless: bool, omit_content: bo "role": "assistant", "content": "tool-followup answer" }}]}), ); - let mut bridges = tool_host(&workspace, durable.clone()); - bridges.allow_stateless_pin = stateless; - bridges.auth_host_factory = Some(home.auth_factory(transport.clone())); - let runner = AgentRunner::from_file( - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/agent/main.rss"), + let entry = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/agent/main.rss"); + let runner = AgentRunner::from_file(&entry, AgentConfig::default()).expect("compile real main"); + AgentRunner::from_bundled_named( + "rss/llm/provider_dispatch.rss", + "dispatch", AgentConfig::default(), ) - .expect("compile real main") - .with_host(bridges) - .with_skip_sleep(true) - .with_cancellation(RunCancellation::with_timeout(Duration::from_secs(30))); - let tools = bundled_tool_registry() - .expect("real tool registry") - .snapshot() - .schemas(); - let decision = vm_value_to_json(&runner.run_with_context(json_to_vm_value(&json!({ + .expect("compile real provider dispatcher"); + if stateless { + prepare_stateless_tools(&entry); + } + let factory = home.auth_factory(transport.clone()); + let registry = bundled_tool_registry().expect("real admitted tool registry"); + let context = json_to_vm_value(&json!({ "profile_id": "profile-loop", "session_id": "session-loop", "run_id": "run-loop", - "metadata": {"registry_identity": bundled_tool_registry().expect("real admitted registry").identity()}, - "credential_id": ID, "provider": "openai", "model": "request-model", "tools": tools, + "metadata": {"registry_identity": registry.identity()}, + "credential_id": ID, "provider": "openai", "model": "request-model", "tools": registry.snapshot().schemas(), "input": {"message": "read note.txt"}, "config": {"max_retries": 2}, "limits": {"max_turns": 8, "max_tool_calls": 8} - }))).expect("actual tool loop decision")); + })); + let mut bridges = tool_host(&workspace, durable.clone(), registry.identity().to_string()); + bridges.allow_stateless_pin = stateless; + bridges.auth_host_factory = Some(factory); + let runner = runner + .with_host(bridges) + .with_skip_sleep(true) + .with_cancellation(RunCancellation::with_timeout(RUN_BUDGET)); + let decision = vm_value_to_json(&runner.run_with_context(context).unwrap_or_else(|error| { + panic!( + "actual tool loop decision: {error:?}; native sends={}, tool starts={}, tool commits={}", + transport.take_sent().len(), + durable.starts.load(Ordering::SeqCst), + durable.commits.load(Ordering::SeqCst), + ) + })); if !stateless { assert_eq!(decision["kind"], "run.failed"); assert_eq!(decision["error"]["code"], "pinned_tool_missing"); diff --git a/tests/provider_openai_opaque_tests.rs b/tests/cases/provider_openai_opaque_tests.rs similarity index 99% rename from tests/provider_openai_opaque_tests.rs rename to tests/cases/provider_openai_opaque_tests.rs index 45c0bab..ed48717 100644 --- a/tests/provider_openai_opaque_tests.rs +++ b/tests/cases/provider_openai_opaque_tests.rs @@ -1,7 +1,6 @@ #![cfg(feature = "config-fixture")] -#[path = "support/opaque_chat_stream.rs"] -mod opaque_chat_stream; +use super::opaque_chat_stream; use std::fs; #[cfg(unix)] @@ -219,7 +218,7 @@ fn opaque_openai_chat_accepts_tool_calls_without_content() { json!([{ "id": "call_without_content", "name": "lookup", - "arguments": {"query": "catalog"} + "arguments_json": "{\"query\":\"catalog\"}" }]) ); assert_eq!(transport.take_sent().len(), 1); @@ -279,7 +278,12 @@ fn opaque_openai_chat_uses_native_auth_and_returns_canonical_text_usage_and_tool ); assert_eq!(result["response"]["tool_calls"][0]["name"], json!("lookup")); assert_eq!( - result["response"]["tool_calls"][0]["arguments"]["query"], + serde_json::from_str::( + result["response"]["tool_calls"][0]["arguments_json"] + .as_str() + .expect("typed argument text"), + ) + .expect("valid tool arguments")["query"], json!("rustscript") ); diff --git a/tests/provider_quota_loop_tests.rs b/tests/cases/provider_quota_loop_tests.rs similarity index 99% rename from tests/provider_quota_loop_tests.rs rename to tests/cases/provider_quota_loop_tests.rs index 0e50cb3..957262d 100644 --- a/tests/provider_quota_loop_tests.rs +++ b/tests/cases/provider_quota_loop_tests.rs @@ -1,8 +1,6 @@ //! Real main/default/native authenticated provider behavior. #![cfg(feature = "config-fixture")] -#[path = "support/provider_opaque_fixture.rs"] -#[allow(dead_code)] -pub mod provider_opaque_fixture; +use super::provider_opaque_fixture; use provider_opaque_fixture::{FixtureHome, json_to_vm_value, vm_value_to_json}; use rustscript_agent::auth::oauth::ScriptedHttpsTransport; use rustscript_agent::{AgentConfig, AgentHostBridges, AgentRunner, RunCancellation}; diff --git a/tests/provider_refresh_transaction_tests.rs b/tests/cases/provider_refresh_transaction_tests.rs similarity index 95% rename from tests/provider_refresh_transaction_tests.rs rename to tests/cases/provider_refresh_transaction_tests.rs index 464c201..e2feaac 100644 --- a/tests/provider_refresh_transaction_tests.rs +++ b/tests/cases/provider_refresh_transaction_tests.rs @@ -82,18 +82,12 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { .unwrap_or_else(|| panic!("missing RSS field {name} in {value:?}")) } -fn optional_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { - let Value::Map(entries) = value else { - panic!("expected named RSS value, got {value:?}"); - }; - entries.iter().find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value), - _ => None, - }) -} - -fn is_null_or_absent(value: &Value, name: &str) -> bool { - matches!(optional_field(value, name), None | Some(Value::Null)) +fn option_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { + match rustscript_vm::option::payload(field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value), + _ => panic!("invalid Option payload for {name}"), + } } fn record(fields: Vec<(&str, Value)>) -> Value { @@ -180,7 +174,7 @@ fn stored_yaml(home: &TempHome) -> String { } fn metadata_of(result: &Value) -> &Value { - field(result, "metadata") + option_field(result, "metadata").expect("persisted metadata") } fn assert_no_secrets(rendered: &str) { @@ -221,7 +215,10 @@ fn expired_credential_refresh_replaces_rotated_tokens_and_persists_generation_fi field(metadata, "expires_at_ms"), &Value::Int(NOW_MS + 3_600_000) ); - assert_eq!(field(metadata, "last_refresh_at_ms"), &Value::Int(NOW_MS)); + assert_eq!( + option_field(metadata, "last_refresh_at_ms").expect("refresh timestamp"), + &Value::Int(NOW_MS) + ); // Exactly one trusted native POST carrying the host-injected stored secret. let sent = transport.take_sent(); @@ -332,7 +329,7 @@ fn invalid_grant_maps_reauth_required_without_save_or_second_request() { assert_eq!(field(&result, "ok"), &Value::Bool(false), "{result:?}"); assert_eq!(field(&result, "code"), &Value::string("reauth_required")); assert_eq!(field(&result, "outcome"), &Value::string("")); - assert!(is_null_or_absent(&result, "metadata")); + assert!(option_field(&result, "metadata").is_none()); assert_eq!(stored_yaml(&home), before, "no save on reauth_required"); assert_eq!(transport.take_sent().len(), 1, "single attempt, no retry"); let rendered = format!("{result:?}"); @@ -354,7 +351,7 @@ fn non_grant_client_error_is_refresh_failed_without_save() { let result = run_refresh(&mut vm, &home_handle, CREDENTIAL_ID); assert_eq!(field(&result, "ok"), &Value::Bool(false), "{result:?}"); assert_eq!(field(&result, "code"), &Value::string("refresh_failed")); - assert!(is_null_or_absent(&result, "metadata")); + assert!(option_field(&result, "metadata").is_none()); assert_eq!(stored_yaml(&home), before); assert_eq!(transport.take_sent().len(), 1); let _ = invoke(&mut vm, "cleanup", &[policy]); @@ -380,7 +377,7 @@ fn malformed_success_lifetime_is_response_invalid_without_leak() { &Value::string("refresh_response_invalid") ); assert_eq!(field(&result, "outcome"), &Value::string("")); - assert!(is_null_or_absent(&result, "metadata")); + assert!(option_field(&result, "metadata").is_none()); assert_eq!( stored_yaml(&home), before, @@ -410,7 +407,7 @@ fn missing_access_token_is_response_invalid_without_save() { &Value::string("refresh_response_invalid") ); assert_eq!(field(&result, "outcome"), &Value::string("")); - assert!(is_null_or_absent(&result, "metadata")); + assert!(option_field(&result, "metadata").is_none()); assert_eq!(stored_yaml(&home), before); assert_eq!(transport.take_sent().len(), 1); assert_no_secrets(&format!("{result:?}")); @@ -435,7 +432,7 @@ fn explicit_null_token_type_is_response_invalid_without_save() { &Value::string("refresh_response_invalid") ); assert_eq!(field(&result, "outcome"), &Value::string("")); - assert!(is_null_or_absent(&result, "metadata")); + assert!(option_field(&result, "metadata").is_none()); assert_eq!(stored_yaml(&home), before); assert_eq!(transport.take_sent().len(), 1); assert_no_secrets(&format!("{result:?}")); @@ -460,7 +457,7 @@ fn nonbearer_success_is_response_invalid_without_leak() { &Value::string("refresh_response_invalid") ); assert_eq!(field(&result, "outcome"), &Value::string("")); - assert!(is_null_or_absent(&result, "metadata")); + assert!(option_field(&result, "metadata").is_none()); assert_eq!(stored_yaml(&home), before); assert_eq!(transport.take_sent().len(), 1); assert_no_secrets(&format!("{result:?}")); @@ -505,7 +502,7 @@ fn unknown_credential_yields_typed_admission_failure_and_zero_send() { field(&result, "code"), &Value::string("credential_not_admitted") ); - assert!(is_null_or_absent(&result, "metadata")); + assert!(option_field(&result, "metadata").is_none()); assert_eq!(stored_yaml(&home), before); assert!( transport.take_sent().is_empty(), diff --git a/tests/provider_stream_codec_rss_tests.rs b/tests/cases/provider_stream_codec_rss_tests.rs similarity index 71% rename from tests/provider_stream_codec_rss_tests.rs rename to tests/cases/provider_stream_codec_rss_tests.rs index 2b49c9b..c1e380f 100644 --- a/tests/provider_stream_codec_rss_tests.rs +++ b/tests/cases/provider_stream_codec_rss_tests.rs @@ -7,94 +7,58 @@ use std::path::PathBuf; use std::sync::Arc; -use rustscript_agent::auth::host::auth_host_catalog; -use rustscript_vm::{ - CapabilityProfile, CompileSourceFileOptions, HostFunctionRegistry, SourceFlavor, Value, Vm, - VmStatus, compile_source_at_path_with_flavor_and_options, - register_http_builtin_module_from_catalog, +use rustscript_agent::runtime::{ + AgentConfig, AgentRunner, RunCancellation, RunDeliveryError, RunEventSink, }; +use rustscript_vm::Value; use serde_json::{Value as JsonValue, json}; -fn fixture_path() -> PathBuf { - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("tests/fixtures/providers/opaque_stream_codec.rss") +struct CodecSources(PathBuf); + +impl Drop for CodecSources { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } } -fn codec_vm() -> Vm { - let path = fixture_path(); - let source = std::fs::read_to_string(&path).expect("read committed codec fixture"); - let catalog = auth_host_catalog(); - let compiled = compile_source_at_path_with_flavor_and_options( - &path, - &source, - SourceFlavor::RustScript, - CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()), +fn codec_vm() -> (AgentRunner, CodecSources) { + let repo = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let sources = + CodecSources(std::env::temp_dir().join(format!("chat-codec-{}", uuid::Uuid::new_v4()))); + std::fs::create_dir_all(sources.0.join("llm")).expect("create codec module root"); + let entry = sources.0.join("codec.rss"); + std::fs::copy( + repo.join("tests/fixtures/providers/opaque_stream_codec.rss"), + &entry, ) - .expect("compile production codec fixture against the restricted catalog"); - let mut vm = Vm::new_shared(Arc::new(compiled.program)); - // Mirrors build_restricted_registry_with_builtins: start empty (installing - // the standard modules on top of restricted() would conflict on identity), - // install the frozen HTTP module from the exact catalog, then admit only - // the explicit builtin list. openai_chat.rss carries transitive http/bytes - // declarations, so their imports must be admitted without invoking HTTP. - let mut registry = HostFunctionRegistry::empty(); - registry.set_capability_profile(CapabilityProfile::deny_all()); - register_http_builtin_module_from_catalog(&mut registry, catalog.as_ref()) - .expect("install frozen http module from the restricted catalog"); - for name in [ - "json::encode", - "json::decode", - "bytes::to_utf8", - "bytes::to_utf8_lossy", - "bytes::to_array_u8", - "bytes::from_utf8", - "http::request::new", - "http::request::set_header", - "http::request::set_body_text", - "http::request::set_body_bytes", - "http::client::request", - "http::client::sse", - "http::response::status", - "http::response::url", - "http::response::header_values", - "http::response::header_names", - "http::response::body", - "http::headers::values", - "http::headers::names", - "http::sse_summary::outcome", - "http::sse_summary::status", - "http::sse_summary::headers", - "http::sse_summary::url", - "http::sse_summary::items", - "http::sse_summary::bytes_received", - "http::sse_summary::bytes_sent", - ] { - // Same admission shape as build_restricted_registry_with_builtins: - // authorize names already installed from the catalog, else admit the - // namespaced builtin. openai_chat.rss carries transitive http/bytes - // declarations, so those imports must be admitted without the test - // ever invoking HTTP. - if registry.contains_name(name) { - registry.authorize_registered_builtin_import(name); - } else { - registry - .allow_builtin(name) - .expect("admit standard builtin"); - } + .expect("copy codec entry"); + for name in ["openai_chat.rss", "openai_stream_codec.rss", "types.rss"] { + let module = PathBuf::from("llm").join(name); + std::fs::copy(repo.join("rss").join(&module), sources.0.join(&module)) + .expect("copy current production codec module"); + } + let runner = AgentRunner::from_file(entry, AgentConfig::default()) + .expect("compile production codec fixture against the restricted catalog"); + (runner, sources) +} + +struct NoEvents; + +impl RunEventSink for NoEvents { + fn deliver(&mut self, value: Value) -> Result<(), RunDeliveryError> { + panic!("pure codec unexpectedly emitted an event: {value:?}"); } - registry - .bind_vm_cached(&mut vm) - .expect("bind restricted registry"); - assert!(matches!( - vm.run().expect("RSS declarations"), - VmStatus::Halted - )); - vm } -fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> Value { - let callable = vm.resolve_exported_callable(name).expect("RSS export"); - vm.invoke_callable(callable, args).expect("RSS invoke") +fn invoke(runner: &AgentRunner, name: &str, input: &JsonValue) -> Value { + runner + .run_named_with_context_and_events( + name, + json_to_value(input), + &mut NoEvents, + &RunCancellation::new(), + ) + .expect("RSS invoke") } fn json_to_value(value: &JsonValue) -> Value { @@ -150,13 +114,13 @@ fn value_to_json(value: &Value) -> JsonValue { #[test] fn stream_body_marks_stream_and_include_usage() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let request = json!({ "model": "gpt-test", "messages": [{"role": "user", "content": [{"type": "text", "text": "hello"}]}], "sampling": {"temperature": 0.5} }); - let body = invoke(&mut vm, "request_body", &[json_to_value(&request)]); + let body = invoke(&vm, "request_body", &request); let Value::String(body) = body else { panic!("request_body must return a string"); }; @@ -179,18 +143,14 @@ fn stream_body_marks_stream_and_include_usage() { #[test] fn split_text_usage_and_done_produce_canonical_response() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let chunks = json!([ {"choices": [{"index": 0, "delta": {"role": "assistant", "content": "Hel"}}]}, {"choices": [{"index": 0, "delta": {"content": "lo "}}]}, {"choices": [{"index": 0, "delta": {"content": "world"}, "finish_reason": "stop"}]}, {"choices": [], "usage": {"prompt_tokens": 7, "completion_tokens": 3, "total_tokens": 10}} ]); - let result = invoke( - &mut vm, - "collect", - &[json_to_value(&chunks), Value::Bool(true)], - ); + let result = invoke(&vm, "collect", &json!({"chunks": chunks, "done": true})); let result = value_to_json(&result); assert_eq!(result["ok"], json!(true), "result: {result}"); let response = &result["response"]; @@ -202,14 +162,23 @@ fn split_text_usage_and_done_produce_canonical_response() { assert_eq!(response["tool_calls"], json!([])); } -fn collect(vm: &mut Vm, chunks: &JsonValue, done: bool) -> JsonValue { - let result = invoke(vm, "collect", &[json_to_value(chunks), Value::Bool(done)]); +fn tool_arguments(call: &JsonValue) -> JsonValue { + serde_json::from_str( + call["arguments_json"] + .as_str() + .expect("typed argument text"), + ) + .expect("valid tool arguments") +} + +fn collect(vm: &AgentRunner, chunks: &JsonValue, done: bool) -> JsonValue { + let result = invoke(vm, "collect", &json!({"chunks": chunks, "done": done})); value_to_json(&result) } #[test] fn stream_body_preserves_user_text_and_splices_tool_schemas() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); // The user text deliberately looks like an internal splice marker; it must // survive verbatim while the real marker slot is replaced by the parts // array. The tool schema is spliced raw into `parameters`. @@ -219,7 +188,7 @@ fn stream_body_preserves_user_text_and_splices_tool_schemas() { "tools": [{"name": "read_file", "description": "reads", "schema_json": "{\"type\":\"object\"}"}], "tool_choice": "auto" }); - let body = invoke(&mut vm, "request_body", &[json_to_value(&request)]); + let body = invoke(&vm, "request_body", &request); let Value::String(body) = body else { panic!("request_body must return a string"); }; @@ -243,12 +212,12 @@ fn stream_body_preserves_user_text_and_splices_tool_schemas() { #[test] fn role_only_and_empty_heartbeat_have_no_text() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let chunks = json!([ {"choices": [{"index": 0, "delta": {"role": "assistant"}}]}, {"choices": []} ]); - let result = collect(&mut vm, &chunks, true); + let result = collect(&vm, &chunks, true); assert_eq!(result["ok"], json!(true), "result: {result}"); assert_eq!(result["response"]["text"], json!("")); assert_eq!(result["response"]["stop_reason"], json!("")); @@ -257,12 +226,12 @@ fn role_only_and_empty_heartbeat_have_no_text() { #[test] fn usage_on_choices_bearing_chunk_is_processed() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let chunks = json!([ {"choices": [{"index": 0, "delta": {"content": "x"}}], "usage": {"prompt_tokens": 5, "completion_tokens": 2, "total_tokens": 7}} ]); - let result = collect(&mut vm, &chunks, true); + let result = collect(&vm, &chunks, true); assert_eq!(result["ok"], json!(true), "result: {result}"); assert_eq!(result["response"]["text"], json!("x")); assert_eq!(result["response"]["usage"]["input_tokens"], json!(5)); @@ -272,7 +241,7 @@ fn usage_on_choices_bearing_chunk_is_processed() { #[test] fn split_tool_arguments_merge_across_two_indices() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let chunks = json!([ {"choices": [{"index": 0, "delta": {"tool_calls": [ {"index": 0, "id": "call_a", "type": "function", "function": {"name": "read_file", "arguments": "{\"pa"}} @@ -284,26 +253,26 @@ fn split_tool_arguments_merge_across_two_indices() { {"index": 1, "id": "call_b", "function": {"name": "search", "arguments": "{\"q\":1}"}} ]}, "finish_reason": "tool_calls"}]} ]); - let result = collect(&mut vm, &chunks, true); + let result = collect(&vm, &chunks, true); assert_eq!(result["ok"], json!(true), "result: {result}"); let calls = &result["response"]["tool_calls"]; assert_eq!(calls.as_array().map(Vec::len), Some(2), "result: {result}"); assert_eq!(calls[0]["id"], json!("call_a")); assert_eq!(calls[0]["name"], json!("read_file")); - assert_eq!(calls[0]["arguments"], json!({"path": "a"})); + assert_eq!(tool_arguments(&calls[0]), json!({"path": "a"})); assert_eq!(calls[1]["id"], json!("call_b")); assert_eq!(calls[1]["name"], json!("search")); - assert_eq!(calls[1]["arguments"], json!({"q": 1})); + assert_eq!(tool_arguments(&calls[1]), json!({"q": 1})); assert_eq!(result["response"]["stop_reason"], json!("tool_calls")); } #[test] fn eof_without_done_fails_and_never_returns_partial_text() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let chunks = json!([ {"choices": [{"index": 0, "delta": {"content": "partial"}}]} ]); - let result = collect(&mut vm, &chunks, false); + let result = collect(&vm, &chunks, false); assert_eq!(result["ok"], json!(false), "result: {result}"); assert_eq!(result["error"]["code"], json!("stream_eof_without_done")); assert_eq!( @@ -315,11 +284,11 @@ fn eof_without_done_fails_and_never_returns_partial_text() { #[test] fn malformed_delta_fails_statically_and_done_does_not_repair() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); // A choice without a delta map is a semantic protocol violation. let chunks = json!([{"choices": [{"index": 0, "finish_reason": "stop"}]}]); // Even a subsequent DONE must not repair the failed state. - let result = collect(&mut vm, &chunks, true); + let result = collect(&vm, &chunks, true); assert_eq!(result["ok"], json!(false), "result: {result}"); assert_eq!(result["error"]["code"], json!("malformed_payload")); assert_eq!(result["error"]["type"], json!("malformed_payload")); @@ -332,13 +301,13 @@ fn malformed_delta_fails_statically_and_done_does_not_repair() { #[test] fn negative_tool_index_is_malformed() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let chunks = json!([ {"choices": [{"index": 0, "delta": {"tool_calls": [ {"index": -5, "id": "call_x", "function": {"name": "n", "arguments": "{}"}} ]}}]} ]); - let result = collect(&mut vm, &chunks, true); + let result = collect(&vm, &chunks, true); assert_eq!(result["ok"], json!(false), "result: {result}"); assert_eq!(result["error"]["code"], json!("malformed_payload")); let message = result["error"]["message"].as_str().unwrap_or_default(); @@ -350,7 +319,7 @@ fn negative_tool_index_is_malformed() { #[test] fn records_after_done_are_ignored() { - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let chunks = json!([ {"choices": [{"index": 0, "delta": {"content": "ok"}}]} ]); @@ -358,11 +327,7 @@ fn records_after_done_are_ignored() { "choices": [{"index": 0, "delta": {"content": "LATE"}}], "usage": {"total_tokens": 99} }); - let result = invoke( - &mut vm, - "late_record", - &[json_to_value(&chunks), json_to_value(&late)], - ); + let result = invoke(&vm, "late_record", &json!({"chunks": chunks, "late": late})); let result = value_to_json(&result); assert_eq!(result["ok"], json!(true), "result: {result}"); assert_eq!( @@ -379,7 +344,7 @@ fn records_after_done_are_ignored() { #[test] fn stream_body_encodes_durable_background_handle_in_tool_content() { const HANDLE: &str = "fedcba9876543210fedcba9876543210"; - let mut vm = codec_vm(); + let (vm, _sources) = codec_vm(); let request = json!({ "model": "gpt-test", "messages": [ @@ -396,7 +361,7 @@ fn stream_body_encodes_durable_background_handle_in_tool_content() { ]} ] }); - let body = invoke(&mut vm, "request_body", &[json_to_value(&request)]); + let body = invoke(&vm, "request_body", &request); let Value::String(body) = body else { panic!("request_body must return a string"); }; diff --git a/tests/provider_tests.rs b/tests/cases/provider_tests.rs similarity index 91% rename from tests/provider_tests.rs rename to tests/cases/provider_tests.rs index ed9cb56..a8cd752 100644 --- a/tests/provider_tests.rs +++ b/tests/cases/provider_tests.rs @@ -10,11 +10,10 @@ //! (method, URL, headers, body) proves the public contract. Real provider //! transcripts still come from `tests/fixtures/providers/`. -#[path = "support/opaque_chat_stream.rs"] +#[path = "../support/opaque_chat_stream.rs"] mod opaque_chat_stream; -#[path = "support/provider_opaque_fixture.rs"] -pub mod provider_opaque_fixture; +use super::provider_opaque_fixture; use std::path::PathBuf; use std::sync::Arc; @@ -610,11 +609,22 @@ fn openai_chat_non_stream_tool_calls() { assert_eq!(calls.len(), 2); assert_eq!(calls[0]["id"], json!("call_ab12")); assert_eq!(calls[0]["name"], json!("read_file")); - assert_eq!(calls[0]["arguments"]["path"], json!("README.md")); - assert_eq!(calls[0]["arguments"]["lines"], json!(40)); + assert_eq!( + serde_json::from_str::( + calls[0]["arguments_json"].as_str().expect("argument text") + ) + .expect("argument object"), + json!({"path": "README.md", "lines": 40}) + ); assert_eq!(calls[1]["id"], json!("call_cd34")); assert_eq!(calls[1]["name"], json!("search_files")); - assert_eq!(calls[1]["arguments"]["pattern"], json!("provider")); + assert_eq!( + serde_json::from_str::( + calls[1]["arguments_json"].as_str().expect("argument text") + ) + .expect("argument object"), + json!({"pattern": "provider"}) + ); assert_eq!(transport.take_sent().len(), 1); } @@ -1266,10 +1276,22 @@ fn openai_chat_stream_tool_call_chunk_aggregation() { assert_eq!(calls.len(), 2); assert_eq!(calls[0]["id"], json!("call_ab12")); assert_eq!(calls[0]["name"], json!("read_file")); - assert_eq!(calls[0]["arguments"], json!({"path": "README.md"})); + assert_eq!( + serde_json::from_str::( + calls[0]["arguments_json"].as_str().expect("argument text") + ) + .expect("argument object"), + json!({"path": "README.md"}) + ); assert_eq!(calls[1]["id"], json!("call_cd34")); assert_eq!(calls[1]["name"], json!("search_files")); - assert_eq!(calls[1]["arguments"], json!({"pattern": "provider"})); + assert_eq!( + serde_json::from_str::( + calls[1]["arguments_json"].as_str().expect("argument text") + ) + .expect("argument object"), + json!({"pattern": "provider"}) + ); assert_eq!(response["usage"]["total_tokens"], json!(30)); let sent = transport.counts.sent.lock().expect("native request"); @@ -1696,145 +1718,6 @@ fn openai_chat_stream_unsupported_mime_fails_closed_without_echo() { assert!(transport.counts.drops.load(Ordering::SeqCst) >= 1); } -// --------------------------------------------------------------------------- -// S3: OpenAI Responses adapter (blocked references) -// --------------------------------------------------------------------------- -// -// `openai_responses.rss` is a typed `not_implemented` stub. The buffered and -// streaming transcripts under `tests/fixtures/providers/openai_responses/` -// document the wire contract; these tests stay ignored until the adapter is -// implemented. The streaming transcript mirrors the real Responses API -// transport: every payload event carries an `event:` line whose value matches -// the `type` field of its `data:` JSON payload, and the stream ends with a -// bare `data: [DONE]`. - -#[ignore = "openai_responses adapter is a not_implemented stub (see module doc)"] -#[test] -fn openai_responses_buffered_transcript_is_referenced() { - let home = openai_home(); - let transport = Arc::new(ScriptedHttpsTransport::new()); - let body: JsonValue = - serde_json::from_str(&read_provider_fixture("openai_responses/response.json")) - .expect("fixture json"); - transport.push_json(200, body); - let runner = harness_runner(&home, Arc::clone(&transport)); - - let result = run_harness( - &runner, - "openai_responses", - canonical_request(false), - openai_profile(), - "synthetic-openai", - ); - - assert!(result["ok"] == json!(false), "{result}"); - assert_eq!(result["error"]["code"], json!("not_implemented")); - assert!(transport.take_sent().is_empty()); -} - -#[ignore = "openai_responses adapter is a not_implemented stub (see module doc)"] -#[test] -fn openai_responses_stream_transcript_matches_real_transport_and_is_referenced() { - let events = sse_events(&read_provider_fixture("openai_responses/stream.sse")); - - // Real Responses API transport shape: each payload event is an - // `event: ` / `data: {json}` pair with the event name matching the - // payload's `type` field, and the stream ends with a bare `data: [DONE]` - // terminator. - let (done, payload_events) = events - .split_last() - .expect("stream fixture must have at least one event"); - for event in payload_events { - let event_line = event - .lines() - .find(|line| line.starts_with("event: ")) - .unwrap_or_else(|| panic!("event must carry an event: line: {event}")); - let data_line = event - .lines() - .find(|line| line.starts_with("data: ")) - .unwrap_or_else(|| panic!("event must carry a data: line: {event}")); - let event_name = event_line.strip_prefix("event: ").expect("event name"); - let payload: JsonValue = - serde_json::from_str(data_line.strip_prefix("data: ").expect("data payload")) - .unwrap_or_else(|error| panic!("data payload must be JSON ({error}): {event}")); - assert_eq!( - payload["type"].as_str(), - Some(event_name), - "event: line must match the data.type field: {event}" - ); - } - assert!( - done.starts_with("data:") && done.contains("[DONE]"), - "stream must end with a bare data: [DONE] terminator, got: {done}" - ); - - let home = openai_home(); - let transport = Arc::new(ScriptedHttpsTransport::new()); - let runner = harness_runner(&home, Arc::clone(&transport)); - - let result = run_harness( - &runner, - "openai_responses", - canonical_request(true), - openai_profile(), - "synthetic-openai", - ); - - assert!(result["ok"] == json!(false), "{result}"); - assert_eq!(result["error"]["code"], json!("not_implemented")); - assert!(transport.take_sent().is_empty()); -} - -// --------------------------------------------------------------------------- -// S4: Anthropic Messages adapter (blocked references) -// --------------------------------------------------------------------------- -// -// `anthropic_messages.rss` is a typed `not_implemented` stub. The buffered and -// streaming transcripts under `tests/fixtures/providers/anthropic/` document -// the wire contract; these tests stay ignored until the adapter is -// implemented. - -#[ignore = "anthropic_messages adapter is a not_implemented stub (see module doc)"] -#[test] -fn anthropic_messages_buffered_transcript_is_referenced() { - let home = openai_home(); - let transport = Arc::new(ScriptedHttpsTransport::new()); - let runner = harness_runner(&home, Arc::clone(&transport)); - - let result = run_harness( - &runner, - "anthropic_messages", - canonical_request(false), - json!({"provider": "anthropic"}), - "synthetic-openai", - ); - - assert!(result["ok"] == json!(false), "{result}"); - assert_eq!(result["error"]["code"], json!("not_implemented")); - assert!(transport.take_sent().is_empty()); -} - -#[ignore = "anthropic_messages adapter is a not_implemented stub (see module doc)"] -#[test] -fn anthropic_messages_stream_transcript_is_referenced() { - let home = openai_home(); - let transport = Arc::new(ScriptedHttpsTransport::new()); - let runner = harness_runner(&home, Arc::clone(&transport)); - let _events = sse_events(&read_provider_fixture("anthropic/stream.sse")); - - let result = run_harness( - &runner, - "anthropic_messages", - canonical_request(true), - json!({"provider": "anthropic"}), - "synthetic-openai", - ); - - assert!(result["ok"] == json!(false), "{result}"); - assert_eq!(result["error"]["code"], json!("not_implemented")); - assert!(transport.take_sent().is_empty()); -} - // --------------------------------------------------------------------------- // Default dispatch aliases through native auth // --------------------------------------------------------------------------- diff --git a/tests/real_tool_continuation_tests.rs b/tests/cases/real_tool_continuation_tests.rs similarity index 92% rename from tests/real_tool_continuation_tests.rs rename to tests/cases/real_tool_continuation_tests.rs index a19cb1f..4866b4e 100644 --- a/tests/real_tool_continuation_tests.rs +++ b/tests/cases/real_tool_continuation_tests.rs @@ -1,11 +1,7 @@ //! Production Responses/tool-loop recovery, with fixture HTTP and real tools. #![cfg(feature = "config-fixture")] -#[path = "support/opaque_chat_stream.rs"] -#[allow(dead_code)] -mod opaque_chat_stream; -#[path = "support/provider_opaque_fixture.rs"] -#[allow(dead_code)] -mod provider_opaque_fixture; +use super::opaque_chat_stream; +use super::provider_opaque_fixture; use opaque_chat_stream::StreamTransport; use provider_opaque_fixture::FixtureHome; use rustscript_agent::auth::oauth::{ @@ -13,7 +9,9 @@ use rustscript_agent::auth::oauth::{ RawHttpsResponse, }; use rustscript_agent::config::ProviderProfile; -use rustscript_agent::{AdmitRunRequest, AgentGatewayConfig, AgentGatewayState}; +use rustscript_agent::{ + AdmitRunRequest, AgentConfig, AgentGatewayConfig, AgentGatewayState, AgentRunner, +}; use serde_json::{Value, json}; use std::{ collections::{HashSet, VecDeque}, @@ -174,11 +172,20 @@ async fn validation_failure_has_durable_output_before_next_responses_turn() { .unwrap() .with_workspace_home(config_home); let service = state.service(); + service + .materialize_cached_runner() + .expect("prepare real workspace and tool runners"); service.set_auth_host_factory(home.auth_factory(transport.clone())); service .set_tool_registry(rustscript_agent::bundled_tool_registry().unwrap()) .unwrap(); service.upsert_provider_profile(ProviderProfile::new(PROVIDER,json!({"profile":PROVIDER,"protocol":"codex-responses","base_url":BASE,"credential_id":"fixture-access"})).unwrap()); + AgentRunner::from_bundled_named( + "rss/llm/provider_dispatch.rss", + "dispatch", + AgentConfig::default(), + ) + .expect("prepare the real provider dispatcher before timed admission"); let run=service.admit(AdmitRunRequest{input:json!("Create note.txt with REAL_TOOL_VALUE, read it, and run ls in the workspace. Recover from invalid arguments."),provider:Some(PROVIDER.into()),model:Some("fixture-model".into()),..Default::default()}).await.unwrap(); service .clone() @@ -194,12 +201,13 @@ async fn validation_failure_has_durable_output_before_next_responses_turn() { events.iter().any(|e| e["event"] == "run.completed"), "{events:?}" ); + let sent = transport.sent.lock().unwrap(); + assert_eq!(sent.len(), 8, "provider transcript incomplete: {events:?}"); assert_eq!( - fs::read_to_string(project.join("note.txt")).unwrap(), + fs::read_to_string(project.join("note.txt")) + .unwrap_or_else(|error| panic!("real write_file output: {error}; events={events:?}")), "REAL_TOOL_VALUE" ); - let sent = transport.sent.lock().unwrap(); - assert_eq!(sent.len(), 8); let contract = sent[0]["instructions"].as_str().unwrap(); assert!( contract.contains("workspace-relative") diff --git a/tests/rss_file_tool_tests.rs b/tests/cases/rss_file_tool_tests.rs similarity index 97% rename from tests/rss_file_tool_tests.rs rename to tests/cases/rss_file_tool_tests.rs index 8549719..50eced5 100644 --- a/tests/rss_file_tool_tests.rs +++ b/tests/cases/rss_file_tool_tests.rs @@ -10,6 +10,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; +use super::rss_tool_carrier::{json_to_vm_value, tool_result, vm_value_to_json}; use rustscript_agent::capabilities::{ ApprovalGate, ArtifactCapability, ArtifactLimits, CancellationFlag, CapabilityLifecycle, CapabilityOwner, CapabilityRisk, DurableStarted, DurableToolLifecycle, FilesystemCapability, @@ -18,61 +19,8 @@ use rustscript_agent::capabilities::{ }; use rustscript_agent::config::FileToolConfig; use rustscript_agent::{AgentConfig, AgentHostBridges, AgentRunner, ToolResult}; -use rustscript_vm::Value as VmValue; use serde_json::{Value, json}; -fn json_to_vm_value(value: &Value) -> VmValue { - match value { - Value::Null => VmValue::Null, - Value::Bool(value) => VmValue::Bool(*value), - Value::Number(value) => { - if let Some(value) = value.as_i64() { - VmValue::Int(value) - } else { - VmValue::Float(value.as_f64().expect("finite json number")) - } - } - Value::String(value) => VmValue::string(value), - Value::Array(values) => VmValue::Array(std::sync::Arc::new( - values.iter().map(json_to_vm_value).collect::>(), - )), - Value::Object(entries) => VmValue::map( - entries - .iter() - .map(|(key, value)| (VmValue::string(key), json_to_vm_value(value))) - .collect(), - ), - } -} - -fn vm_value_to_json(value: &VmValue) -> Value { - match value { - VmValue::Null => Value::Null, - VmValue::Int(value) => json!(value), - VmValue::Float(value) => serde_json::Number::from_f64(*value) - .map(Value::Number) - .unwrap_or(Value::Null), - VmValue::Bool(value) => json!(value), - VmValue::String(value) => Value::String(value.to_string()), - VmValue::Bytes(value) => Value::String(String::from_utf8_lossy(value).into_owned()), - VmValue::Array(values) => Value::Array(values.iter().map(vm_value_to_json).collect()), - VmValue::Map(entries) => Value::Object( - entries - .iter() - .map(|(key, value)| (vm_map_key_to_string(key), vm_value_to_json(value))) - .collect(), - ), - VmValue::Callable(_) => Value::String("".to_string()), - } -} - -fn vm_map_key_to_string(value: &VmValue) -> String { - match value { - VmValue::String(value) => value.to_string(), - other => vm_value_to_json(other).to_string(), - } -} - const REGISTRY_IDENTITY: &str = "rss-file-tool-equivalence"; static NEXT_FIXTURE: AtomicU64 = AtomicU64::new(0); @@ -377,6 +325,24 @@ impl LifecycleClock for JumpClock { } } +/// Traversal parity is independent of scheduler time. The wall-budget tests +/// inject their own advancing monotonic clock. +struct TraversalClock; + +impl LifecycleClock for TraversalClock { + fn now_ms(&self) -> u64 { + SystemClock.now_ms() + } + + fn now(&self) -> Instant { + SystemClock.now() + } + + fn monotonic_ms(&self) -> Option { + Some(0) + } +} + struct CancelAfter { checks: AtomicU64, cancel_at: u64, @@ -476,7 +442,7 @@ fn run_rss_exec(fixture: &Fixture, config: &FileToolConfig, exec: RssExec) -> Rs .run_with_context(json_to_vm_value(&context)) .unwrap_or_else(|error| panic!("rss {} run failed: {error}", exec.module)); RssRun { - result: unwrap_committed(vm_value_to_json(&output)), + result: tool_result(&output), started: exec.durable.started_len(), artifacts, } @@ -494,7 +460,7 @@ fn run_rss_tool( cancellation: Arc, install_artifacts: bool, ) -> RssRun { - let clock = Arc::new(SystemClock); + let clock = Arc::new(TraversalClock); let deadline_ms = clock.now_ms() + 60_000; run_rss_exec( fixture, @@ -515,14 +481,6 @@ fn run_rss_tool( ) } -fn unwrap_committed(value: Value) -> Value { - if value.get("kind").and_then(Value::as_str) == Some("committed") { - value.get("result").cloned().unwrap_or(value) - } else { - value - } -} - fn assert_canonical_envelope(rss: &Value) { let parsed: ToolResult = serde_json::from_value(rss.clone()).expect("canonical tool result schema"); diff --git a/tests/rss_mutating_file_tool_tests.rs b/tests/cases/rss_mutating_file_tool_tests.rs similarity index 97% rename from tests/rss_mutating_file_tool_tests.rs rename to tests/cases/rss_mutating_file_tool_tests.rs index 73b2d42..af37fa1 100644 --- a/tests/rss_mutating_file_tool_tests.rs +++ b/tests/cases/rss_mutating_file_tool_tests.rs @@ -11,6 +11,7 @@ use std::sync::{Arc, Barrier, Mutex}; use std::thread; use std::time::{Duration, Instant}; +use super::rss_tool_carrier::{json_to_vm_value, tool_result, vm_value_to_json}; use rustscript_agent::CancellationReason; use rustscript_agent::capabilities::{ ApprovalGate, ArtifactCapability, ArtifactLimits, CancellationFlag, CapabilityError, @@ -22,61 +23,8 @@ use rustscript_agent::config::FileToolConfig; use rustscript_agent::{ AgentConfig, AgentHostBridges, AgentRunner, ControlCheckHook, RunCancellation, ToolResult, }; -use rustscript_vm::Value as VmValue; use serde_json::{Value, json}; -fn json_to_vm_value(value: &Value) -> VmValue { - match value { - Value::Null => VmValue::Null, - Value::Bool(value) => VmValue::Bool(*value), - Value::Number(value) => { - if let Some(value) = value.as_i64() { - VmValue::Int(value) - } else { - VmValue::Float(value.as_f64().expect("finite json number")) - } - } - Value::String(value) => VmValue::string(value), - Value::Array(values) => VmValue::Array(std::sync::Arc::new( - values.iter().map(json_to_vm_value).collect::>(), - )), - Value::Object(entries) => VmValue::map( - entries - .iter() - .map(|(key, value)| (VmValue::string(key), json_to_vm_value(value))) - .collect(), - ), - } -} - -fn vm_value_to_json(value: &VmValue) -> Value { - match value { - VmValue::Null => Value::Null, - VmValue::Int(value) => json!(value), - VmValue::Float(value) => serde_json::Number::from_f64(*value) - .map(Value::Number) - .unwrap_or(Value::Null), - VmValue::Bool(value) => json!(value), - VmValue::String(value) => Value::String(value.to_string()), - VmValue::Bytes(value) => Value::String(String::from_utf8_lossy(value).into_owned()), - VmValue::Array(values) => Value::Array(values.iter().map(vm_value_to_json).collect()), - VmValue::Map(entries) => Value::Object( - entries - .iter() - .map(|(key, value)| (vm_map_key_to_string(key), vm_value_to_json(value))) - .collect(), - ), - VmValue::Callable(_) => Value::String("".to_string()), - } -} - -fn vm_map_key_to_string(value: &VmValue) -> String { - match value { - VmValue::String(value) => value.to_string(), - other => vm_value_to_json(other).to_string(), - } -} - const REGISTRY_IDENTITY: &str = "rss-mutating-file-tool-equivalence"; static NEXT_FIXTURE: AtomicU64 = AtomicU64::new(0); @@ -550,7 +498,7 @@ fn run_rss_exec(fixture: &Fixture, config: &FileToolConfig, exec: RssExec) -> Rs .run_with_context(json_to_vm_value(&context)) .unwrap_or_else(|error| panic!("rss {} run failed: {error}", exec.module)); RssRun { - result: unwrap_committed(vm_value_to_json(&output)), + result: tool_result(&output), started: exec.durable.started_len(), artifacts, durable: Arc::clone(&exec.durable), @@ -625,14 +573,6 @@ fn mutation_exec( } } -fn unwrap_committed(value: Value) -> Value { - if value.get("kind").and_then(Value::as_str) == Some("committed") { - value.get("result").cloned().unwrap_or(value) - } else { - value - } -} - fn assert_canonical_envelope(rss: &Value) { let _parsed: ToolResult = serde_json::from_value(rss.clone()).expect("canonical tool result schema"); @@ -1867,7 +1807,7 @@ fn write_deadline_before_prepare_has_no_started_record() { .with_host(host) .run_with_context(json_to_vm_value(&context)) .expect("run"); - let result = unwrap_committed(vm_value_to_json(&output)); + let result = tool_result(&output); assert_eq!(result["error"]["code"], "deadline_elapsed"); assert_eq!(durable.started_len(), 0); assert_eq!( diff --git a/tests/rss_tool_architecture_tests.rs b/tests/cases/rss_tool_architecture_tests.rs similarity index 89% rename from tests/rss_tool_architecture_tests.rs rename to tests/cases/rss_tool_architecture_tests.rs index 3df3a38..5a8c4f9 100644 --- a/tests/rss_tool_architecture_tests.rs +++ b/tests/cases/rss_tool_architecture_tests.rs @@ -2,7 +2,7 @@ //! //! These tests exercise the real host catalog, RSS dispatch wiring and the //! production compile/registry path. The executable route coverage lives in -//! `tests/rss_tool_dispatch_tests.rs`; the source-shape assertions that +//! `tests/cases/rss_tool_dispatch_tests.rs`; the source-shape assertions that //! duplicated it were development scaffolding and are not retained. use std::fs; @@ -16,6 +16,10 @@ use rustscript_vm::{ struct AgentParserDialect; impl ParserDialect for AgentParserDialect { + fn is_import_keyword(&self, ident: &str) -> bool { + ident == "import" + } + fn allow_let_mut_binding(&self) -> bool { true } @@ -31,6 +35,22 @@ impl ParserDialect for AgentParserDialect { fn allow_for_in_loop(&self) -> bool { true } + + fn allow_result_syntax(&self) -> bool { + true + } + + fn allow_option_syntax(&self) -> bool { + true + } + + fn allow_result_try(&self) -> bool { + true + } + + fn reject_legacy_null(&self) -> bool { + true + } } fn crate_root() -> PathBuf { @@ -82,7 +102,7 @@ fn production_agent_calls_rss_tools_dispatch() { source_id: 0, allow_implicit_externs: true, allow_implicit_semicolons: false, - enforce_mutable_bindings: true, + enforce_mutable_bindings: false, import_scan_mode: true, }, ) diff --git a/tests/rss_tool_dispatch_tests.rs b/tests/cases/rss_tool_dispatch_tests.rs similarity index 93% rename from tests/rss_tool_dispatch_tests.rs rename to tests/cases/rss_tool_dispatch_tests.rs index e296470..12fa8c0 100644 --- a/tests/rss_tool_dispatch_tests.rs +++ b/tests/cases/rss_tool_dispatch_tests.rs @@ -18,6 +18,8 @@ use rustscript_agent::{AgentConfig, AgentHostBridges, AgentRunner, RunCancellati use rustscript_vm::Value as VmValue; use serde_json::{Value, json}; +use super::rss_tool_carrier::{json_to_vm_value, vm_value_to_json}; + const REGISTRY_IDENTITY: &str = "sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; @@ -27,58 +29,6 @@ fn crate_root() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) } -fn json_to_vm_value(value: &Value) -> VmValue { - match value { - Value::Null => VmValue::Null, - Value::Bool(value) => VmValue::Bool(*value), - Value::Number(value) => { - if let Some(value) = value.as_i64() { - VmValue::Int(value) - } else { - VmValue::Float(value.as_f64().expect("finite json number")) - } - } - Value::String(value) => VmValue::string(value), - Value::Array(values) => VmValue::Array(std::sync::Arc::new( - values.iter().map(json_to_vm_value).collect::>(), - )), - Value::Object(entries) => VmValue::map( - entries - .iter() - .map(|(key, value)| (VmValue::string(key), json_to_vm_value(value))) - .collect(), - ), - } -} - -fn vm_value_to_json(value: &VmValue) -> Value { - match value { - VmValue::Null => Value::Null, - VmValue::Int(value) => json!(value), - VmValue::Float(value) => serde_json::Number::from_f64(*value) - .map(Value::Number) - .unwrap_or(Value::Null), - VmValue::Bool(value) => json!(value), - VmValue::String(value) => Value::String(value.to_string()), - VmValue::Bytes(value) => Value::String(String::from_utf8_lossy(value).into_owned()), - VmValue::Array(values) => Value::Array(values.iter().map(vm_value_to_json).collect()), - VmValue::Map(entries) => Value::Object( - entries - .iter() - .map(|(key, value)| (vm_map_key_to_string(key), vm_value_to_json(value))) - .collect(), - ), - VmValue::Callable(_) => Value::String("".to_string()), - } -} - -fn vm_map_key_to_string(value: &VmValue) -> String { - match value { - VmValue::String(value) => value.to_string(), - other => vm_value_to_json(other).to_string(), - } -} - fn unique_temp_parent(label: &str) -> PathBuf { let sequence = NEXT_FIXTURE.fetch_add(1, Ordering::Relaxed); std::env::temp_dir().join(format!( @@ -218,7 +168,7 @@ fn build_lifecycle( max_output_bytes: 1024 * 1024, max_summary_bytes: 256, }) - .deadline_ms(u64::MAX) + .deadline_ms(SystemClock.now_ms() + 60_000) .clock(Arc::new(SystemClock) as Arc) .tokens(Arc::new(UuidIssuer) as Arc) .durable(durable) @@ -356,9 +306,10 @@ impl rustscript_agent::RunEventSink for DiscardSink { /// agent entry does: the named `approval_context` export of the bundled /// dispatch module with the ordinary dispatch input. fn run_approval_context(input: &Value) -> Value { + let runner = compile_approval_context(); let cancellation = RunCancellation::with_timeout(std::time::Duration::from_secs(30)); let mut sink = DiscardSink; - let output = compile_approval_context() + let output = runner .run_named_with_context_and_events( "approval_context", json_to_vm_value(input), @@ -386,11 +337,6 @@ fn error_code(envelope: &Value) -> &str { .unwrap_or("") } -#[test] -fn dispatch_module_compiles() { - let _ = compile_dispatch(); -} - #[test] fn dispatch_routes_read_file_without_double_prepare() { let fixture = Fixture::new("read-file"); diff --git a/tests/rss_tool_registry_tests.rs b/tests/cases/rss_tool_registry_tests.rs similarity index 100% rename from tests/rss_tool_registry_tests.rs rename to tests/cases/rss_tool_registry_tests.rs diff --git a/tests/runner_tests.rs b/tests/cases/runner_tests.rs similarity index 100% rename from tests/runner_tests.rs rename to tests/cases/runner_tests.rs diff --git a/tests/token_metadata_rss_tests.rs b/tests/cases/token_metadata_rss_tests.rs similarity index 89% rename from tests/token_metadata_rss_tests.rs rename to tests/cases/token_metadata_rss_tests.rs index 05a7dcf..52d70d6 100644 --- a/tests/token_metadata_rss_tests.rs +++ b/tests/cases/token_metadata_rss_tests.rs @@ -37,10 +37,12 @@ pub fn test_cleanup(home: resource) -> bool { true } +struct MetadataPipelineView { metadata_result: TokenMetadataResult } + pub fn test_pipeline( home: resource, intent: TokenMetadataIntent -) -> map { +) -> MetadataPipelineView { let snapshot = config::load_snapshot(&home); let request: TransportPublicRequest = { policy_handle: snapshot.policy_handle, @@ -52,11 +54,11 @@ pub fn test_pipeline( }; let usage: TransportCredentialUse = { kind: "none", - access_handle: null, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null + access_handle: None, + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None }; let response: TransportResultEnvelope = oauth::transport_result(&request, &usage); let parsed_policy: resource = request.policy_handle; @@ -136,34 +138,29 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { .unwrap_or_else(|| panic!("missing RSS field {name}")) } -fn optional_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { - let Value::Map(entries) = value else { - panic!("expected RSS record, got {value:?}"); - }; - entries.iter().find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value), - _ => None, - }) +fn option_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { + match rustscript_vm::option::payload(field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value), + _ => panic!("invalid Option payload for {name}"), + } } fn assert_failure(output: &Value, expected_code: &str) { let result = field(output, "metadata_result"); assert_eq!(field(result, "ok"), &Value::Bool(false)); assert_eq!(field(result, "code"), &Value::string(expected_code)); - assert!(matches!( - optional_field(result, "value"), - None | Some(Value::Null) - )); + assert!(option_field(result, "value").is_none()); let rendered = format!("{output:?}"); assert!(!rendered.contains(ACCESS_SECRET)); assert!(!rendered.contains(REFRESH_SECRET)); } fn selected_intent() -> Value { - selected_intent_with(Value::string("/account/id")) + selected_intent_with(Some("/account/id")) } -fn selected_intent_with(account_pointer: Value) -> Value { +fn selected_intent_with(account_pointer: Option<&str>) -> Value { record(vec![ ("provider", Value::string("selected-provider")), ("source", Value::string("selected-source")), @@ -177,7 +174,12 @@ fn selected_intent_with(account_pointer: Value) -> Value { ), ("expiry_pointer", Value::string("/expires_in")), ("token_type_pointer", Value::string("/token_type")), - ("account_pointer", account_pointer), + ( + "account_pointer", + account_pointer.map_or_else(rustscript_vm::option::none_carrier, |pointer| { + rustscript_vm::option::some_carrier(Value::string(pointer)) + }), + ), ]) } @@ -367,20 +369,11 @@ fn null_account_pointer_and_absent_refresh_slot_are_preserved() { "\"Bearer\"", Some(r#"{"id":"acct_synthetic_metadata"}"#), ); - let output = run_fixture( - 200, - &body, - 1_900_000_000_123, - selected_intent_with(Value::Null), - 1, - ); + let output = run_fixture(200, &body, 1_900_000_000_123, selected_intent_with(None), 1); let result = field(&output, "metadata_result"); assert_eq!(field(result, "ok"), &Value::Bool(true)); - let metadata = field(result, "value"); - assert!(matches!( - optional_field(metadata, "account_id"), - None | Some(Value::Null) - )); + let metadata = option_field(result, "value").expect("parsed metadata"); + assert!(option_field(metadata, "account_id").is_none()); assert_eq!(field(metadata, "has_refresh_token"), &Value::Bool(false)); let rendered = format!("{output:?}"); assert!(!rendered.contains(ACCESS_SECRET)); @@ -394,11 +387,8 @@ fn missing_or_null_account_pointer_values_return_null_account_metadata() { let output = run_fixture(200, &body, 1_900_000_000_123, selected_intent(), 2); let result = field(&output, "metadata_result"); assert_eq!(field(result, "ok"), &Value::Bool(true)); - let metadata = field(result, "value"); - assert!(matches!( - optional_field(metadata, "account_id"), - None | Some(Value::Null) - )); + let metadata = option_field(result, "value").expect("parsed metadata"); + assert!(option_field(metadata, "account_id").is_none()); assert_eq!(field(metadata, "has_refresh_token"), &Value::Bool(true)); let rendered = format!("{output:?}"); assert!(!rendered.contains(ACCESS_SECRET)); @@ -416,7 +406,8 @@ fn expiry_guards_accept_exact_maximum_and_reject_overflow() { Some(r#"{"id":"acct_synthetic_metadata"}"#), ); let output = run_fixture(200, &body, 0, selected_intent(), 2); - let metadata = field(field(&output, "metadata_result"), "value"); + let metadata = + option_field(field(&output, "metadata_result"), "value").expect("parsed metadata"); assert_eq!( field(metadata, "expires_at_ms"), &Value::Int(9_223_372_036_854_775_000) @@ -430,7 +421,8 @@ fn expiry_guards_accept_exact_maximum_and_reject_overflow() { Some(r#"{"id":"acct_synthetic_metadata"}"#), ); let at_boundary = run_fixture(200, &body, 9_223_372_036_854_774_807, selected_intent(), 2); - let metadata = field(field(&at_boundary, "metadata_result"), "value"); + let metadata = + option_field(field(&at_boundary, "metadata_result"), "value").expect("parsed metadata"); assert_eq!(field(metadata, "expires_at_ms"), &Value::Int(i64::MAX)); let one_millisecond_past = @@ -454,10 +446,7 @@ fn malformed_or_duplicate_response_json_fails_closed_without_minting_slots() { field(result, "code"), &Value::string("token_transport_failed") ); - assert!(matches!( - optional_field(result, "value"), - None | Some(Value::Null) - )); + assert!(option_field(result, "value").is_none()); let rendered = format!("{output:?}"); assert!(!rendered.contains(ACCESS_SECRET)); assert!(!rendered.contains(REFRESH_SECRET)); @@ -473,7 +462,7 @@ fn actual_sanitized_oauth_response_uses_native_clock_and_preserves_caller_slots( let result = field(&output, "metadata_result"); assert_eq!(field(result, "ok"), &Value::Bool(true)); assert_eq!(field(result, "code"), &Value::string("ok")); - let metadata = field(result, "value"); + let metadata = option_field(result, "value").expect("parsed metadata"); assert_eq!( field(metadata, "provider"), &Value::string("selected-provider") @@ -493,15 +482,12 @@ fn actual_sanitized_oauth_response_uses_native_clock_and_preserves_caller_slots( ]) ); assert_eq!( - field(metadata, "account_id"), + option_field(metadata, "account_id").expect("selected account"), &Value::string("acct_synthetic_metadata") ); assert_eq!(field(metadata, "generation"), &Value::Int(37)); assert_eq!(field(metadata, "status"), &Value::string("active")); - assert!(matches!( - optional_field(metadata, "last_refresh_at_ms"), - None | Some(Value::Null) - )); + assert!(option_field(metadata, "last_refresh_at_ms").is_none()); assert_eq!(field(metadata, "has_refresh_token"), &Value::Bool(true)); let rendered = format!("{output:?}"); assert!(!rendered.contains(ACCESS_SECRET)); diff --git a/tests/token_save_rss_tests.rs b/tests/cases/token_save_rss_tests.rs similarity index 87% rename from tests/token_save_rss_tests.rs rename to tests/cases/token_save_rss_tests.rs index 5a23050..088005a 100644 --- a/tests/token_save_rss_tests.rs +++ b/tests/cases/token_save_rss_tests.rs @@ -35,7 +35,7 @@ pub fn snapshot(home: resource) -> ConfigSnapshotEnvelope { pub fn capture_tokens(policy: resource, expected_generation: int) -> TransportResultEnvelope { let refresh = auth::refresh_handle("fixture-codex", expected_generation, &policy).handle; - let request = { + let request: TransportPublicRequest = { policy_handle: policy, credential_id: "fixture-codex", method: "POST", @@ -43,12 +43,15 @@ pub fn capture_tokens(policy: resource, expected_generation: int) - public_headers: [{name: "content-type", value: "application/x-www-form-urlencoded"}], public_body: "grant_type=refresh_token" }; - let usage = {kind: "refresh", refresh_handle: refresh}; + let usage: TransportCredentialUse = { + kind: "refresh", access_handle: None, refresh_handle: Some(refresh), + code_handle: None, verifier_handle: None, device_handle: None + }; oauth::transport_result(&request, &usage) } pub fn capture_enrollment(policy: resource) -> TransportResultEnvelope { - let request = { + let request: TransportPublicRequest = { policy_handle: policy, credential_id: "fixture-codex", method: "POST", @@ -56,13 +59,13 @@ pub fn capture_enrollment(policy: resource) -> TransportResultEnvel public_headers: [{name: "content-type", value: "application/x-www-form-urlencoded"}], public_body: "grant_type=authorization_code&client_id=public-client-id" }; - let usage = { + let usage: TransportCredentialUse = { kind: "none", - access_handle: null, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null + access_handle: None, + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None }; oauth::transport_result(&request, &usage) } @@ -71,11 +74,11 @@ pub fn raw_save(request: SaveCredentialRequest) -> AuthSaveResultEnvelope { auth::save_if_generation_result(&request) } -pub fn close_slot(slot: resource) -> null { +pub fn close_slot(slot: resource) -> () { auth::close_slot(slot) } -pub fn cleanup(policy: resource) -> null { +pub fn cleanup(policy: resource) -> () { auth::cleanup_transients(&policy) } "#; @@ -149,18 +152,12 @@ fn field<'a>(value: &'a Value, name: &str) -> &'a Value { .unwrap_or_else(|| panic!("missing RSS field {name} in {value:?}")) } -fn optional_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { - let Value::Map(entries) = value else { - panic!("expected named RSS value, got {value:?}"); - }; - entries.iter().find_map(|(key, value)| match key { - Value::String(key) if key.as_ref() == name => Some(value), - _ => None, - }) -} - -fn is_null_or_absent(value: &Value, name: &str) -> bool { - matches!(optional_field(value, name), None | Some(Value::Null)) +fn option_field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> { + match rustscript_vm::option::payload(field(value, name)).expect("canonical RSS Option") { + (false, None) => None, + (true, Some(value)) if !matches!(value, Value::Null) => Some(value), + _ => panic!("invalid Option payload for {name}"), + } } fn record(fields: Vec<(&str, Value)>) -> Value { @@ -258,9 +255,13 @@ fn capture_slots(vm: &mut Vm, policy: &Value) -> (Value, Value) { &[policy.clone(), Value::Int(EXPECTED_GENERATION)], ); assert_eq!(field(&captured, "ok"), &Value::Bool(true), "{captured:?}"); - let response = field(&captured, "value"); - let access = field(response, "access_slot").clone(); - let refresh = field(response, "refresh_slot").clone(); + let response = option_field(&captured, "value").expect("captured response"); + let access = option_field(response, "access_slot") + .expect("access slot") + .clone(); + let refresh = option_field(response, "refresh_slot") + .expect("refresh slot") + .clone(); assert!(!matches!(access, Value::Null)); assert!(!matches!(refresh, Value::Null)); for secret in [STALE_ACCESS, STALE_REFRESH, WRITER_ACCESS, WRITER_REFRESH] { @@ -291,16 +292,21 @@ fn save_request( ("token_type", Value::string("Bearer")), ("expires_at_ms", Value::Int(expires_at_ms)), ("scopes", Value::array(vec![])), - ("account_id", Value::string("synthetic-token-save-account")), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string( + "synthetic-token-save-account", + )), + ), ("generation", Value::Int(EXPECTED_GENERATION)), ("status", Value::string(status)), - ("last_refresh_at_ms", Value::Null), + ("last_refresh_at_ms", rustscript_vm::option::none_carrier()), ("has_refresh_token", Value::Bool(true)), ]), ), ("access_slot", access), ("refresh_action", Value::string("replace")), - ("refresh_slot", refresh), + ("refresh_slot", rustscript_vm::option::some_carrier(refresh)), ]) } @@ -328,10 +334,15 @@ fn save_input_for(spec: SaveInputSpec<'_>) -> Value { ("token_type", Value::string("Bearer")), ("expires_at_ms", Value::Int(spec.expires_at_ms)), ("scopes", Value::array(vec![])), - ("account_id", Value::string("synthetic-token-save-account")), + ( + "account_id", + rustscript_vm::option::some_carrier(Value::string( + "synthetic-token-save-account", + )), + ), ("generation", Value::Int(spec.expected_generation)), ("status", Value::string(spec.status)), - ("last_refresh_at_ms", Value::Null), + ("last_refresh_at_ms", rustscript_vm::option::none_carrier()), ("has_refresh_token", Value::Bool(spec.has_refresh_token)), ]), ), @@ -353,11 +364,14 @@ fn save_input(provider: &str, source: &str, status: &str, expires_at_ms: i64) -> } fn saved_metadata(result: &Value) -> &Value { - field(field(result, "value"), "metadata") + field( + option_field(result, "value").expect("saved result"), + "metadata", + ) } fn token_result_metadata(result: &Value) -> &Value { - field(result, "value") + option_field(result, "value").expect("saved result") } #[test] @@ -376,11 +390,13 @@ fn native_none_credential_enrollment_saves_generation_one_without_refresh() { let captured = invoke(&mut vm, "capture_enrollment", std::slice::from_ref(&policy)); assert_eq!(field(&captured, "ok"), &Value::Bool(true), "{captured:?}"); - let response = field(&captured, "value"); - let access = field(response, "access_slot").clone(); + let response = option_field(&captured, "value").expect("captured response"); + let access = option_field(response, "access_slot") + .expect("access slot") + .clone(); let refresh = field(response, "refresh_slot").clone(); assert!(!matches!(access, Value::Null)); - assert_eq!(refresh, Value::Null); + assert!(option_field(response, "refresh_slot").is_none()); assert!(!format!("{captured:?}").contains(ENROLLMENT_ACCESS)); assert_eq!( transport.take_sent().len(), @@ -405,7 +421,10 @@ fn native_none_credential_enrollment_saves_generation_one_without_refresh() { ); assert_eq!(field(&saved, "ok"), &Value::Bool(true), "{saved:?}"); assert_eq!(field(&saved, "code"), &Value::string("ok")); - assert_eq!(field(&saved, "outcome"), &Value::string("committed")); + assert_eq!( + option_field(&saved, "outcome").expect("save outcome"), + &Value::string("committed") + ); assert_eq!( field(token_result_metadata(&saved), "generation"), &Value::Int(1) @@ -451,11 +470,20 @@ fn native_refresh_commit_replaces_tokens_and_returns_persisted_metadata() { let result = invoke( &mut vm, "save", - &[home_handle, policy, input, access, refresh], + &[ + home_handle, + policy, + input, + access, + rustscript_vm::option::some_carrier(refresh), + ], ); assert_eq!(field(&result, "ok"), &Value::Bool(true), "{result:?}"); assert_eq!(field(&result, "code"), &Value::string("ok")); - assert_eq!(field(&result, "outcome"), &Value::string("committed")); + assert_eq!( + option_field(&result, "outcome").expect("save outcome"), + &Value::string("committed") + ); let metadata = token_result_metadata(&result); assert_eq!(field(metadata, "generation"), &Value::Int(5)); assert_eq!( @@ -503,15 +531,21 @@ fn invalid_metadata_guard_preserves_native_store_and_enclosing_cleanup_retires_s let result = invoke( &mut vm, "save", - &[home_handle, policy.clone(), input, access, refresh], + &[ + home_handle, + policy.clone(), + input, + access, + rustscript_vm::option::some_carrier(refresh), + ], ); assert_eq!(field(&result, "ok"), &Value::Bool(false), "{result:?}"); assert_eq!( field(&result, "code"), &Value::string("invalid_save_metadata") ); - assert!(is_null_or_absent(&result, "outcome")); - assert!(is_null_or_absent(&result, "value")); + assert!(option_field(&result, "outcome").is_none()); + assert!(option_field(&result, "value").is_none()); assert_eq!(fs::read(home.0.join("auth.yaml")).unwrap(), before); assert_eq!( vm.host_context().resource_count(), @@ -557,12 +591,18 @@ fn native_save_rejection_is_redacted_and_does_not_change_store() { let result = invoke( &mut vm, "save", - &[home_handle, policy.clone(), input, access, refresh], + &[ + home_handle, + policy.clone(), + input, + access, + rustscript_vm::option::some_carrier(refresh), + ], ); assert_eq!(field(&result, "ok"), &Value::Bool(false), "{result:?}"); assert_ne!(field(&result, "code"), &Value::string("ok")); - assert!(is_null_or_absent(&result, "outcome")); - assert!(is_null_or_absent(&result, "value")); + assert!(option_field(&result, "outcome").is_none()); + assert!(option_field(&result, "value").is_none()); assert_eq!(fs::read(home.0.join("auth.yaml")).unwrap(), before); assert_eq!( vm.host_context().resource_count(), @@ -617,13 +657,13 @@ fn native_secret_slot_kind_provenance_rejection_is_redacted_and_cleanup_retires_ policy.clone(), input, refresh_candidate, - Value::Null, + rustscript_vm::option::none_carrier(), ], ); assert_eq!(field(&result, "ok"), &Value::Bool(false), "{result:?}"); assert_eq!(field(&result, "code"), &Value::string("save_rejected")); - assert!(is_null_or_absent(&result, "outcome")); - assert!(is_null_or_absent(&result, "value")); + assert!(option_field(&result, "outcome").is_none()); + assert!(option_field(&result, "value").is_none()); for secret in [STALE_ACCESS, STALE_REFRESH] { assert!(!format!("{result:?}").contains(secret)); } @@ -669,12 +709,18 @@ fn cancelled_native_cas_fails_closed_without_persisted_mutation() { let result = invoke( &mut vm, "save", - &[home_handle, policy.clone(), input, access, refresh], + &[ + home_handle, + policy.clone(), + input, + access, + rustscript_vm::option::some_carrier(refresh), + ], ); assert_eq!(field(&result, "ok"), &Value::Bool(false), "{result:?}"); assert_ne!(field(&result, "code"), &Value::string("ok")); - assert!(is_null_or_absent(&result, "outcome")); - assert!(is_null_or_absent(&result, "value")); + assert!(option_field(&result, "outcome").is_none()); + assert!(option_field(&result, "value").is_none()); assert_eq!(fs::read(home.0.join("auth.yaml")).unwrap(), before); let _ = invoke(&mut vm, "cleanup", &[policy]); @@ -851,7 +897,10 @@ fn prepare_two_generations( "{writer_result:?}" ); assert_eq!( - field(field(&writer_result, "value"), "outcome"), + field( + option_field(&writer_result, "value").expect("saved result"), + "outcome" + ), &Value::string("committed") ); assert_eq!( @@ -878,20 +927,35 @@ fn prepare_two_generations( stale_policy, stale_input, stale_access, - stale_refresh, + rustscript_vm::option::some_carrier(stale_refresh), ], ); if expect_success { assert_eq!(field(&result, "ok"), &Value::Bool(true), "{result:?}"); assert_eq!(field(&result, "code"), &Value::string("ok")); - assert_eq!(field(&result, "outcome"), &Value::string("adopted")); - assert_eq!(field(field(&result, "value"), "generation"), &Value::Int(5)); assert_eq!( - field(field(&result, "value"), "source"), + option_field(&result, "outcome").expect("save outcome"), + &Value::string("adopted") + ); + assert_eq!( + field( + option_field(&result, "value").expect("saved result"), + "generation" + ), + &Value::Int(5) + ); + assert_eq!( + field( + option_field(&result, "value").expect("saved result"), + "source" + ), &Value::string("writer-newer-source") ); assert_eq!( - field(field(&result, "value"), "status"), + field( + option_field(&result, "value").expect("saved result"), + "status" + ), &Value::string("active") ); } else { @@ -900,8 +964,8 @@ fn prepare_two_generations( field(&result, "code"), &Value::string("invalid_saved_metadata") ); - assert!(is_null_or_absent(&result, "outcome")); - assert!(is_null_or_absent(&result, "value")); + assert!(option_field(&result, "outcome").is_none()); + assert!(option_field(&result, "value").is_none()); } assert_eq!(stale_vm.host_context().resource_count(), 2); diff --git a/tests/workspace_rss_entry_tests.rs b/tests/cases/workspace_rss_entry_tests.rs similarity index 99% rename from tests/workspace_rss_entry_tests.rs rename to tests/cases/workspace_rss_entry_tests.rs index a1780b6..ad90e2d 100644 --- a/tests/workspace_rss_entry_tests.rs +++ b/tests/cases/workspace_rss_entry_tests.rs @@ -117,7 +117,7 @@ fn load_snapshot(runner: &AgentRunner, db_name: &str, request_id: &str) -> JsonV db_name, request_id, "load.all", - json!({"load_cap": 4096}), + json!({"max_rows": 512, "max_bytes": 2 * 1024 * 1024, "load_cap": 4096}), ); assert_eq!(result["ok"], true, "RSS storage load failed: {result}"); result["data"].clone() diff --git a/tests/codex_device_contract_tests.rs b/tests/codex_device_contract_tests.rs new file mode 100644 index 0000000..d5c0edf --- /dev/null +++ b/tests/codex_device_contract_tests.rs @@ -0,0 +1,4 @@ +#[path = "cases/codex_device_login_tests.rs"] +mod codex_device_login_tests; +#[path = "cases/codex_device_rss_entry_tests.rs"] +mod codex_device_rss_entry_tests; diff --git a/tests/coding_agent_edge_e2e_tests.rs b/tests/coding_agent_edge_e2e_tests.rs index 01673a2..794e4cd 100644 --- a/tests/coding_agent_edge_e2e_tests.rs +++ b/tests/coding_agent_edge_e2e_tests.rs @@ -1937,25 +1937,6 @@ async fn unsafe_pending_provider_request_fails_closed_without_tool() { fixture.cleanup(); } -#[test] -fn docs_name_both_coding_e2e_commands() { - let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let commands = [ - "cargo test --test coding_agent_e2e_tests", - "cargo test --test coding_agent_edge_e2e_tests", - ]; - for relative in ["README.md", "docs/configuration.md"] { - let text = fs::read_to_string(root.join(relative)).expect(relative); - for command in commands { - assert!(text.contains(command), "{relative} must document {command}"); - } - assert!( - !text.contains("does not cover stop-during-output"), - "{relative} must not claim stop-during-output is uncovered" - ); - } -} - const TOOL_EFFECT_NOTE: &str = "scope-note.txt"; const TOOL_EFFECT_BYTES: &str = "tool-effect-scope-native-bytes"; const TOOL_EFFECT_ADMIT_DEADLINE: Duration = Duration::from_secs(18); diff --git a/tests/dependency_pin_tests.rs b/tests/dependency_pin_tests.rs deleted file mode 100644 index 30d067b..0000000 --- a/tests/dependency_pin_tests.rs +++ /dev/null @@ -1,211 +0,0 @@ -//! Dependency provenance guard for the RustScript VM. -//! -//! This test deliberately uses only `std` so it can also be compiled directly -//! with `rustc --test` when a broken local path dependency prevents Cargo from -//! resolving the workspace. `env!("CARGO_MANIFEST_DIR")` reads the compiler -//! process environment; rustc 1.99 has no `--env` flag: -//! -//! ```bash -//! CARGO_MANIFEST_DIR=$PWD rustc --test \ -//! tests/dependency_pin_tests.rs -o /mnt/TEMP/rustscript/dependency-pin-tests/pin-direct -//! ``` - -use std::path::PathBuf; - -const RUSTSCRIPT_GIT: &str = "https://github.com/rustscript-lang/rustscript.git"; -const RUSTSCRIPT_REV: &str = "b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390"; -const STALE_REV: &str = "b1d6cffede77f49410bf63525f30b9a46b02dc01"; -const PRIOR_SDK_REV: &str = "d9387f98df78dd88d2d1c04eebac4415f844efaf"; -const ABBREVIATED_REV: &str = "b5066dd"; -const PRIOR_ABBREVIATED_REV: &str = "d9387f9"; - -fn manifest() -> String { - std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml")) - .expect("read Cargo.toml") -} - -fn lockfile() -> String { - std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.lock")) - .expect("read Cargo.lock") -} - -fn rustscript_vm_dependency(manifest: &str) -> &str { - manifest - .lines() - .find(|line| line.trim_start().starts_with("rustscript-vm = {")) - .expect("Cargo.toml must declare rustscript-vm") -} - -fn quoted_rev<'a>(dependency: &'a str, key: &str) -> Option<&'a str> { - let needle = format!("{key} = \""); - let start = dependency.find(&needle)? + needle.len(); - let end = start + dependency[start..].find('"')?; - Some(&dependency[start..end]) -} - -#[test] -fn pd_vm_uses_the_reviewed_immutable_git_revision() { - let manifest = manifest(); - let dependency = rustscript_vm_dependency(&manifest); - - assert!( - dependency.contains(&format!("git = \"{RUSTSCRIPT_GIT}\"")), - "rustscript-vm must use the canonical HTTPS Git remote: {dependency}" - ); - assert!( - !dependency.contains(PRIOR_SDK_REV), - "rustscript-vm must not remain on the prior SDK SHA {PRIOR_SDK_REV}: {dependency}" - ); - assert!( - !dependency.contains(STALE_REV), - "rustscript-vm must not mention the stale pin {STALE_REV}: {dependency}" - ); - assert!( - dependency.contains(&format!("rev = \"{RUSTSCRIPT_REV}\"")), - "rustscript-vm must pin the reviewed full commit: {dependency}" - ); - assert!( - !dependency.contains("path ="), - "rustscript-vm must not depend on sibling checkout state: {dependency}" - ); -} - -#[test] -fn pd_vm_pin_is_the_exact_full_sha_and_rejects_stale_or_abbreviated_pins() { - let manifest = manifest(); - let dependency = rustscript_vm_dependency(&manifest); - let rev = quoted_rev(dependency, "rev").expect("rustscript-vm must declare rev"); - - assert_eq!( - rev.len(), - 40, - "rustscript-vm rev must be the full 40-character SHA, not an abbreviation: {rev}" - ); - assert!( - rev.chars().all(|ch| ch.is_ascii_hexdigit()), - "rustscript-vm rev must be hexadecimal: {rev}" - ); - assert_ne!( - rev, PRIOR_SDK_REV, - "rustscript-vm must not remain on the prior SDK SHA {PRIOR_SDK_REV}" - ); - assert_ne!( - rev, STALE_REV, - "rustscript-vm must not remain on the stale SHA {STALE_REV}" - ); - assert_ne!( - rev, ABBREVIATED_REV, - "rustscript-vm must not pin the abbreviated SHA {ABBREVIATED_REV}" - ); - assert_eq!( - rev, RUSTSCRIPT_REV, - "rustscript-vm must pin the frozen full SHA {RUSTSCRIPT_REV}, got {rev}" - ); - assert!( - !dependency.contains(&format!("rev = \"{ABBREVIATED_REV}\"")), - "rustscript-vm must not use an abbreviated rev literal: {dependency}" - ); - assert!( - !dependency.contains(&format!("rev = \"{PRIOR_ABBREVIATED_REV}\"")), - "rustscript-vm must not use the prior SDK abbreviated rev: {dependency}" - ); - assert!( - !dependency.contains(PRIOR_SDK_REV), - "rustscript-vm must not mention the prior SDK pin {PRIOR_SDK_REV}: {dependency}" - ); - assert!( - !dependency.contains(STALE_REV), - "rustscript-vm must not mention the stale pin {STALE_REV}: {dependency}" - ); -} - -#[test] -fn pd_host_function_uses_the_same_frozen_full_sha() { - let manifest = manifest(); - let dependency = manifest - .lines() - .find(|line| line.trim_start().starts_with("pd-host-function = {")) - .expect("Cargo.toml must declare pd-host-function for descriptor macros"); - - assert!( - dependency.contains(&format!("git = \"{RUSTSCRIPT_GIT}\"")), - "pd-host-function must use the canonical HTTPS Git remote: {dependency}" - ); - let rev = quoted_rev(dependency, "rev").expect("pd-host-function must declare rev"); - assert_ne!( - rev, PRIOR_SDK_REV, - "pd-host-function must not remain on the prior SDK SHA {PRIOR_SDK_REV}" - ); - assert_ne!( - rev, STALE_REV, - "pd-host-function must not remain on the stale SHA {STALE_REV}" - ); - assert_eq!( - rev, RUSTSCRIPT_REV, - "pd-host-function must pin the same frozen full SHA as rustscript-vm: {dependency}" - ); - assert_eq!(rev.len(), 40, "pd-host-function rev must be the full SHA"); - assert!( - !dependency.contains("path ="), - "pd-host-function must not depend on sibling checkout state: {dependency}" - ); -} - -#[test] -fn pd_vm_and_pd_host_function_lock_sources_are_canonical_https_at_the_pinned_rev() { - let lockfile = lockfile(); - - // The canonical source line Cargo writes for a git dependency pinned to a - // full revision. Asserting the exact string simultaneously guards: the - // canonical HTTPS remote (no `path`/`file` source), the full 40-character - // revision, and the `#` checkout suffix. - let canonical = format!("git+{RUSTSCRIPT_GIT}?rev={RUSTSCRIPT_REV}#{RUSTSCRIPT_REV}"); - let stale = format!("git+{RUSTSCRIPT_GIT}?rev={STALE_REV}#{STALE_REV}"); - let prior = format!("git+{RUSTSCRIPT_GIT}?rev={PRIOR_SDK_REV}#{PRIOR_SDK_REV}"); - // `b5066dd` is a prefix of the reviewed full SHA, so a bare substring - // `rev=b5066dd` also matches the canonical pin. An abbreviated pin is - // terminated by a quote or a fragment marker. The prior SDK abbreviation - // is rejected the same way; it is not a prefix of the reviewed SHA. The - // stale full SHA `b1d6cffede77...` is rejected separately and is not that prefix. - let abbreviated_quoted = format!("rev={ABBREVIATED_REV}\""); - let abbreviated_fragment = format!("rev={ABBREVIATED_REV}#"); - let prior_abbreviated_quoted = format!("rev={PRIOR_ABBREVIATED_REV}\""); - let prior_abbreviated_fragment = format!("rev={PRIOR_ABBREVIATED_REV}#"); - - for package in ["pd-vm", "pd-host-schema", "pd-host-function"] { - let block = lockfile - .split("\n[[package]]") - .find(|block| block.contains(&format!("\nname = \"{package}\"\n"))) - .unwrap_or_else(|| panic!("Cargo.lock must declare {package}")); - let source = block - .lines() - .find(|line| line.starts_with("source = ")) - .unwrap_or_else(|| panic!("Cargo.lock {package} must declare a source")); - assert!( - !block.contains(&prior), - "Cargo.lock {package} must not resolve the prior SDK SHA {PRIOR_SDK_REV}" - ); - assert!( - !block.contains(PRIOR_SDK_REV), - "Cargo.lock {package} must not mention the prior SDK SHA {PRIOR_SDK_REV}" - ); - assert!( - !block.contains(&stale), - "Cargo.lock {package} must not resolve the stale SHA {STALE_REV}" - ); - assert!( - !source.contains(&abbreviated_quoted) && !source.contains(&abbreviated_fragment), - "Cargo.lock {package} must not resolve an abbreviated SHA: {source}" - ); - assert!( - !source.contains(&prior_abbreviated_quoted) - && !source.contains(&prior_abbreviated_fragment), - "Cargo.lock {package} must not resolve the prior SDK abbreviated SHA: {source}" - ); - assert_eq!( - source.trim(), - format!("source = \"{canonical}\""), - "Cargo.lock {package} must use the canonical HTTPS source at the pinned full rev" - ); - } -} diff --git a/tests/docs_contract_tests.rs b/tests/docs_contract_tests.rs new file mode 100644 index 0000000..634cbda --- /dev/null +++ b/tests/docs_contract_tests.rs @@ -0,0 +1,10 @@ +#[path = "cases/commit_step_timestamp_contract.rs"] +mod commit_step_timestamp_contract; +#[path = "cases/contract_fixtures.rs"] +mod contract_fixtures; +#[path = "cases/docs_consistency_tests.rs"] +mod docs_consistency_tests; +#[path = "cases/documentation_consistency_tests.rs"] +mod documentation_consistency_tests; +#[path = "cases/domain_contract_tests.rs"] +mod domain_contract_tests; diff --git a/tests/effect_contract_tests.rs b/tests/effect_contract_tests.rs new file mode 100644 index 0000000..ffa92aa --- /dev/null +++ b/tests/effect_contract_tests.rs @@ -0,0 +1,11 @@ +#[path = "cases/effect_admin_api_tests.rs"] +mod effect_admin_api_tests; + +#[path = "cases/effect_checkpoint_tests.rs"] +mod effect_checkpoint_tests; + +#[path = "cases/managed_checkpoint_api_tests.rs"] +mod managed_checkpoint_api_tests; + +#[path = "cases/managed_checkpoint_restore_tests.rs"] +mod managed_checkpoint_restore_tests; diff --git a/tests/fixtures/access_result/main.rss b/tests/fixtures/access_result/main.rss index 8671164..a934aa5 100644 --- a/tests/fixtures/access_result/main.rss +++ b/tests/fixtures/access_result/main.rss @@ -30,15 +30,15 @@ pub fn send_access( }; let usage: TransportCredentialUse = { kind: "access", - access_handle: access, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null + access_handle: Some(access), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None }; oauth::transport_result(&request, &usage) } -pub fn close_access(access: resource) -> null { +pub fn close_access(access: resource) -> () { auth::close_access(access) } diff --git a/tests/fixtures/auth_sanitized_stream/main.rss b/tests/fixtures/auth_sanitized_stream/main.rss index b55335c..a11c8a8 100644 --- a/tests/fixtures/auth_sanitized_stream/main.rss +++ b/tests/fixtures/auth_sanitized_stream/main.rss @@ -24,7 +24,7 @@ struct StreamOpenView { code: string, status: int, content_type: string, - stream: resource? + stream: Option> } struct StreamRetryView { @@ -35,26 +35,10 @@ struct StreamRetryView { retry_status: int } -struct AccessValuePresence { - value: AccessHandleEnvelope? -} - -struct RefreshValuePresence { - value: RefreshHandleEnvelope? -} - -struct StreamValuePresence { - value: OAuthStreamEnvelope? -} - -struct RecordValuePresence { - value: OAuthStreamRecord? -} - struct StreamLimitView { ok: bool, code: string, - handle: resource? + handle: Option> } struct RecordParts { @@ -65,29 +49,27 @@ struct RecordParts { literal_index: int } -fn failure_code(failure_value: AuthBridgeFailure?) -> string { - if failure_value != null => { - let failure: AuthBridgeFailure = failure_value; - failure.code.copy() - } else => { - "" +fn failure_code(failure_value: Option) -> string { + match failure_value { + Some(failure) => failure.code, + None => "" } } fn read_parts(envelope: OAuthStreamReadResultEnvelope) -> RecordParts { let ok: bool = envelope.ok; let code: string = failure_code(envelope.error); - let holder: RecordValuePresence = {value: envelope.value}; - let has_value: bool = holder.has("value"); - if !ok || code != "" || !has_value => { - { ok: false, code: code, kind: "", json: "", literal_index: -1 } - } else => { - let record: OAuthStreamRecord = holder.value; - let kind: string = record.kind.copy(); - let json: string = record.json.copy(); - let literal_index_opt: int? = record.literal_index; - let literal_index: int = literal_index_opt.unwrap_or(-1); - { ok: true, code: "", kind: kind, json: json, literal_index: literal_index } + match envelope.value { + None => { ok: false, code: code, kind: "", json: "", literal_index: -1 }, + Some(record) => if !ok || code != "" => { + { ok: false, code: code, kind: "", json: "", literal_index: -1 } + } else => { + let kind: string = record.kind.copy(); + let json: string = record.json.copy(); + let literal_index_opt: Option = record.literal_index; + let literal_index: int = match literal_index_opt { Some(index) => index, None => -1 }; + { ok: true, code: "", kind: kind, json: json, literal_index: literal_index } + } } } @@ -129,50 +111,50 @@ pub fn stream_first_json_result( let issued: AccessHandleResultEnvelope = auth::access_handle_result(credential_id, generation, &policy); let issued_ok: bool = issued.ok; - let issued_failed: bool = issued.error != null; - let holder: AccessValuePresence = {value: issued.value}; - let has_value: bool = holder.has("value"); - if !issued_ok || issued_failed || !has_value => { - { ok: false, code: "access_unavailable", status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } - } else => { - let inner: AccessHandleEnvelope = holder.value; - let handle: resource = inner.handle; - let request: TransportPublicRequest = get_request(policy, credential_id, path); - let usage: TransportCredentialUse = { - kind: "access", - access_handle: handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let started: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &usage, markers); - let start_ok: bool = started.ok; - let start_code: string = failure_code(started.error); - let stream_holder: StreamValuePresence = {value: started.value}; - let has_stream: bool = stream_holder.has("value"); - if !start_ok || start_code != "" || !has_stream => { - { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } + let issued_failed: bool = match issued.error { Some(_) => true, None => false }; + match issued.value { + None => { ok: false, code: "access_unavailable", status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false }, + Some(inner) => if !issued_ok || issued_failed => { + { ok: false, code: "access_unavailable", status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } } else => { - let envelope: OAuthStreamEnvelope = stream_holder.value; - let stream: resource = envelope.stream; - let head: OAuthStreamHead = envelope.head; - let status: int = head.status; - let content_type: string = head.content_type.copy(); - let parts: RecordParts = read_parts(oauth::stream_read_result(&stream)); - oauth::stream_close(stream); - { - ok: true, - code: "", - status: status, - content_type: content_type, - read_ok: parts.ok, - read_code: parts.code, - kind: parts.kind, - json: parts.json, - literal_index: parts.literal_index, - close_ok: true + let handle: resource = inner.handle; + let request: TransportPublicRequest = get_request(policy, credential_id, path); + let usage: TransportCredentialUse = { + kind: "access", + access_handle: Some(handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let started: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &usage, markers); + let start_ok: bool = started.ok; + let start_code: string = failure_code(started.error); + match started.value { + None => { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false }, + Some(envelope) => if !start_ok || start_code != "" => { + { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } + } else => { + let stream: resource = envelope.stream; + let head: OAuthStreamHead = envelope.head; + let status: int = head.status; + let content_type: string = head.content_type.copy(); + let parts: RecordParts = read_parts(oauth::stream_read_result(&stream)); + oauth::stream_close(stream); + { + ok: true, + code: "", + status: status, + content_type: content_type, + read_ok: parts.ok, + read_code: parts.code, + kind: parts.kind, + json: parts.json, + literal_index: parts.literal_index, + close_ok: true + } + } } } } @@ -192,50 +174,50 @@ pub fn stream_post_first_json_result( let issued: AccessHandleResultEnvelope = auth::access_handle_result(credential_id, generation, &policy); let issued_ok: bool = issued.ok; - let issued_failed: bool = issued.error != null; - let holder: AccessValuePresence = {value: issued.value}; - let has_value: bool = holder.has("value"); - if !issued_ok || issued_failed || !has_value => { - { ok: false, code: "access_unavailable", status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } - } else => { - let inner: AccessHandleEnvelope = holder.value; - let handle: resource = inner.handle; - let request: TransportPublicRequest = post_request(policy, credential_id, path, body); - let usage: TransportCredentialUse = { - kind: "access", - access_handle: handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let started: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &usage, markers); - let start_ok: bool = started.ok; - let start_code: string = failure_code(started.error); - let stream_holder: StreamValuePresence = {value: started.value}; - let has_stream: bool = stream_holder.has("value"); - if !start_ok || start_code != "" || !has_stream => { - { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } + let issued_failed: bool = match issued.error { Some(_) => true, None => false }; + match issued.value { + None => { ok: false, code: "access_unavailable", status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false }, + Some(inner) => if !issued_ok || issued_failed => { + { ok: false, code: "access_unavailable", status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } } else => { - let envelope: OAuthStreamEnvelope = stream_holder.value; - let stream: resource = envelope.stream; - let head: OAuthStreamHead = envelope.head; - let status: int = head.status; - let content_type: string = head.content_type.copy(); - let parts: RecordParts = read_parts(oauth::stream_read_result(&stream)); - oauth::stream_close(stream); - { - ok: true, - code: "", - status: status, - content_type: content_type, - read_ok: parts.ok, - read_code: parts.code, - kind: parts.kind, - json: parts.json, - literal_index: parts.literal_index, - close_ok: true + let handle: resource = inner.handle; + let request: TransportPublicRequest = post_request(policy, credential_id, path, body); + let usage: TransportCredentialUse = { + kind: "access", + access_handle: Some(handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let started: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &usage, markers); + let start_ok: bool = started.ok; + let start_code: string = failure_code(started.error); + match started.value { + None => { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false }, + Some(envelope) => if !start_ok || start_code != "" => { + { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } + } else => { + let stream: resource = envelope.stream; + let head: OAuthStreamHead = envelope.head; + let status: int = head.status; + let content_type: string = head.content_type.copy(); + let parts: RecordParts = read_parts(oauth::stream_read_result(&stream)); + oauth::stream_close(stream); + { + ok: true, + code: "", + status: status, + content_type: content_type, + read_ok: parts.ok, + read_code: parts.code, + kind: parts.kind, + json: parts.json, + literal_index: parts.literal_index, + close_ok: true + } + } } } } @@ -256,27 +238,27 @@ pub fn stream_post_body_probe_result( auth::access_handle_result(credential_id, generation, &policy); let ok: bool = issued.ok; let code: string = failure_code(issued.error); - let holder: AccessValuePresence = {value: issued.value}; - let has_value: bool = holder.has("value"); - if !ok || code != "" || !has_value => { - { ok: false, code: code, handle: null } - } else => { - let inner: AccessHandleEnvelope = holder.value; - let handle: resource = inner.handle; - let request: TransportPublicRequest = post_request(policy, credential_id, path, body); - let usage: TransportCredentialUse = { - kind: "access", - access_handle: handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let started: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &usage, markers); - let started_ok: bool = started.ok; - let started_code: string = failure_code(started.error); - { ok: started_ok, code: started_code, handle: usage.access_handle } + match issued.value { + None => { ok: false, code: code, handle: None }, + Some(inner) => if !ok || code != "" => { + { ok: false, code: code, handle: None } + } else => { + let handle: resource = inner.handle; + let request: TransportPublicRequest = post_request(policy, credential_id, path, body); + let usage: TransportCredentialUse = { + kind: "access", + access_handle: Some(handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let started: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &usage, markers); + let started_ok: bool = started.ok; + let started_code: string = failure_code(started.error); + { ok: started_ok, code: started_code, handle: usage.access_handle } + } } } @@ -292,38 +274,38 @@ pub fn stream_with_access_body_first_json_result( let request: TransportPublicRequest = post_request(policy, credential_id, path, body); let usage: TransportCredentialUse = { kind: "access", - access_handle: access, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null + access_handle: Some(access), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None }; let started: OAuthStreamStartResultEnvelope = oauth::stream_start_result(&request, &usage, markers); let start_ok: bool = started.ok; let start_code: string = failure_code(started.error); - let stream_holder: StreamValuePresence = {value: started.value}; - let has_stream: bool = stream_holder.has("value"); - if !start_ok || start_code != "" || !has_stream => { - { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } - } else => { - let envelope: OAuthStreamEnvelope = stream_holder.value; - let stream: resource = envelope.stream; - let head: OAuthStreamHead = envelope.head; - let status: int = head.status; - let content_type: string = head.content_type.copy(); - oauth::stream_close(stream); - { - ok: true, - code: "", - status: status, - content_type: content_type, - read_ok: false, - read_code: "", - kind: "", - json: "", - literal_index: -1, - close_ok: true + match started.value { + None => { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false }, + Some(envelope) => if !start_ok || start_code != "" => { + { ok: false, code: start_code, status: 0, content_type: "", read_ok: false, read_code: "", kind: "", json: "", literal_index: -1, close_ok: false } + } else => { + let stream: resource = envelope.stream; + let head: OAuthStreamHead = envelope.head; + let status: int = head.status; + let content_type: string = head.content_type.copy(); + oauth::stream_close(stream); + { + ok: true, + code: "", + status: status, + content_type: content_type, + read_ok: false, + read_code: "", + kind: "", + json: "", + literal_index: -1, + close_ok: true + } } } } @@ -340,41 +322,41 @@ pub fn stream_open_result( let issued: AccessHandleResultEnvelope = auth::access_handle_result(credential_id, generation, &policy); let issued_ok: bool = issued.ok; - let issued_failed: bool = issued.error != null; - let holder: AccessValuePresence = {value: issued.value}; - let has_value: bool = holder.has("value"); - if !issued_ok || issued_failed || !has_value => { - { ok: false, code: "access_unavailable", status: 0, content_type: "", stream: null } - } else => { - let inner: AccessHandleEnvelope = holder.value; - let handle: resource = inner.handle; - let request: TransportPublicRequest = get_request(policy, credential_id, path); - let usage: TransportCredentialUse = { - kind: "access", - access_handle: handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let started: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &usage, markers); - let start_ok: bool = started.ok; - let start_code: string = failure_code(started.error); - let stream_holder: StreamValuePresence = {value: started.value}; - let has_stream: bool = stream_holder.has("value"); - if !start_ok || start_code != "" || !has_stream => { - { ok: false, code: start_code, status: 0, content_type: "", stream: null } + let issued_failed: bool = match issued.error { Some(_) => true, None => false }; + match issued.value { + None => { ok: false, code: "access_unavailable", status: 0, content_type: "", stream: None }, + Some(inner) => if !issued_ok || issued_failed => { + { ok: false, code: "access_unavailable", status: 0, content_type: "", stream: None } } else => { - let envelope: OAuthStreamEnvelope = stream_holder.value; - let stream: resource = envelope.stream; - let head: OAuthStreamHead = envelope.head; - { - ok: true, - code: "", - status: head.status, - content_type: head.content_type.copy(), - stream: stream + let handle: resource = inner.handle; + let request: TransportPublicRequest = get_request(policy, credential_id, path); + let usage: TransportCredentialUse = { + kind: "access", + access_handle: Some(handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let started: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &usage, markers); + let start_ok: bool = started.ok; + let start_code: string = failure_code(started.error); + match started.value { + None => { ok: false, code: start_code, status: 0, content_type: "", stream: None }, + Some(envelope) => if !start_ok || start_code != "" => { + { ok: false, code: start_code, status: 0, content_type: "", stream: None } + } else => { + let stream: resource = envelope.stream; + let head: OAuthStreamHead = envelope.head; + { + ok: true, + code: "", + status: head.status, + content_type: head.content_type.copy(), + stream: Some(stream) + } + } } } } @@ -386,7 +368,7 @@ pub fn stream_read_one_result(stream: resource) -> RecordParts { } // Explicit typed close of an open stream. -pub fn stream_close(stream: resource) -> null { +pub fn stream_close(stream: resource) -> () { oauth::stream_close(stream) } @@ -403,42 +385,42 @@ pub fn stream_marker_retry_result( let issued: AccessHandleResultEnvelope = auth::access_handle_result(credential_id, generation, &policy); let issued_ok: bool = issued.ok; - let issued_failed: bool = issued.error != null; - let holder: AccessValuePresence = {value: issued.value}; - let has_value: bool = holder.has("value"); - if !issued_ok || issued_failed || !has_value => { - { reject_ok: false, reject_code: "access_unavailable", retry_ok: false, retry_code: "", retry_status: 0 } - } else => { - let inner: AccessHandleEnvelope = holder.value; - let handle: resource = inner.handle; - let request: TransportPublicRequest = get_request(policy, credential_id, path); - let usage: TransportCredentialUse = { - kind: "access", - access_handle: handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let rejected: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &usage, markers_bad); - let reject_ok: bool = rejected.ok; - let reject_code: string = failure_code(rejected.error); - let retried: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &usage, markers_good); - let retry_ok: bool = retried.ok; - let retry_code: string = failure_code(retried.error); - let stream_holder: StreamValuePresence = {value: retried.value}; - let has_stream: bool = stream_holder.has("value"); - if !retry_ok || retry_code != "" || !has_stream => { - { reject_ok: reject_ok, reject_code: reject_code, retry_ok: false, retry_code: retry_code, retry_status: 0 } + let issued_failed: bool = match issued.error { Some(_) => true, None => false }; + match issued.value { + None => { reject_ok: false, reject_code: "access_unavailable", retry_ok: false, retry_code: "", retry_status: 0 }, + Some(inner) => if !issued_ok || issued_failed => { + { reject_ok: false, reject_code: "access_unavailable", retry_ok: false, retry_code: "", retry_status: 0 } } else => { - let envelope: OAuthStreamEnvelope = stream_holder.value; - let stream: resource = envelope.stream; - let head: OAuthStreamHead = envelope.head; - let status: int = head.status; - oauth::stream_close(stream); - { reject_ok: reject_ok, reject_code: reject_code, retry_ok: true, retry_code: "", retry_status: status } + let handle: resource = inner.handle; + let request: TransportPublicRequest = get_request(policy, credential_id, path); + let usage: TransportCredentialUse = { + kind: "access", + access_handle: Some(handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let rejected: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &usage, markers_bad); + let reject_ok: bool = rejected.ok; + let reject_code: string = failure_code(rejected.error); + let retried: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &usage, markers_good); + let retry_ok: bool = retried.ok; + let retry_code: string = failure_code(retried.error); + match retried.value { + None => { reject_ok: reject_ok, reject_code: reject_code, retry_ok: false, retry_code: retry_code, retry_status: 0 }, + Some(envelope) => if !retry_ok || retry_code != "" => { + { reject_ok: reject_ok, reject_code: reject_code, retry_ok: false, retry_code: retry_code, retry_status: 0 } + } else => { + let stream: resource = envelope.stream; + let head: OAuthStreamHead = envelope.head; + let status: int = head.status; + oauth::stream_close(stream); + { reject_ok: reject_ok, reject_code: reject_code, retry_ok: true, retry_code: "", retry_status: status } + } + } } } } @@ -454,18 +436,18 @@ pub fn stream_limit_step( auth::access_handle_result(credential_id, generation, &policy); let ok: bool = issued.ok; let code: string = failure_code(issued.error); - let holder: AccessValuePresence = {value: issued.value}; - let has_value: bool = holder.has("value"); - if !ok || code != "" || !has_value => { - { ok: false, code: code, handle: null } - } else => { - let inner: AccessHandleEnvelope = holder.value; - { ok: true, code: "", handle: inner.handle } + match issued.value { + None => { ok: false, code: code, handle: None }, + Some(inner) => if !ok || code != "" => { + { ok: false, code: code, handle: None } + } else => { + { ok: true, code: "", handle: Some(inner.handle) } + } } } // Release one real issued access capability, freeing exactly one live slot. -pub fn stream_release_result(access: resource) -> null { +pub fn stream_release_result(access: resource) -> () { auth::close_access(access) } @@ -482,30 +464,35 @@ pub fn stream_publication_probe( ) -> StreamLimitView { let issued: AccessHandleResultEnvelope = auth::access_handle_result(credential_id, generation, &policy); - let holder: AccessValuePresence = {value: issued.value}; - let has_value: bool = holder.has("value"); - if !has_value => { - { ok: false, code: "access_unavailable", handle: null } - } else => { - let inner: AccessHandleEnvelope = holder.value; - let handle: resource = inner.handle; - let request: TransportPublicRequest = get_request(policy, credential_id, path); - let usage: TransportCredentialUse = { - kind: "access", - access_handle: handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let started: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &usage, markers); - let ok: bool = started.ok; - let code: string = failure_code(started.error); - { ok: ok, code: code, handle: null } + match issued.value { + None => { ok: false, code: "access_unavailable", handle: None }, + Some(inner) => probe_publication(inner.handle, policy, credential_id, path, markers) } } +fn probe_publication( + handle: resource, + policy: resource, + credential_id: string, + path: string, + markers: array +) -> StreamLimitView { + let request: TransportPublicRequest = get_request(policy, credential_id, path); + let usage: TransportCredentialUse = { + kind: "access", + access_handle: Some(handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let started: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &usage, markers); + let ok: bool = started.ok; + let code: string = failure_code(started.error); + { ok: ok, code: code, handle: None } +} + pub fn stream_refresh_usage_then_access_result( credential_id: string, generation: int, @@ -515,55 +502,55 @@ pub fn stream_refresh_usage_then_access_result( ) -> StreamRetryView { let refresh_issued: RefreshHandleResultEnvelope = auth::refresh_handle_result(credential_id, generation, &policy); - let refresh_holder: RefreshValuePresence = {value: refresh_issued.value}; - let refresh_has: bool = refresh_holder.has("value"); let access_issued: AccessHandleResultEnvelope = auth::access_handle_result(credential_id, generation, &policy); let access_ok: bool = access_issued.ok; - let access_failed: bool = access_issued.error != null; - let access_holder: AccessValuePresence = {value: access_issued.value}; - let access_has: bool = access_holder.has("value"); - if !refresh_has || !access_ok || access_failed || !access_has => { - { reject_ok: false, reject_code: "access_unavailable", retry_ok: false, retry_code: "", retry_status: 0 } - } else => { - let refresh_inner: RefreshHandleEnvelope = refresh_holder.value; - let access_inner: AccessHandleEnvelope = access_holder.value; - let request: TransportPublicRequest = get_request(policy, credential_id, path); - let reject_usage: TransportCredentialUse = { - kind: "refresh", - access_handle: null, - refresh_handle: refresh_inner.handle, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let rejected: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &reject_usage, markers); - let reject_ok: bool = rejected.ok; - let reject_code: string = failure_code(rejected.error); - let usage: TransportCredentialUse = { - kind: "access", - access_handle: access_inner.handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let retried: OAuthStreamStartResultEnvelope = - oauth::stream_start_result(&request, &usage, markers); - let retry_ok: bool = retried.ok; - let retry_code: string = failure_code(retried.error); - let stream_holder: StreamValuePresence = {value: retried.value}; - let has_stream: bool = stream_holder.has("value"); - if !retry_ok || retry_code != "" || !has_stream => { - { reject_ok: reject_ok, reject_code: reject_code, retry_ok: false, retry_code: retry_code, retry_status: 0 } - } else => { - let envelope: OAuthStreamEnvelope = stream_holder.value; - let stream: resource = envelope.stream; - let head: OAuthStreamHead = envelope.head; - let status: int = head.status; - oauth::stream_close(stream); - { reject_ok: reject_ok, reject_code: reject_code, retry_ok: true, retry_code: "", retry_status: status } + let access_failed: bool = match access_issued.error { Some(_) => true, None => false }; + match refresh_issued.value { + None => { reject_ok: false, reject_code: "access_unavailable", retry_ok: false, retry_code: "", retry_status: 0 }, + Some(refresh_inner) => match access_issued.value { + None => { reject_ok: false, reject_code: "access_unavailable", retry_ok: false, retry_code: "", retry_status: 0 }, + Some(access_inner) => if !access_ok || access_failed => { + { reject_ok: false, reject_code: "access_unavailable", retry_ok: false, retry_code: "", retry_status: 0 } + } else => { + let request: TransportPublicRequest = get_request(policy, credential_id, path); + let reject_usage: TransportCredentialUse = { + kind: "refresh", + access_handle: None, + refresh_handle: Some(refresh_inner.handle), + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let rejected: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &reject_usage, markers); + let reject_ok: bool = rejected.ok; + let reject_code: string = failure_code(rejected.error); + let usage: TransportCredentialUse = { + kind: "access", + access_handle: Some(access_inner.handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let retried: OAuthStreamStartResultEnvelope = + oauth::stream_start_result(&request, &usage, markers); + let retry_ok: bool = retried.ok; + let retry_code: string = failure_code(retried.error); + match retried.value { + None => { reject_ok: reject_ok, reject_code: reject_code, retry_ok: false, retry_code: retry_code, retry_status: 0 }, + Some(envelope) => if !retry_ok || retry_code != "" => { + { reject_ok: reject_ok, reject_code: reject_code, retry_ok: false, retry_code: retry_code, retry_status: 0 } + } else => { + let stream: resource = envelope.stream; + let head: OAuthStreamHead = envelope.head; + let status: int = head.status; + oauth::stream_close(stream); + { reject_ok: reject_ok, reject_code: reject_code, retry_ok: true, retry_code: "", retry_status: status } + } + } + } } } } diff --git a/tests/fixtures/codex_device/native_entry.rss b/tests/fixtures/codex_device/native_entry.rss index 2a67d43..df47f03 100644 --- a/tests/fixtures/codex_device/native_entry.rss +++ b/tests/fixtures/codex_device/native_entry.rss @@ -19,7 +19,7 @@ struct CodexDeviceLoginResult { ok: bool, code: string, message: string, - metadata: AuthMetadataEnvelope? + metadata: Option } use auth; diff --git a/tests/fixtures/codex_device/scripted_entry.rss b/tests/fixtures/codex_device/scripted_entry.rss index d79626e..a415346 100644 --- a/tests/fixtures/codex_device/scripted_entry.rss +++ b/tests/fixtures/codex_device/scripted_entry.rss @@ -20,7 +20,7 @@ struct CodexDeviceLoginResult { ok: bool, code: string, message: string, - metadata: AuthMetadataEnvelope? + metadata: Option } pub fn run( diff --git a/tests/fixtures/provider_bridge/main.rss b/tests/fixtures/provider_bridge/main.rss index 47c64a4..fdf1f60 100644 --- a/tests/fixtures/provider_bridge/main.rss +++ b/tests/fixtures/provider_bridge/main.rss @@ -13,20 +13,36 @@ struct OpaqueHostHomeBinding { home: resource } -pub fn run(context: map) -> map { - let binding: OpaqueHostHomeBinding = { "home": context["host_home"], }; - let mut credential_id: string = "fixture-codex"; - let mut path: string = "/oauth/token"; - let mut header_name: string = "accept"; - if context.has("credential_id") { - credential_id = context["credential_id"]; - } - if context.has("path") { - path = context["path"]; - } - if context.has("header_name") { - header_name = context["header_name"]; - } +struct BridgeContext { + host_home: resource, + credential_id: Option, + path: Option, + header_name: Option +} + +struct BridgeReport { + ok: bool, + error_code: string, + cancelled: bool, + deadline_exceeded: bool, + revoked: bool, + status: int, + body: string, + retry_after_ms: int +} + +struct ResponseView { status: int, body: string, retry_after_ms: int } + +fn project_response(response: TransportResponseEnvelope) -> ResponseView { + let retry = match response.retry_after_ms { Some(value) => value, None => 0 }; + {status: response.status, body: response.body_without_secret_fields, retry_after_ms: retry} +} + +pub fn run(context: BridgeContext) -> BridgeReport { + let binding: OpaqueHostHomeBinding = {home: context.host_home}; + let credential_id = match context.credential_id { Some(value) => value, None => "fixture-codex" }; + let path = match context.path { Some(value) => value, None => "/oauth/token" }; + let header_name = match context.header_name { Some(value) => value, None => "accept" }; let intent: OpaqueProviderIntent = { credential_id: credential_id, method: "GET", @@ -35,44 +51,22 @@ pub fn run(context: map) -> map { public_body: "" }; let envelope: TransportResultEnvelope = provider_runtime::call(binding, intent); - let has_response: bool = envelope.has("value"); - let maybe_error: AuthBridgeFailure? = envelope.error; - let error: AuthBridgeFailure = maybe_error.unwrap_or({ - code: "", - cancelled: false, - deadline_exceeded: false, - revoked: false - }); - let maybe_response: TransportResponseEnvelope? = if has_response => { - envelope.value - } else => { - null + let error: AuthBridgeFailure = match envelope.error { + Some(value) => value, + None => {code: "", cancelled: false, deadline_exceeded: false, revoked: false} }; - let response: TransportResponseEnvelope = maybe_response.unwrap_or({ - ok: false, - error: null, - status: 0, - public_headers: [], - body_without_secret_fields: "", - retry_after_ms: 0, - access_slot: null, - refresh_slot: null, - device_handle: null - }); - let mut retry_after_ms: int = 0; - if response.has("retry_after_ms") { - let maybe_retry_after_ms: int? = response.retry_after_ms; - retry_after_ms = maybe_retry_after_ms.unwrap_or(0); - } - let public_response: map = { + let response: ResponseView = match envelope.value { + Some(value) => project_response(value), + None => {status: 0, body: "", retry_after_ms: 0} + }; + { ok: envelope.ok, error_code: error.code, cancelled: error.cancelled, deadline_exceeded: error.deadline_exceeded, revoked: error.revoked, status: response.status, - body: response.body_without_secret_fields, - retry_after_ms: retry_after_ms - }; - public_response + body: response.body, + retry_after_ms: response.retry_after_ms + } } diff --git a/tests/fixtures/provider_refresh_transaction/main.rss b/tests/fixtures/provider_refresh_transaction/main.rss index 58ceda8..54ee8bb 100644 --- a/tests/fixtures/provider_refresh_transaction/main.rss +++ b/tests/fixtures/provider_refresh_transaction/main.rss @@ -19,8 +19,8 @@ struct ProviderRefreshResult { ok: bool, code: string, outcome: string, - metadata: AuthMetadata?, - failure: AuthBridgeFailure? + metadata: Option, + failure: Option } pub fn run_refresh(binding: OpaqueHostHomeBinding, intent: OpaqueRefreshIntent) -> ProviderRefreshResult { @@ -35,32 +35,34 @@ pub fn snapshot(home: resource) -> ConfigSnapshotEnvelope { // for the freshly stored generation and send one authenticated request. pub fn probe_access(credential_id: string, generation: int, policy: resource) -> TransportResultEnvelope { let issued: AccessHandleResultEnvelope = auth::access_handle_result(credential_id, generation, &policy); - let has_value: bool = issued.has("value"); - let issued_ok: bool = issued.ok && issued.error == null && has_value; - if !issued_ok => { - {ok: false, error: null, value: null} - } else => { - let inner: AccessHandleEnvelope = issued.value; - let request: TransportPublicRequest = { - policy_handle: policy, - credential_id: credential_id, - method: "POST", - path: "/backend-api/codex/responses", - public_headers: [{name: "accept", value: "application/json"}], - public_body: "{}" - }; - let usage: TransportCredentialUse = { - kind: "access", - access_handle: inner.handle, - refresh_handle: null, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - oauth::transport_result(&request, &usage) + let no_error: bool = match issued.error { Some(_) => false, None => true }; + let ok: bool = issued.ok; + match issued.value { + None => {ok: false, error: None, value: None}, + Some(inner) => if !ok || !no_error => { + {ok: false, error: None, value: None} + } else => { + let request: TransportPublicRequest = { + policy_handle: policy, + credential_id: credential_id, + method: "POST", + path: "/backend-api/codex/responses", + public_headers: [{name: "accept", value: "application/json"}], + public_body: "{}" + }; + let usage: TransportCredentialUse = { + kind: "access", + access_handle: Some(inner.handle), + refresh_handle: None, + code_handle: None, + verifier_handle: None, + device_handle: None + }; + oauth::transport_result(&request, &usage) + } } } -pub fn cleanup(policy: resource) -> null { +pub fn cleanup(policy: resource) -> () { auth::cleanup_transients(&policy) } diff --git a/tests/fixtures/providers/opaque_stream_codec.rss b/tests/fixtures/providers/opaque_stream_codec.rss index 58cc90f..eb3dbbd 100644 --- a/tests/fixtures/providers/opaque_stream_codec.rss +++ b/tests/fixtures/providers/opaque_stream_codec.rss @@ -3,16 +3,18 @@ // Invokes the real production body builder and codec state machine with plain // public maps. No fake native resource, no mirrored Rust parsing, no // credentials: the records are ordinary decoded chunk objects. -use super::super::super::rss::llm::openai_chat; -use super::super::super::rss::llm::openai_stream_codec; -use super::super::super::rss::llm::types; +use self::llm::openai_chat; +use self::llm::openai_stream_codec; +use self::llm::types; pub fn request_body(request: map) -> string { let model: string = types::request_string(request, "model"); openai_chat::chat_build_public_stream_body(request, model) } -pub fn collect(chunks: array, done: bool) -> map { +pub fn collect(input: map) -> map { + let chunks: array = types::request_array(input, "chunks"); + let done: bool = types::request_bool(input, "done", false); let mut state: map = openai_stream_codec::begin(); let mut i = 0; while i < chunks.length { @@ -26,7 +28,9 @@ pub fn collect(chunks: array, done: bool) -> map { openai_stream_codec::finish(state) } -pub fn late_record(chunks: array, late: map) -> map { +pub fn late_record(input: map) -> map { + let chunks: array = types::request_array(input, "chunks"); + let late: map = types::request_map(input, "late"); let mut state: map = openai_stream_codec::begin(); let mut i = 0; while i < chunks.length { diff --git a/tests/fixtures/providers/responses_codec.rss b/tests/fixtures/providers/responses_codec.rss index 0384d3d..a1f75b0 100644 --- a/tests/fixtures/providers/responses_codec.rss +++ b/tests/fixtures/providers/responses_codec.rss @@ -1,43 +1,52 @@ -// Public-input fixture for the pure RSS OpenAI Responses codec (Task8). -// -// Invokes the real production body builder and codec state machine with plain -// public maps. No fake native resource, no mirrored Rust parsing, no -// credentials: the records are ordinary decoded event objects. -use super::super::super::rss::llm::openai_responses_codec; -use super::super::super::rss::llm::types; +// Public fixture driving the production codec through the restricted runner. +use self::llm::openai_responses_codec as codec; +use self::llm::types; +use self::llm::tool_descriptor; -pub fn request_body(request: map) -> string { +pub fn request_body(args: array) -> string { + let request: map = args[0]; let model: string = types::request_string(request, "model"); - openai_responses_codec::responses_build_public_body(request, model, false) + codec::responses_build_public_body(request, model, false) } -pub fn stream_body(request: map) -> string { +pub fn native_descriptor_body(args: array) -> string { + let mut request: map = args[0]; let model: string = types::request_string(request, "model"); - openai_responses_codec::responses_build_public_body(request, model, true) + let tools: array = types::request_array(request, "tools"); + let descriptor: map = types::array_entry(tools, 0); + let tool = tool_descriptor::from_native(descriptor); + request["tools"] = [tool]; + codec::responses_build_public_body(request, model, false) } -pub fn collect(chunks: array, done: bool) -> map { - let mut state: map = openai_responses_codec::begin(); +pub fn stream_body(args: array) -> string { + let request: map = args[0]; + let model: string = types::request_string(request, "model"); + codec::responses_build_public_body(request, model, true) +} + +pub fn collect(args: array) -> map { + let chunks: array = args[0]; + let done: bool = args[1]; + let mut state: map = codec::begin(); let mut i = 0; while i < chunks.length { - let chunk: map = types::array_entry(chunks, i); - state = openai_responses_codec::push_chunk(state, chunk); + state = codec::push_json(state, chunks[i].copy()); i += 1; } if done { - state = openai_responses_codec::push_done(state); + state = codec::push_done(state); } - openai_responses_codec::finish(state) -} - -pub fn parse_body(body: map) -> map { - openai_responses_codec::responses_parse_body(body) + codec::finish(state) } -pub fn parse_body_text(text: string) -> map { - openai_responses_codec::responses_parse_public_response(text) +pub fn parse_body_text(args: array) -> map { + let text: string = args[0]; + codec::responses_parse_public_response(text) } -pub fn parse_error(text: string, status: int) -> map { - openai_responses_codec::responses_parse_public_error(text, status) +pub fn parse_error(args: array) -> map { + let text: string = args[0]; + let status: int = args[1]; + codec::responses_parse_public_error(text, status) } diff --git a/tests/fixtures/refresh_result/main.rss b/tests/fixtures/refresh_result/main.rss index c3e67e1..7255672 100644 --- a/tests/fixtures/refresh_result/main.rss +++ b/tests/fixtures/refresh_result/main.rss @@ -11,10 +11,6 @@ struct RefreshIssueSendResult { replay_code: string } -struct RefreshValuePresence { - value: RefreshHandleEnvelope? -} - fn failed_refresh_issue_send_result(code: string) -> RefreshIssueSendResult { { ok: false, @@ -29,23 +25,21 @@ fn failed_refresh_issue_send_result(code: string) -> RefreshIssueSendResult { fn empty_transport_response() -> TransportResponseEnvelope { { ok: false, - error: null, + error: None, status: 0, public_headers: [], body_without_secret_fields: "", - retry_after_ms: null, - access_slot: null, - refresh_slot: null, - device_handle: null + retry_after_ms: None, + access_slot: None, + refresh_slot: None, + device_handle: None } } -fn failure_code(failure_value: AuthBridgeFailure?) -> string { - if failure_value != null => { - let failure: AuthBridgeFailure = failure_value; - failure.code.copy() - } else => { - "" +fn failure_code(failure_value: Option) -> string { + match failure_value { + Some(failure) => failure.code, + None => "" } } @@ -72,51 +66,51 @@ pub fn issue_and_send_result( let issued: RefreshHandleResultEnvelope = auth::refresh_handle_result(credential_id, generation, &policy); let issued_ok: bool = issued.ok; - let issued_failed: bool = issued.error != null; - let issued_holder: RefreshValuePresence = {value: issued.value}; - let has_value: bool = issued_holder.has("value"); - if !issued_ok || issued_failed || !has_value => { - failed_refresh_issue_send_result("issue_failed") - } else => { - let inner: RefreshHandleEnvelope = issued_holder.value; - let handle: resource = inner.handle; - let request: TransportPublicRequest = { - policy_handle: policy, - credential_id: credential_id.copy(), - method: "POST", - path: "/oauth/token", - public_headers: [ - {name: "content-type", value: "application/x-www-form-urlencoded"}, - {name: "accept", value: "application/json"} - ], - public_body: "grant_type=refresh_token" - }; - let usage: TransportCredentialUse = { - kind: "refresh", - access_handle: null, - refresh_handle: handle, - code_handle: null, - verifier_handle: null, - device_handle: null - }; - let first: TransportResultEnvelope = oauth::transport_result(&request, &usage); - let first_ok: bool = first.ok; - let first_code: string = failure_code(first.error); - let first_failed: bool = first_code != ""; - let first_value_opt: TransportResponseEnvelope? = first.value; - let first_response: TransportResponseEnvelope = first_value_opt.unwrap_or(empty_transport_response()); - let first_status: int = first_response.status; - let first_body: string = first_response.body_without_secret_fields; - let second: TransportResultEnvelope = oauth::transport_result(&request, &usage); - let replay_ok: bool = second.ok; - let replay_code: string = failure_code(second.error); - { - ok: first_ok && !first_failed, - code: first_code, - first_status: first_status, - first_body: first_body, - replay_ok: replay_ok, - replay_code: replay_code + let issued_failed: bool = match issued.error { Some(_) => true, None => false }; + match issued.value { + None => failed_refresh_issue_send_result("issue_failed"), + Some(inner) => if !issued_ok || issued_failed => { + failed_refresh_issue_send_result("issue_failed") + } else => { + let handle: resource = inner.handle; + let request: TransportPublicRequest = { + policy_handle: policy, + credential_id: credential_id.copy(), + method: "POST", + path: "/oauth/token", + public_headers: [ + {name: "content-type", value: "application/x-www-form-urlencoded"}, + {name: "accept", value: "application/json"} + ], + public_body: "grant_type=refresh_token" + }; + let usage: TransportCredentialUse = { + kind: "refresh", + access_handle: None, + refresh_handle: Some(handle), + code_handle: None, + verifier_handle: None, + device_handle: None + }; + let first: TransportResultEnvelope = oauth::transport_result(&request, &usage); + let first_ok: bool = first.ok; + let first_code: string = failure_code(first.error); + let first_failed: bool = first_code != ""; + let first_value_opt: Option = first.value; + let first_response: TransportResponseEnvelope = match first_value_opt { Some(response) => response, None => empty_transport_response() }; + let first_status: int = first_response.status; + let first_body: string = first_response.body_without_secret_fields; + let second: TransportResultEnvelope = oauth::transport_result(&request, &usage); + let replay_ok: bool = second.ok; + let replay_code: string = failure_code(second.error); + { + ok: first_ok && !first_failed, + code: first_code, + first_status: first_status, + first_body: first_body, + replay_ok: replay_ok, + replay_code: replay_code + } } } } @@ -139,15 +133,15 @@ pub fn send_refresh( }; let usage: TransportCredentialUse = { kind: "refresh", - access_handle: null, - refresh_handle: refresh, - code_handle: null, - verifier_handle: null, - device_handle: null + access_handle: None, + refresh_handle: Some(refresh), + code_handle: None, + verifier_handle: None, + device_handle: None }; oauth::transport_result(&request, &usage) } -pub fn close_refresh(refresh: resource) -> null { +pub fn close_refresh(refresh: resource) -> () { auth::close_refresh(refresh) } diff --git a/tests/gateway_contract_tests.rs b/tests/gateway_contract_tests.rs new file mode 100644 index 0000000..3074c1b --- /dev/null +++ b/tests/gateway_contract_tests.rs @@ -0,0 +1,11 @@ +#[path = "cases/gateway_tests.rs"] +mod gateway_tests; + +#[path = "cases/gateway_workspace_tests.rs"] +mod gateway_workspace_tests; + +#[path = "cases/http_api_migration_tests.rs"] +mod http_api_migration_tests; + +#[path = "cases/metrics_tests.rs"] +mod metrics_tests; diff --git a/tests/guardian_output_budget.rs b/tests/guardian_output_budget.rs index 0309ddd..ff09f27 100644 --- a/tests/guardian_output_budget.rs +++ b/tests/guardian_output_budget.rs @@ -107,6 +107,11 @@ impl ReviewModules { fs::create_dir_all(&agent).expect("create review module tree"); let copied = agent.join("guardian.rss"); fs::write(&copied, &source).expect("write production guardian bytes"); + fs::copy( + production.with_file_name("provider_body.rss"), + agent.join("provider_body.rss"), + ) + .expect("copy production typed provider body module"); assert_eq!( fs::read(&copied).expect("reread copied guardian"), fs::read(&production).expect("reread production guardian"), diff --git a/tests/guardian_sandbox_policy_tests.rs b/tests/guardian_sandbox_policy_tests.rs index 871109a..f7798ee 100644 --- a/tests/guardian_sandbox_policy_tests.rs +++ b/tests/guardian_sandbox_policy_tests.rs @@ -93,6 +93,12 @@ impl ReviewModules { fs::create_dir_all(&agent).expect("create review module tree"); let copied = agent.join("guardian.rss"); fs::write(&copied, &source).expect("write production guardian bytes"); + fs::write( + agent.join("provider_body.rss"), + fs::read(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/agent/provider_body.rss")) + .expect("read production provider body module"), + ) + .expect("write production provider body module"); assert_eq!( fs::read(&copied).expect("reread copied guardian"), fs::read(&production).expect("reread production guardian"), diff --git a/tests/native_auth_contract_tests.rs b/tests/native_auth_contract_tests.rs new file mode 100644 index 0000000..b77d898 --- /dev/null +++ b/tests/native_auth_contract_tests.rs @@ -0,0 +1,17 @@ +#[path = "cases/native_callback_rss_tests.rs"] +mod native_callback_rss_tests; + +#[path = "cases/native_callback_tests.rs"] +mod native_callback_tests; + +#[path = "cases/native_exchange_pair_tests.rs"] +mod native_exchange_pair_tests; + +#[path = "cases/native_manual_input_tests.rs"] +mod native_manual_input_tests; + +#[path = "cases/native_payload_transport_tests.rs"] +mod native_payload_transport_tests; + +#[path = "cases/pkce_session_rss_tests.rs"] +mod pkce_session_rss_tests; diff --git a/tests/oauth_bridge_tests.rs b/tests/oauth_bridge_tests.rs new file mode 100644 index 0000000..25312b2 --- /dev/null +++ b/tests/oauth_bridge_tests.rs @@ -0,0 +1,26 @@ +#[path = "support/credential_use.rs"] +mod credential_use; + +#[path = "support/typed_carriers.rs"] +mod typed_carriers; + +#[path = "cases/oauth_callback_bridge_tests.rs"] +mod oauth_callback_bridge_tests; +#[path = "cases/oauth_capture_bridge_tests.rs"] +mod oauth_capture_bridge_tests; +#[path = "cases/oauth_code_exchange_bridge_tests.rs"] +mod oauth_code_exchange_bridge_tests; +#[path = "cases/oauth_code_generation_bridge_tests.rs"] +mod oauth_code_generation_bridge_tests; +#[path = "cases/oauth_config_public_tests.rs"] +mod oauth_config_public_tests; +#[path = "cases/oauth_device_bridge_tests.rs"] +mod oauth_device_bridge_tests; +#[path = "cases/oauth_payload_bridge_tests.rs"] +mod oauth_payload_bridge_tests; +#[path = "cases/oauth_response_sanitizer_rss_tests.rs"] +mod oauth_response_sanitizer_rss_tests; +#[path = "cases/oauth_timing_bridge_tests.rs"] +mod oauth_timing_bridge_tests; +#[path = "cases/oauth_transport_bridge_tests.rs"] +mod oauth_transport_bridge_tests; diff --git a/tests/pkce_login_rss_tests.rs b/tests/pkce_login_rss_tests.rs index 4d52eb5..1b7a5e3 100644 --- a/tests/pkce_login_rss_tests.rs +++ b/tests/pkce_login_rss_tests.rs @@ -165,16 +165,6 @@ fn result_ok(value: &Value) -> bool { }) } -#[test] -fn fd_observer_rejects_retained_owner_and_reused_integer() { - if owned_fd_observer::spawn_fd_observer_child_unless_child( - "fd_observer_rejects_retained_owner_and_reused_integer", - ) { - return; - } - owned_fd_observer::retained_and_reused_control(assert_fd_closed); -} - #[test] fn configured_pkce_login_runs_the_production_rss_entry_and_persists_a_credential() { let home = IsolatedHome::configured_pkce(); @@ -551,6 +541,102 @@ fn production_runner() -> AuthEntryRunner { const VALID_TOKEN_RESPONSE: &str = r#"{"access_token":"SYNTHETIC_ACCESS_SECRET","refresh_token":"SYNTHETIC_REFRESH_SECRET","token_type":"Bearer","expires_in":3600}"#; +/// The same token response without `refresh_token`: the transport mints no +/// refresh slot, so the guest sees the `None` carrier. +const ACCESS_ONLY_TOKEN_RESPONSE: &str = + r#"{"access_token":"SYNTHETIC_ACCESS_SECRET","token_type":"Bearer","expires_in":3600}"#; + +/// Drives the production command bundle once with a scripted token response and +/// returns the persisted credential record. +/// +/// The failing browser forces the native manual-FD fallback, so the callback is +/// produced deterministically from the enqueued instruction; the token response +/// is the only variable, which is what makes this usable for the `Some`/`None` +/// carrier dual path. +fn run_entry_persisting(token_response: &str) -> serde_yaml::Value { + let runner = production_runner(); + let home = IsolatedHome::configured_pkce(); + let (reader, writer) = pipe_pair(); + let attempted_redirect = Arc::new(Mutex::new(None)); + let browser = Arc::new(FailingBrowser { + attempts: Arc::new(AtomicUsize::new(0)), + redirect_uri: Arc::clone(&attempted_redirect), + }); + let transport = Arc::new(ScriptedHttpsTransport::new()); + transport.push_raw(200, token_response); + let (sender, receiver) = authorization_instruction_channel(); + let callback_writer = thread::spawn(move || { + let instruction = receiver.recv().expect("production PKCE instruction"); + let state = query_value(&instruction.authorization_url, "state") + .expect("state in production authorization URL"); + let mut callback = Url::parse(&instruction.redirect_uri).expect("manual redirect URI"); + callback + .query_pairs_mut() + .append_pair("state", &state) + .append_pair("code", "SYNTHETIC_AUTHORIZATION_CODE"); + let mut writer = fs::File::from(writer); + writeln!(writer, "{}", callback.as_str()).expect("write native manual callback FD"); + }); + let cancellation = RunCancellation::with_timeout(Duration::from_secs(30)); + let outcome = run_production_entry( + &runner, + &home, + &cancellation, + transport.clone(), + (browser, Some(reader)), + Arc::new(ScriptedClock::new(1_900_000_000_000)), + sender, + ); + callback_writer.join().expect("manual callback producer"); + let value = outcome.unwrap_or_else(|error| panic!("production entry failed: {error:?}")); + assert!( + result_ok(&value), + "production command login returned failure: {value:?}" + ); + assert!(!cancellation.watcher_is_armed()); + assert_eq!(transport.take_sent().len(), 1, "exactly one token exchange"); + serde_yaml::from_slice( + &fs::read(home.0.join("auth.yaml")).expect("native credential store after login"), + ) + .expect("decode native credential store") +} + +/// `Some(refresh_slot)`: the exchange minted a refresh slot, so the entry takes +/// the replacement save path and persists both secrets. +#[test] +fn production_entry_persists_refresh_slot_when_the_exchange_mints_one() { + let saved = run_entry_persisting(VALID_TOKEN_RESPONSE); + let credential = &saved["credentials"]["fixture-codex"]; + assert_eq!( + credential["access_token"].as_str(), + Some("SYNTHETIC_ACCESS_SECRET") + ); + assert_eq!( + credential["refresh_token"].as_str(), + Some("SYNTHETIC_REFRESH_SECRET") + ); + assert_eq!(credential["generation"].as_u64(), Some(1)); +} + +/// `None` refresh slot: the exchange mints none, so the entry keeps the +/// credential access-only instead of fabricating a refresh secret. +#[test] +fn production_entry_keeps_credential_access_only_without_a_refresh_slot() { + let saved = run_entry_persisting(ACCESS_ONLY_TOKEN_RESPONSE); + let credential = &saved["credentials"]["fixture-codex"]; + assert_eq!( + credential["access_token"].as_str(), + Some("SYNTHETIC_ACCESS_SECRET") + ); + assert!( + credential + .get("refresh_token") + .is_none_or(serde_yaml::Value::is_null), + "absent refresh slot must not produce a refresh secret" + ); + assert_eq!(credential["generation"].as_u64(), Some(1)); +} + #[test] fn production_entry_falls_back_to_native_manual_fd_after_browser_failure() { if owned_fd_observer::spawn_fd_observer_child_unless_child( diff --git a/tests/plugin_contract_tests.rs b/tests/plugin_contract_tests.rs new file mode 100644 index 0000000..c94f7d2 --- /dev/null +++ b/tests/plugin_contract_tests.rs @@ -0,0 +1,8 @@ +#[path = "cases/plugin_graph_tests.rs"] +mod plugin_graph_tests; + +#[path = "cases/plugin_manifest_tests.rs"] +mod plugin_manifest_tests; + +#[path = "cases/plugin_schema_sync_tests.rs"] +mod plugin_schema_sync_tests; diff --git a/tests/provider_codec_tests.rs b/tests/provider_codec_tests.rs new file mode 100644 index 0000000..29835d7 --- /dev/null +++ b/tests/provider_codec_tests.rs @@ -0,0 +1,18 @@ +#[path = "support/provider_opaque_fixture.rs"] +#[allow(dead_code)] +mod provider_opaque_fixture; + +#[path = "cases/codex_metadata_rss_tests.rs"] +mod codex_metadata_rss_tests; + +#[path = "cases/codex_responses_codec_tests.rs"] +mod codex_responses_codec_tests; + +#[path = "cases/provider_refresh_transaction_tests.rs"] +mod provider_refresh_transaction_tests; + +#[path = "cases/provider_stream_codec_rss_tests.rs"] +mod provider_stream_codec_rss_tests; + +#[path = "cases/provider_tests.rs"] +mod provider_tests; diff --git a/tests/provider_config_fixture_tests.rs b/tests/provider_config_fixture_tests.rs new file mode 100644 index 0000000..e44d7a2 --- /dev/null +++ b/tests/provider_config_fixture_tests.rs @@ -0,0 +1,21 @@ +#[path = "support/opaque_chat_stream.rs"] +#[allow(dead_code)] +mod opaque_chat_stream; + +#[path = "cases/codex_responses_rss_entry_tests.rs"] +mod codex_responses_rss_entry_tests; + +#[path = "cases/provider_access_failure_tests.rs"] +mod provider_access_failure_tests; + +#[path = "cases/provider_auth_rss_entry_tests.rs"] +mod provider_auth_rss_entry_tests; + +#[path = "cases/provider_default_opaque_tests.rs"] +mod provider_default_opaque_tests; + +#[path = "cases/provider_opaque_bridge_tests.rs"] +mod provider_opaque_bridge_tests; + +#[path = "cases/provider_openai_opaque_tests.rs"] +mod provider_openai_opaque_tests; diff --git a/tests/provider_loop_tests.rs b/tests/provider_loop_tests.rs new file mode 100644 index 0000000..a7fc517 --- /dev/null +++ b/tests/provider_loop_tests.rs @@ -0,0 +1,30 @@ +#[path = "support/typed_carriers.rs"] +mod typed_carriers; + +#[path = "support/opaque_chat_stream.rs"] +mod opaque_chat_stream; + +#[path = "support/provider_opaque_fixture.rs"] +#[allow(dead_code)] +mod provider_opaque_fixture; + +#[path = "cases/provider_401_refresh_loop_tests.rs"] +mod provider_401_refresh_loop_tests; + +#[path = "cases/provider_concurrent_generation_loop_tests.rs"] +mod provider_concurrent_generation_loop_tests; + +#[path = "cases/provider_expired_refresh_loop_tests.rs"] +mod provider_expired_refresh_loop_tests; + +#[path = "cases/provider_opaque_loop_tests.rs"] +mod provider_opaque_loop_tests; + +#[path = "cases/provider_opaque_tool_loop_tests.rs"] +mod provider_opaque_tool_loop_tests; + +#[path = "cases/provider_quota_loop_tests.rs"] +mod provider_quota_loop_tests; + +#[path = "cases/real_tool_continuation_tests.rs"] +mod real_tool_continuation_tests; diff --git a/tests/rss_process_tool_tests.rs b/tests/rss_process_tool_tests.rs index badfb63..c777004 100644 --- a/tests/rss_process_tool_tests.rs +++ b/tests/rss_process_tool_tests.rs @@ -728,12 +728,6 @@ fn error_code(value: &Value) -> &str { .unwrap_or("") } -#[test] -fn rss_terminal_and_process_modules_compile() { - let _ = compile_rss("terminal.rss"); - let _ = compile_rss("process.rss"); -} - #[test] fn rss_terminal_and_process_descriptors_match_native() { for (module, expected) in [ @@ -1923,17 +1917,6 @@ fn overflow_process_wait_echo_and_printf_artifact_bytes_match_native() { ); } -#[test] -fn process_fixture_roots_live_under_std_temp_dir() { - let fixture = Fixture::new("portable-root"); - assert!( - fixture.parent.starts_with(std::env::temp_dir()), - "fixture parent {:?} must be under {:?}", - fixture.parent, - std::env::temp_dir() - ); -} - #[test] fn foreground_stdin_empty_multibyte_large_and_child_exit_match_native() { let fixture = Fixture::new("stdin-matrix"); diff --git a/tests/rss_tools_contract_tests.rs b/tests/rss_tools_contract_tests.rs new file mode 100644 index 0000000..4ba8a8b --- /dev/null +++ b/tests/rss_tools_contract_tests.rs @@ -0,0 +1,17 @@ +#[path = "support/rss_tool_carrier.rs"] +mod rss_tool_carrier; + +#[path = "cases/rss_file_tool_tests.rs"] +mod rss_file_tool_tests; + +#[path = "cases/rss_mutating_file_tool_tests.rs"] +mod rss_mutating_file_tool_tests; + +#[path = "cases/rss_tool_architecture_tests.rs"] +mod rss_tool_architecture_tests; + +#[path = "cases/rss_tool_dispatch_tests.rs"] +mod rss_tool_dispatch_tests; + +#[path = "cases/rss_tool_registry_tests.rs"] +mod rss_tool_registry_tests; diff --git a/tests/run_lifecycle_tests.rs b/tests/run_lifecycle_tests.rs index 29affcc..98f5a5b 100644 --- a/tests/run_lifecycle_tests.rs +++ b/tests/run_lifecycle_tests.rs @@ -1346,14 +1346,6 @@ impl DeadlineFailureTrace { } } - fn len(&self) -> usize { - self.records.len() - } - - fn dropped(&self) -> u64 { - self.dropped - } - fn count(&self, kind: DeadlineTraceKind) -> usize { if kind == DeadlineTraceKind::PollSample { return usize::try_from(self.poll_count).unwrap_or(usize::MAX); @@ -1461,13 +1453,6 @@ impl DeadlineFailureTrace { } } -// Pure PID guard mirroring the deadline poll condition (`!empty && all alive`). -// Used only by the deterministic trace-control unit test with synthetic PIDs; -// hook presence never substitutes for a real owned PID in the lifecycle test. -fn deadline_trace_pid_guard_all_alive(pids: &[u32], is_alive: impl Fn(u32) -> bool) -> bool { - !pids.is_empty() && pids.iter().all(|pid| is_alive(*pid)) -} - // Typed boundary-phase ordinals for the fixture-only durable-provider // observation of the second durable provider turn. Fixed tags only: no run id, // payload, path, request/response text, action, secret, or raw event. @@ -1492,8 +1477,6 @@ const DEADLINE_BOUNDARY_DETAIL_LIMIT: usize = 120; /// never be confused and the original phase coverage is untouched. #[cfg(feature = "workspace-io-fault")] const DEADLINE_HARNESS_ERROR_DETAIL_BASE: u64 = 1024; -#[cfg(feature = "workspace-io-fault")] -const DEADLINE_HARNESS_ERROR_UNKNOWN: u8 = 255; /// Disjoint finite interval used by the existing trace record's numeric /// `detail` for the caller-owned turn2 pin-site observation. Values below this /// base stay the original eight boundary phase codes 0..=7, and the public @@ -2007,362 +1990,6 @@ async fn deadline_terminates_child_process_without_residue() { ); } -#[test] -fn deadline_failure_trace_is_bounded_and_rejects_missing_pid() { - // Synthetic unit data only: exercises the bounded trace store and its render path. - // These record values are hand-built here and are never presented as native evidence. - let mut trace = DeadlineFailureTrace::new(); - trace.push(DeadlineTraceKind::Admit, 0); - trace.push(DeadlineTraceKind::WorkerSpawn, 0); - trace.push(DeadlineTraceKind::ProviderEnter, 0); - trace.push(DeadlineTraceKind::ProviderExit, 1); - trace.push(DeadlineTraceKind::NativeBeforeSpawn, 0); - trace.push(DeadlineTraceKind::NativeAfterSpawn, 0); - trace.push(DeadlineTraceKind::PollSample, 1); - trace.push(DeadlineTraceKind::Cleanup, 0); - trace.push(DeadlineTraceKind::Attach, 1); - // Poll observations aggregate without consuming the phase budget. - assert_eq!(trace.len(), 8); - assert_eq!(trace.dropped(), 0); - assert_eq!(trace.count(DeadlineTraceKind::PollSample), 1); - assert_eq!(trace.attach_state(), "ok"); - let early = trace.render_bounded(); - assert!( - early.contains("attach=ok"), - "bounded render must witness attach success: {early}" - ); - for field in [ - "t_admit_first=", - "t_admit_last=", - "t_worker_first=", - "t_worker_last=", - "t_prov_enter_first=", - "t_prov_enter_last=", - "t_prov_exit_first=", - "t_prov_exit_last=", - "t_native_before_first=", - "t_native_after_first=", - "t_poll_first=", - "t_poll_last=", - "t_cleanup_first=", - "t_attach_first=", - ] { - assert!( - early.contains(field), - "bounded render must carry per-kind timing {field}: {early}" - ); - } - // A poll flood aggregates without evicting any phase record. - for index in 0..(DEADLINE_TRACE_CAP as u64 + 8) { - trace.push(DeadlineTraceKind::PollSample, index); - } - assert_eq!(trace.len(), 8); - assert_eq!(trace.dropped(), 0); - assert_eq!( - trace.count(DeadlineTraceKind::PollSample), - 1 + DEADLINE_TRACE_CAP + 8 - ); - let flooded = trace.render_bounded(); - assert!( - flooded.contains("dropped=0"), - "poll flood must not report phase overflow: {flooded}" - ); - assert!( - flooded.contains("attach=ok"), - "attach witness must survive poll flood: {flooded}" - ); - // Phase overflow past the fixed cap stays bounded and explicit. - for _ in 0..(DEADLINE_TRACE_CAP as u64 + 8) { - trace.push(DeadlineTraceKind::Cleanup, 0); - } - assert_eq!(trace.len(), DEADLINE_TRACE_CAP); - // 8 existing phases + 72 new ones overflow the 64-slot store by 16. - assert_eq!(trace.dropped(), 16); - let rendered = trace.render_bounded(); - assert!( - rendered.contains("dropped=16"), - "bounded render must report overflow explicitly: {rendered}" - ); - assert!( - rendered.contains("attach=ok"), - "attach witness must survive overflow: {rendered}" - ); - assert!( - rendered.len() <= DEADLINE_TRACE_RENDER_LIMIT, - "bounded render must stay within the fixed limit" - ); - // Synthetic attach-miss/none witnesses only: same bounded path, never native proof. - let mut miss_trace = DeadlineFailureTrace::new(); - miss_trace.push(DeadlineTraceKind::Attach, 0); - assert_eq!(miss_trace.attach_state(), "miss"); - assert!(miss_trace.render_bounded().contains("attach=miss")); - let empty_trace = DeadlineFailureTrace::new(); - assert_eq!(empty_trace.attach_state(), "none"); - assert!(empty_trace.render_bounded().contains("attach=none")); - assert!(empty_trace.render_bounded().contains("t_admit_first=none")); - // Synthetic `tool.failed` producer envelopes only, shaped like the actual - // `commit_result`/`interrupt` data (`tool_call_id`/`ok`/`truncated`/`error_code`). - // These JSON values are hand-built unit inputs, never actual runtime evidence. - let ok_events = vec![ - json!({"event": "tool.requested", "data": {"tool_call_id": "call-sleep"}}), - json!({"event": "tool.started", "data": {"tool_call_id": "call-sleep"}}), - json!({ - "event": "tool.failed", - "data": { - "tool_call_id": "call-sleep", - "ok": false, - "truncated": false, - "error_code": "terminal_spawn_failed" - } - }), - ]; - assert_eq!( - bounded_tool_failed_code_from_events(&ok_events), - "terminal_spawn_failed" - ); - let absent_events = vec![json!({ - "event": "tool.failed", - "data": {"tool_call_id": "call-sleep", "ok": false, "truncated": false} - })]; - assert_eq!( - bounded_tool_failed_code_from_events(&absent_events), - "absent" - ); - let type_events = vec![json!({ - "event": "tool.failed", - "data": {"tool_call_id": "call-sleep", "ok": false, "error_code": 42} - })]; - assert_eq!( - bounded_tool_failed_code_from_events(&type_events), - "non_string" - ); - let empty_events = vec![json!({ - "event": "tool.failed", - "data": {"tool_call_id": "call-sleep", "ok": false, "error_code": ""} - })]; - assert_eq!(bounded_tool_failed_code_from_events(&empty_events), "empty"); - let long_code = "x".repeat(DEADLINE_TOOL_CODE_LIMIT + 36); - let long_events = vec![json!({ - "event": "tool.failed", - "data": {"tool_call_id": "call-sleep", "ok": false, "error_code": long_code} - })]; - let long_rendered = bounded_tool_failed_code_from_events(&long_events); - assert_eq!(long_rendered.chars().count(), DEADLINE_TOOL_CODE_LIMIT); - assert!(long_rendered.ends_with("...")); - let no_failed = vec![json!({"event": "tool.completed", "data": {"tool_call_id": "c"}})]; - assert_eq!(bounded_tool_failed_code_from_events(&no_failed), "none"); - assert_eq!(bounded_tool_failed_code_from_events(&[]), "none"); - // A missing native PID is still rejected even when synthetic hook records exist. - assert!(!deadline_trace_pid_guard_all_alive(&[], |_| true)); - assert!(deadline_trace_pid_guard_all_alive(&[4242], |_| true)); - assert!(!deadline_trace_pid_guard_all_alive(&[4242], |_| false)); -} - -// Synthetic finite-ordinal projection control for the post-return harness -// error observation. These bounded records are hand-built here; they are not a -// replay of any real producer error, do not reproduce the original remote PID -// failure, and never replace the original lifecycle assertions. -#[cfg(feature = "workspace-io-fault")] -#[test] -fn deadline_harness_error_ordinal_projection_is_finite_and_loss_explicit() { - let empty = DeadlineFailureTrace::new(); - let empty_summary = deadline_boundary_summary(&empty, 0); - assert!( - empty_summary.contains("harness_error_ordinal=none"), - "absent observation must render the fixed none field: {empty_summary}" - ); - let empty_document = deadline_boundary_document(&empty, 0, Duration::ZERO); - assert!( - empty_document.get("harness_error_ordinal").is_some(), - "artifact must carry the fixed field even when absent" - ); - for expected in [0u8, 1, 2, 3, 4, 5, DEADLINE_HARNESS_ERROR_UNKNOWN] { - let mut trace = DeadlineFailureTrace::new(); - trace.push( - DeadlineTraceKind::ProviderBoundary, - DEADLINE_HARNESS_ERROR_DETAIL_BASE + u64::from(expected), - ); - let summary = deadline_boundary_summary(&trace, 0); - assert!( - summary.contains(&format!("harness_error_ordinal={expected}")), - "observed ordinal must render: {summary}" - ); - let document = deadline_boundary_document(&trace, 0, Duration::ZERO); - assert_eq!(document["harness_error_ordinal"], json!(expected)); - } - // The same typed encoder used by the fixture callback transports each fixed - // number into the disjoint detail interval; the consumer decodes it back. - for expected in 0u8..=5 { - let detail = - deadline_boundary_phase_code(ProviderBoundaryPhase::HarnessEndErrorOrdinal(expected)); - assert_eq!( - detail, - DEADLINE_HARNESS_ERROR_DETAIL_BASE + u64::from(expected) - ); - let mut trace = DeadlineFailureTrace::new(); - trace.push(DeadlineTraceKind::ProviderBoundary, detail); - assert_eq!(deadline_harness_error_ordinal(&trace), Some(expected)); - } - let unknown_detail = deadline_boundary_phase_code( - ProviderBoundaryPhase::HarnessEndErrorOrdinal(DEADLINE_HARNESS_ERROR_UNKNOWN), - ); - assert_eq!( - unknown_detail, - DEADLINE_HARNESS_ERROR_DETAIL_BASE + u64::from(DEADLINE_HARNESS_ERROR_UNKNOWN) - ); - let mut unknown = DeadlineFailureTrace::new(); - unknown.push(DeadlineTraceKind::ProviderBoundary, unknown_detail); - assert_eq!( - deadline_harness_error_ordinal(&unknown), - Some(DEADLINE_HARNESS_ERROR_UNKNOWN) - ); - assert!( - deadline_boundary_summary(&unknown, 0).contains("harness_error_ordinal=255"), - "unknown code keeps the fixed 255 ordinal" - ); - // The original eight phase codes stay in 0..=7 and never decode as an - // error ordinal; their coverage meaning is unchanged. - let mut phases = DeadlineFailureTrace::new(); - for (code, _name) in DEADLINE_BOUNDARY_PHASES { - phases.push(DeadlineTraceKind::ProviderBoundary, code); - } - assert_eq!(deadline_harness_error_ordinal(&phases), None); - let phases_document = deadline_boundary_document(&phases, 0, Duration::ZERO); - assert_eq!(phases_document["coverage_complete"], json!(true)); - assert!(phases_document["harness_error_ordinal"].is_null()); - // Loss/drop remains explicit and never fabricates a code: a full bounded - // store drops the ordinal record, so the projection reports absence. - let mut overflow = DeadlineFailureTrace::new(); - for _ in 0..DEADLINE_TRACE_CAP { - overflow.push(DeadlineTraceKind::Cleanup, 0); - } - overflow.push( - DeadlineTraceKind::ProviderBoundary, - DEADLINE_HARNESS_ERROR_DETAIL_BASE + 2, - ); - assert_eq!(overflow.len(), DEADLINE_TRACE_CAP); - assert_eq!(overflow.dropped(), 1); - assert_eq!(deadline_harness_error_ordinal(&overflow), None); - let overflow_summary = deadline_boundary_summary(&overflow, 0); - assert!( - overflow_summary.contains("harness_error_ordinal=none"), - "dropped ordinal must stay explicit absence: {overflow_summary}" - ); - assert!(!overflow_summary.contains("harness_error_ordinal=255")); - let overflow_document = deadline_boundary_document(&overflow, 0, Duration::ZERO); - assert!(overflow_document["harness_error_ordinal"].is_null()); - assert_eq!(overflow_document["invalid_observations"], json!(true)); - // A lost callback (contended existing slot) is explicit as well and never - // implies 255. - let lost_summary = deadline_boundary_summary(&empty, 1); - assert!(lost_summary.contains("lost=1")); - assert!(lost_summary.contains("harness_error_ordinal=none")); -} - -// Synthetic finite error-site projection control for the post-return harness -// pin-required observation. These bounded records are hand-built here; they are -// not a replay of any real producer error, do not reproduce the original remote -// PID failure, and never replace the original lifecycle assertions. -#[cfg(feature = "workspace-io-fault")] -#[test] -fn deadline_harness_pin_site_projection_is_finite_and_loss_explicit() { - let empty = DeadlineFailureTrace::new(); - let empty_summary = deadline_boundary_summary(&empty, 0); - assert!( - empty_summary.contains("harness_pin_site=none"), - "absent site observation must render the fixed none field: {empty_summary}" - ); - let empty_document = deadline_boundary_document(&empty, 0, Duration::ZERO); - assert!( - empty_document.get("harness_pin_site").is_some(), - "artifact must carry the fixed harness_pin_site field even when absent" - ); - assert!(empty_document["harness_pin_site"].is_null()); - // Seven finite sites travel through the same typed encoder/decoder the - // fixture callback uses; none of them decodes as a public ordinal. - for site in 1u8..=7 { - let detail = deadline_boundary_phase_code(ProviderBoundaryPhase::HarnessPinSite(site)); - assert_eq!( - detail, - DEADLINE_HARNESS_PIN_SITE_DETAIL_BASE + u64::from(site) - ); - let mut trace = DeadlineFailureTrace::new(); - trace.push(DeadlineTraceKind::ProviderBoundary, detail); - let summary = deadline_boundary_summary(&trace, 0); - assert!( - summary.contains(&format!("harness_pin_site={site}")), - "observed site must render: {summary}" - ); - let document = deadline_boundary_document(&trace, 0, Duration::ZERO); - assert_eq!(document["harness_pin_site"], json!(site)); - assert!(document["harness_error_ordinal"].is_null()); - assert_eq!(deadline_harness_error_ordinal(&trace), None); - } - // The unclassified site is an actual recorded 255, never an absence. - let unknown_detail = deadline_boundary_phase_code(ProviderBoundaryPhase::HarnessPinSite( - DEADLINE_HARNESS_PIN_SITE_UNKNOWN, - )); - assert_eq!( - unknown_detail, - DEADLINE_HARNESS_PIN_SITE_DETAIL_BASE + u64::from(DEADLINE_HARNESS_PIN_SITE_UNKNOWN) - ); - let mut unknown = DeadlineFailureTrace::new(); - unknown.push(DeadlineTraceKind::ProviderBoundary, unknown_detail); - let unknown_document = deadline_boundary_document(&unknown, 0, Duration::ZERO); - assert_eq!(unknown_document["harness_pin_site"], json!(255)); - assert!(deadline_boundary_summary(&unknown, 0).contains("harness_pin_site=255")); - // Original eight phase codes and their coverage meaning stay untouched; - // a site record does not disturb the disjoint public ordinal projection. - let mut phases = DeadlineFailureTrace::new(); - for (code, _name) in DEADLINE_BOUNDARY_PHASES { - phases.push(DeadlineTraceKind::ProviderBoundary, code); - } - phases.push(DeadlineTraceKind::ProviderBoundary, unknown_detail); - let phases_document = deadline_boundary_document(&phases, 0, Duration::ZERO); - assert_eq!(phases_document["coverage_complete"], json!(true)); - assert_eq!(phases_document["harness_pin_site"], json!(255)); - assert!(phases_document["harness_error_ordinal"].is_null()); - let mut both = DeadlineFailureTrace::new(); - both.push( - DeadlineTraceKind::ProviderBoundary, - DEADLINE_HARNESS_ERROR_DETAIL_BASE + 2, - ); - both.push( - DeadlineTraceKind::ProviderBoundary, - DEADLINE_HARNESS_PIN_SITE_DETAIL_BASE + 6, - ); - assert_eq!(deadline_harness_error_ordinal(&both), Some(2)); - assert_eq!(deadline_harness_pin_site(&both), Some(6)); - // A public ordinal record never decodes as a caller-owned site. - let mut ordinal_only = DeadlineFailureTrace::new(); - ordinal_only.push( - DeadlineTraceKind::ProviderBoundary, - DEADLINE_HARNESS_ERROR_DETAIL_BASE + 7, - ); - assert_eq!(deadline_harness_pin_site(&ordinal_only), None); - // Temporal drop and callback loss stay explicit absence, never 255. - let mut overflow = DeadlineFailureTrace::new(); - for _ in 0..DEADLINE_TRACE_CAP { - overflow.push(DeadlineTraceKind::Cleanup, 0); - } - overflow.push( - DeadlineTraceKind::ProviderBoundary, - DEADLINE_HARNESS_PIN_SITE_DETAIL_BASE + 5, - ); - assert_eq!(overflow.len(), DEADLINE_TRACE_CAP); - assert_eq!(overflow.dropped(), 1); - assert_eq!(deadline_harness_pin_site(&overflow), None); - let overflow_summary = deadline_boundary_summary(&overflow, 0); - assert!(overflow_summary.contains("harness_pin_site=none")); - assert!(!overflow_summary.contains("harness_pin_site=255")); - let overflow_document = deadline_boundary_document(&overflow, 0, Duration::ZERO); - assert!(overflow_document["harness_pin_site"].is_null()); - assert_eq!(overflow_document["invalid_observations"], json!(true)); - let lost_summary = deadline_boundary_summary(&empty, 1); - assert!(lost_summary.contains("lost=1")); - assert!(lost_summary.contains("harness_pin_site=none")); -} - #[tokio::test(flavor = "multi_thread")] async fn deadline_is_cumulative_from_admission() { let provider = ScriptedProvider::new(); @@ -4367,47 +3994,3 @@ async fn capability_host_init_panic_does_not_overwrite_closed_and_redrive_cancel drop(state); fs::remove_dir_all(&parent).expect("isolated init-panic workspace should be removed"); } - -fn collect_test_rs_files(directory: &std::path::Path, out: &mut Vec) { - for entry in fs::read_dir(directory).expect("tests directory should be readable") { - let path = entry.expect("directory entry should be readable").path(); - if path.is_dir() { - collect_test_rs_files(&path, out); - } else if path.extension().is_some_and(|extension| extension == "rs") { - out.push(path); - } - } -} - -#[test] -fn test_sources_do_not_hardcode_sibling_lease_temp_roots() { - let tests_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests"); - let mut sources = Vec::new(); - collect_test_rs_files(&tests_root, &mut sources); - assert!( - !sources.is_empty(), - "tests/ must contain Rust sources to audit" - ); - - let forbidden = [ - format!("/mnt/{}/workspace/", "TEMP"), - format!("{}-t", "coding"), - ["prod", "agent", "task"].join("-"), - format!("/{}s/", "worktree"), - format!("/tmp/{}-agent-", "prod"), - ]; - - let mut violations = Vec::new(); - for path in &sources { - let source = fs::read_to_string(path).expect("test source should be readable"); - for fragment in &forbidden { - if source.contains(fragment.as_str()) { - violations.push(format!("{} contains {fragment}", path.display())); - } - } - } - assert!( - violations.is_empty(), - "test sources must not hardcode sibling lease temp roots: {violations:?}" - ); -} diff --git a/tests/support/credential_use.rs b/tests/support/credential_use.rs new file mode 100644 index 0000000..fee60b1 --- /dev/null +++ b/tests/support/credential_use.rs @@ -0,0 +1,44 @@ +//! Shared `TransportCredentialUse` fixture for integration tests. +//! +//! The published contract declares `kind` plus five handle slots, each a logical +//! `Option>`. A hand-built map that omits a slot, or that puts a +//! physical null in one, is rejected by the callable's argument schema before +//! the adapter ever runs — so tests build the carrier here instead of restating +//! the field list, and adding a slot to the host contract cannot silently +//! desynchronise the fixtures again. +//! +//! `None` is the canonical `{tag: false}` carrier, never a null; a selected slot +//! is `{tag: true, payload: }`. + +use rustscript_vm::{Value, option}; + +/// Every handle slot the published `TransportCredentialUse` declares. +pub const HANDLE_SLOTS: [&str; 5] = [ + "access_handle", + "refresh_handle", + "code_handle", + "verifier_handle", + "device_handle", +]; + +/// A usage map with every handle slot absent. +pub fn none(kind: &str) -> Value { + with_handles(kind, &[]) +} + +/// A usage map whose named slots carry live handles; every other slot is `None`. +pub fn with_handles(kind: &str, handles: &[(&str, Value)]) -> Value { + let mut fields = vec![(Value::string("kind"), Value::string(kind))]; + for slot in HANDLE_SLOTS { + let selected = handles + .iter() + .find(|(name, _)| *name == slot) + .map(|(_, handle)| handle.clone()); + let carrier = match selected { + Some(handle) => option::some_carrier(handle), + None => option::none_carrier(), + }; + fields.push((Value::string(slot), carrier)); + } + Value::map(fields) +} diff --git a/tests/support/owned_fd_observer.rs b/tests/support/owned_fd_observer.rs index 5df7127..63178ae 100644 --- a/tests/support/owned_fd_observer.rs +++ b/tests/support/owned_fd_observer.rs @@ -253,6 +253,10 @@ pub fn observe_closed(fd: i32, original: InputIdentity, predicate: fn(i32)) -> C ClosedObservation(observed) } +#[allow( + dead_code, + reason = "only the instruction-session target runs this control" +)] pub fn retained_and_reused_control(predicate: fn(i32)) { let mut descriptors = [-1; 2]; assert_eq!( diff --git a/tests/support/rss_tool_carrier.rs b/tests/support/rss_tool_carrier.rs new file mode 100644 index 0000000..196e2aa --- /dev/null +++ b/tests/support/rss_tool_carrier.rs @@ -0,0 +1,74 @@ +//! VM projection shared by the RSS tool contract fixtures. +use rustscript_vm::Value as VmValue; +use serde_json::{Value, json}; + +pub fn json_to_vm_value(value: &Value) -> VmValue { + match value { + Value::Null => VmValue::Null, + Value::Bool(value) => VmValue::Bool(*value), + Value::Number(value) => { + if let Some(value) = value.as_i64() { + VmValue::Int(value) + } else { + VmValue::Float(value.as_f64().expect("finite json number")) + } + } + Value::String(value) => VmValue::string(value), + Value::Array(values) => VmValue::Array(std::sync::Arc::new( + values.iter().map(json_to_vm_value).collect::>(), + )), + Value::Object(entries) => VmValue::map( + entries + .iter() + .map(|(key, value)| (VmValue::string(key), json_to_vm_value(value))) + .collect(), + ), + } +} + +pub fn vm_value_to_json(value: &VmValue) -> Value { + match value { + VmValue::Null => Value::Null, + VmValue::Int(value) => json!(value), + VmValue::Float(value) => serde_json::Number::from_f64(*value) + .map(Value::Number) + .unwrap_or(Value::Null), + VmValue::Bool(value) => json!(value), + VmValue::String(value) => Value::String(value.to_string()), + VmValue::Bytes(value) => Value::String(String::from_utf8_lossy(value).into_owned()), + VmValue::Array(values) => Value::Array(values.iter().map(vm_value_to_json).collect()), + VmValue::Map(entries) => Value::Object( + entries + .iter() + .map(|(key, value)| (vm_map_key_to_string(key), vm_value_to_json(value))) + .collect(), + ), + VmValue::Callable(_) => Value::String("".to_string()), + } +} + +fn vm_map_key_to_string(value: &VmValue) -> String { + match value { + VmValue::String(value) => value.to_string(), + other => vm_value_to_json(other).to_string(), + } +} + +/// Restore the public canonical object from the tool's declared replay carrier. +/// An ordinary result passes through unchanged, so the assertions still catch +/// malformed data or errors on fresh executions. +pub fn tool_result(value: &VmValue) -> Value { + let mut value = vm_value_to_json(value); + match value["kind"].as_str() { + Some("replay") => { + let result = &mut value["result"]; + let text = result["data"].as_str().expect("replay data carrier"); + let data: Value = serde_json::from_str(text).expect("canonical replay JSON"); + assert!(data.is_object(), "replay data must be an object"); + result["data"] = data; + result.take() + } + Some("committed") => value["result"].take(), + _ => value, + } +} diff --git a/tests/support/typed_carriers.rs b/tests/support/typed_carriers.rs new file mode 100644 index 0000000..0196239 --- /dev/null +++ b/tests/support/typed_carriers.rs @@ -0,0 +1,12 @@ +//! Strict projections at the Rust side of the RSS boundary. +use rustscript_vm::{Value, option}; + +/// Project a required logical Option field after the caller selects its schema. +/// Null is only a test-side sentinel for None; it is never sent into the VM. +pub fn nullable(carrier: &Value) -> &Value { + match option::payload(carrier).expect("logical Option carrier") { + (false, None) => &Value::Null, + (true, Some(value)) if !matches!(value, Value::Null) => value, + _ => panic!("noncanonical Option carrier"), + } +} diff --git a/tests/telegram_tests.rs b/tests/telegram_tests.rs index 432102b..f576329 100644 --- a/tests/telegram_tests.rs +++ b/tests/telegram_tests.rs @@ -777,49 +777,11 @@ fn telegram_db_path(label: &str) -> std::path::PathBuf { telegram_db_path_in(&telegram_test_root(), label) } -/// The path builder itself: the base directory is explicit so the unit test -/// below pins the layout without touching the process-global env var -/// (parallel tests must never set it). fn telegram_db_path_in(root: &std::path::Path, label: &str) -> std::path::PathBuf { std::fs::create_dir_all(root).expect("telegram test root should be created"); root.join(format!("{label}-{}.db", Uuid::new_v4())) } -#[test] -fn telegram_test_artifacts_land_under_an_explicit_root() { - let base = std::env::temp_dir().join(format!("telegram-root-{}", Uuid::new_v4())); - let db = telegram_db_path_in(&base, "layout"); - assert!( - db.starts_with(&base), - "the database must live under the explicit root, got {db:?}" - ); - assert_eq!(db.parent(), Some(base.as_path())); - assert!( - db.file_name() - .expect("db file name") - .to_string_lossy() - .starts_with("layout-"), - "the label must prefix the unique file name" - ); - let workspace = TelegramWorkspaceGuard::create_in(&base); - let workspace_path = workspace.path().to_path_buf(); - assert!( - workspace_path.starts_with(&base), - "the workspace must live under the explicit root, got {workspace_path:?}" - ); - assert_eq!( - workspace_path.parent(), - Some(base.join("workspaces").as_path()), - "unique workspaces must stay isolated under workspaces/" - ); - workspace.cleanup(); - assert!( - !workspace_path.exists(), - "explicit workspace cleanup must remove the unique directory" - ); - std::fs::remove_dir_all(&base).expect("temporary root should be removed"); -} - /// Owned unique directory for one gateway-binary spawn beneath the assigned /// suite root. Each fixture owns exactly one `main.rss` entry plus its /// SQLite file, so the File snapshot closure can never include a sibling @@ -1448,11 +1410,13 @@ async fn adapter_refreshes_typing_while_work_is_pending_and_stops_after_terminal async fn adapter_retains_terminal_broadcast_while_lag_catchup_is_rendering() { let (base, state) = spawn_fixture().await; let (port, arrived, release_source, holding) = spawn_holding_fixture(); + let (burst_port, burst_arrived, release_burst, burst) = spawn_holding_fixture(); let source = format!( - r#"use stream; use agent; use http; + r#"use stream; use http; pub fn run(input: map) -> map {{ stream::emit({{type:"model.delta",delta:"start"}}); - agent::sleep_ms(1000); + let before_burst=http::request::new("GET","http://127.0.0.1:{burst_port}/"); + let ready=http::client::request(before_burst); stream::emit({{type:"model.delta",delta:"Q"}}); stream::emit({{type:"model.delta",delta:"Q"}}); stream::emit({{type:"model.delta",delta:"Q"}}); @@ -1463,13 +1427,26 @@ async fn adapter_retains_terminal_broadcast_while_lag_catchup_is_rendering() { }}"# ); let mut config = holding_config(port); + config.http.allowed_ports.push(burst_port); config.broadcast_capacity = 1; config.max_events_per_run = 3; let db = telegram_db_path("lag-terminal"); let gateway = test_state(&source, &db, |_| config); - let release_first = hold_first_draft(&state, "startQ"); + let release_first = hold_first_draft(&state, "start"); push_update(&state, fixture_json("updates_dm.json")); let adapter = spawn_adapter(gateway.clone(), test_config(&base)).await; + tokio::time::timeout(std::time::Duration::from_secs(15), burst_arrived) + .await + .expect("native pre-burst arrival deadline") + .expect("native pre-burst request"); + wait_until(std::time::Duration::from_secs(15), || { + state.draft_texts().iter().any(|s| s == "start") + }) + .await; + // Hold delivery of the first draft while the producer overruns the + // one-item live receiver. No sleep decides whether lag is exercised. + release_burst.send(()).expect("release the burst producer"); + burst.join().unwrap(); if tokio::time::timeout(std::time::Duration::from_secs(15), arrived) .await .is_err() @@ -1486,10 +1463,6 @@ async fn adapter_retains_terminal_broadcast_while_lag_catchup_is_rendering() { gateway.service().run_events(run_id) ); } - wait_until(std::time::Duration::from_secs(15), || { - state.draft_texts().iter().any(|s| s.starts_with("startQ")) - }) - .await; let run_id = gateway .service() .session_messages("telegram:fixture_bot:555:") @@ -1867,7 +1840,11 @@ async fn adapter_resumes_undelivered_events_after_restart() { // advances and the terminal is genuinely undelivered at shutdown. let gateway = test_state(ECHO_SOURCE, &db, |config| config); let workspace = gateway.workspace().to_path_buf(); - let adapter = spawn_adapter(gateway.clone(), test_config(&base)).await; + // Keep the deliberately held HTTP response pending until shutdown; the + // fixture's long-poll-derived request timeout must not allow a retry. + let mut blocked_config = test_config(&base); + blocked_config.poll_timeout = std::time::Duration::from_secs(90); + let adapter = spawn_adapter(gateway.clone(), blocked_config).await; wait_until(std::time::Duration::from_secs(15), || { // The blocked request is recorded on arrival, so this is the // deterministic signal that the barrier is armed. @@ -1971,7 +1948,8 @@ async fn adapter_resumes_undelivered_events_after_restart() { }] }), ); - wait_until(std::time::Duration::from_secs(20), || { + // The follow-up includes cold admission on the recovered gateway. + wait_until(std::time::Duration::from_secs(60), || { state .sent_texts() .iter() @@ -2003,8 +1981,11 @@ async fn adapter_resumes_undelivered_events_after_restart() { #[tokio::test] async fn adapter_resume_releases_the_gate_so_new_messages_are_admitted() { let (base, state) = spawn_fixture().await; - let (port, _arrived, release_tx, holding) = spawn_holding_fixture(); - let (config, source) = holding_config_and_source(port); + let (port, arrived, release_tx, holding) = spawn_holding_fixture(); + let (mut config, source) = holding_config_and_source(port); + // Recovery owns the old terminal. Keep the parked request from timing + // out and producing a second terminal during the new gateway's startup. + config.http.request_timeout = std::time::Duration::from_secs(240); push_update(&state, fixture_json("updates_dm.json")); let db = telegram_db_path("resume-gate"); @@ -2015,6 +1996,16 @@ async fn adapter_resume_releases_the_gate_so_new_messages_are_admitted() { state.draft_texts().iter().any(|text| text == "before") }) .await; + tokio::time::timeout(std::time::Duration::from_secs(60), arrived) + .await + .expect("parked worker arrival deadline") + .expect("parked worker requested its fixture"); + let run_id = gateway + .service() + .session_messages("telegram:fixture_bot:555:")[0]["run_id"] + .as_str() + .expect("parked run id") + .to_owned(); adapter.shutdown().await; gateway.cleanup(); @@ -2033,6 +2024,25 @@ async fn adapter_resume_releases_the_gate_so_new_messages_are_admitted() { .any(|text| text.starts_with("[failed]")) }) .await; + let terminal_seq = restored.service().run_events(&run_id).last().unwrap()["seq"] + .as_i64() + .expect("recovered terminal sequence"); + wait_until(std::time::Duration::from_secs(15), || { + restored + .persistence() + .unwrap() + .delivery_get( + "telegram:fixture_bot:555:", + &format!("telegram:run:{run_id}"), + ) + .unwrap()["rows"] + .as_array() + .is_some_and(|rows| { + rows.iter() + .any(|row| row.get(2).and_then(Value::as_i64) == Some(terminal_seq)) + }) + }) + .await; push_update( &state, json!({ @@ -2049,7 +2059,7 @@ async fn adapter_resume_releases_the_gate_so_new_messages_are_admitted() { }] }), ); - wait_until(std::time::Duration::from_secs(20), || { + wait_until(std::time::Duration::from_secs(60), || { state.sent_texts().iter().any(|text| text == "[done]") }) .await; @@ -3072,13 +3082,15 @@ async fn adapter_stops_polling_after_bounded_unauthorized_failures() { #[tokio::test] async fn adapter_stop_command_cancels_the_active_run() { let (base, state) = spawn_fixture().await; - let (port, _arrived, release_tx, holding) = spawn_holding_fixture(); - let (config, source) = holding_config_and_source(port); - push_update(&state, fixture_json("updates_dm.json")); + let mut start = fixture_json("updates_dm.json"); + start["result"][0]["message"]["text"] = json!("start"); + push_update(&state, start); let db = telegram_db_path("stop"); - let gateway = test_state(&source, &db, |_config| config.clone()); + // Keep a real guest invocation pending without an independent HTTP + // timeout competing with the stop command. + let gateway = test_state(CANCEL_ON_START_SOURCE, &db, |config| config); let adapter = spawn_adapter(gateway.clone(), test_config(&base)).await; - wait_until(std::time::Duration::from_secs(15), || { + wait_until(std::time::Duration::from_secs(45), || { state.draft_texts().iter().any(|text| text == "before") }) .await; @@ -3099,16 +3111,14 @@ async fn adapter_stop_command_cancels_the_active_run() { }] }), ); - wait_until(std::time::Duration::from_secs(15), || { + wait_until(std::time::Duration::from_secs(45), || { state .sent_texts() .iter() .any(|text| text.contains("Stopping the active run")) }) .await; - let _ = release_tx.send(()); - holding.join().expect("holding fixture"); - wait_until(std::time::Duration::from_secs(15), || { + wait_until(std::time::Duration::from_secs(45), || { state.sent_texts().iter().any(|text| text == "[stopped]") }) .await; @@ -3137,6 +3147,30 @@ async fn adapter_shutdown_is_bounded() { std::fs::remove_file(&db).expect("temporary db should be removed"); } +// The debug gateway prepares the bundled source and durable store before it +// binds. Keep that cold-start watchdog separate from presentation deadlines. +async fn fixture_gateway_address(captured: &Mutex>) -> String { + let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(180); + loop { + if let Some(address) = captured + .lock() + .expect("capture lock") + .iter() + .find_map(|line| { + line.split_once("listening on http://") + .map(|(_, address)| address.trim().to_owned()) + }) + { + return address; + } + assert!( + tokio::time::Instant::now() < deadline, + "the gateway must print its listening address" + ); + tokio::time::sleep(std::time::Duration::from_millis(25)).await; + } +} + #[tokio::test] async fn gateway_binary_runs_telegram_and_api_on_one_agent_service() { let (base, state) = spawn_fixture().await; @@ -3144,6 +3178,9 @@ async fn gateway_binary_runs_telegram_and_api_on_one_agent_service() { let fixture = BinaryFixture::create_in(&telegram_test_root(), "binary", ECHO_SOURCE); let bin = env!("CARGO_BIN_EXE_rustscript-agent-gateway"); let mut child = tokio::process::Command::new(bin) + // Keep operator config.yaml from overriding this fixture's source. + .env("HOME", &fixture.dir) + .env_remove("RUSTSCRIPT_AGENT_HOME") .env("RUSTSCRIPT_AGENT_GATEWAY_ADDR", "127.0.0.1:0") .env("RUSTSCRIPT_AGENT_ALLOW_ANONYMOUS", "1") .env("RUSTSCRIPT_AGENT_STATE_DB", fixture.db()) @@ -3180,10 +3217,13 @@ async fn gateway_binary_runs_telegram_and_api_on_one_agent_service() { } }); let state_for_obs = state.clone(); + let captured_obs = Arc::clone(&captured); let observation = std::panic::AssertUnwindSafe(async move { + fixture_gateway_address(&captured_obs).await; // The binary's Telegram adapter must process the queued update through - // the shared AgentService and render it to the fixture. - wait_until(std::time::Duration::from_secs(20), || { + // the shared AgentService and render it to the fixture. This bound + // includes cold admission and first-run preparation in the child. + wait_until(std::time::Duration::from_secs(120), || { state_for_obs .sent_texts() .iter() @@ -3307,6 +3347,9 @@ async fn gateway_binary_survives_telegram_startup_failure_and_serves_the_api() { let fixture = BinaryFixture::create_in(&telegram_test_root(), "binary-degraded", ECHO_SOURCE); let bin = env!("CARGO_BIN_EXE_rustscript-agent-gateway"); let mut child = tokio::process::Command::new(bin) + // Keep operator config.yaml from overriding this fixture's source. + .env("HOME", &fixture.dir) + .env_remove("RUSTSCRIPT_AGENT_HOME") .env("RUSTSCRIPT_AGENT_GATEWAY_ADDR", "127.0.0.1:0") .env("RUSTSCRIPT_AGENT_ALLOW_ANONYMOUS", "1") .env("RUSTSCRIPT_AGENT_STATE_DB", fixture.db()) @@ -3341,35 +3384,7 @@ async fn gateway_binary_survives_telegram_startup_failure_and_serves_the_api() { }); let captured_obs = Arc::clone(&captured); let observation = std::panic::AssertUnwindSafe(async move { - // The API must come up: poll the bounded capture until the listening line. - let address = { - let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(20); - let mut found = None; - loop { - { - let guard = captured_obs.lock().expect("capture lock"); - for line in guard.iter() { - if let Some(position) = line.find("listening on http://") { - found = Some( - line[position + "listening on http://".len()..] - .trim() - .to_string(), - ); - break; - } - } - } - if found.is_some() { - break; - } - assert!( - tokio::time::Instant::now() < deadline, - "the gateway must print its listening address" - ); - tokio::time::sleep(std::time::Duration::from_millis(25)).await; - } - found.expect("listening address") - }; + let address = fixture_gateway_address(&captured_obs).await; // The API is alive despite the telegram startup failure. let health = http_get(&address, "/health/detailed").await; assert!( diff --git a/tests/tools_pinned_commit_outcome.rs b/tests/tools_pinned_commit_outcome.rs index 589a7dd..9893bb9 100644 --- a/tests/tools_pinned_commit_outcome.rs +++ b/tests/tools_pinned_commit_outcome.rs @@ -54,23 +54,6 @@ fn temp_root_at(label: &str, nanos: u128) -> PathBuf { dir } -#[test] -fn temp_roots_with_same_label_and_timestamp_are_distinct() { - struct Root(PathBuf); - impl Drop for Root { - fn drop(&mut self) { - fs::remove_dir(&self.0).unwrap(); - } - } - - let nanos = 123_456_789; - let first = Root(temp_root_at("outcome", nanos)); - let second = Root(temp_root_at("outcome", nanos)); - assert_ne!(first.0, second.0); - assert!(first.0.is_dir()); - assert!(second.0.is_dir()); -} - struct Signer { pair: Ed25519KeyPair, key: PublisherKey,