Skip to content

Commit 5060e37

Browse files
committed
Avoid redundant final follow-up after delivered response
1 parent c51700c commit 5060e37

2 files changed

Lines changed: 134 additions & 4 deletions

File tree

‎crates/jcode-todo-policy/src/followup.rs‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ impl FollowUpPolicy {
209209
/// Decide the next follow-up after a turn ended. The caller must already
210210
/// have checked that no user prompt or other follow-up is pending.
211211
pub fn decide(&mut self, snapshot: &TodoSnapshot) -> Decision {
212+
self.decide_after_turn(snapshot, false)
213+
}
214+
215+
/// Decide after a turn, suppressing only the final-response handoff when
216+
/// that turn already delivered a final response. All work and quality
217+
/// gates still run, and clean completion is latched for this todo state.
218+
pub fn decide_after_turn(
219+
&mut self,
220+
snapshot: &TodoSnapshot,
221+
final_response_delivered: bool,
222+
) -> Decision {
212223
let todos = &snapshot.todos;
213224
let goals = &snapshot.goals;
214225
// A circuit breaker clears `armed` while every todo is complete. Re-arm
@@ -347,13 +358,21 @@ impl FollowUpPolicy {
347358
if !self.final_response_requested {
348359
self.final_response_requested = true;
349360
self.final_response_fingerprint = fingerprint;
361+
let notice = format!(
362+
"✅ All todos done. Completion confidence: {}.",
363+
summary.label()
364+
);
365+
if final_response_delivered {
366+
return Decision {
367+
notice: Some(notice),
368+
effects,
369+
..Decision::default()
370+
};
371+
}
350372
return Decision::send(
351373
FollowUpKind::FinalResponse,
352374
TODO_FINAL_RESPONSE_CONTINUATION_MESSAGE.to_string(),
353-
format!(
354-
"✅ All todos done. Completion confidence: {}.",
355-
summary.label()
356-
),
375+
notice,
357376
)
358377
.with_effects(effects);
359378
}

‎crates/jcode-todo-policy/src/followup_tests.rs‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,114 @@ fn snapshot_round_trips_through_json() {
379379
TodoSnapshot::default()
380380
);
381381
}
382+
383+
#[test]
384+
fn delivered_final_latches_completion_without_requesting_another_response() {
385+
let mut policy = FollowUpPolicy::new(true);
386+
let mut state = snapshot(vec![done("ship", ConfidenceState::Verified)]);
387+
state.gate_observations.push(GateObservation {
388+
kind: GateObservationKind::IntentUnderstanding,
389+
group: None,
390+
state: Some("partial".into()),
391+
});
392+
let decision = policy.decide_after_turn(&state, true);
393+
assert_eq!(decision.follow_up, None);
394+
assert!(decision.notice.unwrap().contains("verified"));
395+
assert!(decision.effects.clear_gate_observations);
396+
assert!(policy.final_response_requested);
397+
assert_eq!(
398+
policy.final_response_fingerprint,
399+
serde_json::to_string(&(&state.todos, &state.plan, &state.goals)).ok()
400+
);
401+
// A later turn without a response or with stale observations stays quiet.
402+
assert_eq!(policy.decide_after_turn(&state, false), Decision::default());
403+
assert_eq!(policy.decide_after_turn(&state, true), Decision::default());
404+
assert_eq!(policy.decide(&state), Decision::default());
405+
406+
// Changed completed todos start a fresh cycle and can request a response.
407+
state
408+
.todos
409+
.push(done("follow-up", ConfidenceState::Verified));
410+
assert_eq!(
411+
kind(&policy.decide_after_turn(&state, false)),
412+
Some(FollowUpKind::FinalResponse)
413+
);
414+
state.todos.push(todo("new work", "pending"));
415+
assert_eq!(
416+
kind(&policy.decide_after_turn(&state, true)),
417+
Some(FollowUpKind::IncompleteTodos)
418+
);
419+
}
420+
421+
#[test]
422+
fn absent_final_response_preserves_legacy_decision() {
423+
let state = snapshot(vec![done("ship", ConfidenceState::Verified)]);
424+
let mut legacy = FollowUpPolicy::new(true);
425+
let mut explicit = FollowUpPolicy::new(true);
426+
let decision = explicit.decide_after_turn(&state, false);
427+
assert_eq!(kind(&decision), Some(FollowUpKind::FinalResponse));
428+
assert_eq!(decision, legacy.decide(&state));
429+
assert_eq!(explicit, legacy);
430+
}
431+
432+
#[test]
433+
fn delivered_final_does_not_bypass_work_or_quality_gates() {
434+
let mut review = snapshot(vec![todo("work", "pending")]);
435+
review.long_session_review_due = true;
436+
let mut ownership = snapshot(vec![done("ship", ConfidenceState::Verified)]);
437+
ownership.goals.push(TodoGoal {
438+
delivery_state: Some(DeliveryState::ChangeMade),
439+
..Default::default()
440+
});
441+
let mut spike = done("ship", ConfidenceState::Verified);
442+
spike.confidence_history = vec![ConfidenceState::Speculative, ConfidenceState::Verified];
443+
let mut digest = snapshot(vec![done("ship", ConfidenceState::Verified)]);
444+
digest.gate_observations.push(GateObservation {
445+
kind: GateObservationKind::IntentUnderstanding,
446+
group: None,
447+
state: Some("partial".into()),
448+
});
449+
for (state, expected) in [
450+
(review, FollowUpKind::LongSessionReview),
451+
(
452+
snapshot(vec![todo("work", "pending")]),
453+
FollowUpKind::IncompleteTodos,
454+
),
455+
(ownership, FollowUpKind::Ownership),
456+
(
457+
snapshot(vec![done("ship", ConfidenceState::Plausible)]),
458+
FollowUpKind::CompletionValidation,
459+
),
460+
(snapshot(vec![spike]), FollowUpKind::ConfidenceSpike),
461+
(digest, FollowUpKind::GateDigest),
462+
] {
463+
let mut legacy = FollowUpPolicy::new(true);
464+
legacy.set_gate_digest_enabled(true);
465+
let mut delivered = legacy.clone();
466+
let decision = delivered.decide_after_turn(&state, true);
467+
assert_eq!(kind(&decision), Some(expected));
468+
assert_eq!(decision, legacy.decide(&state));
469+
assert_eq!(delivered, legacy);
470+
assert!(!delivered.final_response_requested);
471+
assert!(delivered.final_response_fingerprint.is_none());
472+
}
473+
}
474+
475+
#[test]
476+
fn delivered_final_preserves_gate_breaker_and_disabled_policy() {
477+
let state = snapshot(vec![done("ship", ConfidenceState::Speculative)]);
478+
let mut policy = FollowUpPolicy::new(true);
479+
for _ in 0..MAX_GATE_ATTEMPTS {
480+
assert_eq!(
481+
kind(&policy.decide_after_turn(&state, true)),
482+
Some(FollowUpKind::CompletionValidation)
483+
);
484+
}
485+
let stopped = policy.decide_after_turn(&state, true);
486+
assert!(stopped.notice.is_some());
487+
assert!(stopped.follow_up.is_none());
488+
assert!(!policy.final_response_requested);
489+
assert!(policy.final_response_fingerprint.is_none());
490+
policy.disable();
491+
assert_eq!(policy.decide_after_turn(&state, true), Decision::default());
492+
}

0 commit comments

Comments
 (0)