Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
6 changes: 6 additions & 0 deletions crates/ironclaw_host_runtime/src/first_party_tools/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ pub(crate) fn resolve_builtin_input_schema_ref(reference: &str) -> Option<Value>
"minimum": 0,
"maximum": 100,
"description": "Maximum triggers to return. Defaults to 100."
},
"run_limit": {
Comment thread
henrypark133 marked this conversation as resolved.
Resolved
"type": "integer",
"minimum": 0,
"maximum": 100,
"description": "Maximum recent runs to embed per trigger. Defaults to 25."
}
},
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ironclaw_host_api::{
};
use ironclaw_triggers::{
TriggerCompletionPolicy, TriggerError, TriggerId, TriggerRecord, TriggerRepository,
TriggerSchedule, TriggerSourceKind, TriggerState,
TriggerRunRecord, TriggerSchedule, TriggerSourceKind, TriggerState,
};
use serde::Deserialize;
use serde_json::{Value, json};
Expand All @@ -24,7 +24,9 @@ use super::{
first_party_capability_manifest, input_error, resource_profile,
};

const TRIGGER_LIST_LIMIT: usize = 100;
const TRIGGER_LIST_MAX_LIMIT: usize = 100;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit — TRIGGER_LIST_MAX_LIMIT used as both default and max while sibling pair uses separate DEFAULT/MAX.

PR introduces TRIGGER_RUN_HISTORY_DEFAULT_LIMIT + TRIGGER_RUN_HISTORY_MAX_LIMIT paired naming. Adjacent TRIGGER_LIST_MAX_LIMIT (renamed from TRIGGER_LIST_LIMIT) remains a single constant doubling as both default and max (unwrap_or(TRIGGER_LIST_MAX_LIMIT).min(TRIGGER_LIST_MAX_LIMIT)). Two different conventions for same concept in same file block.

Fix: Either add TRIGGER_LIST_DEFAULT_LIMIT separate from TRIGGER_LIST_MAX_LIMIT, or rename the new pair to TRIGGER_RUN_HISTORY_LIMIT (single constant). At minimum: comment noting the dual role.

const TRIGGER_RUN_HISTORY_DEFAULT_LIMIT: usize = 25;
Comment thread
henrypark133 marked this conversation as resolved.
Resolved
const TRIGGER_RUN_HISTORY_MAX_LIMIT: usize = 100;

pub const TRIGGER_CREATE_CAPABILITY_ID: &str = "builtin.trigger_create";
pub const TRIGGER_LIST_CAPABILITY_ID: &str = "builtin.trigger_list";
Expand Down Expand Up @@ -205,6 +207,7 @@ struct TriggerRemoveInput {
#[derive(Deserialize)]
struct TriggerListInput {
limit: Option<usize>,
run_limit: Option<usize>,
}

async fn create_trigger(
Expand Down Expand Up @@ -256,7 +259,7 @@ async fn create_trigger(
return Err(hook_error);
}
Ok(json!({
"trigger": trigger_output(&record),
"trigger": trigger_output(&record, &[]),
}))
}

Expand All @@ -268,8 +271,12 @@ async fn list_triggers(
let input: TriggerListInput = serde_json::from_value(input).map_err(|_| input_error())?;
let limit = input
.limit
.unwrap_or(TRIGGER_LIST_LIMIT)
.min(TRIGGER_LIST_LIMIT);
.unwrap_or(TRIGGER_LIST_MAX_LIMIT)
.min(TRIGGER_LIST_MAX_LIMIT);
let run_limit = input
.run_limit
Comment thread
henrypark133 marked this conversation as resolved.
Resolved
.unwrap_or(TRIGGER_RUN_HISTORY_DEFAULT_LIMIT)
.min(TRIGGER_RUN_HISTORY_MAX_LIMIT);
let records = repository
.list_scoped_triggers(
scope.tenant_id.clone(),
Expand All @@ -279,11 +286,25 @@ async fn list_triggers(
limit,
)
.await
.map_err(|error| trigger_repository_error("list_scoped_triggers", error))?
.map_err(|error| trigger_repository_error("list_scoped_triggers", error))?;
let trigger_ids = records
.iter()
.map(|record| record.trigger_id)
.collect::<Vec<_>>();
let mut runs_by_trigger = repository
.list_trigger_run_history_batch(scope.tenant_id.clone(), &trigger_ids, run_limit)
Comment thread
henrypark133 marked this conversation as resolved.
Resolved
.await
.map_err(|error| trigger_repository_error("list_trigger_run_history_batch", error))?;
let output = records
.into_iter()
.map(|record| trigger_output(&record))
.map(|record| {
let runs = runs_by_trigger
.remove(&record.trigger_id)
.unwrap_or_default();
trigger_output(&record, &runs)
})
.collect::<Vec<_>>();
Ok(json!({ "triggers": records }))
Ok(json!({ "triggers": output }))
}

async fn remove_trigger(
Expand All @@ -309,7 +330,7 @@ async fn remove_trigger(
}))
}

fn trigger_output(record: &TriggerRecord) -> Value {
fn trigger_output(record: &TriggerRecord, recent_runs: &[TriggerRunRecord]) -> Value {
json!({
"trigger_id": record.trigger_id.to_string(),
"agent_id": record.agent_id.as_ref().map(|id| id.as_str()),
Expand All @@ -322,11 +343,23 @@ fn trigger_output(record: &TriggerRecord) -> Value {
"next_run_at": record.next_run_at,
"last_run_at": record.last_run_at,
"last_status": record.last_status,
"recent_runs": recent_runs.iter().map(trigger_run_output).collect::<Vec<_>>(),
"is_active": record.has_active_fire(),
"created_at": record.created_at,
})
}

fn trigger_run_output(run: &TriggerRunRecord) -> Value {
json!({
"fire_slot": run.fire_slot,
"run_id": run.run_id.as_ref().map(ToString::to_string),
"thread_id": run.thread_id.as_str(),
"status": run.status,
"submitted_at": run.submitted_at,
"completed_at": run.completed_at,
})
}

fn trigger_remove_output(record: &TriggerRecord) -> Value {
json!({
"trigger_id": record.trigger_id.to_string(),
Expand Down
Loading
Loading