Enforce Substrate Gate grants for research

This commit is contained in:
Meta Vens 2026-05-30 01:51:26 +01:00
commit 2a88bf1de9
8 changed files with 247 additions and 16 deletions

View file

@ -422,6 +422,7 @@ pub use runtime_spine::put_mind_gateway_review;
pub use runtime_spine::put_mind_state_commit_receipt;
pub use runtime_spine::put_runtime_reorient_worker_result;
pub use runtime_spine::put_runtime_role_worker_result;
pub use runtime_spine::put_substrate_gate_repo_access_grant_receipt;
pub use runtime_spine::replace_or_append_epiphany_job_binding;
pub use runtime_spine::runtime_eyes_evidence_packet;
pub use runtime_spine::runtime_hello_frame;
@ -433,6 +434,7 @@ pub use runtime_spine::runtime_role_worker_result;
pub use runtime_spine::runtime_schema_catalog_response;
pub use runtime_spine::runtime_spine_cache;
pub use runtime_spine::runtime_spine_status;
pub use runtime_spine::runtime_substrate_gate_repo_access_grant_receipt;
pub use runtime_spine::runtime_tool_invocation_statuses;
pub use runtime_spine::runtime_worker_launch_request;
pub use runtime_spine::write_runtime_hello_frame;
@ -463,7 +465,9 @@ pub use substrate_gate::SUBSTRATE_GATE_REPO_MUTATION_RECEIPT_TYPE;
pub use substrate_gate::SUBSTRATE_GATE_REPO_SNAPSHOT_RECEIPT_SCHEMA_VERSION;
pub use substrate_gate::SUBSTRATE_GATE_REPO_SNAPSHOT_RECEIPT_TYPE;
pub use substrate_gate::SubstrateGateCultNetContract;
pub use substrate_gate::SubstrateGateRepoAccessGrantReceipt;
pub use substrate_gate::default_substrate_gate_cultnet_contracts;
pub use substrate_gate::substrate_gate_repo_access_grant_for_launch;
pub use surfaces::EpiphanyAcceptanceBundle;
pub use surfaces::EpiphanyContext;
pub use surfaces::EpiphanyContextMissing;

View file

@ -55,6 +55,7 @@ use crate::substrate_gate::SUBSTRATE_GATE_REPO_MUTATION_RECEIPT_SCHEMA_VERSION;
use crate::substrate_gate::SUBSTRATE_GATE_REPO_MUTATION_RECEIPT_TYPE;
use crate::substrate_gate::SUBSTRATE_GATE_REPO_SNAPSHOT_RECEIPT_SCHEMA_VERSION;
use crate::substrate_gate::SUBSTRATE_GATE_REPO_SNAPSHOT_RECEIPT_TYPE;
use crate::substrate_gate::SubstrateGateRepoAccessGrantReceipt;
use crate::thread_state_store::THREAD_STATE_SCHEMA_VERSION;
use crate::thread_state_store::THREAD_STATE_TYPE;
use anyhow::Context;
@ -713,6 +714,7 @@ pub fn runtime_spine_cache(store_path: impl AsRef<Path>) -> Result<CultCache> {
cache.register_entry_type::<MindGatewayReview>()?;
cache.register_entry_type::<MindStateCommitReceipt>()?;
cache.register_entry_type::<EyesEvidencePacket>()?;
cache.register_entry_type::<SubstrateGateRepoAccessGrantReceipt>()?;
cache.register_entry_type::<EpiphanyOpenAiAdapterStatus>()?;
cache.register_entry_type::<EpiphanyOpenAiModelRequest>()?;
cache.register_entry_type::<EpiphanyOpenAiStreamEvent>()?;
@ -1206,6 +1208,47 @@ pub fn runtime_eyes_evidence_packet(
cache.get::<EyesEvidencePacket>(packet_id)
}
pub fn put_substrate_gate_repo_access_grant_receipt(
store_path: impl AsRef<Path>,
receipt: &SubstrateGateRepoAccessGrantReceipt,
) -> Result<()> {
validate_non_empty(
&receipt.receipt_id,
"Substrate Gate access grant receipt id",
)?;
validate_non_empty(
&receipt.runtime_job_id,
"Substrate Gate access grant runtime job",
)?;
validate_non_empty(&receipt.binding_id, "Substrate Gate access grant binding")?;
validate_non_empty(&receipt.role, "Substrate Gate access grant role")?;
validate_non_empty(
&receipt.authority_scope,
"Substrate Gate access grant authority scope",
)?;
validate_non_empty(&receipt.granted_at, "Substrate Gate access grant timestamp")?;
if receipt.granted_operations.is_empty() {
return Err(anyhow!(
"Substrate Gate access grant must name granted operations"
));
}
let mut cache = runtime_spine_cache(store_path)?;
cache.pull_all_backing_stores()?;
require_identity(&cache)?;
cache.put(&receipt.receipt_id, receipt)?;
Ok(())
}
pub fn runtime_substrate_gate_repo_access_grant_receipt(
store_path: impl AsRef<Path>,
receipt_id: &str,
) -> Result<Option<SubstrateGateRepoAccessGrantReceipt>> {
validate_non_empty(receipt_id, "Substrate Gate access grant receipt id")?;
let mut cache = runtime_spine_cache(store_path)?;
cache.pull_all_backing_stores()?;
cache.get::<SubstrateGateRepoAccessGrantReceipt>(receipt_id)
}
pub fn put_coordinator_run_receipt(
store_path: impl AsRef<Path>,
receipt: &EpiphanyCoordinatorRunReceipt,
@ -3169,6 +3212,20 @@ mod tests {
contract: "Eyes packet persists as runtime-spine proof.".to_string(),
};
put_eyes_evidence_packet(&store, &eyes_packet)?;
let substrate_grant = SubstrateGateRepoAccessGrantReceipt {
schema_version: crate::SUBSTRATE_GATE_REPO_ACCESS_GRANT_RECEIPT_SCHEMA_VERSION
.to_string(),
receipt_id: "substrate-grant-1".to_string(),
runtime_job_id: "job-1".to_string(),
binding_id: "research-source-gather-worker".to_string(),
role: "epiphany-eyes".to_string(),
authority_scope: "epiphany.role.research".to_string(),
granted_operations: vec!["read".to_string(), "snapshot".to_string()],
granted_paths: vec![".".to_string()],
granted_at: "2026-05-06T00:06:00Z".to_string(),
contract: "Substrate Gate grant persists as runtime-spine proof.".to_string(),
};
put_substrate_gate_repo_access_grant_receipt(&store, &substrate_grant)?;
let stored = runtime_mind_gateway_review(&store, "mind-role-modeling-job-1")?
.expect("Mind review receipt should persist");
@ -3179,6 +3236,10 @@ mod tests {
let stored_packet = runtime_eyes_evidence_packet(&store, "eyes-packet-1")?
.expect("Eyes evidence packet should persist");
assert_eq!(stored_packet, eyes_packet);
let stored_grant =
runtime_substrate_gate_repo_access_grant_receipt(&store, "substrate-grant-1")?
.expect("Substrate Gate grant should persist");
assert_eq!(stored_grant, substrate_grant);
Ok(())
}

View file

@ -1,3 +1,5 @@
use crate::EpiphanyJobLaunchRequest;
use cultcache_rs::DatabaseEntry;
use serde::Deserialize;
use serde::Serialize;
@ -26,6 +28,34 @@ pub const SUBSTRATE_GATE_REPO_SNAPSHOT_RECEIPT_SCHEMA_VERSION: &str =
pub const SUBSTRATE_GATE_REPO_MUTATION_RECEIPT_SCHEMA_VERSION: &str =
"epiphany.substrate_gate.repo_mutation_receipt.v0";
#[derive(Debug, Clone, PartialEq, Eq, DatabaseEntry)]
#[cultcache(
type = "epiphany.substrate_gate.repo_access_grant_receipt",
schema = "SubstrateGateRepoAccessGrantReceipt"
)]
pub struct SubstrateGateRepoAccessGrantReceipt {
#[cultcache(key = 0)]
pub schema_version: String,
#[cultcache(key = 1)]
pub receipt_id: String,
#[cultcache(key = 2)]
pub runtime_job_id: String,
#[cultcache(key = 3)]
pub binding_id: String,
#[cultcache(key = 4)]
pub role: String,
#[cultcache(key = 5)]
pub authority_scope: String,
#[cultcache(key = 6)]
pub granted_operations: Vec<String>,
#[cultcache(key = 7)]
pub granted_paths: Vec<String>,
#[cultcache(key = 8)]
pub granted_at: String,
#[cultcache(key = 9)]
pub contract: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubstrateGateCultNetContract {
@ -106,6 +136,26 @@ pub fn default_substrate_gate_cultnet_contracts() -> Vec<SubstrateGateCultNetCon
]
}
pub fn substrate_gate_repo_access_grant_for_launch(
receipt_id: String,
runtime_job_id: String,
request: &EpiphanyJobLaunchRequest,
granted_at: String,
) -> SubstrateGateRepoAccessGrantReceipt {
SubstrateGateRepoAccessGrantReceipt {
schema_version: SUBSTRATE_GATE_REPO_ACCESS_GRANT_RECEIPT_SCHEMA_VERSION.to_string(),
receipt_id,
runtime_job_id,
binding_id: request.binding_id.clone(),
role: request.owner_role.clone(),
authority_scope: request.authority_scope.clone(),
granted_operations: vec!["read".to_string(), "snapshot".to_string()],
granted_paths: vec![".".to_string()],
granted_at,
contract: "Substrate Gate granted scoped repository read/snapshot access for this worker launch; mutation remains forbidden without a separate mutation receipt.".to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -132,4 +182,56 @@ mod tests {
.contains(&SUBSTRATE_GATE_REPO_ACCESS_GRANT_RECEIPT_TYPE.to_string())
);
}
#[test]
fn substrate_gate_grant_for_launch_is_read_only() {
let request = EpiphanyJobLaunchRequest {
expected_revision: Some(7),
binding_id: "research-source-gather-worker".to_string(),
kind: epiphany_state_model::EpiphanyJobKind::Specialist,
scope: "research".to_string(),
owner_role: "epiphany-eyes".to_string(),
authority_scope: "epiphany.role.research".to_string(),
linked_subgoal_ids: Vec::new(),
linked_graph_node_ids: Vec::new(),
instruction: "Gather source evidence.".to_string(),
launch_document: crate::EpiphanyWorkerLaunchDocument::Role(
crate::EpiphanyRoleWorkerLaunchDocument {
thread_id: "thread-1".to_string(),
role_id: "research".to_string(),
state_revision: 7,
objective: None,
active_subgoal_id: None,
active_subgoals: Vec::new(),
active_graph_node_ids: Vec::new(),
investigation_checkpoint: None,
scratch: None,
invariants: Vec::new(),
graphs: None,
recent_evidence: Vec::new(),
recent_observations: Vec::new(),
graph_frontier: None,
graph_checkpoint: None,
planning: None,
churn: None,
},
),
output_contract_id: crate::ROLE_WORKER_OUTPUT_CONTRACT_ID.to_string(),
organ_launch_contract: crate::default_launch_organ_contract(
"epiphany.role.research",
"role",
crate::ROLE_WORKER_OUTPUT_CONTRACT_ID,
),
max_runtime_seconds: None,
};
let grant = substrate_gate_repo_access_grant_for_launch(
"grant-1".to_string(),
"job-1".to_string(),
&request,
"2026-05-30T00:00:00Z".to_string(),
);
assert!(grant.granted_operations.contains(&"read".to_string()));
assert!(!grant.granted_operations.contains(&"write".to_string()));
assert!(grant.contract.contains("mutation remains forbidden"));
}
}