Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 9 additions & 7 deletions .agents/skills/web-quality-audit/scripts/analyze.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ usage() {
exit 1
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

if [ -z "$1" ]; then
if [[ -z "$1" ]]; then
usage
fi

Expand Down Expand Up @@ -56,14 +56,16 @@ analyze_html() {
if grep -qE 'http://' "$file"; then
WARNINGS+=("$file: Contains non-HTTPS URLs")
fi

return 0
}

# Process files
if [ -d "$TARGET" ]; then
find "$TARGET" -name "*.html" -o -name "*.htm" | while read -r file; do
if [[ -d "$TARGET" ]]; then
while IFS= read -r -d '' file; do
analyze_html "$file"
done
elif [ -f "$TARGET" ]; then
done < <(find "$TARGET" \( -name "*.html" -o -name "*.htm" \) -print0)
elif [[ -f "$TARGET" ]]; then
Comment thread
coderabbitai[bot] marked this conversation as resolved.
analyze_html "$TARGET"
else
echo "Error: $TARGET is not a valid file or directory" >&2
Expand All @@ -74,14 +76,14 @@ fi
echo '{'
echo ' "issues": ['
for i in "${!ISSUES[@]}"; do
if [ $i -gt 0 ]; then echo ','; fi
if [[ $i -gt 0 ]]; then echo ','; fi
echo -n " \"${ISSUES[$i]}\""
done
echo ''
echo ' ],'
echo ' "warnings": ['
for i in "${!WARNINGS[@]}"; do
if [ $i -gt 0 ]; then echo ','; fi
if [[ $i -gt 0 ]]; then echo ','; fi
echo -n " \"${WARNINGS[$i]}\""
Comment on lines +79 to 87
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Escape JSON string values before emitting output.

At Lines [80] and [87], raw strings are injected into JSON. Filenames containing quotes, backslashes, or newlines can produce invalid JSON.

Suggested fix
+json_escape() {
+  local s="$1"
+  s=${s//\\/\\\\}
+  s=${s//\"/\\\"}
+  s=${s//$'\n'/\\n}
+  s=${s//$'\r'/\\r}
+  s=${s//$'\t'/\\t}
+  printf '%s' "$s"
+}
+
 # Output results as JSON
 echo '{'
 echo '  "issues": ['
 for i in "${!ISSUES[@]}"; do
   if [[ $i -gt 0 ]]; then echo ','; fi
-  echo -n "    \"${ISSUES[$i]}\""
+  printf '    "%s"' "$(json_escape "${ISSUES[$i]}")"
 done
 echo ''
 echo '  ],'
 echo '  "warnings": ['
 for i in "${!WARNINGS[@]}"; do
   if [[ $i -gt 0 ]]; then echo ','; fi
-  echo -n "    \"${WARNINGS[$i]}\""
+  printf '    "%s"' "$(json_escape "${WARNINGS[$i]}")"
 done
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [[ $i -gt 0 ]]; then echo ','; fi
echo -n " \"${ISSUES[$i]}\""
done
echo ''
echo ' ],'
echo ' "warnings": ['
for i in "${!WARNINGS[@]}"; do
if [ $i -gt 0 ]; then echo ','; fi
if [[ $i -gt 0 ]]; then echo ','; fi
echo -n " \"${WARNINGS[$i]}\""
json_escape() {
local s="$1"
s=${s//\\/\\\\}
s=${s//\"/\\\"}
s=${s//$'\n'/\\n}
s=${s//$'\r'/\\r}
s=${s//$'\t'/\\t}
printf '%s' "$s"
}
# Output results as JSON
echo '{'
echo ' "issues": ['
for i in "${!ISSUES[@]}"; do
if [[ $i -gt 0 ]]; then echo ','; fi
printf ' "%s"' "$(json_escape "${ISSUES[$i]}")"
done
echo ''
echo ' ],'
echo ' "warnings": ['
for i in "${!WARNINGS[@]}"; do
if [[ $i -gt 0 ]]; then echo ','; fi
printf ' "%s"' "$(json_escape "${WARNINGS[$i]}")"
done
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.agents/skills/web-quality-audit/scripts/analyze.sh around lines 79 - 87,
The script injects raw ISSUE and WARNING strings into JSON (echo -n "   
\"${ISSUES[$i]}\"" and echo -n "    \"${WARNINGS[$i]}\"") which can break JSON
when values contain quotes, backslashes, or newlines; add a helper function
(e.g., escape_json_string) that replaces backslash -> \\\\, double-quote ->
\\\", newline -> \\n, tab -> \\t, carriage return -> \\r, formfeed -> \\f (or
use jq -R -s / printf with %q if available) and call that function when emitting
values from the ISSUES and WARNINGS arrays so you print escaped strings instead
of raw "${ISSUES[$i]}" and "${WARNINGS[$i]}".

done
echo ''
Expand Down
244 changes: 142 additions & 102 deletions clients/agent-runtime/src/agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::agent::dispatcher::{
};
use crate::agent::memory_loader::{DefaultMemoryLoader, MemoryLoader};
use crate::agent::mission::{
MissionCoordinator, MissionOutcome, MissionPlan, MissionResumeMetadata, MissionState,
MissionTerminationReason,
MissionCheckpoint, MissionCoordinator, MissionOutcome, MissionPlan, MissionResumeMetadata,
MissionState, MissionTerminationReason,
};
use crate::agent::prompt::{PromptContext, SystemPromptBuilder};
use crate::config::Config;
Expand Down Expand Up @@ -829,6 +829,7 @@ impl Agent {
.checkpoints
.get(checkpoint_position)
.map(|checkpoint| checkpoint.index);

if let Err(reason) = coordinator.enforce_pre_checkpoint() {
return self.terminated_mission_outcome(
coordinator,
Expand All @@ -841,137 +842,176 @@ impl Agent {
}

let checkpoint = plan.checkpoints[checkpoint_position].clone();
let checkpoint_start = Instant::now();
self.observer
.record_event(&ObserverEvent::MissionCheckpointProgress {
mission_id: mission_id.to_string(),
checkpoint_index: checkpoint.index,
status: "started".to_string(),
duration: std::time::Duration::ZERO,
});
match self.execute_mission_checkpoint(&checkpoint).await {
Ok(_) => {
let checkpoint_elapsed_ms =
u64::try_from(checkpoint_start.elapsed().as_millis()).unwrap_or(u64::MAX);
if let Err(reason) =
coordinator.record_checkpoint_accounting(checkpoint_elapsed_ms, 0)
{
return self.terminated_mission_outcome(
let (next_plan, outcome, increment) = self
.process_mission_checkpoint(
coordinator,
mission_id,
mission_start,
plan,
&checkpoint,
)
.await?;

plan = next_plan;
if let Some(outcome) = outcome {
return Ok(outcome);
}

if increment {
checkpoint_position = checkpoint_position.saturating_add(1);
}
}

coordinator
.transition(MissionState::Completed)
.map_err(Self::mission_error)?;
let resume_metadata = coordinator.resume_metadata().map_err(Self::mission_error)?;
let checkpoints_completed = resume_metadata
.last_successful_checkpoint
.map_or(0, |index| index + 1);
self.observer
.record_event(&ObserverEvent::MissionCompleted {
mission_id: mission_id.to_string(),
checkpoints_completed,
duration: mission_start.elapsed(),
});

Ok(MissionOutcome {
mission_id: mission_id.to_string(),
state: MissionState::Completed,
termination: None,
checkpoints_completed,
resume_metadata,
})
}

#[allow(clippy::too_many_arguments)]
async fn process_mission_checkpoint(
&mut self,
coordinator: &MissionCoordinator,
mission_id: &str,
mission_start: Instant,
mut plan: MissionPlan,
checkpoint: &MissionCheckpoint,
) -> Result<(MissionPlan, Option<MissionOutcome>, bool)> {
let checkpoint_start = Instant::now();
self.observer
.record_event(&ObserverEvent::MissionCheckpointProgress {
mission_id: mission_id.to_string(),
checkpoint_index: checkpoint.index,
status: "started".to_string(),
duration: std::time::Duration::ZERO,
});

match self.execute_mission_checkpoint(checkpoint).await {
Ok(_) => {
let checkpoint_elapsed_ms =
u64::try_from(checkpoint_start.elapsed().as_millis()).unwrap_or(u64::MAX);
if let Err(reason) =
coordinator.record_checkpoint_accounting(checkpoint_elapsed_ms, 0)
{
return Ok((
plan,
Some(self.terminated_mission_outcome(
coordinator,
mission_id,
reason,
mission_start,
Some(checkpoint.index),
false,
);
}

coordinator
.record_checkpoint_success(checkpoint.index)
.map_err(Self::mission_error)?;
self.observer
.record_event(&ObserverEvent::MissionCheckpointProgress {
mission_id: mission_id.to_string(),
checkpoint_index: checkpoint.index,
status: "completed".to_string(),
duration: checkpoint_start.elapsed(),
});
checkpoint_position = checkpoint_position.saturating_add(1);
)?),
false,
));
}
Err(error) => {
let checkpoint_elapsed_ms =
u64::try_from(checkpoint_start.elapsed().as_millis()).unwrap_or(u64::MAX);
if let Err(reason) =
coordinator.record_checkpoint_accounting(checkpoint_elapsed_ms, 0)
{
return self.terminated_mission_outcome(

coordinator
.record_checkpoint_success(checkpoint.index)
.map_err(Self::mission_error)?;
self.observer
.record_event(&ObserverEvent::MissionCheckpointProgress {
mission_id: mission_id.to_string(),
checkpoint_index: checkpoint.index,
status: "completed".to_string(),
duration: checkpoint_start.elapsed(),
});
Ok((plan, None, true))
}
Err(error) => {
let checkpoint_elapsed_ms =
u64::try_from(checkpoint_start.elapsed().as_millis()).unwrap_or(u64::MAX);
if let Err(reason) =
coordinator.record_checkpoint_accounting(checkpoint_elapsed_ms, 0)
{
return Ok((
plan,
Some(self.terminated_mission_outcome(
coordinator,
mission_id,
reason,
mission_start,
Some(checkpoint.index),
false,
);
}
)?),
false,
));
}

let reason_text = error.to_string();
self.observer.record_event(&ObserverEvent::Error {
component: "mission".to_string(),
message: Self::sanitize_observer_error(
&reason_text,
&[("X-Mission-Context".to_string(), "checkpoint".to_string())],
&reason_text,
),
let reason_text = error.to_string();
self.observer.record_event(&ObserverEvent::Error {
component: "mission".to_string(),
message: Self::sanitize_observer_error(
&reason_text,
&[("X-Mission-Context".to_string(), "checkpoint".to_string())],
&reason_text,
),
});
let recoverable = coordinator.should_replan(&reason_text);
self.observer
.record_event(&ObserverEvent::MissionCheckpointProgress {
mission_id: mission_id.to_string(),
checkpoint_index: checkpoint.index,
status: "failed".to_string(),
duration: checkpoint_start.elapsed(),
});
let recoverable = coordinator.should_replan(&reason_text);
coordinator
.record_checkpoint_failure(checkpoint.index, reason_text.clone(), recoverable)
.map_err(Self::mission_error)?;

if recoverable {
coordinator
.transition(MissionState::Replanning)
.map_err(Self::mission_error)?;
self.observer
.record_event(&ObserverEvent::MissionCheckpointProgress {
mission_id: mission_id.to_string(),
checkpoint_index: checkpoint.index,
status: "failed".to_string(),
duration: checkpoint_start.elapsed(),
status: "replanning".to_string(),
duration: std::time::Duration::ZERO,
});
plan = self.replan_after_failure(&plan, checkpoint.index, &reason_text);
coordinator
.record_checkpoint_failure(
checkpoint.index,
reason_text.clone(),
recoverable,
)
.transition(MissionState::Planned)
.map_err(Self::mission_error)?;
coordinator
.transition(MissionState::Active)
.map_err(Self::mission_error)?;
return Ok((plan, None, false));
}

if recoverable {
coordinator
.transition(MissionState::Replanning)
.map_err(Self::mission_error)?;
self.observer
.record_event(&ObserverEvent::MissionCheckpointProgress {
mission_id: mission_id.to_string(),
checkpoint_index: checkpoint.index,
status: "replanning".to_string(),
duration: std::time::Duration::ZERO,
});
plan = self.replan_after_failure(&plan, checkpoint.index, &reason_text);
coordinator
.transition(MissionState::Planned)
.map_err(Self::mission_error)?;
coordinator
.transition(MissionState::Active)
.map_err(Self::mission_error)?;
continue;
}
return self.terminated_mission_outcome(
Ok((
plan,
Some(self.terminated_mission_outcome(
coordinator,
mission_id,
Self::mission_termination_reason_from_error(&reason_text),
mission_start,
Some(checkpoint.index),
false,
);
}
)?),
false,
))
}
}

coordinator
.transition(MissionState::Completed)
.map_err(Self::mission_error)?;
let resume_metadata = coordinator.resume_metadata().map_err(Self::mission_error)?;
let checkpoints_completed = resume_metadata
.last_successful_checkpoint
.map_or(0, |index| index + 1);
self.observer
.record_event(&ObserverEvent::MissionCompleted {
mission_id: mission_id.to_string(),
checkpoints_completed,
duration: mission_start.elapsed(),
});

Ok(MissionOutcome {
mission_id: mission_id.to_string(),
state: MissionState::Completed,
termination: None,
checkpoints_completed,
resume_metadata,
})
}

pub async fn run_mission(
Expand Down
Loading
Loading