fix(app): rankings carry an id, ties arriving mid-ranking are ranked, timer moves forward

- Action::Rank and ranked() carry a ranking id; answers to a replaced or
  dropped ranking are ignored.
- A candidate tying the ranked ones while a ranking is pending replaces
  it with a new ranking over all of them (plus the running answer when
  the ranking is about interrupting it), keeping the first deadline, so
  only compared candidates are missed as lower_score. A better one makes
  the ranking moot.
- next_deadline returns the collect timer while collecting, and the
  settle deadline only while an ending utterance holds the choice up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 22:13:39 +09:00
parent 46eebcd256
commit e885630e91
2 changed files with 154 additions and 50 deletions

View File

@@ -80,8 +80,9 @@ pub enum Action {
StopAnswer(u64),
/// Not answered, or stopped: for the "놓친 요청" list.
Missed(Request, MissReason),
/// Ask the brain to rank these tied candidates by importance; answer with `ranked`.
Rank(Vec<Request>),
/// Ask the brain to rank these tied candidates by importance; answer with `ranked(id, ..)`. A newer
/// `Rank` replaces an older one, whose answer is then ignored.
Rank { id: u64, candidates: Vec<Request> },
}
#[derive(Debug, Default)]
@@ -99,9 +100,11 @@ pub struct Policy {
collect_until: Option<Instant>,
/// `important`: a ranking the brain was asked for.
ranking: Option<Ranking>,
next_rank_id: u64,
}
struct Ranking {
rank_id: u64,
ids: HashSet<u64>,
deadline: Instant,
/// Asked about interrupting the running answer with this id (it is one of `ids`).
@@ -110,7 +113,8 @@ struct Ranking {
impl Policy {
pub fn new(s: PolicySettings) -> Self {
Self { s, current: None, waiting: vec![], open: HashMap::new(), collect_until: None, ranking: None }
Self { s, current: None, waiting: vec![], open: HashMap::new(), collect_until: None, ranking: None,
next_rank_id: 0 }
}
pub fn settings(&self) -> &PolicySettings {
@@ -169,19 +173,19 @@ impl Policy {
}
PolicyKind::Important => {
let (new, running) = (self.score(&req), self.score(&cur));
self.enqueue(req.clone(), &mut out);
self.enqueue(req, &mut out);
if new > running {
self.interrupt(&mut out);
self.collect_until = None;
self.ranking = None;
} else if new == running && self.ranking.is_none() {
// A tie on priority and wake word: the brain's importance decides.
self.ranking = Some(Ranking {
ids: [cur.id, req.id].into(),
deadline: now + self.s.rank_timeout,
against: Some(cur.id),
});
out.push(Action::Rank(vec![cur, req]));
} else if new == running {
// A tie on priority and wake word: the brain's importance decides, over the
// running answer and every candidate tied with it (a pending ranking is replaced,
// keeping its deadline).
let mut cands = vec![cur.clone()];
cands.extend(self.waiting.iter().filter(|r| self.score(r) == running).cloned());
let deadline = self.ranking.as_ref().map_or(now + self.s.rank_timeout, |r| r.deadline);
out.push(self.ask_rank(cands, deadline, Some(cur.id)));
}
}
}
@@ -189,7 +193,22 @@ impl Policy {
if self.s.kind == PolicyKind::Important && self.waiting.is_empty() && self.ranking.is_none() {
self.collect_until = Some(now + self.s.collect);
}
let score = self.score(&req);
self.enqueue(req, &mut out);
// A candidate arriving while tied candidates are being ranked: a tie joins a new ranking
// (same deadline); a better one makes the ranking moot.
if let Some(r) = &self.ranking {
let ranked_score = self.waiting.iter().find(|q| r.ids.contains(&q.id)).map(|q| self.score(q));
let deadline = r.deadline;
match ranked_score {
Some(t) if score == t => {
let cands: Vec<Request> = self.waiting.iter().filter(|q| self.score(q) == t).cloned().collect();
out.push(self.ask_rank(cands, deadline, None));
}
Some(t) if score > t => self.ranking = None,
_ => {}
}
}
}
out.extend(self.choose(now, None));
out
@@ -205,6 +224,17 @@ impl Policy {
}
}
fn ask_rank(&mut self, candidates: Vec<Request>, deadline: Instant, against: Option<u64>) -> Action {
self.next_rank_id += 1;
self.ranking = Some(Ranking {
rank_id: self.next_rank_id,
ids: candidates.iter().map(|r| r.id).collect(),
deadline,
against,
});
Action::Rank { id: self.next_rank_id, candidates }
}
fn interrupt(&mut self, out: &mut Vec<Action>) {
if let Some(cur) = self.current.take() {
out.push(Action::StopAnswer(cur.id));
@@ -240,10 +270,13 @@ impl Policy {
self.choose(now, None)
}
/// The brain's ranking of the tied candidates, best first. After the deadline it counts as a
/// timeout.
pub fn ranked(&mut self, order: &[u64], now: Instant) -> Vec<Action> {
let Some(r) = self.ranking.take() else { return vec![] };
/// The brain's answer to ranking `rank_id`, best first. An answer to any other ranking (replaced or
/// dropped) is ignored; one after the deadline counts as a timeout.
pub fn ranked(&mut self, rank_id: u64, order: &[u64], now: Instant) -> Vec<Action> {
if self.ranking.as_ref().map(|r| r.rank_id) != Some(rank_id) {
return vec![];
}
let r = self.ranking.take().expect("checked");
let winner = if now >= r.deadline { None } else { order.iter().copied().find(|id| r.ids.contains(id)) };
self.resolve(r, winner, now)
}
@@ -278,16 +311,18 @@ impl Policy {
if let Some(r) = &self.ranking {
return Some(r.deadline);
}
let mut d: Vec<Instant> = vec![];
if self.current.is_none() && !self.waiting.is_empty() {
if let Some(t) = self.collect_until {
d.push(t);
}
if let Some(first) = self.waiting.iter().map(|r| r.arrived_at).min() {
d.push(first + self.s.settle);
}
if self.current.is_some() || self.waiting.is_empty() {
return None;
}
d.into_iter().min()
// Nothing can start before the collect timer ends.
if let Some(t) = self.collect_until {
return Some(t);
}
// The settle deadline matters only while an ending utterance holds the choice up.
if self.open.values().any(|o| o.stopped_at.is_some()) {
return self.waiting.iter().map(|r| r.arrived_at).min().map(|t| t + self.s.settle);
}
None
}
/// Wait for an ending utterance that may still be a call that should come first.
@@ -342,12 +377,8 @@ impl Policy {
Some(_) => tied[0],
None if tied.len() >= 2 => {
let cands: Vec<Request> = tied.iter().map(|&i| self.waiting[i].clone()).collect();
self.ranking = Some(Ranking {
ids: cands.iter().map(|r| r.id).collect(),
deadline: now + self.s.rank_timeout,
against: None,
});
out.push(Action::Rank(cands));
let deadline = now + self.s.rank_timeout;
out.push(self.ask_rank(cands, deadline, None));
return out;
}
None => tied[0],
@@ -413,6 +444,16 @@ mod tests {
fn missed(a: &[Action]) -> Vec<(u64, MissReason)> {
a.iter().filter_map(|x| if let Action::Missed(r, w) = x { Some((r.id, *w)) } else { None }).collect()
}
/// Every ranking asked for: (ranking id, candidate ids).
fn ranks(a: &[Action]) -> Vec<(u64, Vec<u64>)> {
a.iter().filter_map(|x| if let Action::Rank { id, candidates } = x {
Some((*id, candidates.iter().map(|r| r.id).collect())) } else { None }).collect()
}
fn rank_of(a: &[Action]) -> (u64, Vec<u64>) {
let r = ranks(a);
assert_eq!(r.len(), 1, "one ranking asked for: {a:?}");
r[0].clone()
}
fn stopped(a: &[Action]) -> Vec<u64> {
a.iter().filter_map(|x| if let Action::StopAnswer(id) = x { Some(*id) } else { None }).collect()
}
@@ -538,19 +579,18 @@ mod tests {
let (a, _) = t.call(A, true, 0, 100);
let (b, _) = t.call(B, true, 50, 200);
let out = t.p.tick(t.at(800));
let ranks: Vec<Vec<u64>> = out.iter().filter_map(|x| if let Action::Rank(c) = x {
Some(c.iter().map(|r| r.id).collect()) } else { None }).collect();
assert_eq!(ranks, vec![vec![a, b]]);
let (r, ids) = rank_of(&out);
assert_eq!(ids, vec![a, b]);
assert!(answered(&out).is_empty());
assert_eq!(answered(&t.p.ranked(&[b, a], t.at(900))), vec![b]);
assert_eq!(answered(&t.p.ranked(r, &[b, a], t.at(900))), vec![b]);
let mut t = T::new(PolicyKind::Important);
let (a, _) = t.call(A, true, 0, 100);
let (_b, _) = t.call(B, true, 50, 200);
t.p.tick(t.at(800));
let (b, _) = t.call(B, true, 50, 200);
let (r, _) = rank_of(&t.p.tick(t.at(800)));
assert_eq!(t.p.next_deadline(), Some(t.at(800 + 1500)));
assert_eq!(answered(&t.p.tick(t.at(2300))), vec![a], "timeout: earliest-ended wins");
assert!(t.p.ranked(&[2], t.at(2400)).is_empty(), "a late ranking is ignored");
assert!(t.p.ranked(r, &[b], t.at(2400)).is_empty(), "a late ranking is ignored");
// One candidate: no ranking, no extra delay beyond collecting.
let mut t = T::new(PolicyKind::Important);
@@ -574,11 +614,6 @@ mod tests {
assert_eq!(answered(&out), vec![c]);
}
fn ranks(a: &[Action]) -> Vec<Vec<u64>> {
a.iter().filter_map(|x| if let Action::Rank(c) = x { Some(c.iter().map(|r| r.id).collect()) } else { None })
.collect()
}
#[test]
fn important_asks_the_ranking_when_a_new_request_ties_the_running_answer() {
let s = PolicySettings { kind: PolicyKind::Important, collect: Duration::ZERO, ..PolicySettings::default() };
@@ -586,17 +621,19 @@ mod tests {
let mut t = T::with(s.clone());
let (a, _) = t.call(A, true, 0, 100);
let (b, out) = t.call(B, true, 200, 300);
assert_eq!(ranks(&out), vec![vec![a, b]]);
let (r, ids) = rank_of(&out);
assert_eq!(ids, vec![a, b]);
assert!(stopped(&out).is_empty(), "keeps speaking while ranking");
let out = t.p.ranked(&[b, a], t.at(400));
let out = t.p.ranked(r, &[b, a], t.at(400));
assert_eq!(stopped(&out), vec![a]);
assert_eq!(missed(&out), vec![(a, MissReason::Interrupted)]);
assert_eq!(answered(&out), vec![b]);
// The ranking keeps the running answer: the new one waits for it.
let mut t = T::with(s.clone());
let (a, _) = t.call(A, true, 0, 100);
let (b, _) = t.call(B, true, 200, 300);
assert!(stopped(&t.p.ranked(&[a, b], t.at(400))).is_empty());
let (b, out) = t.call(B, true, 200, 300);
let (r, _) = rank_of(&out);
assert!(stopped(&t.p.ranked(r, &[a, b], t.at(400))).is_empty());
assert_eq!(answered(&t.p.answer_done(a, t.at(1000))), vec![b]);
// No ranking in time: the running answer ended first, so it goes on.
let mut t = T::with(s);
@@ -611,8 +648,8 @@ mod tests {
let mut t = T::new(PolicyKind::Important);
let (a, _) = t.call(A, true, 0, 100);
let (b, _) = t.call(B, true, 50, 200);
t.p.tick(t.at(800));
assert_eq!(answered(&t.p.ranked(&[b, a], t.at(2400))), vec![a], "late: earliest-ended wins");
let (r, _) = rank_of(&t.p.tick(t.at(800)));
assert_eq!(answered(&t.p.ranked(r, &[b, a], t.at(2400))), vec![a], "late: earliest-ended wins");
}
#[test]
@@ -623,7 +660,7 @@ mod tests {
let (_late, _) = t.call(B, false, 900, 1000);
let (early, _) = t.call(C, false, 600, 1200); // ended first, arrived last
let out = t.p.answer_done(a, t.at(1300));
assert_eq!(ranks(&out), vec![vec![early, _late]]);
assert_eq!(rank_of(&out).1, vec![early, _late]);
assert_eq!(answered(&t.p.tick(t.at(1300 + 1500))), vec![early]);
}
@@ -639,6 +676,66 @@ mod tests {
assert_eq!(answered(&t.p.tick(t.at(900))), vec![a]);
}
#[test]
fn a_tie_arriving_during_a_ranking_against_the_answer_is_ranked_too() {
let mut t = T::with(PolicySettings { kind: PolicyKind::Important, collect: Duration::ZERO,
..PolicySettings::default() });
let (a, _) = t.call(A, true, 0, 100);
let (b, out) = t.call(B, true, 200, 300);
let (r1, _) = rank_of(&out);
let (c, out) = t.call(C, true, 400, 500);
let (r2, ids) = rank_of(&out);
assert_ne!(r1, r2);
assert_eq!(ids, vec![a, b, c], "the running answer and every tied candidate");
assert_eq!(t.p.next_deadline(), Some(t.at(300 + 1500)), "the first ranking's deadline is kept");
assert!(t.p.ranked(r1, &[b, a], t.at(600)).is_empty(), "the replaced ranking's result is ignored");
let out = t.p.ranked(r2, &[c, b, a], t.at(700));
assert_eq!(stopped(&out), vec![a]);
assert_eq!(answered(&out), vec![c]);
assert_eq!(missed(&out), vec![(a, MissReason::Interrupted), (b, MissReason::LowerScore)]);
}
#[test]
fn a_tie_arriving_during_a_ranking_while_idle_is_ranked_too() {
let mut t = T::new(PolicyKind::Important);
let (a, _) = t.call(A, true, 0, 100);
let (b, _) = t.call(B, true, 50, 200);
let (r1, _) = rank_of(&t.p.tick(t.at(800)));
let (c, out) = t.call(C, true, 850, 900);
let (r2, ids) = rank_of(&out);
assert_eq!(ids, vec![a, b, c]);
assert!(t.p.ranked(r1, &[b], t.at(950)).is_empty());
let out = t.p.ranked(r2, &[c], t.at(1000));
assert_eq!(answered(&out), vec![c]);
assert_eq!(missed(&out), vec![(a, MissReason::LowerScore), (b, MissReason::LowerScore)]);
}
#[test]
fn a_dropped_rankings_late_result_does_not_decide_the_next_one() {
let mut t = T::with(PolicySettings { kind: PolicyKind::Important, collect: Duration::ZERO,
..PolicySettings::default() });
let (a, _) = t.call(A, true, 0, 100);
let (b, out) = t.call(B, true, 200, 300);
let (r1, _) = rank_of(&out);
let (_c, _) = t.call(C, true, 250, 350);
// A ends: the ranking about interrupting it is dropped, and B and C are ranked anew.
let (r2, ids) = rank_of(&t.p.answer_done(a, t.at(400)));
assert_eq!(ids, vec![b, _c]);
assert!(t.p.ranked(r1, &[b, a], t.at(450)).is_empty(), "the dropped ranking's result is ignored");
assert_eq!(answered(&t.p.ranked(r2, &[_c, b], t.at(500))), vec![_c]);
}
#[test]
fn with_no_settle_time_the_next_deadline_is_the_collect_timer() {
let mut t = T::with(PolicySettings { kind: PolicyKind::Important, settle: Duration::ZERO,
..PolicySettings::default() });
let (a, _) = t.call(A, true, 0, 100);
assert_eq!(t.p.next_deadline(), Some(t.at(800)));
assert!(t.p.tick(t.at(100)).is_empty());
assert_eq!(t.p.next_deadline(), Some(t.at(800)), "moves forward, not stuck at a passed time");
assert_eq!(answered(&t.p.tick(t.at(800))), vec![a]);
}
#[test]
fn stop_ends_the_answer_and_drops_the_queue_in_every_policy() {
for kind in [PolicyKind::First, PolicyKind::InOrder, PolicyKind::Latest, PolicyKind::Important] {