use std::future::Future;
use std::pin::Pin;
use std::process::Command;
use std::sync::Arc;
use incurs::command::{CommandBuilder, CommandDef, TypedContext, TypedResult};
use incurs::errors::ValidationError;
use incurs::middleware::MiddlewareFn;
use incurs::schema::{FieldMeta, FieldType, IncurSchema};
use schemars::JsonSchema;
use serde::Serialize;
use serde_json::{Value, json};
use crate::model::{
InvocationRecord, InvocationScopeKind, InvocationSource, InvocationStatus, InvocationSurface,
now_ms,
};
use crate::process::controller;
use crate::store::Store;
#[derive(Clone)]
struct ActiveInvocation {
id: String,
command_path: String,
store: Arc<Store>,
}
tokio::task_local! {
static ACTIVE_INVOCATION: ActiveInvocation;
}
struct PurposeOptions<Options> {
purpose: String,
options: Options,
}
impl<Options: IncurSchema> IncurSchema for PurposeOptions<Options> {
fn fields() -> Vec<FieldMeta> {
let mut fields = Options::fields();
if let Some(field) = fields.iter_mut().find(|field| field.name == "purpose") {
field.required = true;
field.description = Some(
"Brief user-facing summary of what this apoc call is doing. Never include secrets.",
);
} else {
fields.push(purpose_field());
}
fields
}
fn from_raw(raw: &std::collections::BTreeMap<String, Value>) -> Result<Self, ValidationError> {
let purpose = raw
.get("purpose")
.and_then(Value::as_str)
.ok_or_else(|| purpose_error("Purpose is required"))?;
let purpose = purpose.trim();
if purpose.is_empty() {
return Err(purpose_error("Purpose cannot be empty"));
}
if purpose.chars().any(char::is_control) {
return Err(purpose_error(
"Purpose must be one line without control characters",
));
}
let mut normalized = raw.clone();
normalized.insert("purpose".to_string(), Value::String(purpose.to_string()));
Ok(Self {
purpose: purpose.to_string(),
options: Options::from_raw(&normalized)?,
})
}
}
fn purpose_field() -> FieldMeta {
FieldMeta {
name: "purpose",
cli_name: "purpose".to_string(),
description: Some(
"Brief user-facing summary of what this apoc call is doing. Never include secrets.",
),
field_type: FieldType::String,
required: true,
default: None,
alias: None,
deprecated: false,
env_name: None,
}
}
fn purpose_error(message: &str) -> ValidationError {
ValidationError {
message: message.to_string(),
field_errors: Vec::new(),
cause: None,
}
}
pub(crate) fn invocation_purpose(globals: &Value) -> &str {
globals
.get("purpose")
.and_then(Value::as_str)
.expect("tracked apoc commands always carry a validated purpose")
}
pub(crate) struct InvocationScope {
pub(crate) cwd: Option<String>,
pub(crate) kind: InvocationScopeKind,
pub(crate) key: Option<String>,
}
pub(crate) fn active_invocation_id() -> Option<String> {
ACTIVE_INVOCATION.try_with(|active| active.id.clone()).ok()
}
pub(crate) fn active_store() -> Option<Arc<Store>> {
ACTIVE_INVOCATION
.try_with(|active| Arc::clone(&active.store))
.ok()
}
pub(crate) fn invocation_scope(cwd: Option<&str>) -> InvocationScope {
let Some(cwd) = cwd else {
return InvocationScope {
cwd: None,
kind: InvocationScopeKind::Unknown,
key: None,
};
};
let cwd = std::fs::canonicalize(cwd)
.ok()
.unwrap_or_else(|| std::path::PathBuf::from(cwd));
let cwd = cwd.to_string_lossy().into_owned();
let worktree = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.current_dir(&cwd)
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty())
.and_then(|value| {
std::fs::canonicalize(value)
.ok()
.map(|path| path.to_string_lossy().into_owned())
});
match worktree {
Some(worktree) => InvocationScope {
cwd: Some(cwd),
kind: InvocationScopeKind::GitWorktree,
key: Some(worktree),
},
None => InvocationScope {
cwd: Some(cwd.clone()),
kind: InvocationScopeKind::Cwd,
key: Some(cwd),
},
}
}
pub(crate) fn invocation_middleware(store: Option<Arc<Store>>) -> MiddlewareFn {
Arc::new(move |context, next| {
let configured_store = store.clone();
Box::pin(async move {
let store = configured_store.or_else(|| Store::open_default().ok().map(Arc::new));
let Some(store) = store else {
next().await;
return;
};
let controller = controller(context.agent);
let parent_id = active_invocation_id();
let scope = invocation_scope(controller.cwd.as_deref());
let surface = match context
.request
.as_ref()
.map(|request| request.method.as_str())
{
Some("code") => InvocationSurface::Code,
Some(_) => InvocationSurface::Mcp,
None => InvocationSurface::Cli,
};
let at = now_ms();
let invocation = InvocationRecord {
id: controller.invocation_id,
parent_id,
execution_id: controller.execution_id,
source: InvocationSource::Native,
source_id: None,
source_order: None,
source_path: None,
accepted_sequence: 0,
surface,
actor: Some(controller.actor),
agent: controller.agent,
command_path: context.command.clone(),
purpose: None,
normalized_input: serde_json::json!({}),
search_text: context.command.clone(),
pattern_signature: String::new(),
variable_paths: Vec::new(),
cwd: scope.cwd,
scope_kind: scope.kind,
scope_key: scope.key,
started_at: at,
completed_at: None,
updated_at: at,
status: InvocationStatus::Running,
exit_code: None,
error_code: None,
error: None,
};
if let Err(error) = store.start_invocation(&invocation) {
tracing::warn!(error = %error, "could not record apoc invocation");
next().await;
return;
}
let active = ActiveInvocation {
id: invocation.id.clone(),
command_path: invocation.command_path,
store: Arc::clone(&store),
};
ACTIVE_INVOCATION.scope(active, next()).await;
if store
.get_invocation(&invocation.id)
.ok()
.flatten()
.is_some_and(|record| record.status == InvocationStatus::Running)
{
let _ = store.finish_invocation(
&invocation.id,
InvocationStatus::Incomplete,
Some(now_ms()),
None,
None,
None,
);
}
})
})
}
pub(crate) fn tracked_typed<Args, Options, Env, Output, Handler, HandlerFuture>(
name: impl Into<String>,
handler: Handler,
) -> CommandBuilder
where
Args: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
Options: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
Env: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
Output: JsonSchema + Serialize + Send + Sync + 'static,
Handler: Fn(TypedContext<Args, Options, Env>) -> HandlerFuture + Send + Sync + 'static,
HandlerFuture: Future<Output = TypedResult<Output>> + Send + 'static,
{
let handler = Arc::new(handler);
CommandDef::typed::<Args, PurposeOptions<Options>, Env, Output, _, _>(name, move |context| {
let handler = Arc::clone(&handler);
let TypedContext {
agent,
args,
display_name,
env,
mut globals,
options,
request,
format,
format_explicit,
name,
vars,
version,
} = context;
let PurposeOptions { purpose, options } = options;
let mut serialized_options = serde_json::to_value(&options).unwrap_or(Value::Null);
if let Some(object) = serialized_options.as_object_mut() {
object.remove("purpose");
}
let input = redact_history_input(serde_json::json!({
"purpose": &purpose,
"args": &args,
"options": serialized_options,
}));
if !globals.is_object() {
globals = json!({});
}
globals
.as_object_mut()
.expect("globals was normalized to an object")
.insert("purpose".to_string(), Value::String(purpose.clone()));
let context = TypedContext {
agent,
args,
display_name,
env,
globals,
options,
request,
format,
format_explicit,
name,
vars,
version,
};
Box::pin(async move {
let active = ACTIVE_INVOCATION.try_with(Clone::clone).ok();
if let Some(active) = &active {
let search_text = format!(
"{} {}",
active.command_path,
serde_json::to_string(&input).unwrap_or_default()
);
let _ = active.store.update_invocation_input(
&active.id,
Some(purpose),
input,
search_text,
);
}
let result = handler(context).await;
if let Some(active) = active {
finish(&active, &result);
}
result
}) as Pin<Box<dyn Future<Output = TypedResult<Output>> + Send>>
})
}
pub(crate) fn discovery_typed<Args, Options, Env, Output, Handler, HandlerFuture>(
name: impl Into<String>,
handler: Handler,
) -> CommandBuilder
where
Args: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
Options: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
Env: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
Output: JsonSchema + Serialize + Send + Sync + 'static,
Handler: Fn(TypedContext<Args, Options, Env>) -> HandlerFuture + Send + Sync + 'static,
HandlerFuture: Future<Output = TypedResult<Output>> + Send + 'static,
{
let handler = Arc::new(handler);
CommandDef::typed::<Args, Options, Env, Output, _, _>(name, move |context| {
let handler = Arc::clone(&handler);
let input = redact_history_input(serde_json::json!({
"args": &context.args,
"options": &context.options,
}));
Box::pin(async move {
let active = ACTIVE_INVOCATION.try_with(Clone::clone).ok();
if let Some(active) = &active {
let search_text = format!(
"{} {}",
active.command_path,
serde_json::to_string(&input).unwrap_or_default()
);
let _ = active
.store
.update_invocation_input(&active.id, None, input, search_text);
}
let result = handler(context).await;
if let Some(active) = active {
finish(&active, &result);
}
result
}) as Pin<Box<dyn Future<Output = TypedResult<Output>> + Send>>
})
}
fn redact_history_input(mut value: Value) -> Value {
let mut secret_material = Vec::new();
collect_secret_material(&value, None, &mut secret_material);
secret_material.sort_by_key(|value| std::cmp::Reverse(value.len()));
secret_material.dedup();
let redacted_purpose = value.get("purpose").and_then(Value::as_str).map(|purpose| {
secret_material
.iter()
.filter(|secret| !secret.is_empty())
.fold(purpose.to_string(), |value, secret| {
value.replace(secret, "[REDACTED]")
})
});
if let Some(redacted) = redacted_purpose {
value["purpose"] = Value::String(redacted);
}
redact_history_value(&mut value);
value
}
fn collect_secret_material(value: &Value, key: Option<&str>, material: &mut Vec<String>) {
match value {
Value::Array(values) => {
for value in values {
collect_secret_material(value, key, material);
}
}
Value::Object(object) => {
if key.is_some_and(is_secret_field) {
for value in object.values() {
collect_secret_reference(value, material);
}
}
for (key, value) in object {
collect_secret_material(value, Some(key), material);
}
}
Value::String(value) if key.is_some_and(is_secret_field) => {
collect_secret_reference(&Value::String(value.clone()), material);
}
_ => {}
}
}
fn collect_secret_reference(value: &Value, material: &mut Vec<String>) {
match value {
Value::String(value) => {
let reference = value
.split_once('=')
.map_or(value.as_str(), |(_, value)| value);
material.push(reference.to_string());
if let Some((_, reference)) = reference.split_once(':') {
material.push(reference.to_string());
}
}
Value::Array(values) => {
for value in values {
collect_secret_reference(value, material);
}
}
Value::Object(object) => {
let provider = object.get("provider").and_then(Value::as_str);
let reference = object.get("reference").and_then(Value::as_str);
if let Some(reference) = reference {
material.push(reference.to_string());
if let Some(provider) = provider {
material.push(format!("{provider}:{reference}"));
}
} else {
for value in object.values() {
collect_secret_reference(value, material);
}
}
}
_ => {}
}
}
fn redact_history_value(value: &mut Value) {
match value {
Value::Array(values) => {
for value in values {
redact_history_value(value);
}
}
Value::Object(object) => {
for (key, value) in object {
if key == "reference" {
*value = json!({ "redacted": true });
} else if is_secret_field(key) {
*value = redacted_secret_entries(value);
} else {
redact_history_value(value);
}
}
}
_ => {}
}
}
fn is_secret_field(key: &str) -> bool {
matches!(
key,
"secret_env"
| "secretEnv"
| "secret_header"
| "secretHeader"
| "secret_headers"
| "secretHeaders"
| "secrets"
)
}
fn redacted_secret_entries(value: &Value) -> Value {
let mut names = Vec::new();
collect_secret_names(value, &mut names);
names.sort();
names.dedup();
Value::Array(
names
.into_iter()
.map(|name| json!({ "name": name, "redacted": true }))
.collect(),
)
}
fn collect_secret_names(value: &Value, names: &mut Vec<String>) {
match value {
Value::Null => {}
Value::String(value) => {
let name = value
.split_once('=')
.map_or(value.as_str(), |(name, _)| name);
if !name.is_empty() {
names.push(name.to_string());
}
}
Value::Array(values) => {
for value in values {
collect_secret_names(value, names);
}
}
Value::Object(object) => {
names.extend(object.keys().filter(|name| !name.is_empty()).cloned());
}
_ => {}
}
}
fn finish<Output: Serialize>(active: &ActiveInvocation, result: &TypedResult<Output>) {
let (status, exit_code, error_code, error) = match result {
TypedResult::Ok {
data, exit_code, ..
} => {
link_execution(active, data);
(
if exit_code.is_some_and(|code| code != 0) {
InvocationStatus::Failed
} else {
InvocationStatus::Succeeded
},
*exit_code,
exit_code
.filter(|code| *code != 0)
.map(|_| "NONZERO_EXIT".to_string()),
None,
)
}
TypedResult::Error {
code,
message,
exit_code,
..
} => (
InvocationStatus::Failed,
*exit_code,
Some(code.clone()),
Some(message.clone()),
),
};
if let Err(error) = active.store.finish_invocation(
&active.id,
status,
Some(now_ms()),
exit_code,
error_code,
error,
) {
tracing::warn!(error = %error, "could not finish apoc invocation history");
}
}
fn link_execution<Output: Serialize>(active: &ActiveInvocation, output: &Output) {
let Ok(output) = serde_json::to_value(output) else {
return;
};
let execution_id = output
.get("execution")
.and_then(|value| value.get("id"))
.or_else(|| output.get("id"))
.and_then(serde_json::Value::as_str);
let Some(execution_id) = execution_id else {
return;
};
let _ = active
.store
.link_invocation_execution(&active.id, execution_id);
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::redact_history_input;
#[test]
fn history_input_keeps_only_secret_destination_names() {
let redacted = redact_history_input(json!({
"args": {},
"options": {
"secret_env": ["OPENAI_API_KEY=env:APOC_HISTORY_SENTINEL"],
"secret_headers": {
"Authorization": {
"provider": "env",
"reference": "APOC_PROVIDER_SENTINEL",
"version": null
}
},
"nested": {
"secrets": {
"TOKEN": {
"provider": "env",
"reference": "APOC_NESTED_SENTINEL"
}
}
}
}
}));
let encoded = serde_json::to_string(&redacted).expect("serialize redacted input");
assert!(encoded.contains("OPENAI_API_KEY"));
assert!(encoded.contains("Authorization"));
assert!(encoded.contains("TOKEN"));
assert!(!encoded.contains("env:APOC_HISTORY_SENTINEL"));
assert!(!encoded.contains("APOC_PROVIDER_SENTINEL"));
assert!(!encoded.contains("APOC_NESTED_SENTINEL"));
assert!(!encoded.contains("provider"));
assert!(!encoded.contains("reference"));
}
#[test]
fn history_input_preserves_non_secret_environment_values() {
let input = json!({
"options": {
"env": ["RUST_LOG=debug"],
"secret_env": null
}
});
let redacted = redact_history_input(input);
assert_eq!(redacted["options"]["env"], json!(["RUST_LOG=debug"]));
assert_eq!(redacted["options"]["secret_env"], json!([]));
}
#[test]
fn purpose_redacts_recognized_secret_references_before_history() {
let redacted = redact_history_input(json!({
"purpose": "Checking env:APOC_PURPOSE_SENTINEL without exposing APOC_PURPOSE_SENTINEL.",
"args": {},
"options": {
"secret_env": ["TOKEN=env:APOC_PURPOSE_SENTINEL"]
}
}));
let encoded = serde_json::to_string(&redacted).unwrap();
assert!(!encoded.contains("APOC_PURPOSE_SENTINEL"));
assert!(encoded.contains("[REDACTED]"));
assert!(encoded.contains("TOKEN"));
}
}