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..4a8024c 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" diff --git a/docs/deployment.md b/docs/deployment.md index 9cbfc65..ae9fa0d 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 = "fc8b0c8179640a8ffe460a64b3690e35c2f7eb73", ... }` 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=fc8b0c8179640a8ffe460a64b3690e35c2f7eb73#fc8b0c8179640a8ffe460a64b3690e35c2f7eb73`. 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..b5bad8b 100644 --- a/rss/agent/main.rss +++ b/rss/agent/main.rss @@ -13,6 +13,24 @@ use agent_runtime; use json; use super::tools::dispatch as tools; use super::agent::guardian as guardian; +use super::agent::provider_body; + +/// The canonical committed tool result, as far as this harness projects it. +/// `data` is that result's own open payload and therefore its serialized text; +/// nothing else about it is re-shaped here. +struct HarnessToolFailure { + code: string, + message: string +} + +struct HarnessToolResult { + ok: bool, + content: string, + data: string, + truncated: bool, + artifacts: array, + error: Option +} // --------------------------------------------------------------------------- // Typed context accessors (same-module; defensive dynamic navigation) @@ -264,9 +282,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 +323,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) } } @@ -346,7 +395,7 @@ fn seed_messages(context: map, messages: array) -> array { // stay distinct everywhere else. fn native_tool_to_canonical(descriptor: map) -> map { let schema: map = descriptor["schema"]; - let schema_json: string = json::encode(schema); + let schema_json: string = json_text_of(schema); { name: ctx_string(descriptor, "name", ""), description: ctx_string(descriptor, "description", ""), @@ -454,8 +503,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 +526,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 +567,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 +584,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 +629,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 +663,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 +748,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,22 +812,32 @@ 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.guest_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" { + // The committed result is open JSON text; only the fields the public + // envelope needs are declared, and its own `data` payload stays text. + let decoded: HarnessToolResult = json::decode::(canonical_result); + let projected: map = { + 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: "" } } + }; + if projected.has("ok") { + result = tools::result_envelope(call, projected); + 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"); + result = tools::approval_failure(dispatch_input, "managed_outcome_invalid", "Native managed publication format is invalid"); } } } @@ -764,7 +865,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 +901,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 +945,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 +993,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 +1068,33 @@ 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 durable transcript crosses as text: the host parses it, bounds it + // and answers with a fixed envelope. The compacted message array cannot + // be materialized in the guest on this pin, so this loop keeps its own + // append-only transcript and uses the envelope for control only. + 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 +1103,18 @@ 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(); + 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 +1136,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 +1151,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..9b8df66 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 { @@ -162,7 +162,7 @@ fn failure(code: string, message: string) -> map { effective_selector: "", selected_name: "", generation: 0, - directory: null + directory: () } } @@ -171,7 +171,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: map, 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 { @@ -187,6 +187,8 @@ fn resolve(workspaces: map, selector: string, session_selector: string, selector if map_bool(fallback, "found", false) { let path: string = map_string(fallback, "path", ""); 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"}; } @@ -238,7 +240,8 @@ fn open_with_policy(context: map, allow_switch: bool) -> map { } 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 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 +252,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..ec63beb 100644 --- a/rss/auth/code_exchange_contract.rss +++ b/rss/auth/code_exchange_contract.rss @@ -20,7 +20,7 @@ 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", code_handle: Some(code), verifier_handle: Some(verifier)}; oauth::transport(&request, &usage) } @@ -28,18 +28,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..88e2391 100644 --- a/rss/auth/device_contract.rss +++ b/rss/auth/device_contract.rss @@ -15,7 +15,7 @@ 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) } @@ -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..cfcc867 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) } 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..dcad3ff 100644 --- a/rss/auth/timing_contract.rss +++ b/rss/auth/timing_contract.rss @@ -11,6 +11,6 @@ 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) } 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..5e115c9 100644 --- a/rss/config/path.rss +++ b/rss/config/path.rss @@ -2,55 +2,85 @@ 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 no_failure() -> AuthBridgeFailure { + let failure: AuthBridgeFailure = {code: "", cancelled: false, deadline_exceeded: false, revoked: false}; + failure } 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_codec.rss b/rss/llm/openai_responses_codec.rss index 0cd7085..017e743 100644 --- a/rss/llm/openai_responses_codec.rss +++ b/rss/llm/openai_responses_codec.rss @@ -26,7 +26,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, @@ -575,7 +575,7 @@ 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 body: map = json::decode::(body_text); responses_parse_body(body) } @@ -650,7 +650,7 @@ fn responses_output_calls(output: array) -> array { let args_text: string = types::request_string(item, "arguments"); let mut args: map = {}; if args_text != "" { - let decoded: map = json::decode(args_text); + let decoded: map = json::decode::(args_text); args = decoded; } calls[calls.length] = types::tool_call_new(id, name, args); @@ -671,7 +671,7 @@ fn responses_accumulated_calls(tools: array) -> array { let args_text: string = types::request_string(entry, "args"); let mut args: map = {}; if args_text != "" { - let decoded: map = json::decode(args_text); + let decoded: map = json::decode::(args_text); args = decoded; } calls[calls.length] = types::tool_call_new(id, name, args); @@ -703,7 +703,7 @@ fn responses_stop_reason(body: map, calls: array) -> string { /// recognisable error object yields the default `http_error` shape. The /// upstream message is never echoed beyond the provider's own `message` field. pub fn responses_parse_public_error(body_text: string, status: int) -> map { - let payload: map = json::decode(body_text); + 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) @@ -814,8 +814,8 @@ pub fn responses_build_public_body(request: map, model: string, stream: bool) -> 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")) @@ -831,7 +831,7 @@ fn responses_splice_tool_schemas(body: string, tools: array) -> string { 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); + let schema_json: string = json::encode::(schema); result = string_replace_literal( result, "\"" + tool_schema_marker(i) + "\"", @@ -857,18 +857,18 @@ fn responses_finish_body(request: map, head: string, stream: bool) -> string { 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}" + 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); + let encoded: string = json::encode::(index); "__RSS_RESPONSES_SCHEMA_" + encoded + "__" } @@ -882,14 +882,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 +957,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 +965,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 +979,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 => { @@ -993,7 +993,7 @@ fn responses_tool_success(part: map) -> string { result: types::request_map(part, "result"), truncated: types::request_bool(part, "truncated", false) }; - json::encode(output) + json::encode::(output) } /// One assistant message contributes its text as an `output_text` message item @@ -1004,7 +1004,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 +1020,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; @@ -1063,7 +1063,7 @@ fn responses_build_tools(tools: array) -> string { } let mut encoded: string = ""; if wire.length > 0 { - encoded = json::encode(wire); + encoded = json::encode::>(wire); } encoded } 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/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..5a3090e 100644 --- a/rss/providers/opaque_responses.rss +++ b/rss/providers/opaque_responses.rss @@ -25,14 +25,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"); @@ -84,8 +76,8 @@ fn run_responses_mode(context: map, force_stream: bool) -> map { 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,9 +88,22 @@ 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")) } @@ -120,33 +125,52 @@ fn responses_send_buffered(request: map, profile: map, context: map, model: stri 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} + } +} + +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 => { @@ -163,6 +187,56 @@ fn responses_parse_transport_response(response: TransportResponseEnvelope) -> ma } } +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(request: map, profile: map, context: map, model: string) -> map { let binding: ResponsesOpaqueHostHomeBinding = {home: context["host_home"]}; let intent: ResponsesOpaqueProviderIntent = { @@ -176,73 +250,73 @@ fn responses_send_stream(request: map, profile: map, context: map, model: string public_body: responses::responses_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: 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 { + let chunk: map = json::decode::(body); + state = responses::responses_push_chunk(state, chunk); + 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..d8c3548 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 { @@ -312,8 +325,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 +341,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), diff --git a/rss/tools/patch.rss b/rss/tools/patch.rss index 55409be..3f5fcb1 100644 --- a/rss/tools/patch.rss +++ b/rss/tools/patch.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; @@ -25,7 +42,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,7 +88,7 @@ fn succeed(content: string, data: map, truncated: bool, artifacts: array) -> map ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: artifacts } @@ -352,23 +369,61 @@ 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 => { "" } +} + +/// 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 +} + 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 +496,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 +872,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 +888,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(match meta.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured 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 +904,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(match read.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured 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 +974,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(match written.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured 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 +1029,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 = match prepared.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }; fail( types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "capability failed"), {} ) + } else if prepared.kind == "replay" => { + 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 +1048,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 = match committed.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured 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..7a2963b 100644 --- a/rss/tools/read_file.rss +++ b/rss/tools/read_file.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; @@ -25,7 +42,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,12 +84,53 @@ fn succeed(content: string, data: map, truncated: bool, artifacts: array) -> map ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: artifacts } } +/// 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 +} + fn fail_host(envelope: map) -> map { let error: map = types::map_map(envelope, "error"); let code: string = types::map_string(error, "code", "internal_error"); @@ -299,22 +357,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 +428,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 +511,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 +534,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 = match meta.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured 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 = match read.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured 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 +636,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(match prepared.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }) + } else if prepared.kind == "replay" => { + 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 +650,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(match committed.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }) } else => { result } diff --git a/rss/tools/search_files.rss b/rss/tools/search_files.rss index cd66c19..e902458 100644 --- a/rss/tools/search_files.rss +++ b/rss/tools/search_files.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; @@ -584,7 +601,7 @@ fn succeed(content: string, data: map, truncated: bool, artifacts: array) -> map ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: artifacts } @@ -600,8 +617,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,17 +675,53 @@ 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 => { "" } +} + +/// 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 +} + 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" } } } } @@ -899,7 +954,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 +988,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 +1079,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", ""), {}); @@ -1126,13 +1181,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(match prepared.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }) + } else if prepared.kind == "replay" => { + 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 +1195,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(match committed.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }) } else => { result } diff --git a/rss/tools/terminal.rss b/rss/tools/terminal.rss index 5649c4a..89ae017 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; @@ -490,11 +511,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 = { @@ -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/validate.rss b/rss/tools/validate.rss index 85b6489..4223f17 100644 --- a/rss/tools/validate.rss +++ b/rss/tools/validate.rss @@ -50,7 +50,7 @@ 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); + let encoded: string = json::encode::(schema); encoded.length } diff --git a/rss/tools/write_file.rss b/rss/tools/write_file.rss index 3f2148c..8d46843 100644 --- a/rss/tools/write_file.rss +++ b/rss/tools/write_file.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; @@ -25,7 +42,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,7 +88,7 @@ fn succeed(content: string, data: map, truncated: bool, artifacts: array) -> map ok: true, content: content, data: data, - error: null, + error: (), truncated: truncated, artifacts: artifacts } @@ -236,23 +253,61 @@ 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 => { "" } +} + +/// 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 +} + 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 +380,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 +454,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 +474,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(match written.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured 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 +517,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 = match prepared.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured error" } }; fail( types::map_string(error, "code", "internal_error"), types::map_string(error, "message", "capability failed"), {} ) + } else if prepared.kind == "replay" => { + 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 +536,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 = match committed.error { Some(failure) => { code: failure.code, message: failure.message }, None => { code: "internal_error", message: "capability reported a failure without a structured 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..9427e6a 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; @@ -1033,6 +1034,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 +1063,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 +1081,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 +1095,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 +1142,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 +1153,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 +1190,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 +1221,7 @@ fn config_envelope() -> HostTypeSchema { ) } fn metadata_envelope() -> HostTypeSchema { - envelope( + fixed_envelope( "AuthMetadataEnvelope", vec![ ("metadata", metadata_type()), @@ -1219,10 +1230,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 +1248,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 +1279,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 +1430,7 @@ fn close_access_desc() -> HostFunctionDescriptor { key(Access::KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_access, ) } @@ -1426,7 +1442,7 @@ fn close_refresh_desc() -> HostFunctionDescriptor { key(Refresh::KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_refresh, ) } @@ -1438,7 +1454,7 @@ fn close_slot_desc() -> HostFunctionDescriptor { key(SecretSlot::KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_slot, ) } @@ -1576,7 +1592,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,7 +1612,14 @@ fn config_call(vm: &mut Vm, args: &[Value]) -> VmResult { Value::Int(owner.snapshot.allowed.len() as i64), )]), ), - ])) + ]); + // 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) } fn optional_field<'a>(value: &'a Value, name: &str) -> VmResult> { let Value::Map(entries) = value else { @@ -1608,6 +1631,44 @@ fn optional_field<'a>(value: &'a Value, name: &str) -> VmResult>`: 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` (exactly what `optional_field` +/// did), 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")); + }; + 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 { fn token(value: &str) -> bool { !value.is_empty() @@ -1741,11 +1802,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 +2148,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 +2167,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 +2235,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)?; @@ -2250,7 +2329,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 +2346,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 +2375,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 +2425,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 +2466,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 +2526,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 +2544,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 +2575,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 +2727,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 +2742,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 +3001,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..d4e6794 100644 --- a/src/auth/host/device.rs +++ b/src/auth/host/device.rs @@ -117,7 +117,7 @@ pub(super) fn close_desc() -> HostFunctionDescriptor { key(DEVICE_KEY), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, close_call, ) } diff --git a/src/auth/host/fallible.rs b/src/auth/host/fallible.rs index 87d4fe0..0ed9c3f 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", @@ -384,8 +388,8 @@ 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 +530,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 +557,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 +643,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 +795,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 +844,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 +860,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 +900,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..92d22d5 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,19 @@ 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)), - ]), + ])), ), - ("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..e1ab1a0 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,16 @@ 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) { + bind_open_fixture(host, compile_open_fixture()) + } + + fn bind_open_fixture(host: &AuthHost, program: Arc) -> (Vm, Value) { + let mut vm = Vm::new_shared(program); + let catalog = auth_host_catalog(); let mut registry = HostFunctionRegistry::restricted(); host.register(&mut registry, catalog.as_ref()) .expect("install production auth/workspace host"); @@ -762,7 +780,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 +972,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 +1003,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 +1026,17 @@ mod tests { #[test] fn workspace_grant_expires_with_its_finite_owner_deadline() { let fixture = Fixture::new("deadline"); + // Compilation is fixture setup, not part of the finite owner lease. + let program = compile_open_fixture(); 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); + let host = AuthHost::new(fixture.home.clone(), cancellation.clone()) + .expect("empty-credential host"); + let (mut vm, home) = bind_open_fixture(&host, program); 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..9c83e66 100644 --- a/src/effects/broker.rs +++ b/src/effects/broker.rs @@ -1407,21 +1407,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 +1471,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)); 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..5824640 100644 --- a/src/gateway/store.rs +++ b/src/gateway/store.rs @@ -1347,6 +1347,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 +1786,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..645b43d 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, @@ -3047,7 +3107,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 +3127,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 +3818,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 +3920,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 +3933,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 +3946,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 +3991,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 +4029,8 @@ impl AgentHostState { "ok": true, "kind": "clock_monotonic", "ms": ms, + "code": "", + "message": "", }), _ => capability_error_envelope(&CapabilityError::new( "internal_error", @@ -3977,7 +4055,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 +4067,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 +4080,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 +4252,230 @@ 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 request = match parse_open_payload_text(text) { + Ok(request) => request, + Err((code, message)) => return provider_failure(vm, code, message), + }; + // 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)) +} + +/// Projects the refresh answer onto the fixed `AgentRefreshEnvelope`. +/// +/// The durable answer also carries a compacted message array; this pin has no +/// open-array host return, so only the fixed fields cross (see +/// `host_types::AgentRefreshEnvelope`). Nothing is defaulted: a refresh that +/// reports `storage_available` without `ok` still publishes its typed refusal. +fn refresh_envelope(vm: &mut Vm, answer: &JsonValue) -> VmResult { + let ok = answer.get("ok") == Some(&JsonValue::Bool(true)); + let storage_available = answer + .get("storage_available") + .and_then(JsonValue::as_bool) + .unwrap_or(false); + 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)), + ]); + 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 +4516,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 +4794,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 +4938,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 +4968,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,9 +5120,51 @@ 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)) + 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), + ) +} + +/// 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")); + out.insert("result".into(), text("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 @@ -4431,14 +5243,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 +5273,16 @@ fn pinned_outcome( message: &str, canonical_result: &str, ) -> JsonValue { + // Keep the durable receipt unchanged. The typed guest can decode its open + // `data` object only when that leaf crosses as JSON text. + let guest_result = serde_json::from_str::(canonical_result) + .ok() + .and_then(|mut result| { + let data = result.as_object_mut()?.get_mut("data")?; + *data = JsonValue::String(serde_json::to_string(data).ok()?); + serde_json::to_string(&result).ok() + }) + .unwrap_or_default(); json!({ "ok": ok, "text": content, @@ -4457,6 +5291,7 @@ fn pinned_outcome( "code": code, "message": message, "canonical_result": canonical_result, + "guest_result": guest_result, }) } @@ -4511,6 +5346,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 +5357,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 +5565,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 +5587,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 +5643,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 +5749,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 +5838,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 +5900,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 +6066,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 +6221,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 +6301,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 +6418,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 +6547,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 +6641,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 +6666,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 +6693,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 +6742,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 +7346,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..229ee32 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; @@ -546,20 +570,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 +617,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 +635,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 +711,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..9aa70b3 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,83 @@ named_struct!( ] ); +// `agent::refresh_context`. The guest sends its transcript as serialized text +// and gets a *fixed* envelope back: byte/UTF-8/object checks stay in the host. +// +// The durable transcript refresh also produces a compacted message array, but +// this pin admits no open-array host return, and the agent loop cannot append +// to a remotely materialized array. So the refreshed transcript is **not** +// carried back across the boundary: the loop keeps its own append-only +// transcript and only `ok`/`storage_available` (and the typed refusal) cross. +named_struct!( + AgentRefreshEnvelope, + "AgentRefreshEnvelope", + [ + ("ok", HostTypeSchema::Bool), + ("storage_available", HostTypeSchema::Bool), + ("code", HostTypeSchema::String), + ("message", 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 +189,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 +221,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 +249,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 +261,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 +294,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 +305,7 @@ named_struct!( [ ("ok", HostTypeSchema::Bool), ("kind", HostTypeSchema::String), - ("error", optional(named::())) + ("error", option(named::())) ] ); @@ -203,21 +315,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 +474,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 +492,14 @@ named_struct!( ("code", HostTypeSchema::String), ("message", HostTypeSchema::String), ("canonical_result", HostTypeSchema::String), + ("guest_result", HostTypeSchema::String), ( "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..bd82458 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()), @@ -1860,16 +1889,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 +1961,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 @@ -2313,8 +2333,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(()) } } @@ -3215,7 +3245,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 +3259,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 +3276,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 +3297,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..cc0f5fe 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"))?; 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..bc9af2f 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, "被拒绝的回放不得提交第二个结果"); @@ -3836,8 +3840,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 +3870,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 +4201,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 +4231,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 +4470,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 +4490,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 +5764,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 +5834,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..76dcbfe 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!( @@ -869,6 +869,9 @@ async fn unsettled(seconds: u64, mode: &'static str) { config.run_timeout = Duration::from_secs(seconds); }); let service = state.service(); + // Compile fixture code before starting the admitted run's finite clock; + // the provider still runs under that unchanged admission clock. + service.materialize_cached_runner().unwrap(); let provider = Arc::new(ProviderBarrier::default()); let release = ProviderRelease(provider.clone()); service.inject_provider_host(provider.clone()); @@ -877,13 +880,19 @@ async fn unsettled(seconds: u64, mode: &'static str) { let original = bound.cancel.clone(); let context = service.run_context(&run.run_id).unwrap(); let request = checkpoint_request(&bound); + let rt = service.managed_runtime_for_test().unwrap(); + let before = rt + .repository + .checkpoint_context(&request, &read_clock()) + .unwrap(); 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 { + // 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(30), async { while !provider.entered.load(std::sync::atomic::Ordering::Acquire) { tokio::time::sleep(Duration::from_millis(2)).await; } @@ -894,14 +903,13 @@ async fn unsettled(seconds: u64, mode: &'static str) { 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 checkpoint = tokio::spawn(async move { + let result = s.create_managed_checkpoint(req).await; + (result, started.elapsed()) + }); let probe = service.clone(); let r = rt.clone(); let branch = bound.admitted_branch.branch_id.clone(); @@ -918,7 +926,7 @@ async fn unsettled(seconds: u64, mode: &'static str) { } }) .await; - let result = tokio::time::timeout(Duration::from_secs(6), checkpoint).await; + let result = tokio::time::timeout(Duration::from_secs(30), checkpoint).await; drop(release); let worker_result = worker.await; let after = rt @@ -939,8 +947,8 @@ async fn unsettled(seconds: u64, mode: &'static str) { std::panic::resume_unwind(error.into_panic()); } 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 +965,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 +1022,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 +1032,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 +1278,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 +1339,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 +1356,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,9 +1376,13 @@ 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(); let s = service.clone(); let req = request.clone(); + // 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 { s.create_managed_checkpoint(req).await }); 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"}})))); @@ -1412,7 +1427,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 +1443,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()), @@ -1523,6 +1540,7 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { }) .await; let premature = checkpoint.is_finished(); + let original_worker_pending = !worker.is_finished(); gate.release(); let worker_result = worker.await; let checkpoint_result = checkpoint.await; @@ -1532,6 +1550,37 @@ async fn checkpoint_waits_for_whole_tool_batch_complete() { .repository .checkpoint_context(&request, &read_clock()) .unwrap(); + let receipt = match checkpoint_result.unwrap() { + Ok(receipt) => { + assert!( + !premature, + "no mixed checkpoint before second durable output" + ); + receipt + } + Err(error) => { + // Under parallel load the management clock can expire while the + // independent workspace runs. A refusal is therefore legal, but + // only once that clock has run out: bailing out early would skip + // the wait this test exists to prove. + assert_eq!(error.to_string(), "managed_checkpoint_busy"); + assert!( + checkpoint_started.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" + ); + 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 +1597,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..0b19327 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 @@ -1852,7 +1854,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 +2044,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 +2138,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 +2156,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 +2187,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 +2610,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 +2630,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 +2758,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 +2774,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 +3114,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 +3407,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 +3498,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 +3542,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 +3575,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 +3606,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 +3635,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 +3666,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 +4002,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 +4378,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 +4874,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 +5795,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 +6047,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 +6187,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 +6283,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 +6349,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 +6364,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..058a2cb 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); 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/auth_enrollment_host_tests.rs b/tests/auth_enrollment_host_tests.rs index 280fbb9..2469905 100644 --- a/tests/auth_enrollment_host_tests.rs +++ b/tests/auth_enrollment_host_tests.rs @@ -63,6 +63,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)) } @@ -292,7 +303,7 @@ fn exchange( ); 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); @@ -338,8 +349,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(); @@ -382,9 +399,12 @@ 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!(some_slot(field(&exchanged, "refresh_slot")), Value::Int(_)), refresh ); assert_eq!( diff --git a/tests/authorization_instruction_rss_tests.rs b/tests/authorization_instruction_rss_tests.rs index 0eb993f..553d514 100644 --- a/tests/authorization_instruction_rss_tests.rs +++ b/tests/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/callback_result_rss_tests.rs b/tests/callback_result_rss_tests.rs index 6ea3249..8ee1b18 100644 --- a/tests/callback_result_rss_tests.rs +++ b/tests/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)] @@ -753,7 +764,7 @@ fn native_callback_results_preserve_live_family() { ); let wait_value = field(&waited, "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")); diff --git a/tests/config_auth_rss_tests.rs b/tests/config_auth_rss_tests.rs index d8146e7..f912610 100644 --- a/tests/config_auth_rss_tests.rs +++ b/tests/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/dependency_pin_tests.rs b/tests/dependency_pin_tests.rs index 30d067b..19b8897 100644 --- a/tests/dependency_pin_tests.rs +++ b/tests/dependency_pin_tests.rs @@ -13,11 +13,11 @@ use std::path::PathBuf; const RUSTSCRIPT_GIT: &str = "https://github.com/rustscript-lang/rustscript.git"; -const RUSTSCRIPT_REV: &str = "b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390"; +const RUSTSCRIPT_REV: &str = "fc8b0c8179640a8ffe460a64b3690e35c2f7eb73"; const STALE_REV: &str = "b1d6cffede77f49410bf63525f30b9a46b02dc01"; -const PRIOR_SDK_REV: &str = "d9387f98df78dd88d2d1c04eebac4415f844efaf"; -const ABBREVIATED_REV: &str = "b5066dd"; -const PRIOR_ABBREVIATED_REV: &str = "d9387f9"; +const PRIOR_SDK_REV: &str = "b5066dd92fcd7c07df57e3ec2c29dbcf8c19c390"; +const ABBREVIATED_REV: &str = "fc8b0c8"; +const PRIOR_ABBREVIATED_REV: &str = "b5066dd"; fn manifest() -> String { std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml")) @@ -162,8 +162,8 @@ fn pd_vm_and_pd_host_function_lock_sources_are_canonical_https_at_the_pinned_rev 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 + // `fc8b0c8` is a prefix of the reviewed full SHA, so a bare substring + // `rev=fc8b0c8` 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. 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/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_callback_rss_tests.rs b/tests/native_callback_rss_tests.rs index 686d9c8..c1c50ce 100644 --- a/tests/native_callback_rss_tests.rs +++ b/tests/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")); @@ -734,6 +734,17 @@ fn native_owner_revoke_and_vm_reset_close_callback_listener() { } #[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. +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") +} + #[test] fn native_owner_does_not_enable_fixture_callback_injection() { let home = FreshLoginHome::new("native-fixture-injection"); diff --git a/tests/oauth_callback_bridge_tests.rs b/tests/oauth_callback_bridge_tests.rs index ad227fe..447c996 100644 --- a/tests/oauth_callback_bridge_tests.rs +++ b/tests/oauth_callback_bridge_tests.rs @@ -187,6 +187,17 @@ struct QuotaResource; 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. +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 fill_resource_arena(vm: &mut Vm, target: usize) -> Vec { let mut handles = Vec::new(); while vm.host_context().resource_count() < target { @@ -417,13 +428,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, @@ -596,7 +609,8 @@ fn restricted_real_rss_waits_once_and_callback_close_preserves_siblings() { let waited = invoke(&mut vm, "wait", std::slice::from_ref(&callback)); assert_eq!(field(&waited, "outcome"), Value::string("success")); let code = field(&waited, "code_handle"); - assert!(matches!(code, Value::Int(_))); + assert!(matches!(some_slot(&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")); @@ -1578,7 +1592,8 @@ fn owner_cancel_during_rss_browser_begin(revoke_owner: bool, name: &str) { let waited_b = invoke(&mut vm_b, "wait", std::slice::from_ref(&callback_b)); assert_eq!(field(&waited_b, "outcome"), Value::string("success")); let code_b = field(&waited_b, "code_handle"); - assert!(matches!(code_b, Value::Int(_))); + assert!(matches!(some_slot(&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/oauth_capture_bridge_tests.rs index 5ef2a9a..a0b6188 100644 --- a/tests/oauth_capture_bridge_tests.rs +++ b/tests/oauth_capture_bridge_tests.rs @@ -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")) )); diff --git a/tests/oauth_code_exchange_bridge_tests.rs b/tests/oauth_code_exchange_bridge_tests.rs index cc66943..7d65474 100644 --- a/tests/oauth_code_exchange_bridge_tests.rs +++ b/tests/oauth_code_exchange_bridge_tests.rs @@ -189,8 +189,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, diff --git a/tests/oauth_device_bridge_tests.rs b/tests/oauth_device_bridge_tests.rs index 754f4a6..f7d85af 100644 --- a/tests/oauth_device_bridge_tests.rs +++ b/tests/oauth_device_bridge_tests.rs @@ -56,6 +56,17 @@ 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 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 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())); @@ -237,8 +248,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 +263,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, @@ -338,7 +349,7 @@ fn real_rss_start_pending_poll_and_token_slots_save_with_frozen_generation() { Value::string("client_id=public-device-client"), ], ); - let device = field(&started, "device_handle").clone(); + let device = some_slot(field(&started, "device_handle")); assert!( matches!(device, Value::Int(_)), "typed opaque device resource" @@ -388,8 +399,8 @@ fn real_rss_start_pending_poll_and_token_slots_save_with_frozen_generation() { let authorized = invoke(&mut vm, "poll", &poll_args()); assert_eq!(field(&authorized, "status"), &Value::Int(200)); - let access = field(&authorized, "access_slot").clone(); - let refresh = field(&authorized, "refresh_slot").clone(); + let access = some_slot(field(&authorized, "access_slot")); + let refresh = some_slot(field(&authorized, "refresh_slot")); assert!(matches!(access, Value::Int(_)) && matches!(refresh, Value::Int(_))); for secret in [DEVICE_CODE, ACCESS, REFRESH] { assert!(!format!("{started:?}{pending:?}{authorized:?}").contains(secret)); diff --git a/tests/oauth_payload_bridge_tests.rs b/tests/oauth_payload_bridge_tests.rs index 45f2b50..cef68bd 100644 --- a/tests/oauth_payload_bridge_tests.rs +++ b/tests/oauth_payload_bridge_tests.rs @@ -48,6 +48,17 @@ 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 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 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"); @@ -294,7 +305,7 @@ fn real_rss_selected_device_auth_id_json_poll_injects_native_secret() { ); let started = call(&mut vm, "start_device", std::slice::from_ref(&policy)); 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(_))); let public_start = format!("{started:?}"); assert!( diff --git a/tests/oauth_response_sanitizer_rss_tests.rs b/tests/oauth_response_sanitizer_rss_tests.rs index acf2a6e..e293d4d 100644 --- a/tests/oauth_response_sanitizer_rss_tests.rs +++ b/tests/oauth_response_sanitizer_rss_tests.rs @@ -1,3 +1,6 @@ +#[path = "support/credential_use.rs"] +mod credential_use_support; + use std::{fs, path::PathBuf, sync::Arc, time::Duration}; use rustscript_agent::{ @@ -147,14 +150,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/oauth_rss_entry_tests.rs index b87b37a..6116ffc 100644 --- a/tests/oauth_rss_entry_tests.rs +++ b/tests/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 diff --git a/tests/oauth_timing_bridge_tests.rs b/tests/oauth_timing_bridge_tests.rs index bf759c0..24ab280 100644 --- a/tests/oauth_timing_bridge_tests.rs +++ b/tests/oauth_timing_bridge_tests.rs @@ -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/oauth_transport_bridge_tests.rs index c75db86..761bf93 100644 --- a/tests/oauth_transport_bridge_tests.rs +++ b/tests/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); @@ -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"); @@ -1445,6 +1466,18 @@ fn copied_cross_vm_policy_is_denied_before_transport() { } #[cfg(feature = "config-fixture")] +/// 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") +} + #[test] fn partial_slot_issue_rolls_back_first_slot() { let mut vm = vm(); diff --git a/tests/pkce_login_rss_tests.rs b/tests/pkce_login_rss_tests.rs index 4d52eb5..e31edc5 100644 --- a/tests/pkce_login_rss_tests.rs +++ b/tests/pkce_login_rss_tests.rs @@ -551,6 +551,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/provider_openai_opaque_tests.rs b/tests/provider_openai_opaque_tests.rs index 45c0bab..519eb47 100644 --- a/tests/provider_openai_opaque_tests.rs +++ b/tests/provider_openai_opaque_tests.rs @@ -219,7 +219,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 +279,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_stream_codec_rss_tests.rs b/tests/provider_stream_codec_rss_tests.rs index 2b49c9b..c1e380f 100644 --- a/tests/provider_stream_codec_rss_tests.rs +++ b/tests/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/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/telegram_tests.rs b/tests/telegram_tests.rs index 432102b..2970b52 100644 --- a/tests/telegram_tests.rs +++ b/tests/telegram_tests.rs @@ -1448,11 +1448,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 +1465,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 +1501,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 +1878,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 +1986,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 +2019,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 +2034,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 +2062,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 +2097,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 +3120,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 +3149,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 +3185,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 +3216,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 +3255,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 +3385,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 +3422,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/zz_agg_tests.rs b/tests/zz_agg_tests.rs new file mode 100644 index 0000000..0adcc6b --- /dev/null +++ b/tests/zz_agg_tests.rs @@ -0,0 +1,61 @@ +//! Temporary diagnostic: aggregate bundled corpus compile results. + +use std::fs; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use rustscript_vm::{ + CompileSourceFileOptions, SourceFlavor, compile_source_at_path_with_flavor_and_options, + host_api::HostApiCatalog, +}; + +fn collect_rss(dir: &Path, out: &mut Vec) { + let Ok(entries) = fs::read_dir(dir) else { + return; + }; + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() { + collect_rss(&path, out); + } else if path.extension().and_then(|ext| ext.to_str()) == Some("rss") { + out.push(path); + } + } +} + +#[test] +fn aggregate_corpus() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let mut files = Vec::new(); + collect_rss(&root.join("rss"), &mut files); + collect_rss(&root.join("examples"), &mut files); + files.sort(); + let catalog: Arc = rustscript_agent::agent_host_catalog(); + let mut failures: Vec = Vec::new(); + for path in &files { + let relative = path + .strip_prefix(&root) + .unwrap_or(path) + .display() + .to_string(); + let source = fs::read_to_string(path).expect("read source"); + let options = CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()); + if let Err(error) = compile_source_at_path_with_flavor_and_options( + path, + &source, + SourceFlavor::RustScript, + options, + ) { + failures.push(format!("{relative} :: {error}")); + } + } + for failure in &failures { + println!("FAIL {failure}"); + } + println!( + "TOTAL {} OK {} FAIL {}", + files.len(), + files.len() - failures.len(), + failures.len() + ); +}