Add event-triggered routines and workflow skill templates (#756)

* Add event-triggered routines and workflow skill templates

* fix(ci): secrets can't be used in step if conditions [skip-regression-check] (#787)

GitHub Actions step-level `if:` doesn't have access to `secrets` context.
Replace `if: secrets.X != ''` with `continue-on-error: true` and let
the Set token step handle the fallback.

Co-authored-by: Claude Sonnet 4.6 <[email protected]>

* fix(ci): clean up staging pipeline — remove hacks, skip redundant checks [skip-regression-check] (#794)

- Remove continue-on-error from staging-ci.yml app token steps (secrets are configured)
- Skip test.yml and code_style.yml on PRs targeting staging (staging-ci.yml
  already runs tests before promoting, promotion PR gets full CI on main)
- Allow ironclaw-ci[bot] in Claude Code review for bot-created promotion PRs

Co-authored-by: Claude Opus 4.6 <[email protected]>

* fix: address PR review feedback for event_emit security and quality

Security fixes:
- Require approval (UnlessAutoApproved) for event_emit, matching routine_fire
- Enable sanitization on event_emit payload (external JSON reaches LLM)
- Remove user_id parameter from event_emit to prevent IDOR — always use ctx.user_id

Correctness fixes:
- Rename source → event_source in event_emit for consistency with routine_create
- Use json_value_as_filter_string for filter parsing (handles numbers/booleans)
- Case-insensitive matching for event source and event_type
- Add debug logging for missing filter keys in payload
- Fix skill_install_routine_webhook_sim test missing .with_skills()
- Fix schema_validator test for event_emit payload properties

Code quality:
- Move EventEmitTool struct/impl after RoutineHistoryTool (fix split layout)
- Deduplicate routine_to_info into RoutineInfo::from_routine in types.rs
- Add test section headers in e2e_routine_heartbeat.rs
- Clarify event_emit description to specify system_event routines only

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(ci): run fmt + clippy on staging PRs, skip Windows clippy [skip-regression-check] (#802)

- Remove branches:[main] filter from code_style.yml so it runs on all PRs
- Gate clippy-windows with `if: github.base_ref == 'main'` (skip on staging PRs)
- Update rollup job to allow skipped clippy-windows
- Simplify claude-review.yml to only trigger on labeled event (avoids duplicate runs)

Co-authored-by: Claude Opus 4.6 <[email protected]>

* feat: persist user_id in save_job and expose job_id on routine runs (#709)

* feat: persist worker events to DB and fix activity tab rendering

In-process Worker (used by Scheduler::dispatch_job) now persists events
via save_job_event at key execution points: plan creation, LLM
responses, tool_use, tool_result, and job completion/failure/stuck.
Event data shapes match the container worker format so the gateway
activity tab renders them correctly.

Frontend: tool_result errors now show a red X icon with danger styling
instead of a silent empty output. The result event falls back to the
error field when message is absent.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* feat: wire RoutineEngine into gateway for direct manual trigger firing

Replace the message-channel hack in routines_trigger_handler with a
direct call to RoutineEngine::fire_manual(), ensuring FullJob routines
dispatch correctly when triggered from the web UI. Inject the engine
into GatewayState from Agent::run after construction.

Also persists user_id in save_job for both PG and libSQL backends,
removes the source='sandbox' filter so all jobs are visible, and
exposes job_id on RoutineRunInfo for the frontend job link.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: remove stale gateway_state argument from Agent::new test call sites

The gateway_state parameter was removed from Agent::new during rebase
(replaced by post-construction set_routine_engine_slot), but three test
call sites still passed the extra None argument.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: address PR review — restore sandbox source filter, remove blank lines

- Revert removal of `source = 'sandbox'` filter in all SandboxStore
  queries (8 sites across PG and libSQL). Sandbox-specific APIs should
  stay scoped to sandbox jobs; unified job listing for the Jobs tab
  should use a separate query path.
- Remove extra blank lines in agent_loop.rs and worker.rs that caused
  formatting CI failure.

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: address review — regenerate Cargo.lock, add user_id regression test

- Regenerate Cargo.lock from main's lockfile to eliminate dependency
  version downgrades (anyhow, syn, etc.) that were churn from rebase.
- Add regression test verifying user_id round-trips through save_job
  and get_job in the libSQL backend.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* style: remove trailing blank line in libsql jobs.rs

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* test: add Postgres-side regression test for user_id persistence in save_job

Mirrors the existing libSQL test (test_save_job_persists_user_id) for the
Postgres backend. Gated behind #[cfg(feature = "postgres")] + #[ignore]
since it requires a running PostgreSQL instance (integration tier).

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>

* fix: make routine_system_event_emit test create routine before emitting

- Add routine_create step to trace fixture so event_emit has a matching
  routine to fire
- Assert fired_routines > 0, not just key presence (Copilot review)
- Add .with_auto_approve_tools(true) since event_emit now requires approval

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: renumber test headers after system_event test insertion

Test 4 was duplicated (routine_cooldown and heartbeat_findings).
Renumber heartbeat_findings to Test 5 and heartbeat_empty_skip to Test 6.

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: merge staging and add missing RoutineEngine args in test

RoutineEngine::new on staging requires `tools` and `safety` params.
Update system_event_trigger_matches_and_filters test to pass them.

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: address new Copilot review comments

- Add .with_auto_approve_tools(true) to skill_install_routine_webhook_sim
  test so event_emit doesn't block on approval
- Fix module-level doc comment for event_emit to specify system_event trigger

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: deduplicate json_value_as_string helper

Remove private `json_value_as_string` from routine_engine.rs and use
the identical public `json_value_as_filter_string` from routine.rs,
eliminating divergence risk. (Copilot review)

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Henry Park <[email protected]>
Co-authored-by: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
Illia Polosukhin
2026-03-10 11:08:04 -07:00
committed by GitHub
co-authored by Henry Park Claude Sonnet 4.6
parent e8f8ec06e3
commit 6e1ed939cc
20 changed files with 1090 additions and 150 deletions
+115 -2
View File
@@ -27,6 +27,8 @@ mod tests {
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.with_auto_approve_tools(true)
.with_skills()
.build()
.await;
@@ -60,6 +62,8 @@ mod tests {
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.with_auto_approve_tools(true)
.with_skills()
.build()
.await;
@@ -97,6 +101,8 @@ mod tests {
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.with_auto_approve_tools(true)
.with_skills()
.build()
.await;
@@ -197,7 +203,114 @@ mod tests {
}
// -----------------------------------------------------------------------
// Test 6: job_create_status
// Test 6: routine_system_event_emit
// -----------------------------------------------------------------------
#[tokio::test]
async fn routine_system_event_emit() {
let trace = LlmTrace::from_file(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/llm_traces/tools/routine_system_event_emit.json"
))
.expect("failed to load routine_system_event_emit.json");
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.with_auto_approve_tools(true)
.build()
.await;
rig.send_message("Create a system-event routine and emit an event")
.await;
let responses = rig.wait_for_responses(1, Duration::from_secs(15)).await;
rig.verify_trace_expects(&trace, &responses);
let completed = rig.tool_calls_completed();
assert!(
completed.iter().any(|(n, ok)| n == "event_emit" && *ok),
"event_emit should succeed: {completed:?}"
);
let results = rig.tool_results();
let emit_result = results
.iter()
.find(|(n, _)| n == "event_emit")
.expect("event_emit result missing");
assert!(
emit_result.1.contains("fired_routines"),
"event_emit should report fired routine count: {:?}",
emit_result.1
);
// Verify at least one routine actually fired (not just that the key exists).
let emit_json: serde_json::Value =
serde_json::from_str(&emit_result.1).expect("event_emit result should be valid JSON");
assert!(
emit_json["fired_routines"].as_u64().unwrap_or(0) > 0,
"event_emit should have fired at least one routine: {:?}",
emit_result.1
);
rig.shutdown();
}
// -----------------------------------------------------------------------
// Test 7: skill_install_routine_webhook_sim
// -----------------------------------------------------------------------
#[tokio::test]
async fn skill_install_routine_webhook_sim() {
let trace = LlmTrace::from_file(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/llm_traces/tools/skill_install_routine_webhook_sim.json"
))
.expect("failed to load skill_install_routine_webhook_sim.json");
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.with_skills()
.with_auto_approve_tools(true)
.build()
.await;
rig.send_message("Install the workflow skill template and simulate a webhook routine run")
.await;
let responses = rig.wait_for_responses(1, Duration::from_secs(20)).await;
rig.verify_trace_expects(&trace, &responses);
let completed = rig.tool_calls_completed();
assert!(
completed.iter().any(|(n, _)| n == "skill_install"),
"skill_install should be called: {completed:?}"
);
for tool in &["routine_create", "event_emit", "routine_history"] {
assert!(
completed.iter().any(|(n, ok)| n == tool && *ok),
"{tool} should succeed: {completed:?}"
);
}
let results = rig.tool_results();
let emit_result = results
.iter()
.find(|(n, _)| n == "event_emit")
.expect("event_emit result missing");
assert!(
emit_result.1.contains("fired_routines"),
"event_emit should include fired_routines: {:?}",
emit_result.1
);
let _history_result = results
.iter()
.find(|(n, _)| n == "routine_history")
.expect("routine_history result missing");
rig.shutdown();
}
// -----------------------------------------------------------------------
// Test 8: job_create_status
// -----------------------------------------------------------------------
// Uses {{call_cj_1.job_id}} template to forward the dynamic UUID from
// create_job's result into job_status's arguments.
@@ -266,7 +379,7 @@ mod tests {
}
// -----------------------------------------------------------------------
// Test 7: job_list_cancel
// Test 9: job_list_cancel
// -----------------------------------------------------------------------
// Uses {{call_cj_lc.job_id}} template to forward the dynamic UUID from
// create_job into cancel_job.
+147 -3
View File
@@ -255,7 +255,151 @@ mod tests {
}
// -----------------------------------------------------------------------
// Test 3: routine_cooldown
// Test 3: system_event_trigger_matches_and_filters
// -----------------------------------------------------------------------
#[tokio::test]
async fn system_event_trigger_matches_and_filters() {
let (db, _tmp) = create_test_db().await;
let ws = create_workspace(&db);
let trace = LlmTrace::single_turn(
"test-system-event-match",
"event",
vec![TraceStep {
request_hint: None,
response: TraceResponse::Text {
content: "System event handled".to_string(),
input_tokens: 40,
output_tokens: 8,
},
expected_tool_results: vec![],
}],
);
let llm = Arc::new(TraceLlm::from_trace(trace));
let (notify_tx, _notify_rx) = tokio::sync::mpsc::channel(16);
// Create minimal ToolRegistry and SafetyLayer for test.
let tools = Arc::new(ToolRegistry::new());
let safety_config = SafetyConfig {
max_output_length: 100_000,
injection_check_enabled: true,
};
let safety = Arc::new(SafetyLayer::new(&safety_config));
let engine = Arc::new(RoutineEngine::new(
RoutineConfig::default(),
db.clone(),
llm,
ws,
notify_tx,
None,
tools,
safety,
));
let mut filters = std::collections::HashMap::new();
filters.insert("repository".to_string(), "nearai/ironclaw".to_string());
let routine = make_routine(
"github-issue-opened",
Trigger::SystemEvent {
source: "github".to_string(),
event_type: "issue.opened".to_string(),
filters,
},
"Summarize the issue and propose an implementation plan.",
);
db.create_routine(&routine).await.expect("create_routine");
engine.refresh_event_cache().await;
// Matching event should fire.
let fired = engine
.emit_system_event(
"github",
"issue.opened",
&serde_json::json!({
"repository": "nearai/ironclaw",
"issue_number": 42
}),
Some("default"),
)
.await;
assert_eq!(fired, 1, "Expected one routine to fire for matching event");
tokio::time::sleep(Duration::from_millis(300)).await;
let runs = db
.list_routine_runs(routine.id, 10)
.await
.expect("list runs");
assert!(
!runs.is_empty(),
"Expected run history after matching event"
);
// Wrong event type should not fire.
let fired_wrong_type = engine
.emit_system_event(
"github",
"issue.closed",
&serde_json::json!({"repository": "nearai/ironclaw"}),
Some("default"),
)
.await;
assert_eq!(
fired_wrong_type, 0,
"Expected no routine for wrong event type"
);
// Wrong filter value should not fire.
let fired_wrong_filter = engine
.emit_system_event(
"github",
"issue.opened",
&serde_json::json!({"repository": "other/repo"}),
Some("default"),
)
.await;
assert_eq!(
fired_wrong_filter, 0,
"Expected no routine for filter mismatch"
);
// Case-insensitive source/event_type should still match.
let fired_case = engine
.emit_system_event(
"GitHub",
"Issue.Opened",
&serde_json::json!({
"repository": "nearai/ironclaw",
"issue_number": 99
}),
Some("default"),
)
.await;
assert_eq!(
fired_case, 1,
"Expected case-insensitive match on source/event_type"
);
// Case-insensitive filter values should match.
let fired_filter_case = engine
.emit_system_event(
"github",
"issue.opened",
&serde_json::json!({"repository": "NearAI/IronClaw"}),
Some("default"),
)
.await;
assert_eq!(
fired_filter_case, 1,
"Expected case-insensitive match on filter values"
);
}
// -----------------------------------------------------------------------
// Test 4: routine_cooldown
// -----------------------------------------------------------------------
#[tokio::test]
@@ -345,7 +489,7 @@ mod tests {
}
// -----------------------------------------------------------------------
// Test 4: heartbeat_findings
// Test 5: heartbeat_findings
// -----------------------------------------------------------------------
#[tokio::test]
@@ -407,7 +551,7 @@ mod tests {
}
// -----------------------------------------------------------------------
// Test 5: heartbeat_empty_skip
// Test 6: heartbeat_empty_skip
// -----------------------------------------------------------------------
#[tokio::test]
@@ -0,0 +1,64 @@
{
"model_name": "test-routine-system-event-emit",
"expects": {
"tools_used": ["routine_create", "event_emit"],
"all_tools_succeeded": true,
"tool_results_contain": {
"event_emit": "fired_routines"
}
},
"steps": [
{
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_rc_1",
"name": "routine_create",
"arguments": {
"name": "gh-issue-emit-test",
"description": "React to GitHub issue.opened events",
"trigger_type": "system_event",
"event_source": "github",
"event_type": "issue.opened",
"action_type": "full_job",
"prompt": "Summarize the new issue and propose next steps."
}
}
],
"input_tokens": 80,
"output_tokens": 30
}
},
{
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_ee_1",
"name": "event_emit",
"arguments": {
"event_source": "github",
"event_type": "issue.opened",
"payload": {
"repository": "nearai/ironclaw",
"issue_number": 123,
"title": "Support event-driven project workflow"
}
}
}
],
"input_tokens": 140,
"output_tokens": 28
}
},
{
"response": {
"type": "text",
"content": "Created a system-event routine and emitted a matching GitHub event. The routine fired successfully.",
"input_tokens": 200,
"output_tokens": 18
}
}
]
}
@@ -0,0 +1,100 @@
{
"model_name": "test-skill-install-routine-webhook-sim",
"expects": {
"tools_used": ["skill_install", "routine_create", "event_emit", "routine_history"],
"tool_results_contain": {
"event_emit": "fired_routines"
}
},
"steps": [
{
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_skill_install_1",
"name": "skill_install",
"arguments": {
"name": "wf-orchestrator-trace-install-1",
"content": "---\nname: wf-orchestrator-trace-install-1\ndescription: Minimal workflow skill for trace install validation\nactivation:\n keywords: [\"workflow\", \"orchestrator\"]\n---\n\nYou are a minimal workflow skill used for trace install validation.\n"
}
}
],
"input_tokens": 120,
"output_tokens": 32
}
},
{
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_routine_create_1",
"name": "routine_create",
"arguments": {
"name": "wf-webhook-sim-trace",
"description": "Trace routine to simulate webhook event flow",
"trigger_type": "system_event",
"event_source": "github",
"event_type": "issue.opened",
"event_filters": {
"repository": "nearai/ironclaw"
},
"action_type": "full_job",
"prompt": "When issue webhook event arrives, start implementation loop and create branch/PR updates."
}
}
],
"input_tokens": 170,
"output_tokens": 36
}
},
{
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_event_emit_1",
"name": "event_emit",
"arguments": {
"event_source": "github",
"event_type": "issue.opened",
"payload": {
"repository": "nearai/ironclaw",
"issue_number": 4242,
"sender": "trace-bot"
}
}
}
],
"input_tokens": 210,
"output_tokens": 28
}
},
{
"response": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_routine_history_1",
"name": "routine_history",
"arguments": {
"name": "wf-webhook-sim-trace",
"limit": 5
}
}
],
"input_tokens": 240,
"output_tokens": 22
}
},
{
"response": {
"type": "text",
"content": "Installed the skill template, created a system-event routine, emitted a webhook-equivalent event, and verified the routine run history.",
"input_tokens": 280,
"output_tokens": 25
}
}
]
}
+38 -1
View File
@@ -379,6 +379,8 @@ pub struct TestRigBuilder {
llm: Option<Arc<dyn LlmProvider>>,
max_tool_iterations: usize,
injection_check: bool,
auto_approve_tools: Option<bool>,
enable_skills: bool,
enable_routines: bool,
http_exchanges: Vec<HttpExchange>,
extra_tools: Vec<Arc<dyn Tool>>,
@@ -392,6 +394,8 @@ impl TestRigBuilder {
llm: None,
max_tool_iterations: 10,
injection_check: false,
auto_approve_tools: None,
enable_skills: false,
enable_routines: false,
http_exchanges: Vec::new(),
extra_tools: Vec::new(),
@@ -432,6 +436,18 @@ impl TestRigBuilder {
self
}
/// Override agent-level automatic approval of `UnlessAutoApproved` tools.
pub fn with_auto_approve_tools(mut self, enable: bool) -> Self {
self.auto_approve_tools = Some(enable);
self
}
/// Enable skill discovery and registration for this test rig.
pub fn with_skills(mut self) -> Self {
self.enable_skills = true;
self
}
/// Enable the routines system so the scheduler is wired with a `RoutineEngine`,
/// allowing routine jobs to actually execute. Routine tools are always registered
/// but require the engine to dispatch jobs.
@@ -466,6 +482,8 @@ impl TestRigBuilder {
llm,
max_tool_iterations,
injection_check,
auto_approve_tools,
enable_skills,
enable_routines,
http_exchanges: explicit_http_exchanges,
extra_tools,
@@ -491,6 +509,10 @@ impl TestRigBuilder {
let mut config = Config::for_testing(db_path, skills_dir, installed_skills_dir);
config.agent.max_tool_iterations = max_tool_iterations;
config.safety.injection_check_enabled = injection_check;
config.skills.enabled = enable_skills;
if let Some(v) = auto_approve_tools {
config.agent.auto_approve_tools = v;
}
// 3. Create SessionManager + LogBroadcaster.
let session = Arc::new(SessionManager::new(SessionConfig::default()));
@@ -540,7 +562,7 @@ impl TestRigBuilder {
);
builder.with_database(Arc::clone(&db));
builder.with_llm(llm);
let components = builder
let mut components = builder
.build_all()
.await
.expect("AppBuilder::build_all() failed in test rig");
@@ -583,6 +605,21 @@ impl TestRigBuilder {
.register_routine_tools(Arc::clone(db_arc), engine);
}
// Skills tools: ensure tests use temp skill dirs (sandbox-safe) even if
// AppBuilder did not wire them for this environment.
if enable_skills {
let registry = Arc::new(std::sync::RwLock::new(
ironclaw::skills::SkillRegistry::new(temp_dir.path().join("skills"))
.with_installed_dir(temp_dir.path().join("installed_skills")),
));
let catalog = ironclaw::skills::catalog::shared_catalog();
components
.tools
.register_skill_tools(Arc::clone(&registry), Arc::clone(&catalog));
components.skill_registry = Some(registry);
components.skill_catalog = Some(catalog);
}
// Register any extra test-specific tools.
for tool in extra_tools {
components.tools.register(tool).await;