Repository navigation
refactor(store): read the pack baseline slot in the native store #8340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
internet-dot
wants to merge
46
commits into
main
Choose a base branch
from
feat/native-runtime-slice50
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
44486eb
refactor(store): read the command-pack activation slot in the native …
internet-dot eecc8f9
fix(store): refuse a non-text activation slot state
internet-dot 4655765
fix(store): share the schema marker check between the authority write…
internet-dot e46f559
refactor(store): check the extension control schema marker in the nat…
internet-dot da4f585
refactor(store): name a refused activation slot's error code in the n…
internet-dot f21bbdd
Merge remote-tracking branch 'origin/feat/native-runtime-slice42' int…
internet-dot 641ef32
chore(contracts): map the extension control schema reader in the owne…
internet-dot 75d0b3c
refactor(store): read the committed extension control snapshot in the…
internet-dot a458375
refactor(store): resume transitions and read the upgrade's activation…
internet-dot da84eed
refactor(store): read the cache cleanup's activation records in the n…
internet-dot 5b33acb
refactor(store): read the pack migration's legacy evidence in the nat…
internet-dot 5fd5359
refactor(store): read the extension control head for recovery in the …
internet-dot e918ad2
test(store): expect the snapshot layers read on the snapshot route
internet-dot ec4ca4f
Merge branch 'feat/native-runtime-slice46' into feat/native-runtime-s…
internet-dot 7591d83
Merge branch 'feat/native-runtime-slice47' into feat/native-runtime-s…
internet-dot 7331033
test(store): expect the new native reads on the snapshot route
internet-dot 534b718
Merge branch 'feat/native-runtime-slice48' into feat/native-runtime-s…
internet-dot b51fd89
test(store): expect the new native reads on the snapshot route
internet-dot 612e982
test(store): expect the new native reads on the snapshot route
internet-dot 65af8ba
refactor(store): read catalog migration events and the pack baseline …
internet-dot ca4fdab
Merge remote-tracking branch 'origin/main' into feat/native-runtime-s…
internet-dot 5a772eb
fix(store): read the snapshot on the shared connection inside an MCP …
internet-dot 95b5127
fix(store): read the extension control head on the shared connection …
internet-dot 3205f81
Merge branch 'feat/native-runtime-slice46' into feat/native-runtime-s…
internet-dot 91cd8c8
Merge branch 'feat/native-runtime-slice47' into feat/native-runtime-s…
internet-dot b21a067
Merge branch 'feat/native-runtime-slice48' into feat/native-runtime-s…
internet-dot a891ce5
Merge branch 'feat/native-runtime-slice49' into feat/native-runtime-s…
internet-dot 651c53c
fix(store): budget legacy history pages on the encoded reply and stop…
internet-dot 20295bf
fix(store): answer the scoped extension control head read as the nati…
internet-dot 3e0f05e
Merge remote-tracking branch 'origin/feat/native-runtime-slice48' int…
internet-dot 73bb6a0
Merge remote-tracking branch 'origin/feat/native-runtime-slice49' int…
internet-dot 2864e9c
docs(guard): record the scoped extension control reads as a temporary…
internet-dot e3f414f
Merge remote-tracking branch 'origin/feat/native-runtime-slice49' int…
internet-dot 6b624ed
fix(store): let an unusable anchor revision select no legacy pending row
internet-dot 0297abf
Merge remote-tracking branch 'origin/feat/native-runtime-slice48' int…
internet-dot 8ffd933
Merge remote-tracking branch 'origin/feat/native-runtime-slice49' int…
internet-dot 667a34a
test(store): answer the pending-record list in the activation fixture…
internet-dot 3ae77a5
Merge remote-tracking branch 'origin/feat/native-runtime-slice47' int…
internet-dot d199c1a
Merge remote-tracking branch 'origin/feat/native-runtime-slice48' int…
internet-dot b216d67
Merge remote-tracking branch 'origin/feat/native-runtime-slice49' int…
internet-dot 1ec6934
Merge remote-tracking branch 'origin/main' into feat/native-runtime-s…
internet-dot 6e1db5c
Merge remote-tracking branch 'origin/main' into feat/native-runtime-s…
internet-dot d673b1d
fix(store): page the pending command-pack records so cleanup reads ev…
internet-dot c6d2630
Merge remote-tracking branch 'origin/main' into feat/native-runtime-s…
internet-dot 2a7b288
Merge branch 'feat/native-runtime-slice49' of https://github.com/hash…
internet-dot 160c681
merge(main): resolve command pack slot conflicts
internet-dot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor(store): read the extension control head for recovery in the …
…native store
- Loading branch information
commit 5fd53591d70d27f89e9497f95d9ccafd69edafbd
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
rust/crates/guard-runtime/src/guard_store_extension_control_head.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| //! The committed extension-control head and its transitions, read for recovery. | ||
| //! | ||
| //! Port of the SQL reads the authority recovery, the persisted-authority | ||
| //! reader and the legacy secret migration check ran on their own SQLite | ||
| //! connection after the schema check: | ||
| //! | ||
| //! - `extension_control_snapshot_head` answers the snapshot row's revision, | ||
| //! snapshot digest and catalog digest, or `{"snapshot": null}` when there | ||
| //! is no row (or no snapshot table yet). A cell of the wrong type answers | ||
| //! `null` in its place so the caller refuses the row. | ||
| //! - `extension_control_recovery_transition` answers the revision of the | ||
| //! transition recovery must resume: one at the head revision that never | ||
| //! committed, or the one after it. | ||
| //! - `extension_control_transition_committed` answers whether the head | ||
| //! revision's transition committed. | ||
| //! | ||
| //! A store without the transition table has no transitions. | ||
|
|
||
| use rusqlite::Connection; | ||
| use serde_json::{json, Value}; | ||
|
|
||
| use crate::guard_store_args::Args; | ||
| use crate::guard_store_db::{query_one, StoreResult}; | ||
| use crate::guard_store_local_cli_inventory::table_exists; | ||
|
|
||
| const HEAD: &str = "extension_control_snapshot_head"; | ||
| const RECOVERY: &str = "extension_control_recovery_transition"; | ||
| const COMMITTED: &str = "extension_control_transition_committed"; | ||
| const SNAPSHOT_TABLE: &str = "extension_control_authority_snapshot"; | ||
| const TRANSITION_TABLE: &str = "extension_control_authority_transition"; | ||
| const COMMITTED_PHASE: &str = "committed"; | ||
|
|
||
| pub(crate) fn handles(method: &str) -> bool { | ||
| matches!(method, HEAD | RECOVERY | COMMITTED) | ||
| } | ||
|
|
||
| pub(crate) fn dispatch( | ||
| connection: &Connection, | ||
| method: &str, | ||
| args: &Args<'_>, | ||
| ) -> StoreResult<Value> { | ||
| match method { | ||
| RECOVERY => recovery_transition(connection, args.int("revision")?), | ||
| COMMITTED => committed(connection, args.int("revision")?), | ||
| _ => head(connection), | ||
| } | ||
| } | ||
|
|
||
| fn head(connection: &Connection) -> StoreResult<Value> { | ||
| if !table_exists(connection, SNAPSHOT_TABLE)? { | ||
| return Ok(json!({"snapshot": null})); | ||
| } | ||
| let row = query_one( | ||
| connection, | ||
| &format!( | ||
| "select case when typeof(revision) = 'integer' then revision end as revision, \ | ||
| case when typeof(snapshot_digest) = 'text' then snapshot_digest end \ | ||
| as snapshot_digest, \ | ||
| case when typeof(catalog_digest) = 'text' then catalog_digest end \ | ||
| as catalog_digest \ | ||
| from {SNAPSHOT_TABLE} where singleton = 1" | ||
| ), | ||
| &[], | ||
| )?; | ||
| Ok(match row { | ||
| None => json!({"snapshot": null}), | ||
| Some(row) => { | ||
| let cell = |name: &str| row.get(name).cloned().unwrap_or(Value::Null); | ||
| json!({"snapshot": { | ||
| "revision": cell("revision"), | ||
| "snapshot_digest": cell("snapshot_digest"), | ||
| "catalog_digest": cell("catalog_digest"), | ||
| }}) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| fn recovery_transition(connection: &Connection, revision: i64) -> StoreResult<Value> { | ||
| if !table_exists(connection, TRANSITION_TABLE)? { | ||
| return Ok(json!({"revision": null})); | ||
| } | ||
| let row = query_one( | ||
| connection, | ||
| &format!( | ||
| "select revision from {TRANSITION_TABLE} \ | ||
| where (revision = ?1 and phase != ?2) or revision = ?3 \ | ||
| order by revision limit 1" | ||
| ), | ||
| &[ | ||
| Value::from(revision), | ||
| Value::from(COMMITTED_PHASE), | ||
| Value::from(revision.saturating_add(1)), | ||
| ], | ||
| )?; | ||
| let found = row | ||
| .and_then(|row| row.get("revision").filter(|value| value.is_i64()).cloned()) | ||
| .unwrap_or(Value::Null); | ||
| Ok(json!({"revision": found})) | ||
| } | ||
|
|
||
| fn committed(connection: &Connection, revision: i64) -> StoreResult<Value> { | ||
| if !table_exists(connection, TRANSITION_TABLE)? { | ||
| return Ok(json!({"committed": false})); | ||
| } | ||
| let row = query_one( | ||
| connection, | ||
| &format!("select 1 as found from {TRANSITION_TABLE} where revision = ?1 and phase = ?2"), | ||
| &[Value::from(revision), Value::from(COMMITTED_PHASE)], | ||
| )?; | ||
| Ok(json!({"committed": row.is_some()})) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "guard_store_extension_control_head_tests.rs"] | ||
| mod tests; |
165 changes: 165 additions & 0 deletions
165
rust/crates/guard-runtime/src/guard_store_extension_control_head_tests.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| use rusqlite::{params, Connection}; | ||
| use serde_json::{json, Map, Value}; | ||
|
|
||
| use super::{dispatch, handles}; | ||
| use crate::guard_store_args::Args; | ||
| use crate::guard_store_op_kind::{method_kind, Kind}; | ||
|
|
||
| const METHODS: [&str; 3] = [ | ||
| "extension_control_snapshot_head", | ||
| "extension_control_recovery_transition", | ||
| "extension_control_transition_committed", | ||
| ]; | ||
|
|
||
| fn store() -> Connection { | ||
| Connection::open_in_memory().expect("memory store") | ||
| } | ||
|
|
||
| fn schema(connection: &Connection) { | ||
| connection | ||
| .execute_batch( | ||
| "create table extension_control_authority_snapshot ( \ | ||
| singleton integer primary key, revision, catalog_digest, snapshot_digest); \ | ||
| create table extension_control_authority_transition ( \ | ||
| revision integer primary key, phase text not null);", | ||
| ) | ||
| .expect("schema"); | ||
| } | ||
|
|
||
| fn transition(connection: &Connection, revision: i64, phase: &str) { | ||
| connection | ||
| .execute( | ||
| "insert into extension_control_authority_transition values (?1, ?2)", | ||
| params![revision, phase], | ||
| ) | ||
| .expect("transition"); | ||
| } | ||
|
|
||
| fn call(connection: &Connection, method: &str, args: Value) -> Value { | ||
| let Value::Object(map) = args else { | ||
| panic!("object args"); | ||
| }; | ||
| dispatch(connection, method, &Args(&map)).expect("read") | ||
| } | ||
|
|
||
| fn head(connection: &Connection) -> Value { | ||
| call(connection, "extension_control_snapshot_head", json!({})) | ||
| } | ||
|
|
||
| fn recovery(connection: &Connection, revision: i64) -> Value { | ||
| call( | ||
| connection, | ||
| "extension_control_recovery_transition", | ||
| json!({"revision": revision}), | ||
| ) | ||
| } | ||
|
|
||
| fn committed(connection: &Connection, revision: i64) -> Value { | ||
| call( | ||
| connection, | ||
| "extension_control_transition_committed", | ||
| json!({"revision": revision}), | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn every_read_is_routed_as_a_read() { | ||
| for method in METHODS { | ||
| assert!(handles(method)); | ||
| assert!(matches!(method_kind(method), Some(Kind::Read))); | ||
| } | ||
| assert!(!handles("extension_control_snapshot_layers")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_store_without_tables_has_no_head_and_no_transitions() { | ||
| let connection = store(); | ||
| assert_eq!(head(&connection), json!({"snapshot": null})); | ||
| assert_eq!(recovery(&connection, 3), json!({"revision": null})); | ||
| assert_eq!(committed(&connection, 3), json!({"committed": false})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_empty_snapshot_table_has_no_head() { | ||
| let connection = store(); | ||
| schema(&connection); | ||
| assert_eq!(head(&connection), json!({"snapshot": null})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn the_head_comes_back_as_stored() { | ||
| let connection = store(); | ||
| schema(&connection); | ||
| connection | ||
| .execute( | ||
| "insert into extension_control_authority_snapshot values (1, 4, 'cat', 'snap')", | ||
| [], | ||
| ) | ||
| .expect("snapshot"); | ||
| assert_eq!( | ||
| head(&connection), | ||
| json!({"snapshot": {"revision": 4, "snapshot_digest": "snap", "catalog_digest": "cat"}}) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn cells_of_the_wrong_type_read_as_null() { | ||
| let connection = store(); | ||
| schema(&connection); | ||
| connection | ||
| .execute( | ||
| "insert into extension_control_authority_snapshot values (1, '4', ?1, 7)", | ||
| params![b"cat".to_vec()], | ||
| ) | ||
| .expect("snapshot"); | ||
| assert_eq!( | ||
| head(&connection), | ||
| json!({"snapshot": {"revision": null, "snapshot_digest": null, "catalog_digest": null}}) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_uncommitted_head_transition_is_resumed_first() { | ||
| let connection = store(); | ||
| schema(&connection); | ||
| transition(&connection, 4, "anchored"); | ||
| transition(&connection, 5, "prepared"); | ||
| assert_eq!(recovery(&connection, 4), json!({"revision": 4})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_committed_head_resumes_the_next_transition() { | ||
| let connection = store(); | ||
| schema(&connection); | ||
| transition(&connection, 4, "committed"); | ||
| transition(&connection, 5, "prepared"); | ||
| assert_eq!(recovery(&connection, 4), json!({"revision": 5})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nothing_to_resume_past_a_committed_head() { | ||
| let connection = store(); | ||
| schema(&connection); | ||
| transition(&connection, 3, "prepared"); | ||
| transition(&connection, 4, "committed"); | ||
| transition(&connection, 6, "prepared"); | ||
| assert_eq!(recovery(&connection, 4), json!({"revision": null})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn the_committed_check_reads_the_phase() { | ||
| let connection = store(); | ||
| schema(&connection); | ||
| transition(&connection, 4, "committed"); | ||
| transition(&connection, 5, "anchored"); | ||
| assert_eq!(committed(&connection, 4), json!({"committed": true})); | ||
| assert_eq!(committed(&connection, 5), json!({"committed": false})); | ||
| assert_eq!(committed(&connection, 6), json!({"committed": false})); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_revision_argument_is_required() { | ||
| for method in &METHODS[1..] { | ||
| assert!(dispatch(&store(), method, &Args(&Map::new())).is_err()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.