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
6 changes: 4 additions & 2 deletions coderhelm-jira/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ exports.handler = async (event, context) => {
let fields = {};
if (issue.id) {
try {
const res = await api.asApp().requestJira(route`/rest/api/3/issue/${issue.id}?fields=summary,description,labels,assignee,reporter,creator,project,attachment`);
const res = await api.asApp().requestJira(route`/rest/api/3/issue/${issue.id}?fields=summary,description,labels,assignee,reporter,creator,project,attachment,issuetype`);
if (res.ok) {
const full = await res.json();
fields = full.fields || {};
Expand Down Expand Up @@ -128,6 +128,7 @@ exports.handler = async (event, context) => {
reporter: fields.reporter,
creator: fields.creator,
project: fields.project,
issuetype: fields.issuetype ? { name: fields.issuetype.name } : undefined,
},
},
comment: {
Expand Down Expand Up @@ -158,7 +159,7 @@ exports.handler = async (event, context) => {
let fields = {};
if (issue.id) {
try {
const res = await api.asApp().requestJira(route`/rest/api/3/issue/${issue.id}?fields=summary,description,labels,assignee,reporter,creator,project,attachment`);
const res = await api.asApp().requestJira(route`/rest/api/3/issue/${issue.id}?fields=summary,description,labels,assignee,reporter,creator,project,attachment,issuetype`);
if (res.ok) {
const full = await res.json();
fields = full.fields || {};
Expand Down Expand Up @@ -201,6 +202,7 @@ exports.handler = async (event, context) => {
reporter: fields.reporter,
creator: fields.creator,
project: fields.project,
issuetype: fields.issuetype ? { name: fields.issuetype.name } : undefined,
},
},
image_attachments: imageAttachments,
Expand Down
97 changes: 97 additions & 0 deletions services/common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,12 @@ pub struct TicketMessage {
/// The run keeps its run_id across these, like a continuation.
#[serde(default)]
pub model_retries: u32,
/// What the tracker calls the ticket (a Jira issue type such as `Story`
/// or `Bug`, a GitHub issue type or its `bug`/`enhancement` label), when
/// it says. Triage weighs it with the ticket's text (see
/// [`ticket_type_of_jira`], [`ticket_type_of_github`]).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ticket_type: Option<String>,
/// Set in-process when the ticket changed under an open PR: the implement
/// agent gets this note (old→new ticket diff + PR diff) and updates the
/// existing PR instead of starting over. Never serialized.
Expand Down Expand Up @@ -979,6 +985,56 @@ fn default_branch() -> String {
"main".to_string()
}

/// The issue type a Jira issue carries (`fields.issuetype.name`), when the
/// event has it.
pub fn ticket_type_of_jira(issue: &serde_json::Value) -> Option<String> {
issue
.pointer("/fields/issuetype/name")
.and_then(|v| v.as_str())
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string)
}

/// Labels that say what kind of work a GitHub issue is.
const KIND_LABELS: &[&str] = &[
"bug",
"defect",
"regression",
"enhancement",
"feature",
"feature request",
"new feature",
"improvement",
"chore",
"question",
];

/// What a GitHub issue says it is: its issue type (`type.name`), else the
/// first label that names a kind of work.
pub fn ticket_type_of_github(issue: &serde_json::Value) -> Option<String> {
if let Some(t) = issue
.pointer("/type/name")
.and_then(|v| v.as_str())
.map(str::trim)
.filter(|s| !s.is_empty())
{
return Some(t.to_string());
}
issue["labels"]
.as_array()
.into_iter()
.flatten()
.filter_map(|l| l["name"].as_str().or_else(|| l.as_str()))
.map(str::trim)
.find(|l| {
let low = l.to_ascii_lowercase();
let low = low.trim_start_matches("type:").trim_start_matches("kind/");
KIND_LABELS.contains(&low.trim())
})
.map(str::to_string)
}

impl Default for TicketMessage {
/// An empty ticket on `main`, as a message without `base_branch` reads.
fn default() -> Self {
Expand All @@ -1002,6 +1058,7 @@ impl Default for TicketMessage {
resume_from: None,
retry_of_run: false,
model_retries: 0,
ticket_type: None,
ticket_update_note: None,
ticket_history_note: None,
plan_cache_key: None,
Expand Down Expand Up @@ -1216,3 +1273,43 @@ mod worker_contract_tests {
assert!(e.contains("unknown variant `SkillSync`"), "{e}");
}
}

#[cfg(test)]
mod ticket_type_tests {
use super::{ticket_type_of_github, ticket_type_of_jira};
use serde_json::json;

#[test]
fn a_jira_issue_type_is_read() {
let issue = json!({"fields": {"issuetype": {"name": "Story"}, "summary": "s"}});
assert_eq!(ticket_type_of_jira(&issue).as_deref(), Some("Story"));
assert_eq!(ticket_type_of_jira(&json!({"fields": {}})), None);
assert_eq!(
ticket_type_of_jira(&json!({"fields": {"issuetype": {"name": " "}}})),
None
);
}

#[test]
fn a_github_issue_type_or_kind_label_is_read() {
assert_eq!(
ticket_type_of_github(&json!({"type": {"name": "Feature"}, "labels": []})).as_deref(),
Some("Feature")
);
assert_eq!(
ticket_type_of_github(
&json!({"labels": [{"name": "coderhelm"}, {"name": "enhancement"}]})
)
.as_deref(),
Some("enhancement")
);
assert_eq!(
ticket_type_of_github(&json!({"labels": [{"name": "type: bug"}]})).as_deref(),
Some("type: bug")
);
assert_eq!(
ticket_type_of_github(&json!({"labels": [{"name": "coderhelm"}]})),
None
);
}
}
121 changes: 121 additions & 0 deletions services/common/src/run_questions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,80 @@ fn clip(s: &str, max_chars: usize) -> String {
format!("{}…", kept.trim_end())
}

/// Openings of a question that hands a person the run's own search: where
/// the work goes, or what else it needs, with nothing to choose from.
const OPEN_FORMS: &[&str] = &[
"where does the fix belong",
"where does this belong",
"where does it belong",
"where should the fix go",
"where should this go",
"where should the change go",
"which system or part",
"which system is",
"which part of the system",
"what else does this need",
"what else is needed",
"what should happen next",
"how should i proceed",
"how should this be fixed",
"what should i do",
"can you clarify",
"please clarify",
"please provide more",
"any other details",
"anything else i should know",
];

/// Why a question is too open to put to a person, or `None` when it is fit
/// to ask. A person is asked only what they can answer better than the
/// code: a specific question, with the options the run considered (at least
/// two) or its own recommendation in the question ("I plan to ...; anything
/// else?"). An open "where does it belong" with no options never is.
/// CoderHelm's own choice cards (`decision`) are always fit.
pub fn too_open(q: &Question) -> Option<&'static str> {
if !q.decision.is_empty()
|| q.suggestions
.iter()
.filter(|s| !s.trim().is_empty())
.count()
>= 2
{
return None;
}
let text = q
.question
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
.to_ascii_lowercase();
if text.is_empty() {
return Some(WHY_EMPTY);
}
let recommends = [
"i plan to",
"i will",
"i'll",
"i recommend",
"i propose",
"my plan is",
]
.iter()
.any(|p| text.contains(p));
if recommends {
return None;
}
if OPEN_FORMS.iter().any(|f| text.contains(f)) {
return Some(WHY_OPEN);
}
None
}

/// [`too_open`]'s reason for an empty question.
pub const WHY_EMPTY: &str = "it is empty";
/// [`too_open`]'s reason for an open question.
pub const WHY_OPEN: &str = "it hands a person the run's own search (where the work goes, what else it needs) with no options and no recommendation: name the options considered and the one recommended";

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -858,4 +932,51 @@ mod tests {
assert!(digest(&[]).is_empty());
assert_ne!(digest(&[answer("A?", "1")]), digest(&[answer("A?", "2")]));
}

#[test]
fn an_open_question_with_no_options_is_not_asked() {
use super::{too_open, Question, WHY_OPEN};
let open = Question {
question: "Where does the fix belong: which system or part of it?".into(),
why: "The plan didn't say.".into(),
..Default::default()
};
assert_eq!(too_open(&open), Some(WHY_OPEN));
let open = Question {
question: "What else does this need before I build it?".into(),
..Default::default()
};
assert_eq!(too_open(&open), Some(WHY_OPEN));
// The same doubt with the options considered is fit to ask.
let with_options = Question {
question: "Where does the fix belong?".into(),
suggestions: vec![
"In the block schemas (code)".into(),
"In the CMS entries (content)".into(),
],
..Default::default()
};
assert_eq!(too_open(&with_options), None);
// So is a plan with a recommendation.
let plan = Question {
question: "I plan to put the setting on these 3 block schemas and read it in these \
components; anything else?"
.into(),
..Default::default()
};
assert_eq!(too_open(&plan), None);
// A specific yes/no question is fit too.
let specific = Question {
question: "Use the existing `title_size` field instead of creating a new one?".into(),
..Default::default()
};
assert_eq!(too_open(&specific), None);
// CoderHelm's own choice cards always are.
let card = Question {
question: "Where should this go?".into(),
decision: "open_pr:acme/web#1".into(),
..Default::default()
};
assert_eq!(too_open(&card), None);
}
}
5 changes: 5 additions & 0 deletions services/common/src/stop_rules/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ pub fn asking_rule(
if !q.decision.is_empty() {
return None;
}
if let Some(why) = crate::run_questions::too_open(q) {
return Some(why);
}
if read.conflicting_findings {
return Some(WHY_CONFLICT);
}
Expand Down Expand Up @@ -408,6 +411,8 @@ pub fn is_asking_rule(why: &str) -> bool {
WHY_ANSWERABLE,
WHY_FIELD_UNREAD,
WHY_CONFLICT,
crate::run_questions::WHY_OPEN,
crate::run_questions::WHY_EMPTY,
]
.contains(&why)
}
Expand Down
2 changes: 2 additions & 0 deletions services/gateway/src/routes/github_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ async fn handle_issue_event(
payload["sender"]["login"].as_str().unwrap_or(""),
),
image_attachments: vec![],
ticket_type: common::messages::ticket_type_of_github(issue),
..Default::default()
});

Expand Down Expand Up @@ -1576,6 +1577,7 @@ async fn handle_issue_comment(
payload["sender"]["login"].as_str().unwrap_or(""),
),
image_attachments: vec![],
ticket_type: common::messages::ticket_type_of_github(issue),
..Default::default()
});

Expand Down
2 changes: 2 additions & 0 deletions services/gateway/src/routes/jira_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ async fn process_jira_payload(
sender,
requester,
image_attachments,
ticket_type: common::messages::ticket_type_of_jira(issue),
..Default::default()
});

Expand Down Expand Up @@ -1133,6 +1134,7 @@ async fn handle_jira_comment(
.and_then(|s| serde_json::from_str(s).ok())
.unwrap_or_default()
},
ticket_type: common::messages::ticket_type_of_jira(issue),
..Default::default()
})
};
Expand Down
9 changes: 9 additions & 0 deletions services/worker/src/agent/anthropic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,15 @@ pub async fn converse_tool_loop(
.collect();

if tool_uses.is_empty() || response.stop_reason.as_deref() == Some("end_turn") {
// An answer cut at the output limit loses whatever it was to
// end with (a planner's decision block comes last): say so.
if response.stop_reason.as_deref() == Some("max_tokens") {
warn!(
max_tokens,
output_tokens = response.usage.output_tokens,
"Final answer stopped at the output limit; its end is missing"
);
}
// Log the final assistant response
if let Some(ref mut log) = conversation_log {
log.push(json!({
Expand Down
1 change: 1 addition & 0 deletions services/worker/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ mod tests {
resume_from: None,
retry_of_run: false,
model_retries: 0,
ticket_type: None,
ticket_update_note: None,
ticket_history_note: None,
plan_cache_key: None,
Expand Down
2 changes: 2 additions & 0 deletions services/worker/src/passes/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,7 @@ async fn start_fix_ticket(
resume_from: None,
retry_of_run: false,
model_retries: 0,
ticket_type: None,
ticket_update_note: None,
ticket_history_note: None,
plan_cache_key: None,
Expand Down Expand Up @@ -1883,6 +1884,7 @@ pub async fn act(state: &WorkerState, a: TestAction) -> Result<(), Err> {
resume_from: None,
retry_of_run: false,
model_retries: 0,
ticket_type: None,
ticket_update_note: None,
ticket_history_note: None,
plan_cache_key: None,
Expand Down
1 change: 1 addition & 0 deletions services/worker/src/passes/e2e_attribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ async fn start_fix(
resume_from: None,
retry_of_run: false,
model_retries: 0,
ticket_type: None,
ticket_update_note: None,
ticket_history_note: None,
plan_cache_key: None,
Expand Down
Loading
Loading