Skip to content

Commit 9883a70

Browse files
committed
feat(policy): plumb chunk_ids and rejection_reason through proposal pipeline
Prereq plumbing for the agent revise-and-resubmit loop. Two narrow additive proto changes unblock the upcoming /wait endpoint (#1092), prover validation badge (#1097), and reject --guidance surfaces (#1098). - SubmitPolicyAnalysisResponse: add accepted_chunk_ids so the in-sandbox agent gets handles to watch its proposals. Surfaced through the typed grpc_client wrapper and policy.local's POST /v1/proposals 202 body. Closes #1094. - PolicyChunk + StoredDraftChunk + DraftChunkPayload: add validation_result (gateway prover verdict, populated by #1097) and rejection_reason (operator free-form text). Both plain strings; no enums, no parsing on the read path. Closes #1096. - RejectDraftChunk now persists the existing reason field into the chunk's rejection_reason so it round-trips back to the agent via GetDraftPolicy. UndoDraftChunk clears it on the way back to pending so consumers cannot read a stale guidance string from a prior reject -> re-approve -> undo cycle. Whole surface stays gated behind agent_policy_proposals_enabled. Two focused tests cover the round-trip and the undo-clears guarantee. Signed-off-by: Alexander Watson <[email protected]>
1 parent 96d909d commit 9883a70

8 files changed

Lines changed: 287 additions & 9 deletions

File tree

‎crates/openshell-sandbox/src/grpc_client.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,9 @@ impl CachedOpenShellClient {
332332
/// Submit denial summaries and/or agent-authored proposals for policy analysis.
333333
///
334334
/// Returns the gateway response so callers can surface accepted/rejected
335-
/// counts and rejection reasons (e.g., the `policy.local` API forwards
336-
/// these to the in-sandbox agent).
335+
/// counts, rejection reasons, and server-assigned `accepted_chunk_ids`
336+
/// (e.g., the `policy.local` API forwards these to the in-sandbox agent
337+
/// so it can watch proposal state via `GET /v1/proposals/{id}`).
337338
pub async fn submit_policy_analysis(
338339
&self,
339340
sandbox_name: &str,

‎crates/openshell-sandbox/src/mechanistic_mapper.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ pub async fn generate_proposals(summaries: &[DenialSummary]) -> Vec<PolicyChunk>
230230
first_seen_ms,
231231
last_seen_ms,
232232
binary: binary.clone(),
233+
validation_result: String::new(),
234+
rejection_reason: String::new(),
233235
});
234236
}
235237

‎crates/openshell-sandbox/src/policy_local.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ async fn submit_proposal(ctx: &PolicyLocalContext, body: &[u8]) -> (u16, serde_j
451451
"accepted_chunks": response.accepted_chunks,
452452
"rejected_chunks": response.rejected_chunks,
453453
"rejection_reasons": response.rejection_reasons,
454+
"accepted_chunk_ids": response.accepted_chunk_ids,
454455
}),
455456
)
456457
}
@@ -521,6 +522,8 @@ fn policy_chunk_from_add_rule(
521522
first_seen_ms: 0,
522523
last_seen_ms: 0,
523524
binary,
525+
validation_result: String::new(),
526+
rejection_reason: String::new(),
524527
})
525528
}
526529

‎crates/openshell-server/src/grpc/policy.rs‎

Lines changed: 236 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ pub(super) async fn handle_submit_policy_analysis(
13581358
let mut accepted: u32 = 0;
13591359
let mut rejected: u32 = 0;
13601360
let mut rejection_reasons: Vec<String> = Vec::new();
1361+
let mut accepted_chunk_ids: Vec<String> = Vec::new();
13611362

13621363
for chunk in &req.proposed_chunks {
13631364
if chunk.rule_name.is_empty() {
@@ -1391,7 +1392,7 @@ pub(super) async fn handle_submit_policy_analysis(
13911392
.unwrap_or_default();
13921393

13931394
let record = DraftChunkRecord {
1394-
id: chunk_id,
1395+
id: chunk_id.clone(),
13951396
sandbox_id: sandbox_id.clone(),
13961397
draft_version,
13971398
status: "pending".to_string(),
@@ -1419,13 +1420,16 @@ pub(super) async fn handle_submit_policy_analysis(
14191420
} else {
14201421
now_ms
14211422
},
1423+
validation_result: String::new(),
1424+
rejection_reason: String::new(),
14221425
};
14231426
state
14241427
.store
14251428
.put_draft_chunk(&record)
14261429
.await
14271430
.map_err(|e| Status::internal(format!("persist draft chunk failed: {e}")))?;
14281431
accepted += 1;
1432+
accepted_chunk_ids.push(chunk_id);
14291433
}
14301434

14311435
state.sandbox_watch_bus.notify(&sandbox_id);
@@ -1443,6 +1447,7 @@ pub(super) async fn handle_submit_policy_analysis(
14431447
accepted_chunks: accepted,
14441448
rejected_chunks: rejected,
14451449
rejection_reasons,
1450+
accepted_chunk_ids,
14461451
}))
14471452
}
14481453

@@ -1559,7 +1564,7 @@ pub(super) async fn handle_approve_draft_chunk(
15591564
current_time_ms().map_err(|e| Status::internal(format!("timestamp error: {e}")))?;
15601565
state
15611566
.store
1562-
.update_draft_chunk_status(&req.chunk_id, "approved", Some(now_ms))
1567+
.update_draft_chunk_status(&req.chunk_id, "approved", Some(now_ms), None)
15631568
.await
15641569
.map_err(|e| Status::internal(format!("update chunk status failed: {e}")))?;
15651570

@@ -1657,9 +1662,17 @@ pub(super) async fn handle_reject_draft_chunk(
16571662

16581663
let now_ms =
16591664
current_time_ms().map_err(|e| Status::internal(format!("timestamp error: {e}")))?;
1665+
// Persist the reviewer's free-form `reason` into the chunk's
1666+
// `rejection_reason` field so the in-sandbox agent can read it back via
1667+
// GetDraftPolicy / policy.local and revise the proposal.
1668+
let persisted_reason = if req.reason.is_empty() {
1669+
None
1670+
} else {
1671+
Some(req.reason.as_str())
1672+
};
16601673
state
16611674
.store
1662-
.update_draft_chunk_status(&req.chunk_id, "rejected", Some(now_ms))
1675+
.update_draft_chunk_status(&req.chunk_id, "rejected", Some(now_ms), persisted_reason)
16631676
.await
16641677
.map_err(|e| Status::internal(format!("update chunk status failed: {e}")))?;
16651678

@@ -1741,7 +1754,7 @@ pub(super) async fn handle_approve_all_draft_chunks(
17411754
current_time_ms().map_err(|e| Status::internal(format!("timestamp error: {e}")))?;
17421755
state
17431756
.store
1744-
.update_draft_chunk_status(&chunk.id, "approved", Some(now_ms))
1757+
.update_draft_chunk_status(&chunk.id, "approved", Some(now_ms), None)
17451758
.await
17461759
.map_err(|e| Status::internal(format!("update chunk status failed: {e}")))?;
17471760

@@ -1884,9 +1897,12 @@ pub(super) async fn handle_undo_draft_chunk(
18841897

18851898
let (version, hash) = remove_chunk_from_policy(state, &sandbox_id, &chunk).await?;
18861899

1900+
// Clear any prior rejection_reason on the way back to "pending" so an
1901+
// agent reading the chunk via policy.local cannot see a stale guidance
1902+
// string left over from a previous reject → undo round.
18871903
state
18881904
.store
1889-
.update_draft_chunk_status(&req.chunk_id, "pending", None)
1905+
.update_draft_chunk_status(&req.chunk_id, "pending", None, Some(""))
18901906
.await
18911907
.map_err(|e| Status::internal(format!("update chunk status failed: {e}")))?;
18921908

@@ -2108,6 +2124,8 @@ fn draft_chunk_record_to_proto(record: &DraftChunkRecord) -> Result<PolicyChunk,
21082124
first_seen_ms: record.first_seen_ms,
21092125
last_seen_ms: record.last_seen_ms,
21102126
binary: record.binary.clone(),
2127+
validation_result: record.validation_result.clone(),
2128+
rejection_reason: record.rejection_reason.clone(),
21112129
..Default::default()
21122130
})
21132131
}
@@ -3971,6 +3989,8 @@ mod tests {
39713989
.into_inner();
39723990
assert_eq!(submit.accepted_chunks, 1);
39733991
assert_eq!(submit.rejected_chunks, 0);
3992+
assert_eq!(submit.accepted_chunk_ids.len(), 1);
3993+
assert!(!submit.accepted_chunk_ids[0].is_empty());
39743994

39753995
let draft_policy = handle_get_draft_policy(
39763996
&state,
@@ -4116,6 +4136,211 @@ mod tests {
41164136
assert!(history_after_clear.entries.is_empty());
41174137
}
41184138

4139+
/// A reviewer's free-form rejection reason must round-trip through
4140+
/// persistence and surface on the chunk via `GetDraftPolicy`, so the
4141+
/// in-sandbox agent can read the guidance and redraft. The MVP-v2 agent
4142+
/// feedback loop hangs off this guarantee.
4143+
#[tokio::test]
4144+
async fn reject_with_reason_persists_into_chunk_for_agent_readback() {
4145+
use openshell_core::proto::{NetworkBinary, NetworkEndpoint, SandboxPhase, SandboxSpec};
4146+
4147+
let state = test_server_state().await;
4148+
let sandbox_name = "agent-feedback-loop".to_string();
4149+
let sandbox = Sandbox {
4150+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
4151+
id: "sb-feedback".to_string(),
4152+
name: sandbox_name.clone(),
4153+
created_at_ms: 1_000_000,
4154+
labels: std::collections::HashMap::new(),
4155+
}),
4156+
spec: Some(SandboxSpec {
4157+
policy: None,
4158+
..Default::default()
4159+
}),
4160+
phase: SandboxPhase::Ready as i32,
4161+
..Default::default()
4162+
};
4163+
state.store.put_message(&sandbox).await.unwrap();
4164+
4165+
let proposed_rule = NetworkPolicyRule {
4166+
name: "allow_example".to_string(),
4167+
endpoints: vec![NetworkEndpoint {
4168+
host: "api.example.com".to_string(),
4169+
port: 443,
4170+
..Default::default()
4171+
}],
4172+
binaries: vec![NetworkBinary {
4173+
path: "/usr/bin/curl".to_string(),
4174+
..Default::default()
4175+
}],
4176+
};
4177+
4178+
let submit = handle_submit_policy_analysis(
4179+
&state,
4180+
Request::new(SubmitPolicyAnalysisRequest {
4181+
name: sandbox_name.clone(),
4182+
proposed_chunks: vec![PolicyChunk {
4183+
rule_name: "allow_example".to_string(),
4184+
proposed_rule: Some(proposed_rule),
4185+
rationale: "agent intent".to_string(),
4186+
..Default::default()
4187+
}],
4188+
..Default::default()
4189+
}),
4190+
)
4191+
.await
4192+
.unwrap()
4193+
.into_inner();
4194+
let chunk_id = submit.accepted_chunk_ids[0].clone();
4195+
4196+
let guidance = "scope to docs/ paths only, not all repo contents";
4197+
handle_reject_draft_chunk(
4198+
&state,
4199+
Request::new(RejectDraftChunkRequest {
4200+
name: sandbox_name.clone(),
4201+
chunk_id: chunk_id.clone(),
4202+
reason: guidance.to_string(),
4203+
}),
4204+
)
4205+
.await
4206+
.unwrap();
4207+
4208+
let draft = handle_get_draft_policy(
4209+
&state,
4210+
Request::new(GetDraftPolicyRequest {
4211+
name: sandbox_name,
4212+
status_filter: String::new(),
4213+
}),
4214+
)
4215+
.await
4216+
.unwrap()
4217+
.into_inner();
4218+
let rejected = draft
4219+
.chunks
4220+
.iter()
4221+
.find(|c| c.id == chunk_id)
4222+
.expect("rejected chunk should still be visible");
4223+
assert_eq!(rejected.status, "rejected");
4224+
assert_eq!(
4225+
rejected.rejection_reason, guidance,
4226+
"reviewer's free-form reason must round-trip into the chunk for agent readback"
4227+
);
4228+
// validation_result is unpopulated until the prover runs (#1097).
4229+
assert!(rejected.validation_result.is_empty());
4230+
}
4231+
4232+
/// Undo of an approve must clear any `rejection_reason` left over from a
4233+
/// prior reject. Without this, the in-sandbox agent reading chunks via
4234+
/// `policy.local` cannot tell "pending and never rejected" from "pending
4235+
/// but previously rejected with this stale guidance." The only path that
4236+
/// lands a non-empty reason on a pending chunk is reject → re-approve →
4237+
/// undo, so the test walks that sequence.
4238+
#[tokio::test]
4239+
async fn undo_after_reject_clears_stale_rejection_reason() {
4240+
use openshell_core::proto::{NetworkBinary, NetworkEndpoint, SandboxPhase, SandboxSpec};
4241+
4242+
let state = test_server_state().await;
4243+
let sandbox_name = "undo-clears-reason".to_string();
4244+
let sandbox = Sandbox {
4245+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
4246+
id: "sb-undo-clears".to_string(),
4247+
name: sandbox_name.clone(),
4248+
created_at_ms: 1_000_000,
4249+
labels: std::collections::HashMap::new(),
4250+
}),
4251+
spec: Some(SandboxSpec {
4252+
policy: None,
4253+
..Default::default()
4254+
}),
4255+
phase: SandboxPhase::Ready as i32,
4256+
..Default::default()
4257+
};
4258+
state.store.put_message(&sandbox).await.unwrap();
4259+
4260+
let proposed_rule = NetworkPolicyRule {
4261+
name: "allow_example".to_string(),
4262+
endpoints: vec![NetworkEndpoint {
4263+
host: "api.example.com".to_string(),
4264+
port: 443,
4265+
..Default::default()
4266+
}],
4267+
binaries: vec![NetworkBinary {
4268+
path: "/usr/bin/curl".to_string(),
4269+
..Default::default()
4270+
}],
4271+
};
4272+
4273+
let submit = handle_submit_policy_analysis(
4274+
&state,
4275+
Request::new(SubmitPolicyAnalysisRequest {
4276+
name: sandbox_name.clone(),
4277+
proposed_chunks: vec![PolicyChunk {
4278+
rule_name: "allow_example".to_string(),
4279+
proposed_rule: Some(proposed_rule),
4280+
..Default::default()
4281+
}],
4282+
..Default::default()
4283+
}),
4284+
)
4285+
.await
4286+
.unwrap()
4287+
.into_inner();
4288+
let chunk_id = submit.accepted_chunk_ids[0].clone();
4289+
4290+
handle_reject_draft_chunk(
4291+
&state,
4292+
Request::new(RejectDraftChunkRequest {
4293+
name: sandbox_name.clone(),
4294+
chunk_id: chunk_id.clone(),
4295+
reason: "scope too broad".to_string(),
4296+
}),
4297+
)
4298+
.await
4299+
.unwrap();
4300+
4301+
handle_approve_draft_chunk(
4302+
&state,
4303+
Request::new(ApproveDraftChunkRequest {
4304+
name: sandbox_name.clone(),
4305+
chunk_id: chunk_id.clone(),
4306+
}),
4307+
)
4308+
.await
4309+
.unwrap();
4310+
4311+
handle_undo_draft_chunk(
4312+
&state,
4313+
Request::new(UndoDraftChunkRequest {
4314+
name: sandbox_name.clone(),
4315+
chunk_id: chunk_id.clone(),
4316+
}),
4317+
)
4318+
.await
4319+
.unwrap();
4320+
4321+
let draft = handle_get_draft_policy(
4322+
&state,
4323+
Request::new(GetDraftPolicyRequest {
4324+
name: sandbox_name,
4325+
status_filter: String::new(),
4326+
}),
4327+
)
4328+
.await
4329+
.unwrap()
4330+
.into_inner();
4331+
let restored = draft
4332+
.chunks
4333+
.iter()
4334+
.find(|c| c.id == chunk_id)
4335+
.expect("chunk should still be present after undo");
4336+
assert_eq!(restored.status, "pending");
4337+
assert!(
4338+
restored.rejection_reason.is_empty(),
4339+
"undo must clear stale rejection_reason; got: {:?}",
4340+
restored.rejection_reason
4341+
);
4342+
}
4343+
41194344
#[tokio::test]
41204345
async fn draft_chunk_handlers_reject_cross_sandbox_chunk_ids() {
41214346
use openshell_core::proto::{NetworkBinary, NetworkEndpoint, SandboxPhase, SandboxSpec};
@@ -4417,6 +4642,8 @@ mod tests {
44174642
hit_count: 1,
44184643
first_seen_ms: 0,
44194644
last_seen_ms: 0,
4645+
validation_result: String::new(),
4646+
rejection_reason: String::new(),
44204647
};
44214648

44224649
let (version, _) = merge_chunk_into_policy(&store, &chunk.sandbox_id, &chunk)
@@ -4511,6 +4738,8 @@ mod tests {
45114738
hit_count: 1,
45124739
first_seen_ms: 0,
45134740
last_seen_ms: 0,
4741+
validation_result: String::new(),
4742+
rejection_reason: String::new(),
45144743
};
45154744

45164745
let (version, _) = merge_chunk_into_policy(&store, sandbox_id, &chunk)
@@ -4610,6 +4839,8 @@ mod tests {
46104839
hit_count: 1,
46114840
first_seen_ms: 0,
46124841
last_seen_ms: 0,
4842+
validation_result: String::new(),
4843+
rejection_reason: String::new(),
46134844
};
46144845

46154846
let (version, _) = merge_chunk_into_policy(&store, sandbox_id, &chunk)

0 commit comments

Comments
 (0)