Skip to content

Commit 699a62d

Browse files
committed
feat(permissions): Enrich model approval context
Give approval models the current tool description, input schema, complete arguments, and up to five prior tool inputs. Organize review requests with Markdown sections and one trust rule. Trim the oldest history to stay below a conservative 30k-token budget, and use decision-only output for chat approvals. Align CLI approval mocks with the Markdown review request and the decision-only response protocol. Validation: - 13 AI adapter permission tests. - 3 focused core approval and history tests. - CLI command contracts: 45 passed, 1 ignored. - Original CLI product-control test passes after diagnostic cleanup.
1 parent cdc7ba7 commit 699a62d

9 files changed

Lines changed: 887 additions & 204 deletions

File tree

‎src/apps/cli/tests/support/mod.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ fn serve_model_response(
573573
"index": 0,
574574
"delta": {
575575
"role": "assistant",
576-
"content": "DECISION=ALLOW;RISK=LOW;REASON=READ_ONLY"
576+
"content": "DECISION=ALLOW"
577577
},
578578
"finish_reason": "stop"
579579
}]
@@ -739,7 +739,7 @@ fn is_permission_approval_request(request: &[u8]) -> bool {
739739
messages.iter().any(|message| {
740740
message["content"]
741741
.as_str()
742-
.is_some_and(|content| content.contains("You are a permission classifier."))
742+
.is_some_and(|content| content.starts_with("# Permission review\n\n"))
743743
})
744744
})
745745
}

‎src/crates/adapters/ai-adapters/src/permission.rs‎

Lines changed: 575 additions & 141 deletions
Large diffs are not rendered by default.

‎src/crates/assembly/core/src/agentic/execution/round_executor.rs‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,38 @@ fn normalize_deferred_tool_calls_for_replay(tool_calls: &mut [ToolCall]) {
9696
}
9797
}
9898

99+
/// Project only prior assistant tool inputs from the executing session's model
100+
/// context. Tool-result messages and assistant text never enter approval history.
101+
fn permission_tool_history(
102+
messages: &[AIMessage],
103+
) -> Vec<openbitfun_runtime_ports::PermissionDecisionToolCall> {
104+
let mut history = messages
105+
.iter()
106+
.rev()
107+
.filter(|message| message.role == "assistant")
108+
.filter_map(|message| message.tool_calls.as_ref())
109+
.flat_map(|calls| calls.iter().rev())
110+
.take(openbitfun_ai_adapters::permission::MAX_PERMISSION_HISTORY_TOOL_CALLS)
111+
.map(|call| {
112+
let deferred = (call.name == CALL_DEFERRED_TOOL_NAME)
113+
.then(|| parse_call_deferred_tool_input(&call.arguments).ok())
114+
.flatten();
115+
match deferred {
116+
Some(call) => openbitfun_runtime_ports::PermissionDecisionToolCall {
117+
tool: call.tool_name,
118+
arguments: call.args,
119+
},
120+
None => openbitfun_runtime_ports::PermissionDecisionToolCall {
121+
tool: call.name.clone(),
122+
arguments: call.arguments.clone(),
123+
},
124+
}
125+
})
126+
.collect::<Vec<_>>();
127+
history.reverse();
128+
history
129+
}
130+
99131
/// Mutable lifecycle shared by all provider attempts that belong to one
100132
/// logical model round, including attempts made after overflow recovery.
101133
#[derive(Debug)]
@@ -2203,6 +2235,13 @@ impl RoundExecutor {
22032235
permission_approval_on_ask: global_config.tool_permissions.approval.on_ask,
22042236
permission_approval_on_error: global_config.tool_permissions.approval.on_error,
22052237
current_user_message,
2238+
recent_tool_calls: if permission_approval_strategy
2239+
== PermissionApprovalStrategy::Model
2240+
{
2241+
permission_tool_history(&ai_messages)
2242+
} else {
2243+
Vec::new()
2244+
},
22062245
permission_user_input_available,
22072246
..ToolExecutionOptions::default()
22082247
};
@@ -2888,6 +2927,42 @@ pub(super) mod tests {
28882927
}
28892928
}
28902929

2930+
#[test]
2931+
fn permission_history_keeps_latest_five_inputs_in_order_without_results() {
2932+
use crate::util::types::Message as AIMessage;
2933+
let calls = (0..7)
2934+
.map(|index| openbitfun_core_types::ToolCall {
2935+
id: format!("id-{index}"),
2936+
name: format!("tool-{index}"),
2937+
arguments: json!({"index":index}),
2938+
raw_arguments: None,
2939+
})
2940+
.collect::<Vec<_>>();
2941+
let mut first = AIMessage::assistant("assistant text must be excluded".into());
2942+
first.tool_calls = Some(calls[..4].to_vec());
2943+
first.reasoning_content = Some("reasoning must be excluded".into());
2944+
let mut second = AIMessage::assistant("".into());
2945+
second.tool_calls = Some(calls[4..].to_vec());
2946+
second.tool_calls.as_mut().unwrap()[2].name = "CallDeferredTool".into();
2947+
second.tool_calls.as_mut().unwrap()[2].arguments = json!({
2948+
"tool_name":"ActualTool", "args":{"nested":{"flag":true}}
2949+
});
2950+
let mut result = AIMessage::assistant("execution result must be excluded".into());
2951+
result.role = "tool".into();
2952+
result.tool_calls = Some(calls.clone());
2953+
let history = super::permission_tool_history(&[first, result, second]);
2954+
assert_eq!(history.len(), 5);
2955+
for (offset, call) in history[..4].iter().enumerate() {
2956+
assert_eq!(call.tool, format!("tool-{}", offset + 2));
2957+
assert_eq!(call.arguments, json!({"index":offset + 2}));
2958+
}
2959+
assert_eq!(history[4].tool, "ActualTool");
2960+
assert_eq!(history[4].arguments, json!({"nested":{"flag":true}}));
2961+
let encoded = serde_json::to_string(&history).unwrap();
2962+
assert!(!encoded.contains("must be excluded"));
2963+
assert!(super::permission_tool_history(&[]).is_empty());
2964+
}
2965+
28912966
#[test]
28922967
fn model_round_lifecycle_reuses_identity_and_counts_recovery_attempts() {
28932968
let mut lifecycle = ModelRoundLifecycle::new();

0 commit comments

Comments
 (0)