Skip to content
Open
Show file tree
Hide file tree
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 Nov 22, 2026
eecc8f9
fix(store): refuse a non-text activation slot state
internet-dot Nov 22, 2026
4655765
fix(store): share the schema marker check between the authority write…
internet-dot Nov 22, 2026
e46f559
refactor(store): check the extension control schema marker in the nat…
internet-dot Nov 22, 2026
da4f585
refactor(store): name a refused activation slot's error code in the n…
internet-dot Nov 22, 2026
f21bbdd
Merge remote-tracking branch 'origin/feat/native-runtime-slice42' int…
internet-dot Nov 22, 2026
641ef32
chore(contracts): map the extension control schema reader in the owne…
internet-dot Nov 22, 2026
75d0b3c
refactor(store): read the committed extension control snapshot in the…
internet-dot Nov 22, 2026
a458375
refactor(store): resume transitions and read the upgrade's activation…
internet-dot Nov 22, 2026
da84eed
refactor(store): read the cache cleanup's activation records in the n…
internet-dot Nov 22, 2026
5b33acb
refactor(store): read the pack migration's legacy evidence in the nat…
internet-dot Nov 22, 2026
5fd5359
refactor(store): read the extension control head for recovery in the …
internet-dot Nov 22, 2026
e918ad2
test(store): expect the snapshot layers read on the snapshot route
internet-dot Nov 22, 2026
ec4ca4f
Merge branch 'feat/native-runtime-slice46' into feat/native-runtime-s…
internet-dot Nov 22, 2026
7591d83
Merge branch 'feat/native-runtime-slice47' into feat/native-runtime-s…
internet-dot Nov 22, 2026
7331033
test(store): expect the new native reads on the snapshot route
internet-dot Nov 22, 2026
534b718
Merge branch 'feat/native-runtime-slice48' into feat/native-runtime-s…
internet-dot Nov 22, 2026
b51fd89
test(store): expect the new native reads on the snapshot route
internet-dot Nov 22, 2026
612e982
test(store): expect the new native reads on the snapshot route
internet-dot Nov 22, 2026
65af8ba
refactor(store): read catalog migration events and the pack baseline …
internet-dot Nov 22, 2026
ca4fdab
Merge remote-tracking branch 'origin/main' into feat/native-runtime-s…
internet-dot Nov 22, 2026
5a772eb
fix(store): read the snapshot on the shared connection inside an MCP …
internet-dot Nov 22, 2026
95b5127
fix(store): read the extension control head on the shared connection …
internet-dot Nov 22, 2026
3205f81
Merge branch 'feat/native-runtime-slice46' into feat/native-runtime-s…
internet-dot Nov 22, 2026
91cd8c8
Merge branch 'feat/native-runtime-slice47' into feat/native-runtime-s…
internet-dot Nov 22, 2026
b21a067
Merge branch 'feat/native-runtime-slice48' into feat/native-runtime-s…
internet-dot Nov 22, 2026
a891ce5
Merge branch 'feat/native-runtime-slice49' into feat/native-runtime-s…
internet-dot Nov 22, 2026
651c53c
fix(store): budget legacy history pages on the encoded reply and stop…
internet-dot Nov 22, 2026
20295bf
fix(store): answer the scoped extension control head read as the nati…
internet-dot Nov 22, 2026
3e0f05e
Merge remote-tracking branch 'origin/feat/native-runtime-slice48' int…
internet-dot Nov 22, 2026
73bb6a0
Merge remote-tracking branch 'origin/feat/native-runtime-slice49' int…
internet-dot Nov 22, 2026
2864e9c
docs(guard): record the scoped extension control reads as a temporary…
internet-dot Nov 22, 2026
e3f414f
Merge remote-tracking branch 'origin/feat/native-runtime-slice49' int…
internet-dot Nov 22, 2026
6b624ed
fix(store): let an unusable anchor revision select no legacy pending row
internet-dot Nov 22, 2026
0297abf
Merge remote-tracking branch 'origin/feat/native-runtime-slice48' int…
internet-dot Nov 22, 2026
8ffd933
Merge remote-tracking branch 'origin/feat/native-runtime-slice49' int…
internet-dot Nov 22, 2026
667a34a
test(store): answer the pending-record list in the activation fixture…
internet-dot Nov 22, 2026
3ae77a5
Merge remote-tracking branch 'origin/feat/native-runtime-slice47' int…
internet-dot Nov 22, 2026
d199c1a
Merge remote-tracking branch 'origin/feat/native-runtime-slice48' int…
internet-dot Nov 22, 2026
b216d67
Merge remote-tracking branch 'origin/feat/native-runtime-slice49' int…
internet-dot Nov 22, 2026
1ec6934
Merge remote-tracking branch 'origin/main' into feat/native-runtime-s…
internet-dot Nov 22, 2026
6e1db5c
Merge remote-tracking branch 'origin/main' into feat/native-runtime-s…
internet-dot Nov 22, 2026
d673b1d
fix(store): page the pending command-pack records so cleanup reads ev…
internet-dot Nov 22, 2026
c6d2630
Merge remote-tracking branch 'origin/main' into feat/native-runtime-s…
internet-dot Nov 22, 2026
2a7b288
Merge branch 'feat/native-runtime-slice49' of https://github.com/hash…
internet-dot Nov 22, 2026
160c681
merge(main): resolve command pack slot conflicts
internet-dot Nov 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(store): read the command-pack activation slot in the native …
…store
  • Loading branch information
internet-dot committed Nov 22, 2026
commit 44486eb84df6fa4e6a5032e524ade0d19e87f21a
100 changes: 100 additions & 0 deletions rust/crates/guard-runtime/src/guard_store_command_pack_activation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! The command-pack activation slot, read for Python to authenticate.
//!
//! Port of the slot read `catalog_delivery.activation_store._row` and its
//! callers ran on their own SQLite connection. Nothing is authenticated here:
//! the record, its digest and its MAC go back as stored, and Python verifies
//! them with the key it holds. The answer names one state:
//!
//! - `absent`: no extension-control schema yet, or one older than the pack
//! schema, so there is no slot to read;
//! - `schema_invalid`: the schema marker is missing or names no known schema;
//! - `missing`: the pack schema is in place but the slot is empty or holds a
//! row whose sizes cannot be a record;
//! - `present`: the slot row, under `row`.

use rusqlite::Connection;
use serde_json::{json, Map, Value};
use sha2::{Digest, Sha256};

use crate::guard_store_db::{query_one, StoreResult};
use crate::guard_store_local_cli_inventory::table_exists;

/// The first schema version with an activation slot.
const PACK_ACTIVATION_SCHEMA_VERSION: i64 = 5;
/// Every schema version a marker may name.
const KNOWN_VERSIONS: std::ops::RangeInclusive<i64> = 1..=6;
const SLOT_FIELDS: [&str; 5] = [
"revision",
"record_json",
"record_digest",
"record_mac",
"recorded_at",
];

pub(crate) fn handles(method: &str) -> bool {
method == "read_command_pack_activation"
}

pub(crate) fn read(connection: &Connection) -> StoreResult<Value> {
if !table_exists(connection, "extension_control_schema_migration")? {
return Ok(state("absent"));
}
let marker = query_one(
connection,
"select version, checksum from extension_control_schema_migration where singleton = 1",
&[],
)?;
let Some(version) = marker.as_ref().and_then(compatible_version) else {
return Ok(state("schema_invalid"));
};
if version < PACK_ACTIVATION_SCHEMA_VERSION {
return Ok(state("absent"));
}
let slot = query_one(
connection,
"select revision, record_json, record_digest, record_mac, recorded_at \
from extension_command_pack_activation where singleton = 1 \
and length(cast(record_json as blob)) between 1 and 65536 \
and length(record_digest) = 64 and length(record_mac) = 64",
&[],
)?;
let Some(slot) = slot else {
return Ok(state("missing"));
};
let row: Map<String, Value> = SLOT_FIELDS
.iter()
.map(|name| {
(
(*name).to_owned(),
slot.get(*name).cloned().unwrap_or(Value::Null),
)
})
.collect();
Ok(json!({"state": "present", "row": row}))
}

fn state(name: &str) -> Value {
json!({"state": name})
}

/// The marker's version when it carries an integer version and the checksum
/// of that version's schema, as `extension_control_schema_marker_is_compatible`
/// required.
fn compatible_version(marker: &crate::guard_store_db::Row) -> Option<i64> {
let version = marker.get("version").and_then(Value::as_i64)?;
let checksum = marker.get("checksum").and_then(Value::as_str)?;
if !KNOWN_VERSIONS.contains(&version) || checksum != schema_checksum(version) {
return None;
}
Some(version)
}

fn schema_checksum(version: i64) -> String {
hex::encode(Sha256::digest(
format!("hol-guard.extension-control-authority.schema.v{version}").as_bytes(),
))
}

#[cfg(test)]
#[path = "guard_store_command_pack_activation_tests.rs"]
mod tests;
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use rusqlite::{params, Connection};
use serde_json::json;

use super::{handles, read, schema_checksum};
use crate::guard_store_op_kind::{method_kind, Kind};

fn store() -> Connection {
Connection::open_in_memory().expect("memory store")
}

fn mark(connection: &Connection, version: i64, checksum: &str) {
connection
.execute_batch(
"create table extension_control_schema_migration ( \
singleton integer primary key check (singleton = 1), \
version integer not null, checksum text not null); \
create table extension_command_pack_activation ( \
singleton integer primary key check (singleton = 1), \
revision integer not null check (revision >= 0), \
record_json text not null, record_digest text not null, \
record_mac text not null, recorded_at text not null);",
)
.expect("schema");
connection
.execute(
"insert into extension_control_schema_migration values (1, ?1, ?2)",
params![version, checksum],
)
.expect("marker");
}

fn slot(connection: &Connection, record: &str, digest: &str) {
connection
.execute(
"insert into extension_command_pack_activation values (1, 3, ?1, ?2, ?3, 'now')",
params![record, digest, "m".repeat(64)],
)
.expect("slot");
}

#[test]
fn the_read_is_routed_as_a_snapshot() {
assert!(handles("read_command_pack_activation"));
assert!(matches!(
method_kind("read_command_pack_activation"),
Some(Kind::Snapshot)
));
}

#[test]
fn a_store_without_the_pack_schema_has_no_slot() {
let connection = store();
assert_eq!(read(&connection).expect("read"), json!({"state": "absent"}));
mark(&connection, 4, &schema_checksum(4));
assert_eq!(read(&connection).expect("read"), json!({"state": "absent"}));
}

#[test]
fn an_unknown_or_forged_marker_is_invalid() {
for (version, checksum) in [
(5, schema_checksum(4)),
(7, schema_checksum(7)),
(0, schema_checksum(0)),
] {
let connection = store();
mark(&connection, version, &checksum);
assert_eq!(
read(&connection).expect("read"),
json!({"state": "schema_invalid"}),
"{version}"
);
}
let empty = store();
mark(&empty, 5, &schema_checksum(5));
empty
.execute_batch("delete from extension_control_schema_migration")
.expect("clear");
assert_eq!(
read(&empty).expect("read"),
json!({"state": "schema_invalid"})
);
}

#[test]
fn an_empty_or_malformed_slot_is_missing() {
let connection = store();
mark(&connection, 6, &schema_checksum(6));
assert_eq!(
read(&connection).expect("read"),
json!({"state": "missing"})
);
slot(&connection, "{}", "short");
assert_eq!(
read(&connection).expect("read"),
json!({"state": "missing"})
);
}

#[test]
fn a_filled_slot_comes_back_as_stored() {
let connection = store();
mark(&connection, 5, &schema_checksum(5));
slot(&connection, "{\"revision\":3}", &"d".repeat(64));
assert_eq!(
read(&connection).expect("read"),
json!({"state": "present", "row": {
"revision": 3,
"record_json": "{\"revision\":3}",
"record_digest": "d".repeat(64),
"record_mac": "m".repeat(64),
"recorded_at": "now",
}})
);
}
21 changes: 13 additions & 8 deletions rust/crates/guard-runtime/src/guard_store_local_cli_reads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ use crate::guard_store_local_cli_inventory_capabilities::{
capabilities, capability_map, CAPABILITY_BY_CAUSE,
};

#[path = "guard_store_command_pack_activation.rs"]
mod command_pack_activation;

/// Whether `method` is one of these reads or the cause note.
pub(crate) fn handles(method: &str) -> bool {
matches!(
method,
"read_local_cli_revision"
| "read_local_cli_surface"
| "has_local_cli_block_rules"
| "read_local_cli_capabilities"
| "note_local_cli_capability_cause"
)
command_pack_activation::handles(method)
|| matches!(
method,
"read_local_cli_revision"
| "read_local_cli_surface"
| "has_local_cli_block_rules"
| "read_local_cli_capabilities"
| "note_local_cli_capability_cause"
)
}

pub(crate) fn dispatch(
Expand All @@ -35,6 +39,7 @@ pub(crate) fn dispatch(
args: &Args<'_>,
) -> StoreResult<Value> {
match method {
_ if command_pack_activation::handles(method) => command_pack_activation::read(connection),
"read_local_cli_revision" => Ok(Value::from(authority_revision(connection)?)),
"read_local_cli_surface" => read_surface(connection, args.str("cli_id")?),
"has_local_cli_block_rules" => Ok(Value::from(has_block_rules(connection)?)),
Expand Down
1 change: 1 addition & 0 deletions rust/crates/guard-runtime/src/guard_store_op_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub(crate) fn method_kind(method: &str) -> Option<Kind> {
| "get_next_pending_request"
| "get_approval_extension_allow_hint"
| "pending_connector_group_rows"
| "read_command_pack_activation"
| "connector_setup_advice"
| "approval_desktop_notified_at"
| "oldest_approval_request_created_at"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ def prepare_directory_for_store(store):
return _runtime_state_directory(resolved_guard_home(store.guard_home)) / "command-packs"


_SLOT_FIELDS = ("revision", "record_json", "record_digest", "record_mac", "recorded_at")
_SLOT_ERRORS = {
"schema_invalid": "native_command_pack_schema_invalid",
"missing": "native_command_pack_activation_missing",
}


def read_slot(store):
"""The stored activation slot, read by the native store, or None before the pack schema.

The row is returned unauthenticated; callers verify it with their key.
"""
method = "read_command_pack_activation"
answer = store._native_store_call(method, {})
state = answer.get("state") if isinstance(answer, dict) else None
if state == "absent" and set(answer) == {"state"}:
return None
if state in _SLOT_ERRORS and set(answer) == {"state"}:
raise NativePolicySnapshotError(_SLOT_ERRORS[state])
row = answer.get("row") if state == "present" and set(answer) == {"state", "row"} else None
if not isinstance(row, dict) or set(row) != set(_SLOT_FIELDS) or not _slot_types(row):
raise NativePolicySnapshotError(f"native {method} returned no answer")
return row


def _slot_types(row):
revision = row["revision"]
if isinstance(revision, bool) or not isinstance(revision, int) or revision < 0:
return False
return all(isinstance(row[name], str) for name in _SLOT_FIELDS[1:])


def _row(connection):
marker = connection.execute(
"select version, checksum from extension_control_schema_migration where singleton=1"
Expand All @@ -46,12 +78,7 @@ def _row(connection):
@lifecycle.one_runtime
def read(store):
"""Authenticate outside mutation locks; this may read cached metadata pages."""
with store._connect() as connection:
if not connection.execute(
"select 1 from sqlite_master where type='table' and name='extension_control_schema_migration'"
).fetchone():
return None
row = _row(connection)
row = read_slot(store)
if row is None:
return None
from ..native_command_control_authority_store import _key
Expand All @@ -74,12 +101,7 @@ def read(store):
@lifecycle.one_runtime
def header_for_store(store, verifier_key):
"""Small authenticated recheck; never materialize a catalog under a lease."""
with store._connect() as connection:
if not connection.execute(
"select 1 from sqlite_master where type='table' and name='extension_control_schema_migration'"
).fetchone():
return None
row = _row(connection)
row = read_slot(store)
if row is None:
return None
if activation_baseline.is_baseline(row["record_json"]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ def read(store):


def _activation(store):
with store._connect() as connection:
exists = connection.execute(
"select 1 from sqlite_master where type='table' and name='extension_control_schema_migration'"
).fetchone()
row = activation_store._row(connection) if exists else None
row = activation_store.read_slot(store)
if row is None:
return None
from .activation_baseline import is_baseline
Expand Down
8 changes: 2 additions & 6 deletions src/codex_plugin_scanner/guard/catalog_delivery/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,9 @@ def _registry(store, committed, catalog: type[GeneratedCommandCatalog]) -> Gener
@lifecycle.one_runtime
def require_current(store, verifier_key, expected_digest):
"""Reject a captured catalog if another process activated a different record."""
from .activation_store import _row
from .activation_store import read_slot

with store._connect() as connection:
exists = connection.execute(
"select 1 from sqlite_master where type='table' and name='extension_control_schema_migration'"
).fetchone()
row = _row(connection) if exists else None
row = read_slot(store)
if row is not None:
from . import activation_baseline

Expand Down
33 changes: 33 additions & 0 deletions tests/native_activation_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,36 @@ def subset_store(previous):
revocation=identity["revocation"],
)
return store


def activation_slot_call(connect):
"""Stand-in for the native ``read_command_pack_activation`` over a fixture connection.

Fixture stores keep their database in memory, out of the resident's reach,
so this answers the one slot read with the same states the native read uses.
"""
from codex_plugin_scanner.guard.catalog_delivery.activation_store import _row
from codex_plugin_scanner.guard.native_policy_snapshot_constants import NativePolicySnapshotError

states = {
"native_command_pack_schema_invalid": "schema_invalid",
"native_command_pack_activation_missing": "missing",
}

def call(method, args):
assert method == "read_command_pack_activation" and args == {}
with connect() as connection:
if not connection.execute(
"select 1 from sqlite_master where type='table' and name='extension_control_schema_migration'"
).fetchone():
return {"state": "absent"}
try:
row = _row(connection)
except NativePolicySnapshotError as error:
return {"state": states[str(error)]}
if row is None:
return {"state": "absent"}
fields = ("revision", "record_json", "record_digest", "record_mac", "recorded_at")
return {"state": "present", "row": {name: row[name] for name in fields}}

return call
Loading
Loading